1
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ generally batches are more efficient until they get in the way from being too big. \$\endgroup\$ – dandavis Feb 16 '18 at 19:57
2
\$\begingroup\$

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!

\$\endgroup\$
  • \$\begingroup\$ I guess the only time it might make sense the other way is if you only want some of the data sometimes or I have over 32 bytes to return. \$\endgroup\$ – Ryan Detzel Feb 16 '18 at 19:13
  • \$\begingroup\$ @RyanDetzel yes, I think so. But the part about "over 32 bytes" may be not true. The eeproms 24cXX can be read entirely with a single read, for example. Of course there are many different scenarios, and in fact those eeproms let you write the address pointer, in the same way of your original question. \$\endgroup\$ – linuxfan Feb 16 '18 at 19:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.