繁体   English   中英

Linux的i2c开发人员界面具有多个进程

[英]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);
}

因此,有两个可能的关键部分:

  1. Linux的i2c开发人员接口是否可以处理设置I2C_SLAVE的多个进程? 含义:一旦为该适配器/ dev / i2c-2设置了I2C_SLAVE,是否可以进入另一个进程并将其更改为总线上的另一个设备?
  2. 在设备上设置页面寄存器然后设置它自己的page_number后,我不希望另一个进程进入,那么我的读取将不正确。 此处描述的进程共享互斥体是否合适?

有人在这里提出了类似的问题并且响应是Linux很好地处理了对同一适配器的多进程访问。 我想确认这意味着什么以及需要从用户空间担心的线程安全访问的哪些部分。

I2C_SLAVE ioctl()设置的I2C从地址存储在每次打开/dev/i2c-X分配的i2c_client中。 因此,此信息对于/ dev / i2c-X的每个“开口”都是本地的。

关于在I2C设备中设置页面寄存器,只要没有其他进程与同一I2C设备通信就可以。

一般而言,如果您担心多个进程访问一个设备,则应该为该设备编写Linux内核驱动程序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM