From a320068bbe216b6c53b9f049bcaef079b805217b Mon Sep 17 00:00:00 2001 From: Frank Date: Sun, 15 Jan 2023 22:13:46 +0100 Subject: [PATCH] I2C: allow any GPIO on 8266 (experimental) as it turns out, also on 8266 any pin can be assigned to I2C. Only SPI pins are fixed, and GPIO16 should not be used as it does not support interrupts. --- wled00/cfg.cpp | 2 ++ wled00/pin_manager.cpp | 28 +++++++++++----------------- wled00/set.cpp | 6 +++--- wled00/wled.h | 2 +- 4 files changed, 17 insertions(+), 21 deletions(-) diff --git a/wled00/cfg.cpp b/wled00/cfg.cpp index dc52bdb0..8e4d6dc8 100644 --- a/wled00/cfg.cpp +++ b/wled00/cfg.cpp @@ -310,6 +310,7 @@ bool deserializeConfig(JsonObject doc, bool fromFS) { Wire.setPins(i2c_sda, i2c_scl); // this will fail if Wire is initilised (Wire.begin() called prior) #endif // Wire.begin(); // WLEDMM moved into pinManager + Serial.printf("pinmgr success for global i2c %d %d\n", i2c_sda, i2c_scl); } else { Serial.printf("pinmgr not success for global i2c %d %d\n", i2c_sda, i2c_scl); } @@ -324,6 +325,7 @@ bool deserializeConfig(JsonObject doc, bool fromFS) { #else SPI.begin(); #endif + Serial.printf("pinmgr success for global spi %d %d %d\n", spi_mosi, spi_miso, spi_sclk); } else { Serial.printf("pinmgr not success for global spi %d %d %d\n", spi_mosi, spi_miso, spi_sclk); } diff --git a/wled00/pin_manager.cpp b/wled00/pin_manager.cpp index 0c501b68..ff9ac885 100644 --- a/wled00/pin_manager.cpp +++ b/wled00/pin_manager.cpp @@ -518,16 +518,7 @@ bool PinManagerClass::isPinAllocated(byte gpio, PinOwner tag) // bool PinManagerClass::joinWire() { // shortcut in case no parameters provided - #ifdef ARDUINO_ARCH_ESP32 - // ESP32 - i2c pins can be mapped to any GPIO return joinWire(i2c_sda, i2c_scl); - #else - // ESP8266: I2C pins are fixed - if ((i2c_sda < 0) || (i2c_scl < 0)) - return joinWire(i2c_sda, i2c_scl); // special case: -1 = disable i2c - else - return joinWire(4, 5); // normal case - use HW pins -> SDA = 4, SCL = 5 - #endif } bool PinManagerClass::joinWire(int8_t pinSDA, int8_t pinSCL) { @@ -570,19 +561,22 @@ bool PinManagerClass::joinWire(int8_t pinSDA, int8_t pinSCL) { bool wireIsOK = true; #ifdef ARDUINO_ARCH_ESP32 // ESP32 - i2c pins can be mapped to any GPIO wireIsOK = Wire.setPins(pinSDA, pinSCL); // this will fail if Wire is initialised already (i.e. Wire.begin() called prior) - #else // 8266 - I2C pins are fixed - if((pinSDA != 4) || (pinSCL != 5)) { // fixed PINS: SDA = 4, SCL = 5 - DEBUG_PRINT(F("PIN Manager: warning ESP8266 I2C pins are fixed. please use SDA=")); - DEBUG_PRINTF("%d, SCL=%d !\n",4, 5); - return(false); - } + #else // 8266 - I2C pins are fixed -> actually they are not. + //if((pinSDA != 4) || (pinSCL != 5)) { // fixed PINS: SDA = 4, SCL = 5 + // DEBUG_PRINT(F("PIN Manager: warning ESP8266 I2C pins are fixed. please use SDA=")); + // DEBUG_PRINTF("%d, SCL=%d !\n",4, 5); + // return(false); + //} #endif if (wireIsOK == false) { USER_PRINTLN(F("PIN Manager: warning - wire.setPins failed!")); } - //wireIsOK = Wire.begin(); // this will fail if wire is already running - Wire.begin(); // returns void on 8266 + #ifdef ARDUINO_ARCH_ESP32 + wireIsOK = Wire.begin(pinSDA, pinSCL); // this will fail if wire is already running + #else + Wire.begin(pinSDA, pinSCL); // returns void on 8266 + #endif if (wireIsOK == false) { USER_PRINTLN(F("PIN Manager: warning - wire.begin failed!")); diff --git a/wled00/set.cpp b/wled00/set.cpp index f45f5d42..8755b7dd 100644 --- a/wled00/set.cpp +++ b/wled00/set.cpp @@ -532,10 +532,10 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage) } #ifdef ESP8266 - // cannot change pins on ESP8266 + // cannot change pins on ESP8266 --> actually we can // WLEDMM: HW_PIN_xx could be set to -1 --> use pins as defined by the framework! SDA = 4, SCL = 5 - if (hw_sda_pin >= 0 && hw_sda_pin != 4) hw_sda_pin = 4; - if (hw_scl_pin >= 0 && hw_scl_pin != 5) hw_scl_pin = 5; + //if (hw_sda_pin >= 0 && hw_sda_pin != 4) hw_sda_pin = 4; + //if (hw_scl_pin >= 0 && hw_scl_pin != 5) hw_scl_pin = 5; #endif PinManagerPinType i2c[2] = { { hw_sda_pin, true }, { hw_scl_pin, true } }; if (hw_sda_pin >= 0 && hw_scl_pin >= 0 && pinManager.allocateMultiplePins(i2c, 2, PinOwner::HW_I2C)) { diff --git a/wled00/wled.h b/wled00/wled.h index c00c062c..3aba9db8 100644 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -8,7 +8,7 @@ */ // version code in format yymmddb (b = daily build) -#define VERSION 2301150 +#define VERSION 2301151 //uncomment this if you have a "my_config.h" file you'd like to use //#define WLED_USE_MY_CONFIG