From f8843acbdedad4af394502c19b253b10060cf742 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Thu, 28 Oct 2021 15:57:24 +0200 Subject: [PATCH] Fix ESP core 2.0.0 I2C exception --- tasmota/support.ino | 21 +++++++++++++++++++++ tasmota/support_tasmota.ino | 4 ++-- tasmota/xdrv_52_3_berry_wire.ino | 9 +++++---- tasmota/xdsp_17_universal.ino | 8 ++++---- tasmota/xsns_07_sht1x.ino | 2 +- 5 files changed, 33 insertions(+), 11 deletions(-) diff --git a/tasmota/support.ino b/tasmota/support.ino index 26ce2b118..81eb25f1c 100644 --- a/tasmota/support.ino +++ b/tasmota/support.ino @@ -1997,7 +1997,24 @@ const uint8_t I2C_RETRY_COUNTER = 3; uint32_t i2c_active[4] = { 0 }; uint32_t i2c_buffer = 0; +void I2cBegin(int sda, int scl, uint32_t frequency = 100000); +void I2cBegin(int sda, int scl, uint32_t frequency) { +#ifdef ESP8266 + Wire.begin(sda, scl); +#endif #ifdef ESP32 + bool result = Wire.begin(sda, scl, frequency); + AddLog(LOG_LEVEL_DEBUG, PSTR("I2C: Begin %d"), result); +#endif +} + +#ifdef ESP32 +void I2c2Begin(int sda, int scl, uint32_t frequency = 100000); +void I2c2Begin(int sda, int scl, uint32_t frequency) { + bool result = Wire1.begin(sda, scl, frequency); + AddLog(LOG_LEVEL_DEBUG, PSTR("I2C: Bus 2 begin %d"), result); +} + bool I2cValidRead(uint8_t addr, uint8_t reg, uint8_t size, uint32_t bus = 0); bool I2cValidRead(uint8_t addr, uint8_t reg, uint8_t size, uint32_t bus) #else @@ -2007,6 +2024,7 @@ bool I2cValidRead(uint8_t addr, uint8_t reg, uint8_t size) uint8_t retry = I2C_RETRY_COUNTER; bool status = false; #ifdef ESP32 + if (!TasmotaGlobal.i2c_enabled_2) { bus = 0; } TwoWire & myWire = (bus == 0) ? Wire : Wire1; #else TwoWire & myWire = Wire; @@ -2121,6 +2139,7 @@ bool I2cWrite(uint8_t addr, uint8_t reg, uint32_t val, uint8_t size) uint8_t x = I2C_RETRY_COUNTER; #ifdef ESP32 + if (!TasmotaGlobal.i2c_enabled_2) { bus = 0; } TwoWire & myWire = (bus == 0) ? Wire : Wire1; #else TwoWire & myWire = Wire; @@ -2191,6 +2210,7 @@ void I2cScan(uint32_t bus) { Response_P(PSTR("{\"" D_CMND_I2CSCAN "\":\"" D_JSON_I2CSCAN_DEVICES_FOUND_AT)); for (address = 1; address <= 127; address++) { #ifdef ESP32 + if (!TasmotaGlobal.i2c_enabled_2) { bus = 0; } TwoWire & myWire = (bus == 0) ? Wire : Wire1; #else TwoWire & myWire = Wire; @@ -2269,6 +2289,7 @@ bool I2cSetDevice(uint32_t addr) #endif { #ifdef ESP32 + if (!TasmotaGlobal.i2c_enabled_2) { bus = 0; } TwoWire & myWire = (bus == 0) ? Wire : Wire1; #else TwoWire & myWire = Wire; diff --git a/tasmota/support_tasmota.ino b/tasmota/support_tasmota.ino index 4ebe63e5c..344a0c625 100644 --- a/tasmota/support_tasmota.ino +++ b/tasmota/support_tasmota.ino @@ -1901,12 +1901,12 @@ void GpioInit(void) #ifdef USE_I2C TasmotaGlobal.i2c_enabled = (PinUsed(GPIO_I2C_SCL) && PinUsed(GPIO_I2C_SDA)); if (TasmotaGlobal.i2c_enabled) { - Wire.begin(Pin(GPIO_I2C_SDA), Pin(GPIO_I2C_SCL)); + I2cBegin(Pin(GPIO_I2C_SDA), Pin(GPIO_I2C_SCL)); } #ifdef ESP32 TasmotaGlobal.i2c_enabled_2 = (PinUsed(GPIO_I2C_SCL, 1) && PinUsed(GPIO_I2C_SDA, 1)); if (TasmotaGlobal.i2c_enabled_2) { - Wire1.begin(Pin(GPIO_I2C_SDA, 1), Pin(GPIO_I2C_SCL, 1)); + I2c2Begin(Pin(GPIO_I2C_SDA, 1), Pin(GPIO_I2C_SCL, 1)); } #endif #endif // USE_I2C diff --git a/tasmota/xdrv_52_3_berry_wire.ino b/tasmota/xdrv_52_3_berry_wire.ino index 8aa404222..4f25aa00b 100644 --- a/tasmota/xdrv_52_3_berry_wire.ino +++ b/tasmota/xdrv_52_3_berry_wire.ino @@ -31,6 +31,7 @@ TwoWire & getWire(bvm *vm) { be_getmember(vm, 1, "bus"); int32_t bus = be_toint(vm, -1); // bus is 1 or 2 be_pop(vm, 1); + if (!TasmotaGlobal.i2c_enabled_2) { bus = 1; } if (2 != bus) { return Wire; } else { @@ -47,11 +48,11 @@ int32_t getBus(bvm *vm) { /*********************************************************************************************\ * Native functions mapped to Berry functions - * + * * import wire - * + * * wire.get_free_heap() -> int - * + * \*********************************************************************************************/ extern "C" { // Berry: `init([bus:int = 0]) -> nil @@ -105,7 +106,7 @@ extern "C" { int32_t b_wire_requestfrom(struct bvm *vm) { int32_t top = be_top(vm); // Get the number of arguments TwoWire & myWire = getWire(vm); - if ( (top == 3 || (top == 4 && be_isbool(vm, 4))) + if ( (top == 3 || (top == 4 && be_isbool(vm, 4))) && be_isint(vm, 2) && be_isint(vm, 3) ) { int32_t address = be_toint(vm, 2); int32_t quantity = be_toint(vm, 3); diff --git a/tasmota/xdsp_17_universal.ino b/tasmota/xdsp_17_universal.ino index ed8e52e60..d8be8b94b 100644 --- a/tasmota/xdsp_17_universal.ino +++ b/tasmota/xdsp_17_universal.ino @@ -161,11 +161,11 @@ uDisplay *udisp; replacepin(&cp, Pin(GPIO_OLED_RESET)); if (wire_n == 1) { - Wire.begin(sda, scl); + I2cBegin(sda, scl); } #ifdef ESP32 if (wire_n == 2) { - Wire1.begin(sda, scl); + I2c2Begin(sda, scl); } if (I2cSetDevice(i2caddr, wire_n - 1)) { I2cSetActiveFound(i2caddr, "DSP-I2C", wire_n - 1); @@ -257,11 +257,11 @@ uDisplay *udisp; scl = replacepin(&cp, Pin(GPIO_I2C_SCL, wire_n)); sda = replacepin(&cp, Pin(GPIO_I2C_SDA, wire_n)); if (wire_n == 0) { - Wire.begin(sda, scl); + I2cBegin(sda, scl); } #ifdef ESP32 if (wire_n == 1) { - Wire1.begin(sda, scl, 400000); + I2c2Begin(sda, scl, 400000); } if (I2cSetDevice(i2caddr, wire_n)) { I2cSetActiveFound(i2caddr, "FT5206", wire_n); diff --git a/tasmota/xsns_07_sht1x.ino b/tasmota/xsns_07_sht1x.ino index 6fe5c51db..5b5419648 100644 --- a/tasmota/xsns_07_sht1x.ino +++ b/tasmota/xsns_07_sht1x.ino @@ -165,7 +165,7 @@ void ShtDetect(void) sht_type = 1; AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_I2C D_SHT1X_FOUND)); } else { - Wire.begin(sht_sda_pin, sht_scl_pin); + I2cBegin(sht_sda_pin, sht_scl_pin); sht_type = 0; } }