Crash Handler#

The Wi-SUN Node Monitoring application includes a crash handler component that reports and recovers from application asserts and crashes. The crash handler’s information can be printed to RTT traces or sent over the air.

Before the board is reset, the assert file name and line number are saved.

Info on previous crash: [ASSERT in wisun_mac.c on line 1234]

If a hard fault occurs, the core registers (including the LR) are also saved.

Info on previous crash: [FAULT CFSR: 0x00008200
R0:   0x20001234, R1:    0x00000000, R2:   0x20004567, R3:   0xE000ED28
R12:  0xDEADBEEF, LR:    0x08012345, RET:  0x0800ABCD, XPSR: 0x21000000
HFSR: 0x40000000, MMFAR: 0x00000000, BFAR: 0x20005000, AFSR: 0x00000000]

Using Wi-SUN Crash Handler in Another Project#

  1. Copy sl_wisun_crash_handler.c and sl_wisun_crash_handler.h to the project, and add them to compilation.

  2. Add RMU (emlib_rmu) component to the project.

  3. Add linker flags below to the project. In Studio, go to Project -> Properties -> C/C++ Build -> Settings -> GNU ARM C Linker -> Miscellaneous. The component will work without them, but it will be unable to capture some asserts on GCC.

    -Wl,--wrap=__stack_chk_fail,--wrap=__assert_func
  4. Call crash handler initialization in app_init.

    #include "sl_wisun_crash_handler.h"
    ...
    void app_init(void)
    {
        sl_wisun_crash_handler_init();
    }
  5. After initialization, read crash reason.

    #include "sl_wisun_crash_handler.h"
    ...
    const sl_wisun_crash_t *crash;
    ...
    crash = sl_wisun_crash_handler_read();
    if (crash) {
        switch (crash->type) {
        case SL_WISUN_CRASH_TYPE_XXX:
            break;
        ...
        }
        sl_wisun_crash_handler_clear();
    }
  6. Alternatively, call sl_wisun_check_previous_crash() to fill the crash_info_string, and then print the string. This string will be available during the entire application runtime. (The Wi-SUN Node Monitoring application makes it available via a CoAP request.)

    #include "sl_wisun_crash_handler.h"
    ...
    int crash_type;
    ...
    crash_type = sl_wisun_check_previous_crash();
    if (crash_type) {
        printf("%s\n", crash_info_string);
    }

WARNING: If the project uses Gecko Bootloader, the bootloader's bss section may overlap with the application's noinit section, causing the crash handler to not function properly. In the Wi-SUN Node Monitoring application, this is avoided by increasing the stack size SL_STACK_SIZE, which pushes the application's noinit section further into RAM. Another possibility is to modify the generated linker script and swap the placement of application's bss and noinit sections.