This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

SPI & I2C r/w access from multiple tasks

My application has multiple tasks reading and writing to SPI or I2C sensors. Each task is somewhat dedicated to dealing just with 1 sensor. Are the SPI & I2C drivers capable of handling multiple tasks wanting to access them synchronously? I.e. do the drivers handle the blocking, etc? Or do i have to setup my own Semaphore to make sure only one task accesses SPI at once? 

  • Hi Pixbroker,

           The SPI and I2C drivers can be opened only once for a particular peripheral instance but can handle being accessed multiple times even from multiple tasks. The drivers usually present 2 modes, a blocking mode and a callback mode. The blocking mode which is the default way the drivers work use an internal semaphore to ensure that the driver access calls block till they're completed. In the callback mode the driver access calls return immediately but call a user-provided callback function when the operation is complete. To handle multiple access which could happen when running multiple tasks, the drivers maintains a queue of transactions and handles them in the order they came in. Answering your second question specifically, you don't have to create your own semaphore, it's done by the driver in the default blocking mode.

    Let me know if this helps,

    Moses