I encountered a HardFault while working on a project, which was quite troublesome. HardFaults are typically difficult to locate, and the project has been on hold since then. After seeing a sharing from Zheng Nian Jun, I decided to share this useful resource with everyone; good things should not be kept hidden.
Have you ever encountered HardFault issues while using STM32?
I was troubled by this issue for a long time, so here’s a brief summary regarding it.
Phenomenon restoration: When simulating in debug mode, if the program runs at full speed and then stops, it will enter the HardFault_Handler function, resulting in a HardFault, also known as a hard error. The causes can generally be categorized as follows:
(1) Array out-of-bounds operation;
(2) Memory overflow and out-of-bounds access;
(3) Stack overflow, causing the program to crash;
(4) Interrupt handling errors;
For locating HardFault issues, there are several methods online, which generally involve checking certain addresses and analyzing registers and function call stacks in debug mode. This can be quite frustrating.
Here, I will share a simple and intuitive method for locating HardFault errors by using the open-source library: CmBacktrace. This library has been previously introduced, and in this note, we will put it into practice.
Introduction to CmBacktrace
CmBacktrace (Cortex Microcontroller Backtrace) is an open-source library designed for automatic tracking and locating of error codes for ARM Cortex-M series MCUs, along with automatic analysis of error causes. Its main features include:
-
Supported errors include: -
Assertion (assert) -
Faults (Hard Fault, Memory Management Fault, Bus Fault, Usage Fault, Debug Fault) -
Fault cause automatic diagnosis: Automatically analyzes the cause of the fault when it occurs and locates the code location of the fault without the need for manual analysis of complex fault registers; -
Compatible with Cortex-M0/M3/M4/M7 MCUs; -
Supports IAR, KEIL, and GCC compilers;
Porting and Usage (Keil)
Source code address for CmBacktrace:
https://github.com/armink/CmBacktrace
Copy the cm_backtrace folder to our project directory and add it to the Keil project, then add the header files and select C99 mode:
At this point, the compilation will produce several errors:
This is because some preprocessor macros were not found. Open and modify the contents of the cmb_cfg.h
file. The default content of the cmb_cfg.h
file is:
The modified content of cmb_cfg.h
becomes:
At this point, there will still be an error due to the redefinition of the HardFault_Handler function in cmb_fault.c
and stm32f10x_it.c
:
You need to disable the HardFault_Handler function in stm32f10x_it.c
:
Now it can compile successfully. Next, let’s take a look at the effect of this library.
We write the following test function:
Swipe left and right to see all code>>>
void fault_test_entry(fault_test_case_E _test_case)
{
switch (_test_case)
{
case FAULT_TEST_BY_DIV0:
fault_test_by_div0();
break;
case FAULT_TEST_BY_UNALIGN:
fault_test_by_unalign();
break;
default:
printf("test case error!\n");
break;
}
}
static void fault_test_by_div0(void)
{
volatile int * SCB_CCR = (volatile int *) 0xE000ED14; // SCB->CCR
int x, y, z;
*SCB_CCR |= (1 << 4); /* bit4: DIV_0_TRP. */
x = 10;
y = 0;
z = x / y;
printf("z:%d\n", z);
}
Then call the test function in the main function:
Download and run the program:
You can see that the listed information is very detailed, including the reason for the error. Following its prompt, we run the command:
Swipe left and right to see all code>>>
addr2line -e stm32f10x_demo.axf -a -f 0800162a 080016b7 08001719
Running this command requires the addr2line.exe tool, which is located in the tools folder of the CmBacktrace source directory:
There are both 32-bit and 64-bit versions available; choose according to our environment and copy it to the folder where the executable file .axf of our Keil project is located:
Open the command window in this folder by holding down the Shift key and right-clicking:
Run the above command:
You can see that the addr2line.exe
tool has located the line numbers related to the error; let’s see what the corresponding lines of code are:
It can be seen that the corresponding line number is indeed where the error occurred.
Using the CmBacktrace library can effectively and quickly help us locate errors like HardFault. The addresses following the addr2line command are the addresses related to the error, which can involve deep content. If we do not use the CmBacktrace library, we might have to analyze these low-level contents ourselves; related knowledge can be found in the book: “Cortex-M3/M4 Authority Guide”.
This concludes my note sharing. If there are any errors, please feel free to point them out! Thank you.
This note’s Keil project and CmBacktrace source code can be obtained by replying with the keyword: tracking library in the chat interface of this public account. For a detailed introduction to CmBacktrace, please read the full article.
You might also like:
A Collection of Essential Techniques for C Language and Embedded Bit Operations
SFUD | A Simple and Practical Open Source Project to Easily Handle SPI Flash
We are waiting for you to join:
Sendmin the chat interface to get the directory of past notes
Leave a Comment
Your email address will not be published. Required fields are marked *