From 1e8fcfb0aa1f8575a8db0be88cb5828346f100fc Mon Sep 17 00:00:00 2001 From: Frank Date: Wed, 11 Jan 2023 22:26:35 +0100 Subject: [PATCH] I2C pin settings bugfix (8266) * It was not possible any more to set I2C pins back to "-1", to disable I2C. This fixes the problem. * HW_PIN_SDA / HW_PIN_SCL may contain "-1" --> use PIN numbers directly on 8266 (I2C pins cannot be changed any way on 8266) --- wled00/pin_manager.cpp | 14 ++++++++++---- wled00/set.cpp | 14 ++++++++++++-- wled00/wled.h | 2 +- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/wled00/pin_manager.cpp b/wled00/pin_manager.cpp index 489c308e..0c501b68 100644 --- a/wled00/pin_manager.cpp +++ b/wled00/pin_manager.cpp @@ -523,7 +523,10 @@ bool PinManagerClass::joinWire() { // shortcut in case no parameters provided return joinWire(i2c_sda, i2c_scl); #else // ESP8266: I2C pins are fixed - return joinWire(HW_PIN_SDA, HW_PIN_SCL); + 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 } @@ -568,9 +571,10 @@ bool PinManagerClass::joinWire(int8_t pinSDA, int8_t pinSCL) { #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 != HW_PIN_SDA) || (pinSCL != HW_PIN_SCL)) { + 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",HW_PIN_SDA, HW_PIN_SCL); + DEBUG_PRINTF("%d, SCL=%d !\n",4, 5); + return(false); } #endif if (wireIsOK == false) { @@ -583,7 +587,9 @@ bool PinManagerClass::joinWire(int8_t pinSDA, int8_t pinSCL) { if (wireIsOK == false) { USER_PRINTLN(F("PIN Manager: warning - wire.begin failed!")); } else { - USER_PRINTLN(F("PIN Manager: wire.begin successfull.")); + USER_PRINT(F("PIN Manager: wire.begin successfull! ")); + USER_PRINT(F("I2C bus is active. SDA=")); + USER_PRINTF("%d SCL=%d.\n", pinSDA, pinSCL); } #ifdef ARDUINO_ARCH_ESP32S3 diff --git a/wled00/set.cpp b/wled00/set.cpp index b75cf08c..0f6ac30b 100644 --- a/wled00/set.cpp +++ b/wled00/set.cpp @@ -533,8 +533,9 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage) #ifdef ESP8266 // cannot change pins on ESP8266 - if (hw_sda_pin >= 0 && hw_sda_pin != HW_PIN_SDA) hw_sda_pin = HW_PIN_SDA; - if (hw_scl_pin >= 0 && hw_scl_pin != HW_PIN_SCL) hw_scl_pin = HW_PIN_SCL; + // 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; #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)) { @@ -546,6 +547,10 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage) // Wire.begin(); // WLEDMM moved into pinManager } else { // there is no Wire.end() + if (hw_sda_pin < 0 || hw_scl_pin < 0) { // WLEDMM bugfix allow pin = -1 + i2c_sda = -1; + i2c_scl = -1; + } DEBUG_PRINTLN(F("Could not allocate I2C pins.")); uint8_t i2c[2] = { static_cast(i2c_scl), static_cast(i2c_sda) }; pinManager.deallocateMultiplePins(i2c, 2, PinOwner::HW_I2C); // just in case deallocation of old pins @@ -572,6 +577,11 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage) #endif } else { //SPI.end(); + if (hw_mosi_pin < 0 || hw_sclk_pin < 0) { // WLEDMM bugfix allow pin = -1 + spi_mosi = hw_mosi_pin; + spi_miso = hw_miso_pin; + spi_sclk = hw_sclk_pin; + } DEBUG_PRINTLN(F("Could not allocate SPI pins.")); uint8_t spi[3] = { static_cast(spi_mosi), static_cast(spi_miso), static_cast(spi_sclk) }; pinManager.deallocateMultiplePins(spi, 3, PinOwner::HW_SPI); // just in case deallocation of old pins diff --git a/wled00/wled.h b/wled00/wled.h index ac25e8ff..a1332278 100644 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -8,7 +8,7 @@ */ // version code in format yymmddb (b = daily build) -#define VERSION 2301111 +#define VERSION 2301112 //uncomment this if you have a "my_config.h" file you'd like to use //#define WLED_USE_MY_CONFIG