Skip to main content
AAgar.2
Associate III
June 10, 2020
Solved

How to handle HardFault?

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

This topic has been closed for replies.
Tesla DeLorean
Tesla DeLoreanBest answer
Guru
June 10, 2020

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 (See Profile) Up vote any posts that you find helpful, it shows what's working..

3 replies

Tesla DeLorean
Tesla DeLoreanBest answer
Guru
June 10, 2020

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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
Tesla DeLorean
Guru
June 10, 2020
Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..