I spent today few hours before i found "issue" in I2C master. Without i2c_tools example i would never find it.
If i am performing only READ or only WRITE requests there is no issue, but the problem is when i want to do read and write request one after another. To make it possible we have to perform steps:
-
- READ or WRITE
- i2c_driver_delete(i2c_port);
According to my knowledge and espressif docs "i2c_master_driver_initialize();" should be performed only 1 time at beginning and to read/write only those steps are required:
https://docs.espressif.com/projects/esp ... ster-write
https://docs.espressif.com/projects/esp ... aster-read
Any idea if this is a bug or is it expected?
i2c slave device is esp32
i2c master issue
Re: i2c master issue
Not sure if this code helps you or not ... but you may just be off slightly on the protocol (unless I am misunderstanding). I use this code for multi-read from battery charger I2C interface. The difference between your code and mine is an additional register # and a master_start. I've only ever conducted multi-reads or multi-writes (not both in same request). And the protocol can sometimes be device specific. I do not reset the I2C interface driver after each request.
- // len = number of registers to read
- i2c_cmd_handle_t cmd = i2c_cmd_link_create();
- i2c_ack_type_t nack = I2C_MASTER_NACK;
- i2c_ack_type_t ack = I2C_MASTER_ACK;
- i2c_master_start(cmd);
- i2c_master_write_byte(cmd, i2c_addr << 1 | WRITE_BIT, ACK_CHECK_EN);
- i2c_master_write_byte(cmd, starting_reg, ACK_CHECK_EN);
- i2c_master_start(cmd);
- // here ^
- i2c_master_write_byte(cmd, i2c_addr << 1 | READ_BIT, ACK_CHECK_EN);
- for (int i = 0; i < (len - 1); i++)
- {
- ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_read_byte(cmd, &values[i], ack));
- }
- ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_read_byte(cmd, &values[len-1], nack));
- i2c_master_stop(cmd);
- ret = i2c_master_cmd_begin(i2c_num, cmd, 500 / portTICK_RATE_MS);
- ESP_ERROR_CHECK_WITHOUT_ABORT(ret);
- i2c_cmd_link_delete(cmd);
Re: i2c master issue
Solved. It could be some bug in my code, which i overlooked and then fixed, but that re-init somehow masked my previous bug.
Who is online
Users browsing this forum: Baidu [Spider] and 172 guests