# \_\_hal\_lock(\_\_handle\_\_)

**URL:** <https://forums.freertos.org/t/hal-lock-handle/10556>\
**Category:** ST\
**Created:** [August 27, 2020, 9:13am UTC](https://forums.freertos.org/t/hal-lock-handle/10556 "2020-08-27T09:13:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pavel213](https://avatars.discourse-cdn.com/v4/letter/p/e19adc/32.png) [@pavel213](https://forums.freertos.org/u/pavel213)\
**Post date:** [August 27, 2020, 9:13am UTC](https://forums.freertos.org/t/hal-lock-handle/10556/1 "2020-08-27T09:13:15Z")

</div>

Hello all  
I am using STM32F7 micro with HAL libraries. I had ported freertos successfully in my code. code is running fine.  
But i want to know that HAL libraries use \_\_HAL\_LOCK( **HANDLE** ) and \_\_HAL\_UNLOCK( **HANDLE** ) macros in all their library .c files. Though i m using rtos do i have to remove these both macros manually or is there any work around available.  
Thanks and regards, Pavel

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [August 27, 2020, 3:54pm UTC](https://forums.freertos.org/t/hal-lock-handle/10556/2 "2020-08-27T15:54:33Z")

</div>

I don’t know what \_\_HAL\_LOCK() and \_\_HAL\_UNLOCK() do, but ST’s own tools generate FreeRTOS projects - what do they do for this? Can you just use a project as provided by ST?

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [August 27, 2020, 11:14pm UTC](https://forums.freertos.org/t/hal-lock-handle/10556/3 "2020-08-27T23:14:44Z")

</div>

I think the HAL comes with a version of these macros designed for FreeRTOS (using likely either a semaphore or a mutex). If not, it wouldn’t be that hard to create such a macro. Just removing them would break a lot of the code, and it has enough problems on its own, you don’t need to add to them.

---

<div class="post-metadata">

**Author:** ![pavel213](https://avatars.discourse-cdn.com/v4/letter/p/e19adc/32.png) [@pavel213](https://forums.freertos.org/u/pavel213)\
**Post date:** [August 28, 2020, 7:06am UTC](https://forums.freertos.org/t/hal-lock-handle/10556/4 "2020-08-28T07:06:40Z")

</div>

> [@rtel](#):
>
> \_\_HAL\_LOCK()

this is the code snippet

#if (USE\_RTOS == 1U)  
/\* Reserved for future use \*/  
#error “USE\_RTOS should be 0 in the current HAL release”  
#else  
#define \_\_HAL\_LOCK( **HANDLE** )   
do{   
if(( **HANDLE** )-\>Lock == HAL\_LOCKED)   
{   
return HAL\_BUSY;   
}   
else   
{   
( **HANDLE** )-\>Lock = HAL\_LOCKED;   
}   
}while (0U)

#define \_\_HAL\_UNLOCK( **HANDLE** )   
do{   
( **HANDLE** )-\>Lock = HAL\_UNLOCKED;   
}while (0U)  
#endif /\* USE\_RTOS \*/

if I define USE\_RTOS = 1 then code does not build. So do i have to replace \_\_HAL\_LOCK() and \_\_HAL\_UNLOCK() with semaphore or mutex in all hal libraries to use freertos properly.

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [August 28, 2020, 11:24am UTC](https://forums.freertos.org/t/hal-lock-handle/10556/5 "2020-08-28T11:24:28Z")

</div>

Looks like in typical ST fashion, they haven’t implemented that functionality for FreeRTOS. You will need to modify THAT file to implement the \_\_HAL\_LOCK and \_\_HAL\_UNLOCK functionality. There should likely be some other function to setup the lock handle too.

---

<div class="post-metadata">

**Author:** ![jefftenney](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/jefftenney/32/1314_2.png) [@jefftenney](https://forums.freertos.org/u/jefftenney)\
**Post date:** [August 28, 2020, 2:50pm UTC](https://forums.freertos.org/t/hal-lock-handle/10556/6 "2020-08-28T14:50:58Z")

</div>

> [@pavel213](#):
>
> So do i have to replace \_\_HAL\_LOCK() and \_\_HAL\_UNLOCK() with semaphore or mutex in all hal libraries to use freertos properly.

No, I don’t think so. That would be pretty difficult since the “handle” they provide is the address of a device control block in the HAL for a specific device. I reviewed a couple of uses of these “locks”, and they appear merely to be for rejecting recursive calls into the HAL for that device from user-provided callback functions called by the HAL. It’s hard to see that they do anything else of any use. They are not even interrupt safe (even when _not_ using an OS).

The HAL is not OS friendly, but I think you can use these macros as-is. Just be sure to use the HAL assuming nothing about it is threadsafe. (Redefining these macros yourself wouldn’t make the HAL threadsafe.) I haven’t looked but maybe there is an app note or user guide from ST about using the HAL with FreeRTOS. That would shed more light here.
