Accessing I2C Devices from User-Space Applications Print

 

One of our customers sent us the following email describing his experiences with accessing I2C devices from user-space Linux applications:

Turns out it is easier than I expected to add I2C support to a user space app.

All you need to do is download the i2c-tools-3.1.0.tar.bz2, and grab the i2c-dev.h and put it in your user space apps directory. Without doing error checking and such, it is as simple as this:

#include "i2c-dev.h"
..
char filename[20]="/dev/i2c-1";
..
open(filename,O_RDWR); //open the file. (appropriate nod in initramfs required)
ioctl(file,I2C_SLAVE,0x3E); //open device 0x3E on I2cbus1, in this case my GPIO expansion
Response=i2c_smbus_read_byte_data(file,0x00);
//response contains data read from address 0x00 on device 0x3E

I have my sensors app working this way now in an image, and I can confirm this works.