Follow+Star Public Account Number, don’t miss exciting content
Author | strongerHuang
Public Account | Embedded Column
Embedded Column
1
There are many types of Fault faults, taking this article’s Cortex-M3 as an example, mainly including:
-
HardFault: Hard Fault
-
MemManage: Memory Management Fault
-
BusFault: Bus Fault
-
UsageFault: Usage Fault
void HardFault_Handler(void){ /* Go to infinite loop when Hard Fault exception occurs */ while (1) { }}
void MemManage_Handler(void){ /* Go to infinite loop when Memory Manage exception occurs */ while (1) { }}
void BusFault_Handler(void){ /* Go to infinite loop when Bus Fault exception occurs */ while (1) { }}
void UsageFault_Handler(void){ /* Go to infinite loop when Usage Fault exception occurs */ while (1) { }}
Embedded Column
2
Each type of Fault fault must have certain causes. If your code generates a Fault fault interrupt, it indicates that certain parts of the code have caused the Fault fault.
1. HardFault: Hard Fault
From the description in the screenshot, you will find that a hard fault is an “unprogrammable” fault, because if memory management faults, bus faults, and usage faults cannot be executed, they will be treated as hard faults.
For example: a bus fault generated when accessing a vector is also treated as a hard fault. So, you will find that many times the faults encountered are hard faults.
-
Accessed an address outside the range set by the MPU
-
Wrote data to a read-only region
-
Accessed an address that is only allowed to be accessed at the privileged level while at the user level
-
Instruction fetching, usually referred to as “prefetch abort”
-
Data read/write, usually referred to as “data abort”
-
PUSH action of the stack at the beginning of interrupt handling, referred to as “stack entry error”
-
POP action of the stack at the end of interrupt handling, referred to as “stack exit error”
Embedded Column
3
I wonder if everyone usually has effective measures to avoid these?
Here are a few simple measures to deal with faults:
1. Determine program errors through the values of fault status registers
In the fault interrupt function, read the fault status (described above), for example, for hard faults:
void HardFault_Handler(void){ // Read status register, print status register, determine the cause of the fault printf("Status x information"); while (1) { }}
If you don’t want the system to be in a deadlock state, you can perform a soft reset in the interrupt.
2. Analyze and predict the code in advance
For example: use static code analysis tools to analyze the code and find bugs.
Not long ago, I shared an article: Recommended Static Code Analysis Tools
3. Other diagnostic methods
Reply in the background 『Cortex-M3』『Microcontroller』 to read more related articles.
Click “Read the Original” to see more shares, welcome to share, collect, like, and check.