[英]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.