Hardfault waiting for event flags after setting flags in timer expiry. (CMSIS OS v2 API)

I’m using FreeRTOS v10.2.1 via the CMSIS-OS v2 API on an stm32l0 controller.

There’s an instance when start a one-shot timer and when the timer expires, I proceed to set an event flag. I have a thread waiting on these flags succesfully, however when the control loop proceeds back to waiting on the event flag, I hardfault in the PendSV_Handler() function.

So in summary to reproduce:

  1. Create a one-shot timer
  2. Create a thread
  3. Create an event group
  4. Have thread wait on any flags of the event group
  5. Start the one-shot timer
  6. When timer expires, set a flag of the event group
  7. Thread wakes up, does some processing
  8. Thread waits on event flags again
  9. Hardfault on step 8.

Any idea why this is happening? Thanks

Except fatal application programming bugs the main reasons for application crashes are

  1. stack overflows
  2. wrong interrupt priorities for certain ports/MCUs e.g. with Cortex-M CPUs

This especially applies to such strange issues like you’ve encountered (code runs fine and next time the same code hardfaults).

You should define configASSERT and also enable stack overflow checking for development/debugging.
Using assertions can help to catch a number of otherwise occurring HardFaults with the possibility to trace back and identify the problem.
Having configASSERT defined (in FreeRTOSConfig.h as documented) makes debugging a lot easier by stopping the debugger if the application hangs. If the hang is caused by an assertion, the application is halted in the forever loop usually the last part of the assertion code (as in example 2 following the configASSERT link) and check the call stack for the cause of the assertion.

Edit: Do you use FreeRTOS timers or a HW timer ?

Thanks, I enabled stack overflow checking and it turns out the timer task was overflowing.
(I’m using the freertos timers)