Fix ESP32 core 2.x I2C device detection speed

This commit is contained in:
Theo Arends 2021-10-31 15:13:28 +01:00
parent 58935bf708
commit dba1f32c82
30 changed files with 32 additions and 51 deletions

View File

@ -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)) {

View File

@ -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];

View File

@ -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)) {

View File

@ -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);

View File

@ -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)) {

View File

@ -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;

View File

@ -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])) {

View File

@ -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()) {

View File

@ -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;

View File

@ -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;

View File

@ -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
}

View File

@ -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);

View File

@ -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;

View File

@ -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)

View File

@ -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;

View File

@ -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;

View File

@ -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");

View File

@ -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);

View File

@ -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))) {

View File

@ -79,7 +79,7 @@ float scd30_Temp = 0.0;
void Scd30Detect(void)
{
if (I2cActive(SCD30_ADDRESS)) { return; }
if (!I2cSetDevice(SCD30_ADDRESS)) { return; }
scd30.begin();

View File

@ -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

View File

@ -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

View File

@ -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();

View File

@ -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);

View File

@ -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;

View File

@ -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();

View File

@ -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);

View File

@ -137,7 +137,7 @@ uint16_t T67XX::getFirmwareVersion(void)
void T67XX::detect(void)
{
if (I2cActive(T67XX_I2C_ADDR))
if (!I2cSetDevice(T67XX_I2C_ADDR))
{
return;
}

View File

@ -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();

View File

@ -181,7 +181,9 @@ 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);
@ -189,20 +191,15 @@ int HM330XInit() {
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;
@ -211,8 +208,6 @@ int HM330XInit() {
HM330Xdata->warmup_counter = HM330X_WARMUP_DELAY;
I2cSetActiveFound(HM330X_DEFAULT_ADDRESS, "HM330X");
return HM330X_NO_ERROR;
}
void HM330XEverySecond() {