understanding I2C

User avatar
esp32me
Posts: 1
Joined: Thu Jan 21, 2021 7:54 am

understanding I2C

Postby esp32me » Sun Mar 07, 2021 9:57 am

Hello!

After starting with Arduino boards and Arduino IDE I'm now using ESP32-boards and VS Code/PlatformIO with ESP-IDF. So I read I2C-chapter of ESP-IDF programming guide/API reference and want to have a better understanding of these functions.

A typical code from GitHub looks like this (and it works):

Code: Select all

i2c_cmd_handle_t cmd = i2c_cmd_link_create();
    i2c_master_start(cmd);
    i2c_master_write_byte(cmd, (ESP_SLAVE_ADDR << 1) | READ_BIT, ACK_CHECK_EN);
    if (size > 1) {
        i2c_master_read(cmd, data_rd, size - 1, ACK_VAL);
    }
    i2c_master_read_byte(cmd, data_rd + size - 1, NACK_VAL);
    i2c_master_stop(cmd);
    esp_err_t ret = i2c_master_cmd_begin(i2c_num, cmd, 1000 / portTICK_RATE_MS);
    i2c_cmd_link_delete(cmd);
My questions:
1. i2c_master_read() is reading several bytes at once?
2. But for the last byte a NACK is mandatory, so that's the reason, why there is an additional i2c_master_read_byte()?
3. What is the meaning of i2c_master_cmd_begin()-function? Are the master_read calls only a kind of preparation and the data transmission from slave to master really starts with the master_cmd_begin?


Kind regards
Jürgen
Too old for object-oriented programming, but too young to die.

ESP_Sprite
Posts: 9759
Joined: Thu Nov 26, 2015 4:08 am

Re: understanding I2C

Postby ESP_Sprite » Sun Mar 07, 2021 10:18 am

You're right in that the for loop receives all but the last byte of the data (and ACKs those read), and the final byte is read by the line after the for loop and NACKed.

All commands until the i2c_master_cmd_begin() command indeed are just setup. You're preparing the 'recipe' for the I2C actions that need to be executed. Only when you call i2c_master_cmd_begin(), this recipe is sent to the I2C controller which will then (more-or-less standalone) execute these instructions.

jmcornil
Posts: 21
Joined: Mon Feb 10, 2020 1:42 pm

Re: understanding I2C

Postby jmcornil » Tue Apr 27, 2021 8:12 am

Hello

I have a question about the i2c_master_cmd_begin() function.

First of all, I make a cmd with i2c_cmd_link_create(), i2c_master_start(),... and so on till i2c_master_stop().
(it is a cmd to read 14 registers of a mpu_6050)

If I then run i2c_master_cmd_begin() with this cmd all works fine and I get my values.

But if I run another time i2c_master_cmd_begin() with the same cmd I get an error flag equal to -1.

I don't understand why I cannot send twice the same cmd to i2c_master_cmd_begin().

Any hint ? Thank you in advance.

I am working with : esp-idf v4.1-dev-2020-ga7bbc74a2-dirty

Who is online

Users browsing this forum: audris, noweare and 103 guests