I am trying to read and write from I2C devices on embedded linux (4.6). I can use i2cset
, i2cget
, and i2cdetect
to interact with my devices fine. I can also use echo
and ca
t to read/write from the device driver (i2c-ocores
).
However, any code I build that tries to do this with calls to write()
and read()
always fail with a "Connection timed out". Opening the device file works:
fd = open("/dev/i2c-10", O_RDWR);
Setting the slave address works:
ioctl(fd, I2C_SLAVE, addr);
But writing or reading returns -1
and sets errno to "Connection timed out"
write(fd, buffer, len);
Also, once I try to write or read this way, it breaks the I2C bus and I have to reload the module to get it working again with i2c-tools
. I have also tried using the various other ways to write to the i2c drivers (ioctl
calls with i2c_rdwr_ioctl_data
messages, i2c_smbus_ioctl_data
messages) to no avail, they return the same error messages.
I can't for the life of me think of any reason that write()
and read()
calls would work any differently in my code than in i2c-tools
and echo
. In fact, I've even compiled my program with the i2c-tools
source code (busybox 1.26.2) and directly used the same functions that i2cset
and i2cget
use... and it still doesn't work.
Any help or suggestions are very welcome.