From f32b64d5de918bf91a9e521bb946a127ce513b54 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Thu, 7 Nov 2019 16:56:05 +0100 Subject: [PATCH] Refactor I2C driver detection phase 2 --- API.md | 66 ++++++++++++++++++++++++++++++++++++ I2CDEVICES.md | 3 ++ tasmota/_changelog.ino | 1 + tasmota/support.ino | 20 +++++++---- tasmota/xdrv_10_scripter.ino | 5 +-- tasmota/xdsp_08_ILI9488.ino | 3 +- tasmota/xdsp_10_RA8876.ino | 3 +- tasmota/xsns_07_sht1x.ino | 7 ++-- tasmota/xsns_13_ina219.ino | 17 +++++----- tasmota/xsns_14_sht3x.ino | 8 +++-- tasmota/xsns_16_tsl2561.ino | 10 +++--- tasmota/xsns_32_mpu6050.ino | 16 +++------ tasmota/xsns_44_sps30.ino | 23 ++++++------- tasmota/xsns_45_vl53l0x.ino | 41 +++++++--------------- tasmota/xsns_46_MLX90614.ino | 15 ++++---- tasmota/xsns_54_ina226.ino | 35 +++++++++++++------ 16 files changed, 173 insertions(+), 100 deletions(-) diff --git a/API.md b/API.md index ef74a1af6..4f3813870 100644 --- a/API.md +++ b/API.md @@ -68,3 +68,69 @@ FUNC_DISPLAY_FONT_SIZE | | 6.1.1.7 | FUNC_DISPLAY_ROTATION | | 6.1.1.7 | FUNC_DISPLAY_DRAW_STRING | | 6.1.1.7 | FUNC_DISPLAY_ONOFF | | 6.1.1.7 | + +## Init sequence +The following list contains the callback init sequence + +CFG: Loaded from flash at FB, Count 1581 +xdrv - FUNC_SETTINGS_OVERRIDE +xdrv - FUNC_PIN_STATE +xsns - FUNC_PIN_STATE +xdrv - FUNC_MODULE_INIT +xlgt - FUNC_MODULE_INIT +xdrv - FUNC_PRE_INIT +xnrg - FUNC_PRE_INIT +SRC: Restart +xdrv - FUNC_SET_POWER +xlgt - FUNC_SET_CHANNELS +xdrv - FUNC_SET_DEVICE_POWER +Project tasmota Wemos 2 Version 7.0.0.3(tasmota)-STAGE +xdrv - FUNC_INIT +xsns - FUNC_INIT +I2C: ADS1115 found at 0x48 +xdrv - FUNC_LOOP +xsns - FUNC_LOOP +xdrv - FUNC_EVERY_50_MSECOND +xlgt - FUNC_SET_CHANNELS +xsns - FUNC_EVERY_50_MSECOND +xdrv - FUNC_EVERY_100_MSECOND +xsns - FUNC_EVERY_100_MSECOND +xdrv - FUNC_EVERY_250_MSECOND +xsns - FUNC_EVERY_250_MSECOND +xdrv - FUNC_EVERY_SECOND +xsns - FUNC_EVERY_SECOND +WIF: Attempting connection... +WIF: Network (re)scan started... +WIF: Attempting connection... +WIF: Attempting connection... +WIF: Attempting connection... +WIF: Network 0, AP1, SSId indebuurt1, Channel 1, BSSId 24:D3:F2:97:C0:A1, RSSI -86, Encryption 1 +WIF: Network 1, AP2, SSId indebuurt2, Channel 5, BSSId A0:AB:1B:7D:42:AC, RSSI -42, Encryption 1 +WIF: Network 2, AP-, SSId indebuurt3, Channel 12, BSSId 60:E3:27:58:77:E6, RSSI -84, Encryption 1 +WIF: Connecting to AP2 indebuurt2 in mode 11N as wemos2... +WIF: Attempting connection... +WIF: Attempting connection... +WIF: Attempting connection... +WIF: Connected +xdrv - FUNC_WEB_ADD_HANDLER +xsns - FUNC_WEB_ADD_HANDLER +HTP: Web server active on wemos2 with IP address 192.168.2.191 +NTP: Drift 0, (UTC) Wed Nov 06 13:57:08 2019, (DST) Sun Mar 31 02:00:00 2019, (STD) Sun Oct 27 03:00:00 2019 +APP: Boot Count 500 +MQT: Attempting connection... +MQT: Connected +MQT: tele/wemos2/LWT = Online (retained) +MQT: cmnd/wemos2/POWER = +MQT: Subscribe to cmnd/wemos2/# +MQT: Subscribe to cmnd/sonoffs/# +MQT: Subscribe to cmnd/DVES_15568C_fb/# +xdrv - FUNC_MQTT_SUBSCRIBE +MQT: tele/wemos2/INFO1 = {"Module":"Generic","Version":"7.0.0.3(tasmota)","FallbackTopic":"cmnd/DVES_15568C_fb/","GroupTopic":"cmnd/sonoffs/"} +MQT: tele/wemos2/INFO2 = {"WebServerMode":"Admin","Hostname":"wemos2","IPAddress":"192.168.2.191"} +MQT: tele/wemos2/INFO3 = {"RestartReason":"Software/System restart"} +MQT: stat/wemos2/RESULT = {"POWER1":"OFF"} +MQT: stat/wemos2/POWER1 = OFF +MQT: stat/wemos2/RESULT = {"POWER2":"ON"} +MQT: stat/wemos2/POWER2 = ON +xdrv - FUNC_MQTT_INIT +CFG: Saved to flash at FA, Count 1582, Bytes 4096 \ No newline at end of file diff --git a/I2CDEVICES.md b/I2CDEVICES.md index 9268a3d46..6358280ed 100644 --- a/I2CDEVICES.md +++ b/I2CDEVICES.md @@ -60,3 +60,6 @@ Index | Define | Driver | Device | Address(es) | Description 34 | USE_PAJ7620 | xsns_50 | PAJ7620 | 0x73 | Gesture sensor 35 | USE_INA226 | xsns_54 | INA226 | 0x40 - 0x41, 0x44 - 0x45 | Low voltage current sensor 36 | USE_HIH6 | xsns_55 | HIH6130 | 0x27 | Temperature and Humidity sensor + 37 | USE_24C256 | xdrv_10 | 24C256 | 0x50 | Scripter EEPROM storage + 38 | USE_DISPLAY_ILI9488 | xdsp_08 | FT6236 | 0x38 | Touch panel controller + 39 | USE_DISPLAY_RA8876 | xdsp_10 | FT5316 | 0x38 | Touch panel controller diff --git a/tasmota/_changelog.ino b/tasmota/_changelog.ino index 06a6fc670..80f514ab8 100644 --- a/tasmota/_changelog.ino +++ b/tasmota/_changelog.ino @@ -3,6 +3,7 @@ * Initial support for I2C driver runtime control using command I2CDriver and document I2CDEVICES.md * Fix random crash caused by UPNP flood * Add support for Honeywell HPMA115S0 particle concentration sensor by David Hunt (#6843) + * Remove driver xsns_12_ads1115_i2cdev replaced by xsns_12_ads1115 * * 7.0.0.2 20191102 * Add command WebColor19 to control color of Module and Name (#6811) diff --git a/tasmota/support.ino b/tasmota/support.ino index b5d1dfeb6..16eaa9c79 100644 --- a/tasmota/support.ino +++ b/tasmota/support.ino @@ -1486,6 +1486,17 @@ void I2cScan(char *devs, unsigned int devs_len) } } +void I2cResetActive(uint32_t addr, uint32_t count = 1) +{ + addr &= 0x7F; // Max I2C address is 127 + count &= 0x7F; // Max 4 x 32 bits available + while (count-- && (addr < 128)) { + i2c_active[addr / 32] &= ~(1 << (addr % 32)); + addr++; + } +// AddLog_P2(LOG_LEVEL_DEBUG, PSTR("I2C: Active %08X,%08X,%08X,%08X"), i2c_active[0], i2c_active[1], i2c_active[2], i2c_active[3]); +} + void I2cSetActive(uint32_t addr, uint32_t count = 1) { addr &= 0x7F; // Max I2C address is 127 @@ -1506,19 +1517,14 @@ bool I2cActive(uint32_t addr) return false; } -bool I2cDevice(uint32_t addr) +bool I2cSetDevice(uint32_t addr) { addr &= 0x7F; // Max I2C address is 127 if (I2cActive(addr)) { return false; // If already active report as not present; } Wire.beginTransmission((uint8_t)addr); - return (0 == Wire.endTransmission()); -} - -bool I2cSetDevice(uint32_t addr) -{ - bool result = I2cDevice(addr); + bool result = (0 == Wire.endTransmission()); if (result) { I2cSetActive(addr, 1); } diff --git a/tasmota/xdrv_10_scripter.ino b/tasmota/xdrv_10_scripter.ino index 4a49122ae..26584d7d7 100644 --- a/tasmota/xdrv_10_scripter.ino +++ b/tasmota/xdrv_10_scripter.ino @@ -38,6 +38,7 @@ keywords if then else endif, or, and are better readable for beginners (others m \*********************************************************************************************/ #define XDRV_10 10 +#define XI2C_37 37 // See I2CDEVICES.md #define SCRIPT_DEBUG 0 @@ -4729,8 +4730,8 @@ bool Xdrv10(uint8_t function) #ifdef USE_24C256 #ifndef USE_SCRIPT_FATFS - if (i2c_flg) { - if (I2cDevice(EEPROM_ADDRESS)) { + if (I2cEnabled(XI2C_37)) { + if (I2cSetDevice(EEPROM_ADDRESS)) { // found 32kb eeprom char *script; script=(char*)calloc(EEP_SCRIPT_SIZE+4,1); diff --git a/tasmota/xdsp_08_ILI9488.ino b/tasmota/xdsp_08_ILI9488.ino index b46976331..cba8137da 100644 --- a/tasmota/xdsp_08_ILI9488.ino +++ b/tasmota/xdsp_08_ILI9488.ino @@ -22,6 +22,7 @@ #ifdef USE_DISPLAY_ILI9488 #define XDSP_08 8 +#define XI2C_38 38 // See I2CDEVICES.md #define COLORED 1 #define UNCOLORED 0 @@ -112,7 +113,7 @@ void ILI9488_InitDriver() color_type = COLOR_COLOR; // start digitizer with fixed adress - if (i2c_flg && I2cDevice(FT6236_address)) { + if (I2cEnabled(XI2C_38) && I2cSetDevice(FT6236_address)) { FT6236begin(FT6236_address); FT6236_found=1; } else { diff --git a/tasmota/xdsp_10_RA8876.ino b/tasmota/xdsp_10_RA8876.ino index b88bf002c..47d48725d 100644 --- a/tasmota/xdsp_10_RA8876.ino +++ b/tasmota/xdsp_10_RA8876.ino @@ -22,6 +22,7 @@ #ifdef USE_DISPLAY_RA8876 #define XDSP_10 10 +#define XI2C_39 39 // See I2CDEVICES.md #define COLORED 1 #define UNCOLORED 0 @@ -97,7 +98,7 @@ void RA8876_InitDriver() #endif color_type = COLOR_COLOR; - if (i2c_flg && I2cDevice(FT5316_address)) { + if (I2cEnabled(XI2C_39) && I2cSetDevice(FT5316_address)) { FT6236begin(FT5316_address); FT5316_found=1; } else { diff --git a/tasmota/xsns_07_sht1x.ino b/tasmota/xsns_07_sht1x.ino index 7e0693b1c..75df93671 100644 --- a/tasmota/xsns_07_sht1x.ino +++ b/tasmota/xsns_07_sht1x.ino @@ -226,10 +226,6 @@ bool Xsns07(uint8_t function) bool result = false; switch (function) { -// case FUNC_PREP_BEFORE_TELEPERIOD: // As this is not a real I2C device it may interfere with other sensors - case FUNC_INIT: // Move detection to restart only removing interference - ShtDetect(); - break; case FUNC_EVERY_SECOND: ShtEverySecond(); break; @@ -241,6 +237,9 @@ bool Xsns07(uint8_t function) ShtShow(0); break; #endif // USE_WEBSERVER + case FUNC_INIT: // Move detection to restart only removing interference + ShtDetect(); + break; } return result; } diff --git a/tasmota/xsns_13_ina219.ino b/tasmota/xsns_13_ina219.ino index f9b8e31a4..03df212c0 100644 --- a/tasmota/xsns_13_ina219.ino +++ b/tasmota/xsns_13_ina219.ino @@ -198,13 +198,14 @@ bool Ina219CommandSensor(void) void Ina219Detect(void) { - for (int i=0; i, {m} = , {e} = @@ -76,12 +63,13 @@ const char HTTP_SNS_VL53L0X[] PROGMEM = #define USE_VL_MEDIAN -void Vl53l0Every_250MSecond() { +void Vl53l0Every_250MSecond(void) +{ + if (!vl53l0x_ready) { return; } + uint16_t tbuff[5],tmp; uint8_t flag; - if (!vl53l0x_ready) return; - // every 200 ms uint16_t dist = sensor.readRangeContinuousMillimeters(); if (dist==0 || dist>2000) { @@ -116,9 +104,7 @@ void Vl53l0Every_250MSecond() { void Vl53l0Show(boolean json) { - if (!vl53l0x_ready) { - return; - } + if (!vl53l0x_ready) { return; } if (json) { ResponseAppend_P(PSTR(",\"VL53L0X\":{\"" D_JSON_DISTANCE "\":%d}"), vl53l0x_distance); @@ -127,7 +113,6 @@ void Vl53l0Show(boolean json) WSContentSend_PD(HTTP_SNS_VL53L0X, vl53l0x_distance); #endif } - } /*********************************************************************************************\ @@ -141,9 +126,6 @@ bool Xsns45(byte function) bool result = false; switch (function) { - case FUNC_INIT: - Vl53l0Detect(); - break; case FUNC_EVERY_250_MSECOND: Vl53l0Every_250MSecond(); break; @@ -155,6 +137,9 @@ bool Xsns45(byte function) Vl53l0Show(0); break; #endif // USE_WEBSERVER + case FUNC_INIT: + Vl53l0Detect(); + break; } return result; } diff --git a/tasmota/xsns_46_MLX90614.ino b/tasmota/xsns_46_MLX90614.ino index d8afcbae7..943d32a65 100644 --- a/tasmota/xsns_46_MLX90614.ino +++ b/tasmota/xsns_46_MLX90614.ino @@ -29,13 +29,12 @@ uint8_t mlx_ready; float obj_temp; float amb_temp; -void MLX90614_Init() { - - if (!I2cDevice(I2_ADR_IRT)) { - return; - } +void MLX90614_Init(void) +{ + if (!I2cSetDevice(I2_ADR_IRT)) { return; } mlx_ready=1; + AddLog_P2(LOG_LEVEL_DEBUG, S_LOG_I2C_FOUND_AT, "MLX90614", I2_ADR_IRT); // not needed on tasmota //Wire.begin(); @@ -122,9 +121,6 @@ bool Xsns46(byte function) bool result = false; switch (function) { - case FUNC_INIT: - MLX90614_Init(); - break; case FUNC_EVERY_SECOND: MLX90614_Every_Second(); break; @@ -136,6 +132,9 @@ bool Xsns46(byte function) MLX90614_Show(0); break; #endif // USE_WEBSERVER + case FUNC_INIT: + MLX90614_Init(); + break; } return result; } diff --git a/tasmota/xsns_54_ina226.ino b/tasmota/xsns_54_ina226.ino index e92122571..59dfec774 100644 --- a/tasmota/xsns_54_ina226.ino +++ b/tasmota/xsns_54_ina226.ino @@ -176,6 +176,19 @@ bool Ina226TestPresence(uint8_t device) } +void Ina226ResetActive(void) +{ + Ina226SlaveInfo_t *p = slaveInfo; + + for (uint32_t i = 0; i < INA226_MAX_ADDRESSES; i++) { + p = &slaveInfo[i]; + // Address + uint8_t addr = p->address; + if (addr) { + I2cResetActive(addr); + } + } +} /* * Initialize INA226 devices @@ -183,20 +196,17 @@ bool Ina226TestPresence(uint8_t device) void Ina226Init() { - - uint32_t i; slavesFound = 0; Ina226SlaveInfo_t *p = slaveInfo; - //AddLog_P2( LOG_LEVEL_NONE, "Ina226Init"); - AddLog_P2( LOG_LEVEL_NONE, "Size of Settings: %d bytes", sizeof(Settings)); +// AddLog_P2( LOG_LEVEL_NONE, "Size of Settings: %d bytes", sizeof(Settings)); - if (!i2c_flg) - AddLog_P2(LOG_LEVEL_DEBUG, "INA226: Initialization failed: No I2C support"); +// if (!i2c_flg) +// AddLog_P2(LOG_LEVEL_DEBUG, "INA226: Initialization failed: No I2C support"); // Clear slave info data @@ -209,9 +219,11 @@ void Ina226Init() // Detect devices - for (i = 0; (i < INA226_MAX_ADDRESSES); i++){ + for (i = 0; i < INA226_MAX_ADDRESSES; i++){ uint8_t addr = pgm_read_byte(probeAddresses + i); + if (I2cActive(addr)) { continue; } + // Skip device probing if the full scale current is zero //AddLog_P2( LOG_LEVEL_NONE, "fs_i[%d]: %d", i, Settings.ina226_i_fs[i]); @@ -246,6 +258,8 @@ void Ina226Init() // store data in slave info struct. + I2cSetActive(addr); + p = &slaveInfo[i]; // Address p->address = addr; @@ -399,6 +413,7 @@ bool Ina226CommandSensor() // Device-less commands switch (p1){ case 1: // Rerun init + Ina226ResetActive(); Ina226Init(); Response_P(PSTR("{\"Sensor54-Command-Result\":{\"SlavesFound\":%d}}"),slavesFound); break; @@ -542,9 +557,6 @@ bool Xsns54(byte callback_id) case FUNC_EVERY_SECOND: Ina226EverySecond(); break; - case FUNC_INIT: - Ina226Init(); - break; case FUNC_JSON_APPEND: Ina226Show(1); break; @@ -558,6 +570,9 @@ bool Xsns54(byte callback_id) result = Ina226CommandSensor(); } break; + case FUNC_INIT: + Ina226Init(); + break; } // Return boolean result return result;