I2C registry read after a write requires to wait (delay)

37 views
Skip to first unread message

Alessandro Balzarelli

unread,
Dec 19, 2024, 4:06:56 AM12/19/24
to bcm2835
hello,

I'm writing a simple code on RPI0W to write a registry and then read its value (the slave is an I2C to CAN module).
bcm2835_i2c_begin (); bcm2835_i2c_setSlaveAddress (0x25); bcm2835_i2c_write (buf,1);  bcm2835_i2c_read (buf,1); printf("User Register = %X \r\n",buf[0]); bcm2835_i2c_end ();
Above code is not working unless I put a delay (i.e. usleep) between the read and write. Is it expected?  Is there a wait to know if/when the write is really completed instead of just waiting and try?

Thanks a lot!!
Alex

mike james

unread,
Dec 19, 2024, 5:10:17 AM12/19/24
to bcm2835
There is often a slow read problem with I2C. You can't 
know how long a device will take to respond. The solution is to either use
clock stretching or polling devices usually but not always support both.
If you know a maximum time it takes the device to respond you can just wait
longer than this and then read.
mikej 

Alessandro Balzarelli

unread,
Dec 20, 2024, 4:07:16 AM12/20/24
to bcm2835
Thank you Mike! I'm going to use a very short delay and do retries trying to read the register.
Reply all
Reply to author
Forward
0 new messages