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



我已经编写了一个快速的用户空间程序,以使用此处描述的I2C Dev接口访问I2C设备:

问题是我不确定如何使此多进程和多线程安全,或者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 Dev接口是否处理多个过程设置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内核驱动程序。

最新更新