linux i2c设备注册,Linux - 在使用之前是否需要注册(实例化)i2c设备?(Linux - Is it necessary to register (instantiate) i2c d...

Linux - 在使用之前是否需要注册(实例化)i2c设备?(Linux - Is it necessary to register (instantiate) i2c devices before using them?)

我对使用Linux的i2c dev接口的用户空间程序如何能够注册(实例化?)i2c设备感到困惑。

使用i2c设备的名称和地址定义i2c_board_info结构,执行i2c_register_board_info()

有一个这样的设备条目:i2c1: i2c@400a0000 {

/* ... master properties skipped ... */

clock-frequency = <100000>;

flash@50 {

compatible = "atmel,24c256";

reg = <0x50>;

};

pca9532: gpio@60 {

compatible = "nxp,pca9532";

gpio-controller;

#gpio-cells = <2>;

reg = <0x60>;

};

};

通过定义i2c_board_info结构显式实例化devies,然后在i2c设备驱动程序的init中调用i2c_new_device()

我没有devicetree条目,当我grep i2c_board_info,i2c_register_board_info()或i2c_new_device()的代码时,我找不到任何东西。 但是下面的代码仍然有用,怎么样?

#include

void read_from_device(uint8_t *read_data)

{

int result;

file_desc = open("/dev/i2c-2", O_RDWR);

ioctl(file_desc, I2C_SLAVE, device_address);

i2c_smbus_write_byte_data(file_desc, DEVICE_PAGE_ADDRESS, page_number);

result = i2c_smbus_read_byte_data(file_desc, device_register_address);

*read_data = result;

close(file_desc);

}

这是否意味着我们不一定要注册(实例化)i2c设备才能使用它们? 这是否适用于i2c驱动程序以及使用i2c-dev接口的用户空间程序?

I'm confused on how userspace programs using Linux's i2c dev interface is able to register (instantiate?) i2c devices.

From my understanding by reading this: https://www.kernel.org/doc/Documentation/i2c/instantiating-devices, it seems we need to either:

Define a i2c_board_info struct with name and address of the i2c device, do a i2c_register_board_info()

Have a devicetree entry such as this: i2c1: i2c@400a0000 {

/* ... master properties skipped ... */

clock-frequency = <100000>;

flash@50 {

compatible = "atmel,24c256";

reg = <0x50>;

};

pca9532: gpio@60 {

compatible = "nxp,pca9532";

gpio-controller;

#gpio-cells = <2>;

reg = <0x60>;

};

};

Instantiate devies explicitly by defining a i2c_board_info struct, then call i2c_new_device() in the init of the i2c device driver

But how is this done for user space programs using the i2c-dev interface described here https://www.kernel.org/doc/Documentation/i2c/dev-interface?

I don't have a devicetree entry, and when I grep the code for i2c_board_info, i2c_register_board_info(), or i2c_new_device() I don't find anything. But the code below still works, how?

#include

void read_from_device(uint8_t *read_data)

{

int result;

file_desc = open("/dev/i2c-2", O_RDWR);

ioctl(file_desc, I2C_SLAVE, device_address);

i2c_smbus_write_byte_data(file_desc, DEVICE_PAGE_ADDRESS, page_number);

result = i2c_smbus_read_byte_data(file_desc, device_register_address);

*read_data = result;

close(file_desc);

}

Does this mean we don't necessarily have to register (instantiate) i2c devices in order to use them? Does that apply to both i2c drivers as well as userspace programs using i2c-dev interface?

原文:https://stackoverflow.com/questions/41288441

更新时间:2020-02-14 06:28

最满意答案

i2c-dev驱动程序绑定到总线(i2c_adapter),而不是特定的i2c设备(i2c_client)。 因此,您只需创建总线设备即可使用i2c-dev,无需为客户端添加设备。 事实上,除非你使用I2C_SLAVE_FORCE ioctl,否则你会发现i2c-dev不会让你使用绑定到另一个驱动程序的I2C地址。

这与spidev驱动程序相反,spidev驱动程序绑定到特定的spi从设备而不是整个总线。 I2C早于现代Linux设备模型,有些事情与其他地方不同。

如果您希望内核驱动程序控制I2C设备,则需要有一个设备供驱动程序绑定。 例外的是所谓的“旧式”I2C驱动程序,它探测一组地址并绑定到设备(如果发现任何地址似乎是正确的类型)。

The i2c-dev driver binds to the bus (i2c_adapter), not a specific i2c device (i2c_client). So you only need to create the bus device to use i2c-dev and adding devices for the clients isn't necessary. In fact, you'll find i2c-dev will not let you use an I2C address bound to another driver unless you use the I2C_SLAVE_FORCE ioctl.

This is the opposite to the spidev driver, which binds to a specific spi slave device and not the bus as a whole. I2C predates the modern Linux device model and some things are different than is done in other places.

If you want a kernel driver to control the I2C device then there needs to be a device for the driver to bind to. The exception would be so-called "old style" I2C drivers which probe a set of addresses and bind to devices if any are found that appear to be the correct kind.

2016-12-25

相关问答

我们的(免费)培训材料有一个关于i2c子系统的部分: http : //free-electrons.com/doc/training/linux-kernel/linux-kernel-slides.pdf 此外,实际实验室正是为i2c设备编写输入驱动程序: http : //free-electrons.com/doc/training/linux-kernel/linux-kernel-labs.pdf Our (free) training materials have a section

...

确保内核在2.6内核分支中对sysfs的工作方式不同。 我也经历过类似的问题。 它与处理设备树有关。 设备树将变得不平整,但这只会启动设备的实际发现。 如果设备实际不存在,则不会探测它们,也不会在sysfs中创建条目。 设备树使用 Linux板支持代码调用of_platform_populate(NULL,NULL,NULL,NULL)来启动树根处的设备发现。 参数都是NULL,因为从树的根开始时,不需要提供起始节点(第一个NULL),父结构设备(最后一个NULL),并且我们没有使用匹配表(然而)

...

我终于解决了我的问题! 我不是软件问题,而是FPGA配置问题。 请点击此链接 ,发布#13以了解答案。 谢谢大家 ;-) I finally solved my problem! I was not a software problem but rather a FPGA configuration issue. Follow this link, post #13 to know the answer. Thanks to all ;-)

我觉得你很困惑。 MAX77651数据表中的 75。 ADDRESS | 7-BIT SLAVE ADDRESS | 8-BIT WRITE ADDRESS | 8-BIT READ ADDRESS

Main Address | 0x48, 0b 100 1000 | 0x90, 0b 1001 0000 | 0x91, 0b 1001 0001

(ADDR = 1)* | | |

...

为什么重复启动基于i2c操作不支持在Linux? 事实上,他们得到了支持。 如果您正在寻找在用户空间中执行重复启动条件的方法,则可能需要使用I2C_RDWR请求执行ioctl() , I2C_RDWR所述(请参阅原始问题中的最后一个代码片段)和此处 (代码有问题)。 下面描述了在内核空间中执行重复启动的方式。 在Linux内核中,默认情况下,对组合 (写入/读取)消息执行具有重复启动条件的 I2C读取操作。 以下是如何执行组合I2C传输的示例: /**

* Read set of registe

...

我不确定这是否为时已晚,但我已经在使用mono的BBB上完成了这项工作。 您需要查看DLLImport属性。 基本上,您需要编写一个小C ++文件并编译它,其中包含一个自定义方法,该方法只调用名为invoke_ioctl(...)的ioctl(),然后将编译后的DLL放入BBB上的/ bin目录并将其添加到C#中项目代码。 [DllImport("pinvoke.dll")]

[SuppressUnmanagedCodeSecurityAttribute()] //Optional, for sp

...

正如Nick Felker在他的回答中所写, DS18B20具有1-Wire接口,您无法直接将它与Android Things连接到Raspberry Pi。 您应该使用工业(例如DS2482-100 )或基于MCU的定制(例如在该项目中)1-Wire I2C转换器或其他(例如USB < - > 1-Wire,UART < - > 1-Wire)转换器。 As Nick Felker wrote in his answer DS18B20 has 1-Wire interface and you

...

i2c-dev驱动程序绑定到总线(i2c_adapter),而不是特定的i2c设备(i2c_client)。 因此,您只需创建总线设备即可使用i2c-dev,无需为客户端添加设备。 事实上,除非你使用I2C_SLAVE_FORCE ioctl,否则你会发现i2c-dev不会让你使用绑定到另一个驱动程序的I2C地址。 这与spidev驱动程序相反,spidev驱动程序绑定到特定的spi从设备而不是整个总线。 I2C早于现代Linux设备模型,有些事情与其他地方不同。 如果您希望内核驱动程序控制I2C

...

我想现在通过@Alexandre Belloni的评论更好地理解设备驱动程序模型并阅读这组演示幻灯片: http ://free-electrons.com/doc/training/linux-kernel/linux-kernel-slides.pdf。 相关幻灯片来自第221页至第236页。 有3种类型的设备驱动程序: 字符 网络 块 但是,存在作为字符设备驱动程序的子类存在的特定“框架”,其实现相同类型的设备的驱动程序的公共部分。 例如,用于硬件监控的主板上的温度传感器将在hwmon框架下

...

我连接了一个逻辑分析仪以嗅探I2C线路,看来当我连接LA时,I2C线路不再起作用,ERRNO返回IO故障(#5)。 逻辑分析仪只是一种测量设备。 它将捕获的数据转换为时序图,解码您设置的协议。 因此,它不会对任何I2C读写错误负责( 直到您的接地和h / w连接正确 )。 对于超时问题,您可以尝试降低i2c clock-frequency或ioctl I2C_TIMEOUT 。 It turns out the SOM has an internal regulator for the I2C l

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值