# Semaphore release leeds to hardfault

**URL:** <https://forums.freertos.org/t/semaphore-release-leeds-to-hardfault/11683>\
**Category:** Kernel\
**Created:** [February 2, 2021, 7:24pm UTC](https://forums.freertos.org/t/semaphore-release-leeds-to-hardfault/11683 "2021-02-02T19:24:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nfhktwrbq](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/nfhktwrbq/32/1233_2.png) [@nfhktwrbq](https://forums.freertos.org/u/nfhktwrbq)\
**Post date:** [February 2, 2021, 7:24pm UTC](https://forums.freertos.org/t/semaphore-release-leeds-to-hardfault/11683/1 "2021-02-02T19:24:52Z")

</div>

Hello, I use stm32cubeIDE. And stm32f103 mc. In my project GPIO generates interruption which release the semaphore:  
`osSemaphoreRelease(buttonSemHandle);`

All my interrupts has a priority not above the priority set by configMAX\_SYSCALL\_INTERRUPT\_PRIORITY == 5!  
`HAL_NVIC_SetPriority(EXTI4_IRQn, 5, 5);`

Semaphore was created like:  
`buttonSemHandle = osSemaphoreNew(10, 0, &buttonSem_attributes);`

In task i acquire the semaphore:  
`osSemaphoreAcquire(buttonSemHandle, 100) == osOK`

Fnd after few task executes I get hard fault. If 100 change to infinity then I get harfault as soon as task acquire semaphore.

Hard fault happened at `xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xStateListItem ) );`

In IDE - `Bus? memory management or usage fault (FORCED) Bus fault address register (BFAR): 0xa5a5a5a5`

If do not use semaphores that is all ok. Where may be problems?

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [February 2, 2021, 7:37pm UTC](https://forums.freertos.org/t/semaphore-release-leeds-to-hardfault/11683/2 "2021-02-02T19:37:24Z")

</div>

Have you defined `configASSERT` and stack overflow checking as mentioned here: [FreeRTOS - Open Source RTOS Kernel for small embedded systems](https://www.freertos.org/FAQHelp.html)

Looking at the pattern `0xa5a5a5a5`, it is probably a stack overflow. Try increasing the size of your task’s stack.

Thanks.

---

<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:** [February 3, 2021, 2:37am UTC](https://forums.freertos.org/t/semaphore-release-leeds-to-hardfault/11683/3 "2021-02-03T02:37:57Z")

</div>

Another thing to watch out for is that you aren’t putting some of these objects on the main function stack, as that gets reused for interrupts.

---

<div class="post-metadata">

**Author:** ![nfhktwrbq](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/nfhktwrbq/32/1233_2.png) [@nfhktwrbq](https://forums.freertos.org/u/nfhktwrbq)\
**Post date:** [February 3, 2021, 8:58pm UTC](https://forums.freertos.org/t/semaphore-release-leeds-to-hardfault/11683/4 "2021-02-03T20:58:43Z")

</div>

I check `configASSERT` - stm32cubeIDE defined this macros. Sometimes also I get infinite loop in this macro:  
`configASSERT( pxQueue->uxItemSize == 0 );` in xQueueGiveFromISR function.

> [@richard-damon](#):
>
> putting some of these objects on the main function stack,

`osSemaphoreNew(10, 0, &buttonSem_attributes)` seems like allocate at heap.

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [February 3, 2021, 9:06pm UTC](https://forums.freertos.org/t/semaphore-release-leeds-to-hardfault/11683/5 "2021-02-03T21:06:58Z")

</div>

> [@nfhktwrbq](#):
>
> pxQueue-\>uxItemSize == 0

was a symptom for stack overflows/memory corruption a couple of times here in the forum.  
You should post the code of the function where you create the tasks.  
Besides defining configASSERT do you have also [stack overflow checking](https://www.freertos.org/Stacks-and-stack-overflow-checking.html) enabled (for development/debugging) ?

---

<div class="post-metadata">

**Author:** ![nfhktwrbq](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/nfhktwrbq/32/1233_2.png) [@nfhktwrbq](https://forums.freertos.org/u/nfhktwrbq)\
**Post date:** [February 4, 2021, 7:47pm UTC](https://forums.freertos.org/t/semaphore-release-leeds-to-hardfault/11683/6 "2021-02-04T19:47:01Z")

</div>

I tried running recursive factorial function that exactly get stack overflow and in cube I got corresponding error message. In semaphore case seems like no stack overflow.

Code here:

> **[nfhktwrbq/ledMatrix\_m2](https://github.com/nfhktwrbq/ledMatrix_m2)**
>
> Contribute to nfhktwrbq/ledMatrix\_m2 development by creating an account on GitHub.

Define semaphore:  
`ledMatrix_m2/Core/Src/main.c`

Release semaphore:  
`ledMatrix_m2/Core/Src/irq.c`

Acquire semaphore:  
`ledMatrix_m2/Core/Src/radio.c`

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [February 4, 2021, 8:45pm UTC](https://forums.freertos.org/t/semaphore-release-leeds-to-hardfault/11683/7 "2021-02-04T20:45:05Z")

</div>

I doubt that with `DEBUG_PRINT_MAIN` enabled (using `printf` internally if I’m right) a stack size of just 32 \* 4 (= 128) is sufficient. I’d propose to try e.g. 1000 or more at least with `DEBUG_PRINT_MAIN` enabled or remove the prints.
