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

折月煮酒 提交于 2019-12-05 03:27:23

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;
}

I ran into this today. The i2c_smbus_* functions are defined in:

/usr/include/linux/i2c-dev.h

...but when I would try to cross-compile for ARM on an older version of Ubuntu, I was running into errors such:

i2c_smbus_read_block_data was not declared in this scope

Turns out the functions are not defined in the equivalent ARM-specific location:

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

When cross-compiling, this 2nd older header file is the one used. Had to re-declare locally a few of the inline i2c_smbus_... functions to get around the problem.

From the i2c Linux kernel documentation:

Please note that there are two files named "i2c-dev.h" out there, one is distributed with the Linux kernel and is meant to be included from kernel driver code, the other one is distributed with i2c-tools and is meant to be included from user-space programs. You obviously want the second one here.

So you need to include the i2c-dev.h from i2c-tools not from the Linux kernel.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!