Page 1 of 1

Need help using I2c with ESP32 SDK

Posted: Sat Apr 07, 2018 8:12 am
by MickPF
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

Re: Need help using I2c with ESP32 SDK

Posted: Sun Apr 08, 2018 2:16 am
by ESP_Sprite
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.

Re: Need help using I2c with ESP32 SDK

Posted: Sun Apr 08, 2018 2:36 am
by WiFive
Look at some examples on GitHub like https://github.com/yanbe/bme280-esp-idf ... ain/main.c

Re: Need help using I2c with ESP32 SDK

Posted: Mon Apr 09, 2018 6:54 am
by MickPF
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