i2c master issue

chegewara
Posts: 2378
Joined: Wed Jun 14, 2017 9:00 pm

i2c master issue

Postby chegewara » Mon Sep 06, 2021 9:28 am

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:

Image

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

User avatar
seraffimo
Posts: 6
Joined: Wed May 19, 2021 2:46 am

Re: i2c master issue

Postby seraffimo » Tue Sep 07, 2021 4:11 pm

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.
  1.    
  2.     // len = number of registers to read
  3.  
  4.     i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  5.     i2c_ack_type_t nack = I2C_MASTER_NACK;
  6.     i2c_ack_type_t ack = I2C_MASTER_ACK;
  7.  
  8.     i2c_master_start(cmd);
  9.     i2c_master_write_byte(cmd, i2c_addr << 1 | WRITE_BIT, ACK_CHECK_EN);
  10.     i2c_master_write_byte(cmd, starting_reg, ACK_CHECK_EN);
  11.     i2c_master_start(cmd);
  12.     // here ^
  13.     i2c_master_write_byte(cmd, i2c_addr << 1 | READ_BIT, ACK_CHECK_EN);
  14.  
  15.     for (int i = 0; i < (len - 1); i++)
  16.     {
  17.         ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_read_byte(cmd, &values[i], ack));
  18.     }
  19.  
  20.     ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_read_byte(cmd, &values[len-1], nack));
  21.     i2c_master_stop(cmd);
  22.     ret = i2c_master_cmd_begin(i2c_num, cmd, 500 / portTICK_RATE_MS);
  23.     ESP_ERROR_CHECK_WITHOUT_ABORT(ret);
  24.  
  25.     i2c_cmd_link_delete(cmd);

chegewara
Posts: 2378
Joined: Wed Jun 14, 2017 9:00 pm

Re: i2c master issue

Postby chegewara » Wed Sep 08, 2021 2:07 pm

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: Bing [Bot] and 99 guests