Have you encountered the HardFault issue while using STM32?
I was troubled by this issue for a long time, and here’s a brief summary of it.
Phenomenon restoration: During simulation debugging in debug mode, when running at full speed and then stopping, the program jumps to the HardFault_Handler function, resulting in a HardFault, which is a serious error. The causes of this can be categorized into the following types:
(1) Array out of bounds operations;
(2) Memory overflow, out-of-bounds access;
(3) Stack overflow, program crash;
(4) Interrupt handling errors;
For locating the HardFault issue, there are several methods available online, generally revolving around: in debug mode, checking certain addresses, analyzing registers, function call stacks, etc. This can be quite troublesome.
Here, I will share a simple and intuitive method for locating HardFault errors using the open-source library: CmBacktrace. This library has been introduced previously, and in this note, we will practice it.
Introduction to CmBacktrace
CmBacktrace (Cortex Microcontroller Backtrace) is an open-source library for automatic tracking and locating of error codes for ARM Cortex-M series MCUs, with automatic analysis of error causes. Its main features are as follows:
-
Supported errors include: -
Assertions (assert) -
Faults (Hard Fault, Memory Management Fault, Bus Fault, Usage Fault, Debug Fault) -
Fault cause automatic diagnosis: It can automatically analyze the cause of the fault when it occurs and locate the code position where the fault happened, without needing to manually analyze complex fault registers; -
Compatible with Cortex-M0/M3/M4/M7 MCUs; -
Supports IAR, KEIL, 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, also add the header files and check the C99 mode:
At this point, compilation will produce several errors:
This is because some preprocessor macros were not found. Open and modify the cmb_cfg.h
file content. The default content of cmb_cfg.h
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 comment out the HardFault_Handler function in stm32f10x_it.c
:
At this point, the compilation can pass. Now let’s see the effect of this library.
We write the following test function:
Swipe left and right to see all the 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 the 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 code directory:
There are both 32bit and 64bit versions, choose according to our environment, and copy it to the folder where the executable file .axf of our keil project is located:
In this file, enter the cmd window by holding down the Shift key while clicking the right mouse button:
Run the command above:
You can see that the addr2line.exe
tool has located the line numbers of the error-related code, let’s see what the corresponding code is:
It can be seen that the corresponding line number is exactly where the error occurred.
It can be seen that using the CmBacktrace library can help us effectively and quickly locate errors like HardFault. The addresses following the addr2line command are related to the errors, and these addresses can involve deep content. If we do not use the CmBacktrace library, we might have to analyze these lower-level contents ourselves; related knowledge can be read in: “The Definitive Guide to Cortex-M3/M4”.
This concludes my note sharing for this time. If there are any errors, please feel free to point them out! Thank you.
The Keil project and CmBacktrace source code for this note can be obtained by replying with the keyword: Tracking Library in the chat interface of this public account. For detailed introduction about CmBacktrace, you can read the full text.
You may also like:
Summary of Essential Techniques for C Language and Embedded Bit Operations
SFUD | A simple and practical open-source project to help you easily manage SPI Flash
We are waiting for you to come:
Sendmin the chat interface to get the directory of previous notes