2026-01-13 4:16 PM
When I Debug cmwx1zzabz_0xx, I get a hard fault somewhere near a call to HAL_PWR_EnterSLEEPMode(). It happens just after all the init code, somewhere in the first call to MX_LoRaWAN_Process(). If I step into MX_LoRaWAN_Process() and continue stepping, there is no fault.
Note that I commented out the calls to DBG_Disable() and DBG_ProbesInit() in SystemApp_Init() since they drop the ST-Link connection over USB.
2026-01-14 7:10 AM
How to debug Cortex-M Hard Faults.
2026-01-14 2:01 PM - last edited on 2026-01-15 12:51 AM by Andrew Neil
Thanks for the useful article. I recompiled with optimizations off. The PC after the Hard Fault suggests this line in HAL_PWR_EnterSTOPMode():
...
/* Select Stop mode entry --------------------------------------------------*/
if(STOPEntry == PWR_STOPENTRY_WFI)
{
/* Request Wait For Interrupt */
__WFI();
}
else
{
/* Request Wait For Event */
__SEV();
__WFE();
__WFE();
}
/* Reset SLEEPDEEP bit of Cortex System Control Register */
CLEAR_BIT(SCB->SCR, SCB_SCR_SLEEPDEEP_Msk);The request waits for event before executing the problem CLEAR_BIT macro. If I set a breakpoint on it, run to it and continue, the fault doesn't occur.
I tried replacing the CLEAR_BIT macro by reading the register to a local variable, clearing the bit in the variable and writing back to the register. This appears to fix the problem, but only if I build with Optimizations off (-O0). Any suggestions?
Edited to apply source code formatting - please see How to insert source code for future reference.