Skip to main content
Xxoyo.1
Associate III
May 6, 2021
Question

How to reset STM32 when a hardfault is triggered?

Is there a way to programmatically reset my STM32 program back to the beginning like when pressing the Hardware Reset Button ?

I would like to reset my program when a random hard-fault has occurred, if possible, i'd also like to store to flash the date/time and some user message when the hard-fault has occurred .

Thanks!

This topic has been closed for replies.

7 replies

Steffen Rose
Associate III
May 6, 2021

For the Reset:

possible

NVIC_SystemReset();

help you.

Steffen

Javier1
Principal
May 6, 2021

You could try this:

Search for your hardfault handler in stm32xxxxit.c

void HardFault_Handler(void)
{
 /* USER CODE BEGIN HardFault_IRQn 0 */
 store_to_flash_the datetime();
 some_user_message_when_the_hardfault_has_occurred();
 NVIC_SystemReset();
 /* USER CODE END HardFault_IRQn 0 */
 while (1)
 {
 /* USER CODE BEGIN W1_HardFault_IRQn 0 */
 /* USER CODE END W1_HardFault_IRQn 0 */
 }
}

hit me up in https://www.linkedin.com/in/javiermuñoz/
Xxoyo.1
Xxoyo.1Author
Associate III
May 7, 2021

Yes, this is exactly what i was trying to search for! Thank You very much!

Ozone
Principal
May 7, 2021

> I would like to reset my program when a random hard-fault has occurred, if possible, i'd also like to store to flash the date/time and some user message when the hard-fault has occurred .

You got something wrong here.

Hardfault do not occur randomly, but indicate a serious problem with your firmware.

Resetting it will only mask the problem.

"Randomness" suggests either a stack overflow or a race condition.

Xxoyo.1
Xxoyo.1Author
Associate III
May 7, 2021

I'm totally aware of this, but for my situation instead of tracing the root cause, the most cost effective way is to reset the device.

the stack overflow/race condition is a bit tricky for me to debug since i'm inexperienced and the hardfault happens infrequently, so resetting the device seems to be optimal solution.

Thanks for your suggestion!

Ozone
Principal
May 7, 2021

> ... the most cost effective way is to reset the device.

In my experience, such short-term thinking comes with dire consequences mid- or longterm.