我编写了一个快速的用户空间程序来访问i2c设备,使用这里描述的i2c / dev接口: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很好地处理了对同一个适配器的多个进程访问。我想确认这意味着什么,以及我需要从用户空间担心线程安全访问的哪些部分。
发布于 2016-12-16 15:13:33
I2C ioctl()设置的I2C_SLAVE从地址存储在每次打开/dev/i2c-X时分配的i2c_client中。因此,这个信息对于/dev/i2c-X的每个“开始”都是本地的。
关于在I2C设备中设置页面寄存器,只要没有其他进程与同一个I2C设备对话,就可以了。
一般来说,如果您关心通过多个进程访问一个设备,您应该为该设备编写一个Linux内核驱动程序。
https://stackoverflow.com/questions/41172797
复制相似问题