Issues with i2c_slave_read_buffer

User avatar
monkeyonkeyboard
Posts: 2
Joined: Tue May 28, 2019 6:18 am

Issues with i2c_slave_read_buffer

Postby monkeyonkeyboard » Tue May 28, 2019 11:29 am

Hi everyone,
I'm trying to send a roughly 29 byte Protobuf message over I2C + a 3 byte header between two ESP32s. The I2C master can send the messages fine, however, I'm having trouble using i2c_slave_read_buffer because the bytes are out of order. The first byte of my header is always 0xB, so the buffer output of i2c_slave_read_buffer should start with 0xB, but in reality I get about 1/4 of the previous message, two null bytes, then 3/4 of the full message. I've tried calling i2c_reset_rx_fifo before and after my call to the I2C slave read function, but it doesn't help.

The documentation is vague as to whether or not the FreeRTOS ringbuffer is emptied when the slave receives a new message, but from looking at the source code it doesn't appear to be. Is there a reason for this? Wouldn't it make sense to empty the ringbuffer once a begin bit is detected and make the buffer available for use after an end bit is detected?

My current plan is to create an interrupt which copies the buffer, but it seems inefficient to have two interrupts running at once (my one along with the default one), not to mention the fact that there is no interrupt (as far as I can see) to determine when the slave detects a begin bit. If anyone has any suggestions, that would be greatly appreciated.

Cheers,
Matt.

User avatar
monkeyonkeyboard
Posts: 2
Joined: Tue May 28, 2019 6:18 am

Re: Issues with i2c_slave_read_buffer

Postby monkeyonkeyboard » Thu May 30, 2019 5:05 am

In case anyone is looking for a solution, I've mostly resolved the issue by reading the buffer one byte at a time:
  1. uint8_t byte = 0;
  2.  
  3. // attempt to read in bytes one by one
  4. while (true){
  5.     i2c_slave_read_buffer(I2C_NUM_0, &byte, 1, portMAX_DELAY);
  6.     buf[i++] = byte;
  7.     // ... other code, terminate while loop once end byte is received
  8. }
If the buffer is out of order, the best solution is to call i2c_reset_rx_fifo() and wait about 10ms.

Ritesh
Posts: 1383
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Re: Issues with i2c_slave_read_buffer

Postby Ritesh » Sat Jun 01, 2019 10:52 am

Great.. It will be helpful for others who are using I2C or planning to use into future.

Thanks for your effort with solution as well.
Regards,
Ritesh Prajapati

Who is online

Users browsing this forum: Baidu [Spider], Google [Bot] and 158 guests