Need help using I2c with ESP32 SDK

User avatar
MickPF
Posts: 45
Joined: Tue Apr 03, 2018 8:47 pm

Need help using I2c with ESP32 SDK

Postby MickPF » Sat Apr 07, 2018 8:12 am

Hello,

I examined the example of the esp-idf, I looked at "ESP-IDF Progamming Guide" (https://esp-idf.readthedocs.io/en/latest/index.html) and "don't understand"/miss one thing in the handling/programming "i2c bus":
During the initialization of the i2c I have to say "I'm the master of the bus" and which i2c bus ("I2C_NUM_0" or "I2C_NUM_1") to use (aka "/dev/i2c-1" under linux). So far so good...

Code: Select all

static esp_err_t i2c_example_master_read_slave(i2c_port_t i2c_num, uint8_t* data_rd, size_t size)
{
    if (size == 0) {
        return ESP_OK;
    }
    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);
    return ret;
}
Communication (receiving or sending data) with a device over an i2c bus (for example "I2C_NUM_0") requires three more arguments: command direction (reading or writing), the client's address on the bus and the register address/offset/index inside the client's address before reading or writing data from/to this client.
The first call "i2c_master_write_byte" in the example above sends the "command direction" and the client's address on the bus. Right?
But how to address the the register address/offset/index inside the client's address? Should I place it in the first byte of the data array?

I examined the include "driver/i2c.h" and didn't find any help, it caused confusion only: the data type "i2c_cmd_handle_t" is defined as

Code: Select all

typedef void* i2c_cmd_handle_t;    /*!< I2C command handle  */
without any further definitions. Even if it is later replaced by anything, what contains it?

Thanks in advance,
Michael
Nobody is perfect!
My name is NOT Nobody...


Nobody lives forever to correct his mistakes someday in the future, if not right now!
Covid-19 is not responsible for any human failures!

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

Re: Need help using I2c with ESP32 SDK

Postby ESP_Sprite » Sun Apr 08, 2018 2:16 am

That is because the i2c protocol doesn't specify registers/offsets/indexes/..., it just specifies a method of data communication. How to set a certain register is left up to the implementer of whatever device you're talking to. *usually*, you set it by starting a write transaction, then in the first (or first 2) bytes after the address, you specify the register/offset/... If you want to write, you then specify your data and end the transaction. If you want to read, you end the transaction immediately and start a read transaction for your data. However, as I've said before, this really depends on the device you're talking to and you should consult its datasheet for the exact procedure.

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: Need help using I2c with ESP32 SDK

Postby WiFive » Sun Apr 08, 2018 2:36 am

Look at some examples on GitHub like https://github.com/yanbe/bme280-esp-idf ... ain/main.c

User avatar
MickPF
Posts: 45
Joined: Tue Apr 03, 2018 8:47 pm

Re: Need help using I2c with ESP32 SDK

Postby MickPF » Mon Apr 09, 2018 6:54 am

Hello,

Many thanks for your advices. Meanwhile I saw/recognized it in the esp-idf example "peripherals/i2c" in the function "i2c_example_master_sensor_test".

Kind Regards,
Michael
Nobody is perfect!
My name is NOT Nobody...


Nobody lives forever to correct his mistakes someday in the future, if not right now!
Covid-19 is not responsible for any human failures!

Who is online

Users browsing this forum: Baidu [Spider], Google [Bot], Majestic-12 [Bot] and 116 guests