Why are i2c_smbus function not available? (I2C – Embedded Linux)

Mahendra Gunawardena picture Mahendra Gunawardena · Aug 6, 2014 · Viewed 21.9k times · Source

There are many references to using i2c_smbus_ functions when developing embedded Linux software to communicate on the I2C bus. When i2c_smbus functions such as i2c_smbus_read_word_data are referenced in software project for ARM8 processor errors such as ‘i2c_smbus_read_word_data’ was not declared in this scope are generated at compile.

Investigation of the following header files indicate the absence of most i2c_smbus function definition.

  • /usr/arm-linux-gnueabi/include/linux/i2c.h
  • /usr/arm-linux-gnueabi/include/linux/i2c-dev.h

Also in that following reference i2c.h file has all the i2c_smbus defined.

How can this problem be resolved?

Research references

  1. Using I2C from userspace in Linux
  2. I2C Communication from Linux Userspace – Part II
  3. I2C dev interface

Answer

Yasushi Shoji picture Yasushi Shoji · Sep 15, 2014

Because you are using a wrong header file for your application.

If you see an extern on the function i2c_smbus_read_word_data() in your header, it's a header file for your kernel, but not for your application. The Linux kernel has i2c_smbus_read_word_data() and other i2c smbus functions for its internal use. But they are a) not system calls, or b) not accessible from your application.

Instead, get i2c-tools from lm-sensors and install it. If you are using Debian, just

sudo apt-get install libi2c-dev

and use i2c_smbus_read_word_data() or any other interfaces they offer. i2c-dev is a header only package, meaning that there is no library to link to. All functions are inline functions defined using ioctl().

e.g.)

static inline __s32 i2c_smbus_access(int file, char read_write, __u8 command,
                                     int size, union i2c_smbus_data *data)
{
        struct i2c_smbus_ioctl_data args;

        args.read_write = read_write;
        args.command = command;
        args.size = size;
        args.data = data;
        return ioctl(file,I2C_SMBUS,&args);
}
   :
static inline __s32 i2c_smbus_read_word_data(int file, __u8 command)
{
        union i2c_smbus_data data;
        if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
                             I2C_SMBUS_WORD_DATA,&data))
                return -1;
        else
                return 0x0FFFF & data.word;
}