AnsweredAssumed Answered

STM32L Hard fault after waking up from EXTI

Question asked by kikugawa.sho on Dec 18, 2012
Latest reply on Dec 18, 2012 by kikugawa.sho
I'm programming for an STM32L152xx and ran into the part hard faulting when it goes to sleep but wakes up from an external interrupt.

Since I need the lower power sleep mode, I'm doing the necessary steps to get there (enabling the right flags in PWR->CR, switching the clock to MSI, calling WFI). When I wake up the part with an external interrupt, right after it's done handling the interrupt, it hard faults. I can break at the last line and hit "Step Over" and the processor jumps to the hard fault handler. The reason being the processor was in an invalid state at some point. I can't find anything obvious in the stack and Joseph Yui's hard fault handler hangs at the first line (R4 is always 0)

The catch is though, this only happens if I enable optimizations. If I take it off, the software runs just fine. The IDE is Rowley CrossStudio, which is using GCC. I'm sure the optimization it performs is the -O flag.

If this something GCC related, I can wait until I transfer over to IAR (I'm waiting for a license) assuming they don't use GCC.

Some other oddities if it interests people:
       
  • It happens if the processor goes to sleep. If I take out the WFI call, it doesn't hard fault
  •    
  • If I don't clear the EXTI pending flag for the EXTI interrupt, it will hang in the interrupt and the hard fault will never happen.

Outcomes