Experience Sharing on Using ARM Cortex-M Series MCU Error Code Automatic Tracking Library

Experience Sharing on Using ARM Cortex-M Series MCU Error Code Automatic Tracking Library

Have you encountered HardFault issues while using STM32?

Experience Sharing on Using ARM Cortex-M Series MCU Error Code Automatic Tracking Library

I was troubled by this issue for quite a while, so here’s a brief summary.

Phenomenon reproduction: During debugging in simulation mode, when running at full speed and then stopping, the program hits the HardFault_Handler function, resulting in a HardFault, or hard error. The causes can be roughly categorized as follows:

(1) Array out-of-bounds operation;

(2) Memory overflow, out-of-bounds access;

(3) Stack overflow, program runaway;

(4) Interrupt handling errors;

For locating HardFault issues, there are several methods available online, mainly revolving around: in debug mode, checking certain addresses, analyzing registers, function call stacks, etc., which can be quite troublesome.

Here, I’ll 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 put it into practice.

Introduction to CmBacktrace

CmBacktrace (Cortex Microcontroller Backtrace) is an open-source library designed for automatic tracking, locating error codes, and automatic analysis of error causes for ARM Cortex-M series MCUs. Its main features are as follows:

  • 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, locating the code position where the fault happened, without the need for manual analysis of 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

Experience Sharing on Using ARM Cortex-M Series MCU Error Code Automatic Tracking Library

Copy the cm_backtrace folder to our project directory and add it to the Keil project, including header files and enabling C99 mode:

Experience Sharing on Using ARM Cortex-M Series MCU Error Code Automatic Tracking Library

Experience Sharing on Using ARM Cortex-M Series MCU Error Code Automatic Tracking Library

At this point, compiling will produce several errors:

Experience Sharing on Using ARM Cortex-M Series MCU Error Code Automatic Tracking Library

This is because some preprocessor macros are not found. Open and modify the cmb_cfg.h file content. The default content of cmb_cfg.h is:

Experience Sharing on Using ARM Cortex-M Series MCU Error Code Automatic Tracking Library

The modified cmb_cfg.h content becomes:

Experience Sharing on Using ARM Cortex-M Series MCU Error Code Automatic Tracking Library

At this point, there will still be an error regarding the redefinition of the HardFault_Handler function in cmb_fault.c and stm32f10x_it.c:

Experience Sharing on Using ARM Cortex-M Series MCU Error Code Automatic Tracking Library

You need to comment out the HardFault_Handler function in stm32f10x_it.c:

Experience Sharing on Using ARM Cortex-M Series MCU Error Code Automatic Tracking Library

Now you can compile successfully. Let’s see the effect of this library.

We write the following test function:

Swipe left and right to view 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:

Experience Sharing on Using ARM Cortex-M Series MCU Error Code Automatic Tracking Library

Experience Sharing on Using ARM Cortex-M Series MCU Error Code Automatic Tracking Library

Download and run the program:

Experience Sharing on Using ARM Cortex-M Series MCU Error Code Automatic Tracking Library

You can see that the listed information is very detailed, including the cause of the error. According to its prompt, we run the command:

Swipe left and right to view 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 code directory:

Experience Sharing on Using ARM Cortex-M Series MCU Error Code Automatic Tracking Library

There are both 32-bit and 64-bit versions, choose according to our environment, and copy it to the folder where the executable file .axf of our Keil project is located:

Experience Sharing on Using ARM Cortex-M Series MCU Error Code Automatic Tracking Library

In this folder, enter the cmd window by holding the Shift key and right-clicking:

Experience Sharing on Using ARM Cortex-M Series MCU Error Code Automatic Tracking Library

Run the command above:

Experience Sharing on Using ARM Cortex-M Series MCU Error Code Automatic Tracking Library

You can see that the addr2line.exe tool has pinpointed the line numbers of the code related to the error. Let’s see what the corresponding line of code is:

Experience Sharing on Using ARM Cortex-M Series MCU Error Code Automatic Tracking Library

As you can see, the corresponding line number is indeed the place where the error occurred.

It can be seen that using this CmBacktrace library helps us effectively and quickly locate errors like HardFault. The addresses following the addr2line command are related to the error, and these addresses can involve deep content. If we do not use the CmBacktrace library, we may have to analyze these low-level contents ourselves. Related knowledge can be read in: “Cortex-M3/M4 Authority Guide”.

This concludes my note sharing for this time. Please feel free to point out any mistakes! Thank you.

Original source: Embedded Miscellaneous

Copyright belongs to the original author or platform, for learning reference and academic research only. If there is any infringement, please contact for deletion~ Thank you.

Finally

The author has collected some embedded learning materials, reply in the public account1024 to find the download link!

Recommended Good Articles  Click on the blue font to jump
☞ Album|Linux Application Programming Handbook
☞ Album|Learn Some Network Knowledge
☞ Album|Manual C Language

☞ Album|Manual C++ Language
☞ Album|Experience Sharing
☞ Album|From Microcontroller to Linux
☞ Album|Energy Control Technology
☞  MCU Advanced Album 
☞  Embedded C Language Advanced Album 
☞  Experience Sharing




Leave a Comment