[英]Linux's i2c dev interface with multiple processes
我编写了一个快速的用户空间程序,使用此处描述的i2c开发人员界面访问i2c设备: https : //www.kernel.org/doc/Documentation/i2c/dev-interface
问题是我不确定如何使此多进程和多线程安全,或者Linux是否已经处理了该问题。
这是代码的准系统:
#include <linux/i2c-dev.h>
void read_from_device(void)
{
int result;
file_desc = open(/dev/i2c-2, O_RDWR);
/* Possible critical section #1 starting here? */
ioctl(file_desc, I2C_SLAVE, device_address);
/* Possible critical section #2 starting here
* This device requires writing to a page register first
* before accessing the wanted register */
i2c_smbus_write_byte_data(file_desc, DEVICE_PAGE_ADDRESS, page_number);
/* I don't want another process in come in between here and
* setting a different page address before accessing the register*/
result = i2c_smbus_read_byte_data(file_desc, device_register_address);
/* Critical section end for both possibilities */
close(file_desc);
}
因此,有两个可能的关键部分:
有人在这里提出了类似的问题,并且响应是Linux很好地处理了对同一适配器的多进程访问。 我想确认这意味着什么以及需要从用户空间担心的线程安全访问的哪些部分。
由I2C_SLAVE
ioctl()设置的I2C从地址存储在每次打开/dev/i2c-X
分配的i2c_client
中。 因此,此信息对于/ dev / i2c-X的每个“开口”都是本地的。
关于在I2C设备中设置页面寄存器,只要没有其他进程与同一I2C设备通信就可以。
一般而言,如果您担心多个进程访问一个设备,则应该为该设备编写Linux内核驱动程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.