2024-02-18 08:51 AM
I am wondering if HAL functions have some builtin protection mechanism or if I have to wrap them around a semaphore/mutex take/give.
For example, if two tasks call `HAL_ADC_Start(&hadc1);` or `HAL_UART_Receive_IT`, what will happen? Shall I use a semaphore/mutex at OS level?
Solved! Go to Solution.
2024-02-18 12:46 PM
In the vast majority of cases, HAL functions on independent peripherals can be called in different threads with no issues in terms of code errors or race conditions or other bad behavior. This is true for your example of HAL_ADC_Start and HAL_UART_Receive_IT.
In general, only one thread should access a given peripheral. You shouldn't call HAL_UART_Receive_IT in two different threads. The lock mechanism in HAL is not thread-safe, although it does provide some minimal and superficial protection.
2024-02-18 12:46 PM
In the vast majority of cases, HAL functions on independent peripherals can be called in different threads with no issues in terms of code errors or race conditions or other bad behavior. This is true for your example of HAL_ADC_Start and HAL_UART_Receive_IT.
In general, only one thread should access a given peripheral. You shouldn't call HAL_UART_Receive_IT in two different threads. The lock mechanism in HAL is not thread-safe, although it does provide some minimal and superficial protection.