# Recovering from run time errors on stm32l0

I have the independent watch dog running and I get rare resets for unknown reasons.

How can I catch these errors and do a controlled reset. I want to store some things in EEPROM and then restart the application.

Unplugging the device however, the device needs to forget everything it knows. So I only want to restore EEPROM if an error occured.

void HardFault_Handler(void){
EEPROM_save_ram();
}

void Reset_Handler(void){
EEPROM_restore_ram();
}


Is there a bit that shows me if its a warm restart? A watchdog reset? or just a normal cold start?

• You do not want to do something like this in the hardfault handler! A hardfault gets raised if something is really wrong and at that point you can't be sure that you're RAM content is valid, or even if the stack is still usable. – erebos Sep 20 '16 at 9:43
• @erebos Yes you make a valid point – Flying Swissman Sep 21 '16 at 7:42

## 1 Answer

Yes there is normally a bit that will show you power-on/reset cause (have you checked the datasheet?).

One way to catch the errors might be to configure watchdog as interrupt instead of reset (not sure about STM32F0, but I have used this on similar chips).

As for saving and restoring RAM to/from EEPROM - it might be easier to just dump RAM contents in the reset handler (before CRT clears .bss and copies .data sections).

• I've found out how to see the reset cause. When is the reset handler called exactly? At the very beginning of a power on? – Flying Swissman Sep 20 '16 at 10:00
• Reset handler is called when ROM bootloader hands over control to "user" code. That would be the entry code in your code. You can pretty much treat it as "the first code that's executed" after power on. – domen Sep 20 '16 at 11:00