cancel
Showing results for 
Search instead for 
Did you mean: 

How to handle HardFault?

AAgar.2
Associate III

There is a function called HardFault_Handler in the STM32 MCUs (full code below). This function I usually work with when debugging but am unsure how to use this for handling error. What is the correct protocol to handle errors in this function? If a fault has happened ideally I would like to clear the MCU ram and reset the MCU. However what if the handler code causes more faults. Is there a standard protocol for handling faults (not just HardFaults)?

void HardFault_Handler(void)
{
  /* USER CODE BEGIN HardFault_IRQn 0 */
 
  /* USER CODE END HardFault_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_HardFault_IRQn 0 */
    /* USER CODE END W1_HardFault_IRQn 0 */
  }
}

1 ACCEPTED SOLUTION

Accepted Solutions

You can technically return if you remediate the issue it faulted over, but that requires a very deep understanding of the CPU/ISA

What I normally do is output enough diagnostic information about registers, and perhaps a partial stack dump, so I can provide useful answers about the failure to the boss/customers rather than shrug my shoulders.

If you don't do anything complicated it shouldn't double-fault. Zeroing the imternal RAM using simple code (looping with internal registers, no function calls) should work fine.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

3 REPLIES 3

You can technically return if you remediate the issue it faulted over, but that requires a very deep understanding of the CPU/ISA

What I normally do is output enough diagnostic information about registers, and perhaps a partial stack dump, so I can provide useful answers about the failure to the boss/customers rather than shrug my shoulders.

If you don't do anything complicated it shouldn't double-fault. Zeroing the imternal RAM using simple code (looping with internal registers, no function calls) should work fine.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

That makes sense. What registers are useful for diagnostic info? For clearing RAM I can just memset to NULL for all vars?

Whether the app can continue would be determined on the nature of the failure. I would think in buffer overflow or peripheral failure the app cannot continue after fault.

Appreciate the support clive.