Skip to content

Commit

Permalink
Fix ESP32 core 2.x I2C device detection speed
Browse files Browse the repository at this point in the history
  • Loading branch information
arendst committed Oct 31, 2021
1 parent 58935bf commit dba1f32
Show file tree
Hide file tree
Showing 30 changed files with 32 additions and 51 deletions.
2 changes: 1 addition & 1 deletion tasmota/xdrv_15_pca9685.ino
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ uint16_t pca9685_pin_pwm_value[16];

void PCA9685_Detect(void)
{
if (I2cActive(USE_PCA9685_ADDR)) { return; }
if (!I2cSetDevice(USE_PCA9685_ADDR)) { return; }

uint8_t buffer;
if (I2cValidRead8(&buffer, USE_PCA9685_ADDR, PCA9685_REG_MODE1)) {
Expand Down
2 changes: 1 addition & 1 deletion tasmota/xsns_09_bmp.ino
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ void BmpDetect(void)
memset(bmp_sensors, 0, bmp_sensor_size); // Init defaults to 0

for (uint32_t i = 0; i < BMP_MAX_SENSORS; i++) {
if (I2cActive(bmp_addresses[i])) { continue; }
if (!I2cSetDevice(bmp_addresses[i])) { continue; }
uint8_t bmp_type = I2cRead8(bmp_addresses[i], BMP_REGISTER_CHIPID);
if (bmp_type) {
bmp_sensors[bmp_count].bmp_address = bmp_addresses[i];
Expand Down
2 changes: 1 addition & 1 deletion tasmota/xsns_10_bh1750.ino
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ bool Bh1750Read(uint32_t sensor_index) {

void Bh1750Detect(void) {
for (uint32_t i = 0; i < sizeof(Bh1750.addresses); i++) {
if (I2cActive(Bh1750.addresses[i])) { continue; }
if (!I2cSetDevice(Bh1750.addresses[i])) { continue; }

Bh1750_sensors[Bh1750.count].address = Bh1750.addresses[i];
if (Bh1750SetMTreg(Bh1750.count)) {
Expand Down
2 changes: 1 addition & 1 deletion tasmota/xsns_11_veml6070.ino
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ char str_uvrisk_text[10];

void Veml6070Detect(void)
{
if (I2cActive(VEML6070_ADDR_L)) { return; }
if (!I2cSetDevice(VEML6070_ADDR_L)) { return; }

// init the UV sensor
Wire.beginTransmission(VEML6070_ADDR_L);
Expand Down
2 changes: 1 addition & 1 deletion tasmota/xsns_12_ads1115.ino
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ void Ads1115Detect(void)
for (uint32_t i = 0; i < sizeof(Ads1115.addresses); i++) {
if (!Ads1115.found[i]) {
Ads1115.address = Ads1115.addresses[i];
if (I2cActive(Ads1115.address)) { continue; }
if (!I2cSetDevice(Ads1115.address)) { continue; }
uint16_t buffer;
if (I2cValidRead16(&buffer, Ads1115.address, ADS1115_REG_POINTER_CONVERT) &&
I2cValidRead16(&buffer, Ads1115.address, ADS1115_REG_POINTER_CONFIG)) {
Expand Down
2 changes: 1 addition & 1 deletion tasmota/xsns_13_ina219.ino
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ void Ina219Detect(void)
{
for (uint32_t i = 0; i < sizeof(ina219_type); i++) {
uint16_t addr = ina219_addresses[i];
if (I2cActive(addr)) { continue; }
if (!I2cSetDevice(addr)) { continue; }
if (Ina219SetCalibration(Settings->ina219_mode, addr)) {
I2cSetActiveFound(addr, ina219_types);
ina219_type[i] = 1;
Expand Down
2 changes: 1 addition & 1 deletion tasmota/xsns_14_sht3x.ino
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ bool Sht3xRead(float &t, float &h, uint8_t sht3x_address)
void Sht3xDetect(void)
{
for (uint32_t i = 0; i < SHT3X_MAX_SENSORS; i++) {
if (I2cActive(sht3x_addresses[i])) { continue; }
if (!I2cSetDevice(sht3x_addresses[i])) { continue; }
float t;
float h;
if (Sht3xRead(t, h, sht3x_addresses[i])) {
Expand Down
6 changes: 1 addition & 5 deletions tasmota/xsns_19_mgs.ino
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,9 @@

bool mgs_detected = false;

void MGSInit(void) {
gas.begin(MGS_SENSOR_ADDR);
}

void MGSPrepare(void)
{
if (I2cActive(MGS_SENSOR_ADDR)) { return; }
if (!I2cSetDevice(MGS_SENSOR_ADDR)) { return; }

gas.begin(MGS_SENSOR_ADDR);
if (!gas.isError()) {
Expand Down
2 changes: 1 addition & 1 deletion tasmota/xsns_21_sgp30.ino
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ float sgp30_abshum;

void sgp30_Init(void)
{
if (I2cActive(SGP30_ADDRESS)) { return; }
if (!I2cSetDevice(SGP30_ADDRESS)) { return; }

if (sgp.begin()) {
sgp30_type = true;
Expand Down
2 changes: 1 addition & 1 deletion tasmota/xsns_24_si1145.ino
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ bool Si1145Read(void)

void Si1145Detect(void)
{
if (I2cActive(SI114X_ADDR)) { return; }
if (!I2cSetDevice(SI114X_ADDR)) { return; }

if (Si1145Begin()) {
si1145_type = true;
Expand Down
2 changes: 0 additions & 2 deletions tasmota/xsns_26_lm75ad.ino
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ void LM75ADDetect(void)
{
for (uint32_t i = 0; i < sizeof(lm75ad_addresses); i++) {
lm75ad_address = lm75ad_addresses[i];
if (I2cActive(lm75ad_address)) {
continue; }
if (!I2cSetDevice(lm75ad_address)) {
continue; // do not make the next step without a confirmed device on the bus
}
Expand Down
2 changes: 1 addition & 1 deletion tasmota/xsns_27_apds9960.ino
Original file line number Diff line number Diff line change
Expand Up @@ -1806,7 +1806,7 @@ void APDS9960_loop(void) {
#endif // USE_APDS9960_GESTURE

void APDS9960_detect(void) {
if (APDS9960_type || I2cActive(APDS9960_I2C_ADDR)) { return; }
if (APDS9960_type || !I2cSetDevice(APDS9960_I2C_ADDR)) { return; }

APDS9960_type = I2cRead8(APDS9960_I2C_ADDR, APDS9960_ID);

Expand Down
2 changes: 1 addition & 1 deletion tasmota/xsns_29_mcp230xx.ino
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ void MCP230xx_ApplySettings(void)

void MCP230xx_Detect(void)
{
if (I2cActive(USE_MCP230xx_ADDR)) { return; }
if (!I2cSetDevice(USE_MCP230xx_ADDR)) { return; }

uint8_t buffer;

Expand Down
2 changes: 1 addition & 1 deletion tasmota/xsns_30_mpr121.ino
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ void Mpr121Init(struct mpr121 *pS, bool initial)
// Loop through I2C addresses
for (uint32_t i = 0; i < sizeof(pS->i2c_addr[i]); i++) {

if (initial && I2cActive(pS->i2c_addr[i])) { continue; }
if (initial && !I2cSetDevice(pS->i2c_addr[i])) { continue; }

// Soft reset sensor and check if connected at I2C address
pS->connected[i] = (I2cWrite8(pS->i2c_addr[i], MPR121_SRST_REG, MPR121_SRST_VAL)
Expand Down
2 changes: 1 addition & 1 deletion tasmota/xsns_31_ccs811.ino
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ uint8_t ecnt = 0;

void CCS811Detect(void)
{
if (I2cActive(CCS811_ADDRESS)) { return; }
if (!I2cSetDevice(CCS811_ADDRESS)) { return; }

if (!ccs.begin(CCS811_ADDRESS)) {
CCS811_type = 1;
Expand Down
2 changes: 1 addition & 1 deletion tasmota/xsns_31_ccs811_v2.ino
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ void CCS811Detect(void)
int active_index = 1;
for (i = 0, pccsd = ccsd; i < MAXDEVICECOUNT; i++, pccsd++) {
pccsd->address = CCS811_addresses[ i];
if (I2cActive( pccsd->address)) { continue; }
if (!I2cSetDevice( pccsd->address)) { continue; }
if (!pccsd->ccsinstance.begin(pccsd->address)) {
pccsd->device_found = 1;
CCS811_devices_found += 1;
Expand Down
2 changes: 1 addition & 1 deletion tasmota/xsns_33_ds3231.ino
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ bool DS3231chipDetected = false;
----------------------------------------------------------------------*/
void DS3231Detect(void)
{
if (I2cActive(USE_RTC_ADDR)) { return; }
if (!I2cSetDevice(USE_RTC_ADDR)) { return; }

if (I2cValidRead(USE_RTC_ADDR, RTC_STATUS, 1)) {
I2cSetActiveFound(USE_RTC_ADDR, "DS3231");
Expand Down
2 changes: 1 addition & 1 deletion tasmota/xsns_36_mgc3130.ino
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ void MGC3130_loop()

void MGC3130_detect(void)
{
if (MGC3130_type || I2cActive(MGC3130_I2C_ADDR)) { return; }
if (MGC3130_type || !I2cSetDevice(MGC3130_I2C_ADDR)) { return; }

MGC3130_xfer = Pin(GPIO_MGC3130_XFER);
MGC3130_reset = Pin(GPIO_MGC3130_RESET);
Expand Down
2 changes: 1 addition & 1 deletion tasmota/xsns_41_max44009.ino
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void Max4409Detect(void)
for (uint32_t i = 0; 0 != max44009_addresses[i]; i++) {

max44009_address = max44009_addresses[i];
if (I2cActive(max44009_address)) { continue; }
if (!I2cSetDevice(max44009_address)) { continue; }

if ((I2cValidRead8(&buffer1, max44009_address, REG_LOWER_THRESHOLD)) &&
(I2cValidRead8(&buffer2, max44009_address, REG_THRESHOLD_TIMER))) {
Expand Down
2 changes: 1 addition & 1 deletion tasmota/xsns_42_scd30.ino
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ float scd30_Temp = 0.0;

void Scd30Detect(void)
{
if (I2cActive(SCD30_ADDRESS)) { return; }
if (!I2cSetDevice(SCD30_ADDRESS)) { return; }

scd30.begin();

Expand Down
2 changes: 1 addition & 1 deletion tasmota/xsns_50_paj7620.ino
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ void PAJ7620ReadGesture(void)

void PAJ7620Detect(void)
{
if (I2cActive(PAJ7620_ADDR)) { return; }
if (!I2cSetDevice(PAJ7620_ADDR)) { return; }

PAJ7620SelectBank(0);
PAJ7620SelectBank(0); // do it twice
Expand Down
2 changes: 1 addition & 1 deletion tasmota/xsns_54_ina226.ino
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ void Ina226Init()
for (i = 0; i < INA226_MAX_ADDRESSES; i++){
uint8_t addr = pgm_read_byte(probeAddresses + i);

if (I2cActive(addr)) { continue; }
if (!I2cSetDevice(addr)) { continue; }

// Skip device probing if the full scale current is zero

Expand Down
2 changes: 1 addition & 1 deletion tasmota/xsns_55_hih_series.ino
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ bool Hih6Read(void)

void Hih6Detect(void)
{
if (I2cActive(HIH6_ADDR)) { return; }
if (!I2cSetDevice(HIH6_ADDR)) { return; }

if (TasmotaGlobal.uptime < 2) { delay(20); } // Skip entering power on comand mode
Hih6.type = Hih6Read();
Expand Down
2 changes: 1 addition & 1 deletion tasmota/xsns_58_dht12.ino
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ bool Dht12Read(void)

void Dht12Detect(void)
{
if (I2cActive(DHT12_ADDR)) { return; }
if (!I2cSetDevice(DHT12_ADDR)) { return; }

if (Dht12Read()) {
I2cSetActiveFound(DHT12_ADDR, Dht12.name);
Expand Down
1 change: 0 additions & 1 deletion tasmota/xsns_59_ds1624.ino
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ void DS1624_HotPlugUp(uint32_t idx)
{
uint32_t addr = DS1624_Idx2Addr(idx);

if (I2cActive(addr)) { return; }
if (!I2cSetDevice(addr)) { return; }

uint8_t config;
Expand Down
6 changes: 1 addition & 5 deletions tasmota/xsns_65_hdc1080.ino
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,7 @@ bool HdcRead(void) {
*/

void HdcDetect(void) {
if (I2cActive(HDC1080_ADDR)) {
// AddLog(LOG_LEVEL_DEBUG, PSTR("HdcDetect: Address = 0x%02X already in use."), HDC1080_ADDR);

return;
}
if (!I2cSetDevice(HDC1080_ADDR)) { return; }

hdc_manufacturer_id = HdcReadManufacturerId();
hdc_device_id = HdcReadDeviceId();
Expand Down
2 changes: 1 addition & 1 deletion tasmota/xsns_70_veml6075.ino
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ bool VEML6075init(void)
}

void VEML6075Detect(void) {
if (I2cActive(veml6075_sensor.address)) return;
if (!I2cSetDevice(veml6075_sensor.address)) return;

if (VEML6075init()) {
I2cSetActiveFound(veml6075_sensor.address, veml6075_sensor.types);
Expand Down
2 changes: 1 addition & 1 deletion tasmota/xsns_89_t67xx.ino
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ uint16_t T67XX::getFirmwareVersion(void)

void T67XX::detect(void)
{
if (I2cActive(T67XX_I2C_ADDR))
if (!I2cSetDevice(T67XX_I2C_ADDR))
{
return;
}
Expand Down
5 changes: 1 addition & 4 deletions tasmota/xsns_92_scd40.ino
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,7 @@ float scd40_Temp = 0.0;

void Scd40Detect(void)
{
if (I2cActive(SCD40_ADDRESS)) {
AddLog(LOG_LEVEL_DEBUG, PSTR("SCD40 I2c already active in Scd40Detect()") );
return;
}
if (!I2cSetDevice(SCD40_ADDRESS)) { return; }

scd40.begin();

Expand Down
15 changes: 5 additions & 10 deletions tasmota/xsns_93_hm330x.ino
Original file line number Diff line number Diff line change
Expand Up @@ -181,28 +181,25 @@ int HM330XSendI2CCommand(uint8_t cmd) {
return ret;
}

int HM330XInit() {
void HM330XInit(void) {
if (!I2cSetDevice(HM330X_DEFAULT_ADDRESS)) { return; }

int8_t set_pin = Pin(GPIO_HM330X_SET);
if (set_pin >= 0) {
pinMode(set_pin, OUTPUT);
digitalWrite(set_pin, 1);
delay(5);
}

if (I2cActive(HM330X_DEFAULT_ADDRESS)) {
AddLog(LOG_LEVEL_DEBUG, PSTR("HM3: Address 0x%02X already in use"), HM330X_DEFAULT_ADDRESS);
return HM330X_ERROR_PARAM;
}

if (HM330X_NO_ERROR != HM330XSendI2CCommand(HM330X_SELECT_COMM_CMD)) {
AddLog(LOG_LEVEL_DEBUG, PSTR("HM3: no response from address 0x%02X"), HM330X_DEFAULT_ADDRESS);
return HM330X_ERROR_COMM;
return;
}

HM330Xdata = (HM330XDATA*)calloc(1,sizeof(HM330XDATA));
if (!HM330Xdata) {
AddLog(LOG_LEVEL_DEBUG, PSTR("HM3: out of memory"));
return HM330X_ERROR_MEMALLOC;
return;
}

HM330Xdata->valid = false;
Expand All @@ -211,8 +208,6 @@ int HM330XInit() {
HM330Xdata->warmup_counter = HM330X_WARMUP_DELAY;

I2cSetActiveFound(HM330X_DEFAULT_ADDRESS, "HM330X");

return HM330X_NO_ERROR;
}

void HM330XEverySecond() {
Expand Down

0 comments on commit dba1f32

Please sign in to comment.