2
votes

I haven't seen any explanation on this.

Say the I2C master hasn't initiated anything. The physical I2C slave is not sending anything out. But in C code of the master, I could read the I2C. The read() returns with how ever many bytes that I call in read(). So what is really happening?

Here is my contention. In the C level, we should not read the bus any time. The underlying hardware/low level driver should only sample the bus when there is a I2C Start Bit and buffer the data internally. And when there is data in the buffer, then the kernel should allow the user code to do the read(). It shouldn’t allow the user code read() to read I2C if there is no data in the buffer.

But I could execute the following code.

I haven’t seen any documentation that talks about this. Any insight?

…                                        //this is a master
fd = open("/dev/i2c-1", O_RDWR );        //let’s say there is no error
ioctl(fd, I2C_SLAVE, 0x2a);              //let’s say that there is no error
read(fd, buffer, 10);                    //this is where I have a question
…

Thanks,

Bobby.

1
I'm short on time right now. But the safest kind of documentation when programming against the linux kernel is the source itself. See lxr.free-electrons.com/source/drivers/i2c/i2c-dev.c static ssize_t i2cdev_read If you still have trouble understanding you can also try to get an idea by: lwn.net/Kernel/LDD3. if you are still having problems I should have time tomorrow at the same time. - Alexander Oh
Can you briefly tell what exactly is your question? - Sorcrer
It issues I2C bus request from slave command. - Over Killer
Thanks for the comment. Let me clarify a bit. My question is related to the behavior in the i2c driver inside the Linux kernel. In a typical I2C hardware inside the chip, there is usually a hardware buffer that buffer in a byte or so from the I2C bus. At the end of the buffering, the hardware would trigger an interrupt for the software to process it. My question is this. When a user code executes a read() on a open I2C file descriptor, is it reading the hardware buffer? or is it reading the bus at that time. - user3173991
My original scenario is that, on the I2C bus, there is no response from a slave. On the master, which is execute the code above, I can still do a read(). That leads me to think that Linux is reading the bus at the time of read(). If read is not blocking (waiting until there a data), then just reading the bus could lead to frame error and such. - user3173991

1 Answers

0
votes

As far as I know the i2c kernel driver reads are non-blocking and should not be aware of any slave internal buffers. However if you are reading only one device register it should be safe. When it comes to block reads you have to ensure data integrity yourself. That can be achieved via polling the data ready bit of your i2c slave or manually handling the interrupts from the device.

Also I would encourage you to use the smbus commands instead of i2c read if possible. There are some examples at the i2c doc(https://www.kernel.org/doc/Documentation/i2c/dev-interface), also the smbus specification - http://smbus.org/specs/smbus20.pdf.