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)
This commit is contained in:
Frank
2023-01-11 22:26:35 +01:00
parent 49bb9be12c
commit 1e8fcfb0aa
3 changed files with 23 additions and 7 deletions

View File

@@ -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

View File

@@ -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<uint8_t>(i2c_scl), static_cast<uint8_t>(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<uint8_t>(spi_mosi), static_cast<uint8_t>(spi_miso), static_cast<uint8_t>(spi_sclk) };
pinManager.deallocateMultiplePins(spi, 3, PinOwner::HW_SPI); // just in case deallocation of old pins

View File

@@ -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