C++ 为什么 i2c_smbus 功能不可用?(I2C – 嵌入式 Linux)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25159064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why are i2c_smbus function not available? (I2C – Embedded Linux)
提问by Mahendra Gunawardena
There are many references to using i2c_smbus_functions when developing embedded Linux software to communicate on the I2C bus. When i2c_smbusfunctions such as i2c_smbus_read_word_dataare referenced in software project for ARM8 processor errors such as ‘i2c_smbus_read_word_data' was not declared in this scopeare generated at compile.
在开发嵌入式 Linux 软件以在 I2C 总线上进行通信时,有许多使用i2c_smbus_函数的参考资料。当在软件项目中引用i2c_smbus函数(如i2c_smbus_read_word_data)时,ARM8处理器错误(如“ i2c_smbus_read_word_data”未在此范围内声明)在编译时生成。
Investigation of the following header files indicate the absence of most i2c_smbusfunction definition.
对以下头文件的调查表明大多数i2c_smbus函数定义缺失。
- /usr/arm-linux-gnueabi/include/linux/i2c.h
- /usr/arm-linux-gnueabi/include/linux/i2c-dev.h
- /usr/arm-linux-gnueabi/include/linux/i2c.h
- /usr/arm-linux-gnueabi/include/linux/i2c-dev.h
Also in that following reference i2c.hfile has all the i2c_smbus defined.
同样在以下参考i2c.h文件中定义了所有 i2c_smbus。
How can this problem be resolved?
如何解决这个问题?
Research references
研究参考
回答by Yasushi Shoji
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.
如果您在头文件中看到extern
函数i2c_smbus_read_word_data()
上的 ,则它是内核的头文件,而不是应用程序的头文件。Linux 内核具有i2c_smbus_read_word_data()
和其他 i2c smbus 函数供其内部使用。但它们 a) 不是系统调用,或 b) 无法从您的应用程序访问。
Instead, get i2c-tools from lm-sensorsand install it. If you are using Debian, just
相反,从 lm-sensors获取i2c-tools并安装它。如果您使用的是 Debian,只需
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()
.
并使用i2c_smbus_read_word_data()
或他们提供的任何其他接口。i2c-dev 是一个只有头文件的包,这意味着没有要链接的库。所有函数都是使用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;
}
回答by Stéphane
I ran into this today. The i2c_smbus_*
functions are defined in:
我今天遇到了这个。该i2c_smbus_*
函数被定义在:
/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:
...但是当我尝试在旧版本的 Ubuntu 上为 ARM 进行交叉编译时,我遇到了以下错误:
i2c_smbus_read_block_data was not declared in this scope
Turns out the functions are notdefined in the equivalent ARM-specific location:
原来这些函数没有在等价的 ARM 特定位置定义:
/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.
交叉编译时,使用的是第二个较旧的头文件。不得不在本地重新声明一些内联 i2c_smbus_... 函数来解决这个问题。
回答by jcoffland
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.
请注意,有两个名为“i2c-dev.h”的文件,一个是随 Linux 内核分发的,旨在包含在内核驱动程序代码中,另一个是随 i2c-tools 分发的,旨在用于包含在用户空间程序中。你显然想要这里的第二个。
So you need to include the i2c-dev.h
from i2c-tools not from the Linux kernel.
所以你需要包含i2c-dev.h
来自 i2c-tools 的而不是来自 Linux 内核的。