I2C with ESP-IDF vs. esp32-arduino

Posts: 3
Joined: Mon Aug 14, 2017 12:18 am

I2C with ESP-IDF vs. esp32-arduino

Postby tbnobody » Tue Aug 15, 2017 10:59 am


I try to read data from a DS3231/DS1307 module using a ESP-WROOM-32. I have a working Arduino code snippet which in general does the following:

Code: Select all

#define DS3231_I2C_ADDRESS 0x68
Wire.write(0); // set DS3231 register pointer to 00h
Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
Now I wanted to try the same using just the ESP-IDF Framework:

Code: Select all

#define DS3231_ADDR            0x68

// Initialize
i2c_config_t conf;
conf.mode = I2C_MODE_MASTER;
conf.sda_io_num = 21;
conf.scl_io_num = 22;
conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
conf.master.clk_speed = 100000;
ESP_ERROR_CHECK(i2c_param_config(I2C_NUM_0, &conf));
ESP_ERROR_CHECK(i2c_driver_install(I2C_NUM_0, conf.mode, 0, 0, 0));

// Read data
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, (DS3231_ADDR << 1) | I2C_MASTER_WRITE, 1 /* expect ack */));
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, 0x0, 1));
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, (DS3231_ADDR << 1) | I2C_MASTER_READ, 1 /* expect ack */));
uint8_t data[7];
ESP_ERROR_CHECK(i2c_master_read(cmd, data, 7, 0));
ESP_ERROR_CHECK(i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000/portTICK_PERIOD_MS));
In both scenarios the hardware is exactly the same, but the Arduino code works and the IDF code doesn't. The i2c_master_cmd_begin method just gives a timeout (0x107) as result.
I already found this issue: https://github.com/espressif/esp-idf/issues/680 but I am not sure whether it's the same problem because reading the RTC never works for me (even not the first couple of minutes).
Can somewhere else maybe reproduce this issue or give me a hint what's maybe wrong with the IDF code?

Thank you very much in advance.

User avatar
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: I2C with ESP-IDF vs. esp32-arduino

Postby kolban » Thu Aug 17, 2017 3:43 am

I have found great benefit in getting a $5-$10 Logic Analyzer on eBay. Trying to diagnose I2C or SPI puzzles without one is really hard. With the analyzer attached, you can truly see what is going on at the high speed bus/bit level.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

Posts: 14
Joined: Wed Apr 26, 2017 2:24 pm

Re: I2C with ESP-IDF vs. esp32-arduino

Postby chaitz » Tue Feb 25, 2020 4:41 pm


I am facing a similar issue, how was this issue resolved finally? Any inputs or pointers will be helpful. The logic analyzer output shows that the clock is held low on I2C Write and the timeout occurs. However with Arduino code everything works fine.

Who is online

Users browsing this forum: Bing [Bot] and 43 guests