diff --git a/tasmota/include/i18n.h b/tasmota/include/i18n.h index e228989e1..8cb8a5da1 100644 --- a/tasmota/include/i18n.h +++ b/tasmota/include/i18n.h @@ -100,9 +100,6 @@ #define D_JSON_OTHER_HTTP_ERROR "Other http error" #define D_JSON_HSBCOLOR "HSBColor" #define D_JSON_HUMIDITY "Humidity" -#define D_JSON_I2CSCAN_DEVICES_FOUND_AT "Device(s) found at" -#define D_JSON_I2CSCAN_UNKNOWN_ERROR_AT "Unknown error at" -#define D_JSON_I2CSCAN_NO_DEVICES_FOUND "No devices found" #define D_JSON_ID "Id" #define D_JSON_ILLUMINANCE "Illuminance" #define D_JSON_IMPORT_ACTIVE "ImportActive" @@ -868,7 +865,6 @@ const char JSON_SNS_RANGE[] PROGMEM = ",\"%s\":{\"" D_JSON_RANGE "\":%d}"; const char JSON_SNS_GNGPM[] PROGMEM = ",\"%s\":{\"" D_JSON_TOTAL_USAGE "\":%s,\"" D_JSON_FLOWRATE "\":%s}"; const char S_LOG_I2C_FOUND_AT[] PROGMEM = D_LOG_I2C "%s " D_FOUND_AT " 0x%x"; -const char S_LOG_I2C_FOUND_AT_PORT[] PROGMEM = D_LOG_I2C "%s " D_FOUND_AT " 0x%x (" D_PORT " %d)"; const char S_RSLT_POWER[] PROGMEM = D_RSLT_POWER; const char S_RSLT_RESULT[] PROGMEM = D_RSLT_RESULT; diff --git a/tasmota/tasmota_support/support_a_i2c.ino b/tasmota/tasmota_support/support_a_i2c.ino index 25d81427c..a3fef3c2f 100644 --- a/tasmota/tasmota_support/support_a_i2c.ino +++ b/tasmota/tasmota_support/support_a_i2c.ino @@ -50,15 +50,16 @@ bool I2c2Begin(int sda, int scl, uint32_t frequency) { #endif bool I2cValidRead(uint8_t addr, uint8_t reg, uint8_t size, uint8_t bus = 0) { - uint8_t retry = I2C_RETRY_COUNTER; - bool status = false; + i2c_buffer = 0; #ifdef ESP32 - if (!TasmotaGlobal.i2c_enabled_2) { bus = 0; } + if (bus && !TasmotaGlobal.i2c_enabled_2) { return false; } // Error TwoWire & myWire = (bus == 0) ? Wire : Wire1; #else + if (bus) { return false; } // Second I2c bus ESP32 only TwoWire & myWire = Wire; #endif - i2c_buffer = 0; + uint8_t retry = I2C_RETRY_COUNTER; + bool status = false; while (!status && retry) { myWire.beginTransmission(addr); // start transmission to device myWire.write(reg); // sends register address to read from @@ -146,14 +147,14 @@ int32_t I2cRead24(uint8_t addr, uint8_t reg, uint8_t bus = 0) { } bool I2cWrite(uint8_t addr, uint8_t reg, uint32_t val, uint8_t size, uint8_t bus = 0) { - uint8_t x = I2C_RETRY_COUNTER; #ifdef ESP32 - if (!TasmotaGlobal.i2c_enabled_2) { bus = 0; } + if (bus && !TasmotaGlobal.i2c_enabled_2) { return false; } // Error TwoWire & myWire = (bus == 0) ? Wire : Wire1; #else - if (bus != 0) { return false; } // Second I2c bus ESP32 only + if (bus) { return false; } // Second I2c bus ESP32 only TwoWire & myWire = Wire; #endif + uint8_t x = I2C_RETRY_COUNTER; do { myWire.beginTransmission((uint8_t)addr); // start transmission to device myWire.write(reg); // sends register address to write to @@ -176,31 +177,31 @@ bool I2cWrite16(uint8_t addr, uint8_t reg, uint32_t val, uint8_t bus = 0) { bool I2cReadBuffer(uint8_t addr, uint8_t reg, uint8_t *reg_data, uint16_t len, uint8_t bus = 0) { #ifdef ESP32 - if (!TasmotaGlobal.i2c_enabled_2) { bus = 0; } + if (bus && !TasmotaGlobal.i2c_enabled_2) { return true; } // Error TwoWire & myWire = (bus == 0) ? Wire : Wire1; #else - if (bus != 0) { return false; } // Second I2c bus ESP32 only + if (bus) { return true; } // Second I2c bus ESP32 only TwoWire & myWire = Wire; #endif myWire.beginTransmission((uint8_t)addr); myWire.write((uint8_t)reg); myWire.endTransmission(); if (len != myWire.requestFrom((uint8_t)addr, (uint8_t)len)) { - return 1; + return true; // Error } while (len--) { *reg_data = (uint8_t)myWire.read(); reg_data++; } - return 0; + return false; // OK } int8_t I2cWriteBuffer(uint8_t addr, uint8_t reg, uint8_t *reg_data, uint16_t len, uint8_t bus = 0) { #ifdef ESP32 - if (!TasmotaGlobal.i2c_enabled_2) { bus = 0; } + if (bus && !TasmotaGlobal.i2c_enabled_2) { return 1; } // Error TwoWire & myWire = (bus == 0) ? Wire : Wire1; #else - if (bus != 0) { return false; } // Second I2c bus ESP32 only + if (bus) { return 1; } // Second I2c bus ESP32 only TwoWire & myWire = Wire; #endif myWire.beginTransmission((uint8_t)addr); @@ -210,7 +211,7 @@ int8_t I2cWriteBuffer(uint8_t addr, uint8_t reg, uint8_t *reg_data, uint16_t len reg_data++; } myWire.endTransmission(); - return 0; + return 0; // OK } void I2cScan(uint8_t bus = 0) { @@ -229,39 +230,40 @@ void I2cScan(uint8_t bus = 0) { // 4: other error // 5: timeout +#ifdef ESP32 + if (bus && !TasmotaGlobal.i2c_enabled_2) { return; } + TwoWire & myWire = (bus == 0) ? Wire : Wire1; + Response_P(PSTR("{\"" D_CMND_I2CSCAN "\":\"Device(s) found on bus%d at"), bus +1); +#else + if (bus) { return; } // Second I2c bus ESP32 only + TwoWire & myWire = Wire; + Response_P(PSTR("{\"" D_CMND_I2CSCAN "\":\"Device(s) found at")); +#endif uint8_t error = 0; uint8_t address = 0; uint8_t any = 0; - - 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 - if (bus != 0) { return; } // Second I2c bus ESP32 only - TwoWire & myWire = Wire; -#endif myWire.beginTransmission(address); error = myWire.endTransmission(); if (0 == error) { any = 1; -#ifdef ESP32 - ResponseAppend_P(PSTR(" 0x%02x:%d"), address, bus); -#else ResponseAppend_P(PSTR(" 0x%02x"), address); -#endif } else if (error != 2) { // Seems to happen anyway using this scan any = 2; - Response_P(PSTR("{\"" D_CMND_I2CSCAN "\":\"Error %d at 0x%02x bus %d"), error, address, bus); + Response_P(PSTR("{\"" D_CMND_I2CSCAN "\":\"Error %d at 0x%02x"), error, address); +#ifdef ESP32 + if (bus) { + ResponseAppend_P(PSTR(" (bus2)")); + } +#endif break; } } if (any) { ResponseAppend_P(PSTR("\"}")); } else { - Response_P(PSTR("{\"" D_CMND_I2CSCAN "\":\"" D_JSON_I2CSCAN_NO_DEVICES_FOUND "\"}")); + Response_P(PSTR("{\"" D_CMND_I2CSCAN "\":\"No devices found\"}")); } } @@ -294,11 +296,11 @@ void I2cSetActive(uint32_t addr, uint32_t count = 1, uint8_t bus = 0) { void I2cSetActiveFound(uint32_t addr, const char *types, uint8_t bus = 0) { I2cSetActive(addr, bus); #ifdef ESP32 - if (1 == bus) { - AddLog(LOG_LEVEL_INFO, S_LOG_I2C_FOUND_AT_PORT, types, addr, bus); + if (bus) { + AddLog(LOG_LEVEL_INFO, PSTR("I2C: %s found at 0x%02x (bus2)"), types, addr); } else #endif // ESP32 - AddLog(LOG_LEVEL_INFO, S_LOG_I2C_FOUND_AT, types, addr); + AddLog(LOG_LEVEL_INFO, PSTR("I2C: %s found at 0x%02x"), types, addr); } bool I2cActive(uint32_t addr, uint8_t bus = 0) { @@ -311,9 +313,10 @@ bool I2cActive(uint32_t addr, uint8_t bus = 0) { bool I2cSetDevice(uint32_t addr, uint8_t bus = 0) { #ifdef ESP32 - if (!TasmotaGlobal.i2c_enabled_2) { bus = 0; } + if (bus && !TasmotaGlobal.i2c_enabled_2) { return false; } // If no second bus report as not present; TwoWire & myWire = (bus == 0) ? Wire : Wire1; #else + bus = 0; TwoWire & myWire = Wire; #endif addr &= 0x7F; // Max I2C address is 127 @@ -325,10 +328,11 @@ bool I2cSetDevice(uint32_t addr, uint8_t bus = 0) { uint32_t err = myWire.endTransmission(); if (err && (err != 2)) { #ifdef ESP32 - AddLog(LOG_LEVEL_DEBUG, PSTR("I2C: Error %d at 0x%02x bus %d"), err, addr, bus); -#else - AddLog(LOG_LEVEL_DEBUG, PSTR("I2C: Error %d at 0x%02x"), err, addr); + if (bus) { + AddLog(LOG_LEVEL_DEBUG, PSTR("I2C: Error %d at 0x%02x (bus2)"), err, addr); + } else #endif + AddLog(LOG_LEVEL_DEBUG, PSTR("I2C: Error %d at 0x%02x"), err, addr); } return (0 == err); } diff --git a/tasmota/tasmota_xsns_sensor/xsns_09_bmp.ino b/tasmota/tasmota_xsns_sensor/xsns_09_bmp.ino index 136d4e912..bc8f588f0 100644 --- a/tasmota/tasmota_xsns_sensor/xsns_09_bmp.ino +++ b/tasmota/tasmota_xsns_sensor/xsns_09_bmp.ino @@ -57,7 +57,8 @@ const char kBmpTypes[] PROGMEM = "BMP180|BMP280|BME280|BME680"; typedef struct { - uint8_t bmp_address; // I2C bus address + uint8_t bmp_address; // I2C address + uint8_t bmp_bus; // I2C bus char bmp_name[7]; // Sensor name - "BMPXXX" uint8_t bmp_type; uint8_t bmp_model; @@ -119,17 +120,19 @@ bool Bmp180Calibration(uint8_t bmp_idx) { bmp180_cal_data = (bmp180_cal_data_t*)malloc(BMP_MAX_SENSORS * sizeof(bmp180_cal_data_t)); } if (!bmp180_cal_data) { return false; } - uint8_t bus= (bmp_idx>=2) ? 1 : 0; // first two BMP's at bus 0, additional at bus 1 (ESP32 32 only) - bmp180_cal_data[bmp_idx].cal_ac1 = I2cRead16(bmp_sensors[bmp_idx].bmp_address, BMP180_AC1,bus); - bmp180_cal_data[bmp_idx].cal_ac2 = I2cRead16(bmp_sensors[bmp_idx].bmp_address, BMP180_AC2,bus); - bmp180_cal_data[bmp_idx].cal_ac3 = I2cRead16(bmp_sensors[bmp_idx].bmp_address, BMP180_AC3,bus); - bmp180_cal_data[bmp_idx].cal_ac4 = I2cRead16(bmp_sensors[bmp_idx].bmp_address, BMP180_AC4,bus); - bmp180_cal_data[bmp_idx].cal_ac5 = I2cRead16(bmp_sensors[bmp_idx].bmp_address, BMP180_AC5,bus); - bmp180_cal_data[bmp_idx].cal_ac6 = I2cRead16(bmp_sensors[bmp_idx].bmp_address, BMP180_AC6,bus); - bmp180_cal_data[bmp_idx].cal_b1 = I2cRead16(bmp_sensors[bmp_idx].bmp_address, BMP180_VB1,bus); - bmp180_cal_data[bmp_idx].cal_b2 = I2cRead16(bmp_sensors[bmp_idx].bmp_address, BMP180_VB2,bus); - bmp180_cal_data[bmp_idx].cal_mc = I2cRead16(bmp_sensors[bmp_idx].bmp_address, BMP180_MC,bus); - bmp180_cal_data[bmp_idx].cal_md = I2cRead16(bmp_sensors[bmp_idx].bmp_address, BMP180_MD,bus); + + uint8_t address = bmp_sensors[bmp_idx].bmp_address; + uint8_t bus = bmp_sensors[bmp_idx].bmp_bus; + bmp180_cal_data[bmp_idx].cal_ac1 = I2cRead16(address, BMP180_AC1, bus); + bmp180_cal_data[bmp_idx].cal_ac2 = I2cRead16(address, BMP180_AC2, bus); + bmp180_cal_data[bmp_idx].cal_ac3 = I2cRead16(address, BMP180_AC3, bus); + bmp180_cal_data[bmp_idx].cal_ac4 = I2cRead16(address, BMP180_AC4, bus); + bmp180_cal_data[bmp_idx].cal_ac5 = I2cRead16(address, BMP180_AC5, bus); + bmp180_cal_data[bmp_idx].cal_ac6 = I2cRead16(address, BMP180_AC6, bus); + bmp180_cal_data[bmp_idx].cal_b1 = I2cRead16(address, BMP180_VB1, bus); + bmp180_cal_data[bmp_idx].cal_b2 = I2cRead16(address, BMP180_VB2, bus); + bmp180_cal_data[bmp_idx].cal_mc = I2cRead16(address, BMP180_MC, bus); + bmp180_cal_data[bmp_idx].cal_md = I2cRead16(address, BMP180_MD, bus); // Check for Errors in calibration data. Value never is 0x0000 or 0xFFFF if (!bmp180_cal_data[bmp_idx].cal_ac1 | !bmp180_cal_data[bmp_idx].cal_ac2 | @@ -162,18 +165,19 @@ bool Bmp180Calibration(uint8_t bmp_idx) { void Bmp180Read(uint8_t bmp_idx) { if (!bmp180_cal_data) { return; } - uint8_t bus = bmp_idx >>1; // First two BMP's at bus 0, additional at bus 1 - I2cWrite8(bmp_sensors[bmp_idx].bmp_address, BMP180_REG_CONTROL, BMP180_TEMPERATURE, bus); + uint8_t address = bmp_sensors[bmp_idx].bmp_address; + uint8_t bus = bmp_sensors[bmp_idx].bmp_bus; + I2cWrite8(address, BMP180_REG_CONTROL, BMP180_TEMPERATURE, bus); delay(5); // 5ms conversion time - int ut = I2cRead16(bmp_sensors[bmp_idx].bmp_address, BMP180_REG_RESULT, bus); + int ut = I2cRead16(address, BMP180_REG_RESULT, bus); int32_t xt1 = (ut - (int32_t)bmp180_cal_data[bmp_idx].cal_ac6) * ((int32_t)bmp180_cal_data[bmp_idx].cal_ac5) >> 15; int32_t xt2 = ((int32_t)bmp180_cal_data[bmp_idx].cal_mc << 11) / (xt1 + (int32_t)bmp180_cal_data[bmp_idx].cal_md); int32_t bmp180_b5 = xt1 + xt2; bmp_sensors[bmp_idx].bmp_temperature = ((bmp180_b5 + 8) >> 4) / 10.0f; - I2cWrite8(bmp_sensors[bmp_idx].bmp_address, BMP180_REG_CONTROL, BMP180_PRESSURE3, bus); // Highest resolution + I2cWrite8(address, BMP180_REG_CONTROL, BMP180_PRESSURE3, bus); // Highest resolution delay(2 + (4 << BMP180_OSS)); // 26ms conversion time at ultra high resolution - uint32_t up = I2cRead24(bmp_sensors[bmp_idx].bmp_address, BMP180_REG_RESULT, bus); + uint32_t up = I2cRead24(address, BMP180_REG_RESULT, bus); up >>= (8 - BMP180_OSS); int32_t b6 = bmp180_b5 - 4000; @@ -265,33 +269,34 @@ bool Bmx280Calibrate(uint8_t bmp_idx) { } if (!Bme280CalibrationData) { return false; } - uint8_t bus = bmp_idx >>1; // First two BMP's at bus 0, additional at bus 1 - Bme280CalibrationData[bmp_idx].dig_T1 = I2cRead16LE(bmp_sensors[bmp_idx].bmp_address, BME280_REGISTER_DIG_T1, bus); - Bme280CalibrationData[bmp_idx].dig_T2 = I2cReadS16_LE(bmp_sensors[bmp_idx].bmp_address, BME280_REGISTER_DIG_T2, bus); - Bme280CalibrationData[bmp_idx].dig_T3 = I2cReadS16_LE(bmp_sensors[bmp_idx].bmp_address, BME280_REGISTER_DIG_T3, bus); - Bme280CalibrationData[bmp_idx].dig_P1 = I2cRead16LE(bmp_sensors[bmp_idx].bmp_address, BME280_REGISTER_DIG_P1, bus); - Bme280CalibrationData[bmp_idx].dig_P2 = I2cReadS16_LE(bmp_sensors[bmp_idx].bmp_address, BME280_REGISTER_DIG_P2, bus); - Bme280CalibrationData[bmp_idx].dig_P3 = I2cReadS16_LE(bmp_sensors[bmp_idx].bmp_address, BME280_REGISTER_DIG_P3, bus); - Bme280CalibrationData[bmp_idx].dig_P4 = I2cReadS16_LE(bmp_sensors[bmp_idx].bmp_address, BME280_REGISTER_DIG_P4, bus); - Bme280CalibrationData[bmp_idx].dig_P5 = I2cReadS16_LE(bmp_sensors[bmp_idx].bmp_address, BME280_REGISTER_DIG_P5, bus); - Bme280CalibrationData[bmp_idx].dig_P6 = I2cReadS16_LE(bmp_sensors[bmp_idx].bmp_address, BME280_REGISTER_DIG_P6, bus); - Bme280CalibrationData[bmp_idx].dig_P7 = I2cReadS16_LE(bmp_sensors[bmp_idx].bmp_address, BME280_REGISTER_DIG_P7, bus); - Bme280CalibrationData[bmp_idx].dig_P8 = I2cReadS16_LE(bmp_sensors[bmp_idx].bmp_address, BME280_REGISTER_DIG_P8, bus); - Bme280CalibrationData[bmp_idx].dig_P9 = I2cReadS16_LE(bmp_sensors[bmp_idx].bmp_address, BME280_REGISTER_DIG_P9, bus); + uint8_t address = bmp_sensors[bmp_idx].bmp_address; + uint8_t bus = bmp_sensors[bmp_idx].bmp_bus; + Bme280CalibrationData[bmp_idx].dig_T1 = I2cRead16LE(address, BME280_REGISTER_DIG_T1, bus); + Bme280CalibrationData[bmp_idx].dig_T2 = I2cReadS16_LE(address, BME280_REGISTER_DIG_T2, bus); + Bme280CalibrationData[bmp_idx].dig_T3 = I2cReadS16_LE(address, BME280_REGISTER_DIG_T3, bus); + Bme280CalibrationData[bmp_idx].dig_P1 = I2cRead16LE(address, BME280_REGISTER_DIG_P1, bus); + Bme280CalibrationData[bmp_idx].dig_P2 = I2cReadS16_LE(address, BME280_REGISTER_DIG_P2, bus); + Bme280CalibrationData[bmp_idx].dig_P3 = I2cReadS16_LE(address, BME280_REGISTER_DIG_P3, bus); + Bme280CalibrationData[bmp_idx].dig_P4 = I2cReadS16_LE(address, BME280_REGISTER_DIG_P4, bus); + Bme280CalibrationData[bmp_idx].dig_P5 = I2cReadS16_LE(address, BME280_REGISTER_DIG_P5, bus); + Bme280CalibrationData[bmp_idx].dig_P6 = I2cReadS16_LE(address, BME280_REGISTER_DIG_P6, bus); + Bme280CalibrationData[bmp_idx].dig_P7 = I2cReadS16_LE(address, BME280_REGISTER_DIG_P7, bus); + Bme280CalibrationData[bmp_idx].dig_P8 = I2cReadS16_LE(address, BME280_REGISTER_DIG_P8, bus); + Bme280CalibrationData[bmp_idx].dig_P9 = I2cReadS16_LE(address, BME280_REGISTER_DIG_P9, bus); if (BME280_CHIPID == bmp_sensors[bmp_idx].bmp_type) { // #1051 - Bme280CalibrationData[bmp_idx].dig_H1 = I2cRead8(bmp_sensors[bmp_idx].bmp_address, BME280_REGISTER_DIG_H1, bus); - Bme280CalibrationData[bmp_idx].dig_H2 = I2cReadS16_LE(bmp_sensors[bmp_idx].bmp_address, BME280_REGISTER_DIG_H2, bus); - Bme280CalibrationData[bmp_idx].dig_H3 = I2cRead8(bmp_sensors[bmp_idx].bmp_address, BME280_REGISTER_DIG_H3, bus); - Bme280CalibrationData[bmp_idx].dig_H4 = (I2cRead8(bmp_sensors[bmp_idx].bmp_address, BME280_REGISTER_DIG_H4, bus) << 4) | (I2cRead8(bmp_sensors[bmp_idx].bmp_address, BME280_REGISTER_DIG_H4 + 1,bus) & 0xF); - Bme280CalibrationData[bmp_idx].dig_H5 = (I2cRead8(bmp_sensors[bmp_idx].bmp_address, BME280_REGISTER_DIG_H5 + 1, bus) << 4) | (I2cRead8(bmp_sensors[bmp_idx].bmp_address, BME280_REGISTER_DIG_H5,bus) >> 4); - Bme280CalibrationData[bmp_idx].dig_H6 = (int8_t)I2cRead8(bmp_sensors[bmp_idx].bmp_address, BME280_REGISTER_DIG_H6, bus); - I2cWrite8(bmp_sensors[bmp_idx].bmp_address, BME280_REGISTER_CONTROL, 0x00, bus); // sleep mode since writes to config can be ignored in normal mode (Datasheet 5.4.5/6 page 27) + Bme280CalibrationData[bmp_idx].dig_H1 = I2cRead8(address, BME280_REGISTER_DIG_H1, bus); + Bme280CalibrationData[bmp_idx].dig_H2 = I2cReadS16_LE(address, BME280_REGISTER_DIG_H2, bus); + Bme280CalibrationData[bmp_idx].dig_H3 = I2cRead8(address, BME280_REGISTER_DIG_H3, bus); + Bme280CalibrationData[bmp_idx].dig_H4 = (I2cRead8(address, BME280_REGISTER_DIG_H4, bus) << 4) | (I2cRead8(address, BME280_REGISTER_DIG_H4 + 1, bus) & 0xF); + Bme280CalibrationData[bmp_idx].dig_H5 = (I2cRead8(address, BME280_REGISTER_DIG_H5 + 1, bus) << 4) | (I2cRead8(address, BME280_REGISTER_DIG_H5, bus) >> 4); + Bme280CalibrationData[bmp_idx].dig_H6 = (int8_t)I2cRead8(address, BME280_REGISTER_DIG_H6, bus); + I2cWrite8(address, BME280_REGISTER_CONTROL, 0x00, bus); // sleep mode since writes to config can be ignored in normal mode (Datasheet 5.4.5/6 page 27) // Set before CONTROL_meas (DS 5.4.3) - I2cWrite8(bmp_sensors[bmp_idx].bmp_address, BME280_REGISTER_CONTROLHUMID, 0x01, bus); // 1x oversampling - I2cWrite8(bmp_sensors[bmp_idx].bmp_address, BME280_REGISTER_CONFIG, 0xA0, bus); // 1sec standby between measurements (to limit self heating), IIR filter off - I2cWrite8(bmp_sensors[bmp_idx].bmp_address, BME280_REGISTER_CONTROL, 0x27, bus); // 1x oversampling, normal mode + I2cWrite8(address, BME280_REGISTER_CONTROLHUMID, 0x01, bus); // 1x oversampling + I2cWrite8(address, BME280_REGISTER_CONFIG, 0xA0, bus); // 1sec standby between measurements (to limit self heating), IIR filter off + I2cWrite8(address, BME280_REGISTER_CONTROL, 0x27, bus); // 1x oversampling, normal mode } else { - I2cWrite8(bmp_sensors[bmp_idx].bmp_address, BME280_REGISTER_CONTROL, 0xB7, bus); // 16x oversampling, normal mode (Adafruit) + I2cWrite8(address, BME280_REGISTER_CONTROL, 0xB7, bus); // 16x oversampling, normal mode (Adafruit) } return true; } @@ -299,8 +304,9 @@ bool Bmx280Calibrate(uint8_t bmp_idx) { void Bme280Read(uint8_t bmp_idx) { if (!Bme280CalibrationData) { return; } - uint8_t bus = bmp_idx >>1; // First two BMP's at bus 0, additional at bus 1 - int32_t adc_T = I2cRead24(bmp_sensors[bmp_idx].bmp_address, BME280_REGISTER_TEMPDATA, bus); + uint8_t address = bmp_sensors[bmp_idx].bmp_address; + uint8_t bus = bmp_sensors[bmp_idx].bmp_bus; + int32_t adc_T = I2cRead24(address, BME280_REGISTER_TEMPDATA, bus); adc_T >>= 4; int32_t vart1 = ((((adc_T >> 3) - ((int32_t)Bme280CalibrationData[bmp_idx].dig_T1 << 1))) * ((int32_t)Bme280CalibrationData[bmp_idx].dig_T2)) >> 11; @@ -310,7 +316,7 @@ void Bme280Read(uint8_t bmp_idx) { float T = (t_fine * 5 + 128) >> 8; bmp_sensors[bmp_idx].bmp_temperature = T / 100.0f; - int32_t adc_P = I2cRead24(bmp_sensors[bmp_idx].bmp_address, BME280_REGISTER_PRESSUREDATA, bus); + int32_t adc_P = I2cRead24(address, BME280_REGISTER_PRESSUREDATA, bus); adc_P >>= 4; int64_t var1 = ((int64_t)t_fine) - 128000; @@ -331,7 +337,7 @@ void Bme280Read(uint8_t bmp_idx) { if (BMP280_CHIPID == bmp_sensors[bmp_idx].bmp_type) { return; } - int32_t adc_H = I2cRead16(bmp_sensors[bmp_idx].bmp_address, BME280_REGISTER_HUMIDDATA, bus); + int32_t adc_H = I2cRead16(address, BME280_REGISTER_HUMIDDATA, bus); int32_t v_x1_u32r = (t_fine - ((int32_t)76800)); v_x1_u32r = (((((adc_H << 14) - (((int32_t)Bme280CalibrationData[bmp_idx].dig_H4) << 20) - @@ -360,19 +366,24 @@ struct bme68x_dev *bme_dev = nullptr; struct bme68x_conf *bme_conf = nullptr; struct bme68x_heatr_conf *bme_heatr_conf = nullptr; +uint8_t bmp68x_bus = 0; + +// bme68x callbacks static void Bme68x_Delayus(uint32_t period, void *intf_ptr) { delayMicroseconds(period); } int8_t Bme68x_i2c_read(uint8_t reg_addr, uint8_t *reg_data, uint32_t len, void *intf_ptr) { uint8_t dev_addr = *(uint8_t*)intf_ptr; - return I2cReadBuffer(dev_addr, reg_addr, reg_data, (uint16_t)len); + return I2cReadBuffer(dev_addr, reg_addr, reg_data, (uint16_t)len, bmp68x_bus); } int8_t Bme68x_i2c_write(uint8_t reg_addr, const uint8_t *reg_data, uint32_t len, void *intf_ptr) { uint8_t dev_addr = *(uint8_t*)intf_ptr; - return I2cWriteBuffer(dev_addr, reg_addr, (uint8_t *)reg_data, (uint16_t)len); + return I2cWriteBuffer(dev_addr, reg_addr, (uint8_t *)reg_data, (uint16_t)len, bmp68x_bus); } bool Bme680Init(uint8_t bmp_idx) { + bmp68x_bus = bmp_sensors[bmp_idx].bmp_bus; + if (!bme_dev) { bme_heatr_conf = (bme68x_heatr_conf*)malloc(BMP_MAX_SENSORS * sizeof(bme68x_heatr_conf)); bme_conf = (bme68x_conf*)malloc(BMP_MAX_SENSORS * sizeof(bme68x_conf)); @@ -420,6 +431,8 @@ bool Bme680Init(uint8_t bmp_idx) { void Bme680Read(uint8_t bmp_idx) { if (!bme_dev) { return; } + bmp68x_bus = bmp_sensors[bmp_idx].bmp_bus; + int8_t rslt = BME68X_OK; if (BME680_CHIPID == bmp_sensors[bmp_idx].bmp_type) { @@ -478,6 +491,7 @@ void BmpDetect(void) { uint8_t bmp_type = I2cRead8(bmp_addresses[i], BMP_REGISTER_CHIPID, bus); if (bmp_type) { bmp_sensors[bmp_count].bmp_address = bmp_addresses[i]; + bmp_sensors[bmp_count].bmp_bus = bus; bmp_sensors[bmp_count].bmp_type = bmp_type; bmp_sensors[bmp_count].bmp_model = 0; @@ -501,7 +515,7 @@ void BmpDetect(void) { } if (success) { GetTextIndexed(bmp_sensors[bmp_count].bmp_name, sizeof(bmp_sensors[bmp_count].bmp_name), bmp_sensors[bmp_count].bmp_model, kBmpTypes); - I2cSetActiveFound(bmp_sensors[bmp_count].bmp_address, bmp_sensors[bmp_count].bmp_name, bus); + I2cSetActiveFound(bmp_sensors[bmp_count].bmp_address, bmp_sensors[bmp_count].bmp_name, bmp_sensors[bmp_count].bmp_bus); bmp_count++; } } @@ -527,26 +541,31 @@ void BmpRead(void) { } } -void BmpShow(bool json) -{ +void BmpShow(bool json) { for (uint32_t bmp_idx = 0; bmp_idx < bmp_count; bmp_idx++) { if (bmp_sensors[bmp_idx].bmp_type) { float bmp_sealevel = ConvertPressureForSeaLevel(bmp_sensors[bmp_idx].bmp_pressure); float bmp_temperature = ConvertTemp(bmp_sensors[bmp_idx].bmp_temperature); float bmp_pressure = ConvertPressure(bmp_sensors[bmp_idx].bmp_pressure); - char name[12]; + char name[16]; + // BMP280 strlcpy(name, bmp_sensors[bmp_idx].bmp_name, sizeof(bmp_sensors[bmp_idx].bmp_name)); -#ifdef ESP32 - if (TasmotaGlobal.i2c_enabled_2) { - uint8_t bus = bmp_idx >>1; - if (bmp_count > 1) { - snprintf_P(name, sizeof(name), PSTR("%s%c%02X%c%1d"), name, IndexSeparator(), bmp_sensors[bmp_idx].bmp_address, IndexSeparator(), bus); // BMXXXX-XX-X - } - } else -#endif if (bmp_count > 1) { - snprintf_P(name, sizeof(name), PSTR("%s%c%02X"), name, IndexSeparator(), bmp_sensors[bmp_idx].bmp_address); // BMXXXX-XX + // BMP280-77 + snprintf_P(name, sizeof(name), PSTR("%s%c%02X"), name, IndexSeparator(), bmp_sensors[bmp_idx].bmp_address); +#ifdef ESP32 + if (TasmotaGlobal.i2c_enabled_2) { // Second bus enabled + uint8_t bus = bmp_sensors[0].bmp_bus; + for (uint32_t i = 1; i < bmp_count; i++) { + if (bus != bmp_sensors[i].bmp_bus) { // Different busses + // BMP280-77-1 + snprintf_P(name, sizeof(name), PSTR("%s%c%1d"), name, IndexSeparator(), bmp_sensors[bmp_idx].bmp_bus +1); + break; + } + } + } +#endif } char pressure[33];