I2C user space read / write problems

I am writing a user-space program to read and write to / from EEPROM using open (), ioctl (), read () and write (), but it doesn't seem to work I expect.

I think, first of all, I have to ask, is this the entire I2C read and write protocol handled by read () and write () calls with an I2C file descriptor? According to the link here , the entire transaction is handled by read () and write ().

If so, how do read () and write () know which register address to read? Most of the places I've read say to use the first byte of the data buffer passed to read () and write () to store the register address for reading or writing. But what if the device I'm communicating with uses 16-bit registers and register data? How do read () and write () know if the address is 8 or 16 bits long?

Thanks and I can provide some code snippets if needed.

+3


source to share


1 answer


I think that the basic protocol is implemented with three system calls: read

, write

and ioctl

as described in the documentation I²C core . However, I recommend using libi2c

, so you don't need to worry about these details. I wrote about how to use this library here:

http://blog.davidegrayson.com/2012/06/using-ic-on-raspberry-pi.html

I have successfully wrapped it in a C ++ class which you can see here:



https://github.com/DavidEGrayson/minimu9-ahrs/blob/master/I2CBus.cpp

In addition, the I²C device addresses are 7-bit. If your device implements some sort of 16-bit address, it will probably be implemented internally by transferring data to the 7-bit device address.

+1


source







All Articles