I2c driver fail after esp_restart()
Posted: Wed Dec 20, 2023 6:10 pm
Hello,
I am working on a sensor project implemented on an ESP32-H2 using zigbee and lightsleep.
I noticed that I have an issue when I call the function esp_zb_factory_reset() to reset my zigbee settings and reset the ESP32. Following the reboot my i2c bus seem unresponsive. I downsized my code to a minimum to be able to reproduce the bug and noticed that I have the same behavior after using esp_restart() for a reset. Here my code using an i2c-scan instead of reading values from the sensor (the issue is the same).
The first time my code run the i2c bus is properly initialized and the sensor is responding. After the restart the bus is initialized and the functions return no errors. But then when the commands get sent over the i2c bus nothing happen like if the bus is not properly initialized and a timeout is awaiting to happen.
I did a few things to try troubleshooting:
* If I do not delete the driver before resetting the ESP32 I have no issue. The driver is properly loaded after reset and the sensor is responsive over the i2c bus. Nonetheless I need to delete the driver. My sensor is running on battery and read values over i2c every 5min. I want to save every bit of energy I can
* I tried changing the pins, the sensor, using an ESP32-C6 instead of an ESP32-H2 I have exactly the same issue
* I tried to reset the fifo and deactivate/activate the i2c module before deleting the driver. That changes nothing
Any idea? Thank you very much for the help.
I am working on a sensor project implemented on an ESP32-H2 using zigbee and lightsleep.
I noticed that I have an issue when I call the function esp_zb_factory_reset() to reset my zigbee settings and reset the ESP32. Following the reboot my i2c bus seem unresponsive. I downsized my code to a minimum to be able to reproduce the bug and noticed that I have the same behavior after using esp_restart() for a reset. Here my code using an i2c-scan instead of reading values from the sensor (the issue is the same).
- void app_main(void) {
- i2c_config_t i2c_cfg = {
- .mode = I2C_MODE_MASTER,
- .sda_io_num = GPIO_NUM_15,
- .scl_io_num = GPIO_NUM_23,
- .sda_pullup_en = false,
- .scl_pullup_en = false,
- .master = {
- .clk_speed = 100000
- }
- };
- ESP_ERROR_CHECK(i2c_param_config(I2C_NUM_0, &i2c_cfg));
- ESP_ERROR_CHECK(i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0));
- printf("i2c scan: \n");
- for (uint8_t i = 1; i < 127; i++) {
- int ret;
- i2c_cmd_handle_t cmd = i2c_cmd_link_create();
- i2c_master_start(cmd);
- i2c_master_write_byte(cmd, (i << 1) | I2C_MASTER_WRITE, 1);
- i2c_master_stop(cmd);
- ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 100 / portTICK_PERIOD_MS);
- i2c_cmd_link_delete(cmd);
- if (ret == ESP_OK) {
- printf("Found device at: 0x%2x\n", i);
- }
- }
- ESP_ERROR_CHECK(i2c_driver_delete(I2C_NUM_0));
- esp_restart();
- }
I did a few things to try troubleshooting:
* If I do not delete the driver before resetting the ESP32 I have no issue. The driver is properly loaded after reset and the sensor is responsive over the i2c bus. Nonetheless I need to delete the driver. My sensor is running on battery and read values over i2c every 5min. I want to save every bit of energy I can
* I tried changing the pins, the sensor, using an ESP32-C6 instead of an ESP32-H2 I have exactly the same issue
* I tried to reset the fifo and deactivate/activate the i2c module before deleting the driver. That changes nothing
Code: Select all
i2c_reset_tx_fifo(I2C_NUM_0);
i2c_reset_rx_fifo(I2C_NUM_0);
periph_module_disable(PERIPH_I2C0_MODULE);
periph_module_enable(PERIPH_I2C0_MODULE);