Hello,
I'm using FreeRTOS and I'am wondering if the HAL Driver functions are thread safe.
So if I call for example the HAL_I2C_Master_Transmit function from multiple threads, do they interfere with each other, or are they thread safe?
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
2019-03-06 8:05 AM
This discussion is locked. Please start a new topic to ask your question.
1 REPLY 1
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
2019-03-06 3:15 PM
If you read stm32f7xx_hal_i2c.c you'll observe __HAL_LOCK() isn't thread safe because it doesn't atomically test and set (__HANDLE__)->Lock. I.e. task A tests and finds it's unlocked, and task B preempts it and also finds it's unlocked and locks it, task A resumes and locks it too.
The API isn't thread-safe more than one way.
- Tasks may call the same function and fail in some fashion.
- Tasks may call different functions and fail in some fashion.
The general options are:
- Wrap the entire API with a mutex.
- Design your software so only one task accesses the I2C peripheral, e.g. using a queue or ring-buffer/signal, and possibly state-machine.
Option 2 is the better option IMHO because its software is more organised and more easily comprehended (i.e. less bugs).
