cancel
Showing results for 
Search instead for 
Did you mean: 

How to reset STM32 when a hardfault is triggered?

Xxoyo.1
Associate III

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!

7 REPLIES 7
Steffen Rose
Associate III

For the Reset:

possible

NVIC_SystemReset();

help you.

Steffen

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 */
  }
}

we dont need to firmware by ourselves, lets talk

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

Ozone
Lead

> 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.

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!

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

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

there is a catch.... hardfault interruption handler gets called in response to many different events.

maybe the thing that broke its the very thing you need to use inside the hardfault handler..... so you endup in a very tight hardaulf handler loop.

And youre in very high priority interruption so systick and other forms of interrupt based stuff wont work

we dont need to firmware by ourselves, lets talk