Electronic – Is it better to make multiple small i2c requests or one larger one

i2c

I have four registers on my slave (an arduino)

0x00 - device id
0x01 - button state (four buttons)
0x02 - led state (four buttons)
0x03 - led mode (how the leds react to button presses if at all)

Currently, to get a value off the slave I issue a write with an address. This address is stored in a pointer on the slave and then when the next read comes in I return the data in the register at that pointer address.

Basically, on address 0x48

W 0x48 0x01
R 0x48        ; button state at 0x01
W 0x48 0x02
R 0x48        ; led state at 0x02

And I do this over and over checking for state changes. I'm not really sure why I did it this way, some datasheet or blog post lead me in this direction but now I'm questioning it. Now I'm thinking I should just skip the writes and return all the register values on every read. So one read would return four bytes in this example instead of one.

Is this better? It seems less error prone but I'm not sure what costs are associate with returning multiple bytes etc.

Best Answer

If you count the bytes going forth and back:

W 0x48 0x01, R 0x48 (+ button state back): 4 bytes

W 0x48 0x02, R 0x48 (+ led state at 0x02): another 4 bytes

...to obtain 2 bytes the bus uses 8 bytes. So to obtain 4 bytes, the bus will use 16. Definitely, a single read - 5 bytes - is a better choice!

Related Topic