I2C read write single byte

15 Mar 2011

I have a question regarding the I2C interface. In the case of reading or writing a single byte why do the commands use an integer instead of a char? For example the read command description is:

int read(int ack) Read a single byte from the I2C bus

Shouldn't this be: char read(int ack)

and write respectively: int write(char data)

15 Mar 2011

I'm not certain it's the reason but I have a possible explanation.

The native size of an ARM register is 32 bits, which exactly corresponds to an int. When returning a byte (char), the compiler would have to add extra code to clear the top 24 bits. Granted, it's a single instruction, but those can add up.

15 Mar 2011

This sounds like a reasonable explanation to me.

In terms of endianess, if you want to send a byte value, lets say 0x04 do you have to shift the value or not? For example write(0x04 >> 24);