Reputation: 73
I'm writing a user-space program to read and write to/from an EEPROM using open(), ioctl(), read(), and write(), but it doesn't seem to be working the way I expect.
I guess first of all I have to ask, is the entire I2C protocol for read and write handled by calls to read() and write() with an I2C file descriptor? According to the link here, the entire transaction is handled by read() and write().
If that is so, then how do read() and write() know which register address to read from? Most places I've read say to use the first byte of the data buffer passed to read() and write() to store the address of the register to read from or write to. But then what if the device I am communicating with uses 16-bit register addresses and register data? How do read() and write() know if the address is 8 or 16 bits long?
Upvotes: 4
Views: 3038
Reputation: 87386
I think the underlying protocol is implemented with three system calls: read
, write
, and ioctl
, as described in the kernel I²C documentation. However, I recommend using libi2c
so you don't have to worry about those details. I have written about how to use that library here:
http://blog.davidegrayson.com/2012/06/using-ic-on-raspberry-pi.html
I successfully wrapped it in a C++ class, which you can see here:
https://github.com/DavidEGrayson/minimu9-ahrs/blob/master/I2CBus.cpp
Also, in I²C, device addresses are 7-bit. If your device implements some kind of 16-bit address, that would probably be implemented inside data transfers to the 7-bit address of a device.
Upvotes: 3