nass nass - 1 month ago 5x
C Question

how big can i2c_msg.buf size be?

I am trying to use the i2c interface on an embedded device.


(the userspace header file, not the kernel same-named one!) the i2c_msg is defined as

struct i2c_msg {
__u16 addr; // slave address
unsigned short flags;
short len; // msg length
char *buf; // pointer to msg data

and I am reading up on
struct i2c_msg
<kernel root>/Documentation/i2c
, but it doesn't seem to mention anywhere how big can the
array be (i.e. wht would the maxsize of

The nearest of an answer I have come across is in doc-file
, where it states that:

..., the third the number of bytes to read/write (must be less than the length of the buffer, also should be less than 64k since msg.len is u16.)

but this refers to i2c function "i2c_master_send". so it is not apparent what is going on,

really go up to 64k? ie. can I send up to 64kBytes of data in one write operation (& to one device) down the i2c bus ?

Thank you for your help.


In theory, yes, you can. The I2c protocol is a very "flexible" protocol, both in speed as well as in length.

If a slow device can't cope with the speed of a faster device, the slow device is allowed to "slow down" the fast device by keeping the clock signal low for as long as it's needed. In theory forever, but in practice it would be unwise not to put some kind of time-out mechanism on the I2c master. For instance, "try to send or receive max. 5 times data from the addressee". As long as the clock is held low, nobody else can start a communication on the I2c bus. So far concerning the speed part.

Regarding the length part, also here I2c can have an "infinitive" amount of bytes that can be sent/received over the bus. The only two "markers" that are important and relevant to identify an I2c communication, are the start and stop combination. In between, you can have an endless amount of bytes sent over the bus. But also here, common sense is to be used.

First off, I don't know of any device that would take 64k of bytes (to take your number) as data. The largest consumers (I2c devices) I've seen so far, took a couple of hundred bytes as data. But such devices are really very, very rare. On average, an I2c device takes from one to maximum a couple of bytes of data traffic, both reading and writing.

So, the datatype short for the field len in the structure struct i2c_msg is more than large enough to cover all possible current and future I2c communications.

More information can be found here (spec) and here (manual).