I2C concurrent access to Linux, mutex

I am writing a multi-threaded C program in embedded Linux that accesses from the user space the number of I2C devices (slaves). In addition, I am accessing the same I2C device from multiple threads. I use the SMBUS functions (i2c_smbus_write_byte_data, i2c_smbus_read_byte_data, i2c_smbus_read_i2c_block_data, ...).

Is there any protection against concurrent access built-in or do I need to add mutexes on my own?

For example: I have a read function that reads data from a single sensor through I2C. But the same function can also be called from another thread, which leads to possible concurrent access. Should I use some static mutex in this function or is it already in I2C access functions?

+6
source share
2 answers

I 2 C is a shared bus with several devices that can be accessed from both multiple processes and threads. Thus, the Linux driver code I 2 C uses the mutex to control access to each I 2 C bus.

For SMBus functions, see the Linux kernel function i2c_smbus_xfer() in i2c-core-smbus.c . It receives a lock for adapter I 2 C before starting the transfer (look at the source code and look at the call to i2c_lock_adapter() ). All SMBus transactions are based on this feature.

For i 2 C functions, see the Linux kernel function i2c_transfer() in i2c-core-base.c . Before starting the transfer, it receives a lock for the adapter I 2 C. All transactions I 2 C are based on this function.

So, there is protection against built-in built-in access.

+4
source

Use mutex in your program. The driver is not able to know the operations that each thread will do.

-1
source

Source: https://habr.com/ru/post/944649/


All Articles