Pergunta

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

in i2c-dev.h (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 from <kernel root>/Documentation/i2c, but it doesn't seem to mention anywhere how big can the *buf array be (i.e. wht would the maxsize of len be).

The nearest of an answer I have come across is in doc-file writing-clients:327 , 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,

Can buf[len-1] 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.

Foi útil?

Solução

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).

Outras dicas

GeertVc did a pretty good job of answering. If dealing with EEPROMs, they can have 64K bytes of data. But, they will typically have an internal buffer and use this buffer to perform writes. As an example, the AT24C32, a 4K byte device, is organized as 128 pages of 32 bytes each. The largest memory block that can be written at once is 32 bytes, beginning at the start of a page boundary. In every case, you need to research the device being attached to the I2C bus.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top