为什么 i2c_smbus 功能不可用?(I2C – 嵌入式 Linux) [英] Why are i2c_smbus function not available? (I2C – Embedded Linux)

查看:25
本文介绍了为什么 i2c_smbus 功能不可用?(I2C – 嵌入式 Linux)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开发嵌入式 Linux 软件以在 I2C 总线上进行通信时,有很多使用 i2c_smbus_ 函数的参考资料.当 i2c_smbus 函数(例如 i2c_smbus_read_word_data)在软件项目中被引用时,会在编译.

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.

对以下头文件的调查表明缺少大多数i2c_smbus 函数定义.

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

也在下面的参考i2c.h 文件定义了所有 i2c_smbus.

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

如何解决这个问题?

研究参考

  1. 在 Linux 中使用来自用户空间的 I2C
  2. Linux 用户空间的 I2C 通信 - 第二部分
  3. I2C 开发接口

推荐答案

因为您为应用程序使用了错误的头文件.

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

如果您在头文件中看到函数 i2c_smbus_read_word_data() 上的 extern,则它是您的内核 的头文件,但不是用于您的应用.Linux 内核具有 i2c_smbus_read_word_data() 和其他 i2c smbus 函数供其内部使用.但它们 a) 不是系统调用,或者 b) 无法从您的应用程序访问.

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.

相反,从 Linux Kernel Wiki 获取 i2c-tools 并安装它.如果您使用的是 Debian,只需

Instead, get i2c-tools from Linux Kernel Wiki and install it. If you are using Debian, just

sudo apt-get install libi2c-dev

并使用 i2c_smbus_read_word_data() 或他们提供的任何其他接口.

and use i2c_smbus_read_word_data() or any other interfaces they offer.

版本说明

i2c-dev,直到 3.x 版,曾经是一个只有头文件的包,这意味着没有要链接的库.所有函数都是使用 ioctl() 定义的内联函数.

i2c-dev, untill version 3.x, used be a header only package, meaning that there was no library to link to. All functions were inline functions defined using ioctl().

例如)

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

但从 v4.0 开始,它开始成为带有 libi2c.so.0i2c/smbus.h 的标准共享库.您必须在源代码中包含头文件

But since v4.0, it start to be a standard shared library with libi2c.so.0 and i2c/smbus.h. You have to include the header file in your source code

#include <i2c/smbus.h>

并将 libi2c.so.0-li2c

gcc -o a.out main.o -li2c

这篇关于为什么 i2c_smbus 功能不可用?(I2C – 嵌入式 Linux)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆