Showing results for 
Search instead for 
Did you mean: 

PSP switches to the handler stack upon function return


This was a tricky bug to find. I am doing context switching. Everything seems to working well until a context switch happens while one of my tasks has called a different function. Specifically, HAL_GetTick(). To be clear the switch is happening while I am in HAL_GetTick(). As I am stepping through everything seems to be fine. The CONTROL register is in thread mode, PSP is pointing to the thread stack. MSP is pointing to my handler stack. LR is pointing to the area HAL_GetTick() was called from. Upon return to the task, PSP suddenly points to the MSP stack. The CONTROL register is still in thread mode and the code proceeds normally, but as soon as I call HAL_GetTick() again, a hard fault is thrown.

The fault doesn't happen if a context switch doesn't happen inside a function that the task has called.

Yes, I am disabling interrupts when entering the SysTick and PendSV interrupt routines.

I am running on a STM32 Nucleo-144


Pavel A.
Evangelist III

Use FreeRTOS or RMX or other available RTOS.

Neither. I am writing my own.