From e04d8033dc3060fda61ee315e3199b7e0f10aabf Mon Sep 17 00:00:00 2001 From: TD-er Date: Mon, 21 Apr 2025 20:39:08 +0200 Subject: [PATCH] [I2C] Fix issues with GPIO 21 & 22 when no I2C is configured --- lib/AS_BH1750/AS_BH1750A.cpp | 2 +- src/src/Commands/GPIO.cpp | 13 +++++++++++-- src/src/ESPEasyCore/ESPEasyGPIO.cpp | 13 +++++++++---- src/src/Helpers/Hardware_I2C.cpp | 8 ++++++++ 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/lib/AS_BH1750/AS_BH1750A.cpp b/lib/AS_BH1750/AS_BH1750A.cpp index 1056d2797..1e6ebf4bb 100644 --- a/lib/AS_BH1750/AS_BH1750A.cpp +++ b/lib/AS_BH1750/AS_BH1750A.cpp @@ -74,7 +74,7 @@ bool AS_BH1750A::begin(sensors_resolution_t mode, bool autoPowerDown) { _virtualMode = mode; _autoPowerDown = autoPowerDown; - Wire.begin(); +// Wire.begin(); defineMTReg(BH1750_MTREG_DEFAULT); // eigentlich normalerweise unnötig, da standard diff --git a/src/src/Commands/GPIO.cpp b/src/src/Commands/GPIO.cpp index 0daf654be..52917bff5 100644 --- a/src/src/Commands/GPIO.cpp +++ b/src/src/Commands/GPIO.cpp @@ -652,10 +652,19 @@ void createAndSetPortStatus_Mode_State(uint32_t key, uint8_t newMode, int8_t new } #endif - + auto it = globalMapPortStatus.find(key); + if (it == globalMapPortStatus.end()) { +#ifdef ESP32 + if (getPluginFromKey(key).value == PLUGIN_GPIO_INT) { + gpio_reset_pin((gpio_num_t)getPortFromKey(key)); + } +#endif + } // If it doesn't exist, it is now created. globalMapPortStatus[key].mode = newMode; - auto it = globalMapPortStatus.find(key); + if (it == globalMapPortStatus.end()) { + it = globalMapPortStatus.find(key); + } if (it != globalMapPortStatus.end()) { // Should always be true, as it would be created if it didn't exist. diff --git a/src/src/ESPEasyCore/ESPEasyGPIO.cpp b/src/src/ESPEasyCore/ESPEasyGPIO.cpp index 0df42a3e2..964cf712e 100644 --- a/src/src/ESPEasyCore/ESPEasyGPIO.cpp +++ b/src/src/ESPEasyCore/ESPEasyGPIO.cpp @@ -10,6 +10,8 @@ #include "../Helpers/PortStatus.h" +#include + //******************************************************************************** // Internal GPIO write @@ -23,8 +25,11 @@ void GPIO_Internal_Write(int pin, uint8_t value) if (it->second.mode == PIN_MODE_PWM) { set_Gpio_PWM(pin, value); } else { - pinMode(pin, OUTPUT); - digitalWrite(pin, value); + //gpio_reset_pin(static_cast(pin)); + //pinMode(pin, OUTPUT); + //digitalWrite(pin, value); + DIRECT_PINMODE_OUTPUT(pin); + DIRECT_pinWrite(pin, value); } } } @@ -36,7 +41,7 @@ void GPIO_Internal_Write(int pin, uint8_t value) bool GPIO_Internal_Read(int pin) { if (checkValidPortRange(PLUGIN_GPIO, pin)) - return digitalRead(pin)==HIGH; + return DIRECT_pinRead(pin) != 0u; return false; } @@ -81,7 +86,7 @@ bool GPIO_Read_Switch_State(int pin, uint8_t pinMode) { // Do not read from the pin while mode is set to PWM or servo. // See https://github.com/letscontrolit/ESPEasy/issues/2117#issuecomment-443516794 - return digitalRead(pin) == HIGH; + return DIRECT_pinRead(pin) != 0u; } #ifdef USES_P009 diff --git a/src/src/Helpers/Hardware_I2C.cpp b/src/src/Helpers/Hardware_I2C.cpp index 58dc7304f..49dcafc8c 100644 --- a/src/src/Helpers/Hardware_I2C.cpp +++ b/src/src/Helpers/Hardware_I2C.cpp @@ -148,6 +148,14 @@ void I2CBegin(int8_t sda, int8_t scl, uint32_t clockFreq, uint32_t clockStretch) // No need to change the clock speed. return; } + if (sda == -1 || scl == -1) { + Wire.end(); + last_sda = sda; + last_scl = scl; + return; + } + + #ifdef ESP32 if (((sda != last_sda) || (scl != last_scl)) && (last_sda != -1) && (last_scl != -1)) {