From 0475212b54cbebb48171c156ed954d1cd3bb194b Mon Sep 17 00:00:00 2001 From: Stephan Hadinger Date: Wed, 10 Mar 2021 22:20:21 +0100 Subject: [PATCH] ESP32 support for secondary I2C controller --- CHANGELOG.md | 1 + tasmota/support.ino | 12 +++++++++--- tasmota/support_command.ino | 7 ++++++- tasmota/support_tasmota.ino | 6 ++++++ tasmota/tasmota.h | 6 ++++++ tasmota/tasmota.ino | 3 +++ tasmota/tasmota_template.h | 4 ++-- 7 files changed, 33 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ded922bad..bdb20b6f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ All notable changes to this project will be documented in this file. - Crash protection in ext_vnsprintf_P (#11202) - Extent compile time SetOptions support (#11204) - ESP32 Extent BLE (#11212) +- ESP32 support for secondary I2C controller ### Changed - ESP32 core library from v1.0.5-rc6 to v1.0.5 diff --git a/tasmota/support.ino b/tasmota/support.ino index d5ba2d914..85e1a7341 100644 --- a/tasmota/support.ino +++ b/tasmota/support.ino @@ -1969,7 +1969,8 @@ int8_t I2cWriteBuffer(uint8_t addr, uint8_t reg, uint8_t *reg_data, uint16_t len return 0; } -void I2cScan(char *devs, unsigned int devs_len) +void I2cScan(char *devs, unsigned int devs_len, uint32_t bus = 0); +void I2cScan(char *devs, unsigned int devs_len, uint32_t bus) { // Return error codes defined in twi.h and core_esp8266_si2c.c // I2C_OK 0 @@ -1984,8 +1985,13 @@ void I2cScan(char *devs, unsigned int devs_len) snprintf_P(devs, devs_len, PSTR("{\"" D_CMND_I2CSCAN "\":\"" D_JSON_I2CSCAN_DEVICES_FOUND_AT)); for (address = 1; address <= 127; address++) { - Wire.beginTransmission(address); - error = Wire.endTransmission(); +#ifdef ESP32 + TwoWire & myWire = (bus == 0) ? Wire : Wire1; +#else + TwoWire & myWire = Wire; +#endif + myWire.beginTransmission(address); + error = myWire.endTransmission(); if (0 == error) { any = 1; snprintf_P(devs, devs_len, PSTR("%s 0x%02x"), devs, address); diff --git a/tasmota/support_command.ino b/tasmota/support_command.ino index 1a16279ad..528f9927b 100644 --- a/tasmota/support_command.ino +++ b/tasmota/support_command.ino @@ -2071,9 +2071,14 @@ void CmndWifiPower(void) #ifdef USE_I2C void CmndI2cScan(void) { - if (TasmotaGlobal.i2c_enabled) { + if ((1 == XdrvMailbox.index) && (TasmotaGlobal.i2c_enabled)) { I2cScan(TasmotaGlobal.mqtt_data, sizeof(TasmotaGlobal.mqtt_data)); } +#ifdef ESP32 + if ((2 == XdrvMailbox.index) && (TasmotaGlobal.i2c_enabled_2)) { + I2cScan(TasmotaGlobal.mqtt_data, sizeof(TasmotaGlobal.mqtt_data), 1); + } +#endif } void CmndI2cDriver(void) diff --git a/tasmota/support_tasmota.ino b/tasmota/support_tasmota.ino index 9604be4ee..4c20486b2 100644 --- a/tasmota/support_tasmota.ino +++ b/tasmota/support_tasmota.ino @@ -1742,6 +1742,12 @@ void GpioInit(void) if (TasmotaGlobal.i2c_enabled) { Wire.begin(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)); + } +#endif #endif // USE_I2C TasmotaGlobal.devices_present = 0; diff --git a/tasmota/tasmota.h b/tasmota/tasmota.h index 5f2f73c0b..87e5f01b3 100644 --- a/tasmota/tasmota.h +++ b/tasmota/tasmota.h @@ -84,6 +84,12 @@ const uint8_t MAX_PCF8574 = 4; // Max number of PCF8574 devices const uint8_t MAX_RULE_SETS = 3; // Max number of rule sets of size 512 characters const uint16_t MAX_RULE_SIZE = 512; // Max number of characters in rules +#ifdef ESP32 +const uint8_t MAX_I2C = 2; // Max number of I2C controllers (ESP32 = 2) +#else +const uint8_t MAX_I2C = 0; // Max number of I2C controllers (ESP8266 = 0, no choice) +#endif + // Changes to the following MAX_ defines need to be in line with enum SettingsTextIndex const uint8_t MAX_MQTT_PREFIXES = 3; // Max number of MQTT prefixes (cmnd, stat, tele) const uint8_t MAX_SSIDS = 2; // Max number of SSIDs diff --git a/tasmota/tasmota.ino b/tasmota/tasmota.ino index 637d2b84b..54db9ae17 100644 --- a/tasmota/tasmota.ino +++ b/tasmota/tasmota.ino @@ -141,6 +141,9 @@ struct { bool blinkstate; // LED state bool pwm_present; // Any PWM channel configured with SetOption15 0 bool i2c_enabled; // I2C configured +#ifdef ESP32 + bool i2c_enabled_2; // I2C configured, second controller on ESP32, Wire1 +#endif bool ntp_force_sync; // Force NTP sync bool skip_light_fade; // Temporarily skip light fading bool restart_halt; // Do not restart but stay in wait loop diff --git a/tasmota/tasmota_template.h b/tasmota/tasmota_template.h index c61329260..7b9bac855 100644 --- a/tasmota/tasmota_template.h +++ b/tasmota/tasmota_template.h @@ -386,8 +386,8 @@ const uint16_t kGpioNiceList[] PROGMEM = { \*-------------------------------------------------------------------------------------------*/ #ifdef USE_I2C - AGPIO(GPIO_I2C_SCL), // I2C SCL - AGPIO(GPIO_I2C_SDA), // I2C SDA + AGPIO(GPIO_I2C_SCL) + MAX_I2C, // I2C SCL + AGPIO(GPIO_I2C_SDA) + MAX_I2C, // I2C SDA #endif #ifdef USE_SPI