UncleRus / esp-idf-lib

Component library for ESP32-xx and ESP8266
https://esp-idf-lib.readthedocs.io/en/latest/
1.33k stars 414 forks source link

[sht3x]: i2cdev: Could not write to device [0x44 at 0] #624

Open FeOAr opened 3 months ago

FeOAr commented 3 months ago

The issue

Snipaste_2024-03-30_21-46-22

I am using the example of SHT30, which can function normally when the I2C speed is 100K, but other chips can reach 200K using the same "i2cdev" lib. The last discovered issue may be that the i2c device was not fully configured before the first measurement. Just add a delay to use it normally.

void task(void *pvParameters)
{
    float temperature;
    float humidity;

    TickType_t last_wakeup = xTaskGetTickCount();
    vTaskDelay(pdMS_TO_TICKS(10));  // Add delay before measurement

    while (1)
    {
        // perform one measurement and do something with the results
        ESP_ERROR_CHECK(sht3x_measure(&dev, &temperature, &humidity));
        printf("SHT3x Sensor: %.2f °C, %.2f %%\n", temperature, humidity);

        // wait until 5 seconds are over
        vTaskDelayUntil(&last_wakeup, pdMS_TO_TICKS(1000));
    }
}

Which SDK are you using?

esp-idf

Which version of SDK are you using?

5.1.1

Which build target have you used?

Component causing the issue

sht3x

Anything in the logs that might be useful for us?

E (320) i2cdev: Could not write to device [0x44 at 0]: -1 (ESP_FAIL)
ESP_ERROR_CHECK failed: esp_err_t 0xffffffff (ESP_FAIL) at 0x4200870b

Additional information or context

No response

Confirmation

cmorganBE commented 2 months ago

Using sht4x driver here (with the default of 1MHz). Seeing similar issues with periodic failures during configuration (retries resolve), and periodically during run-time where the write to start a measurement read fails:

E (668146) i2cdev: Could not read from device [0x44 at 0]: -1 (ESP_FAIL)

Note that these failures are seemingly random, we read the temp/humidity every 500ms, several reads are successful, then a failure, then the next several are successful etc.

cmorganBE commented 2 months ago

Modified the sht4x driver code to slow from 1MHz down to 100khz, and changed i2c bus pullups from 4.6k down to 2.6k. Signals on the scope look very clean vs. the original 1MHz 4.6k pullup.

Still seeing the i2cdev errors here though.

cmorganBE commented 1 month ago

I was able to catch the issue here. In the good case:

bad case:

cmorganBE commented 1 month ago

Alright, root cause identified for my case, opening a PR today to resolve. The issue was that for the SH4Tx driver pdMS_TO_TICKS() was being used. ticks on my system (and likely many others) is 10ms (100hz). The delay time was 10ms / (10ms / tick) = 1 tick, but vTaskDelay() is NOT guaranteed to delay for at least the value given, see https://www.freertos.org/a00127.html, there is no mention of "at least" which is typical for delay functions. So for single tick delays you could delay for 10ms, or 6ms or 15ms, it does its best to get close.

In the case where the delay is supposed to be 10ms but its 6ms the SH4Tx isn't ready yet to respond, hence the error.

This is corroborated by:

So maybe this isn't the same issue as the OP reported with the sht3x not reading correctly since it looks like that driver has had the correct tick calculation with margin in place since 8dd5853b (from 2019).

FeOAr commented 1 month ago

Thank you very much for your answer. I now have a rough understanding. I will take some time to reproduce it with an oscilloscope later.

cmorganBE commented 1 month ago

@FeOAr I'm guessing you have a different issue than I have. I wouldn't have posted here but it looked like the same issue initially. PR is #632