This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Hard fault - memset and printf

Hi,

I'm using NRF52840 and having a lot of issues with hard fault.

From time to time, I get an application crash with the following log:

HARD FAULT at 0x00000000
R0: 0x00000010 R1: 0x00000000 R2: 0x20007848 R3: 0x00000000
R12: 0x20012FCC LR: 0x00031F27 PSR: 0x00000000

Only when I run a specific function in a loop I can reproduce this error.

After a long investigation, I understood that the hard fault occurs due to a specific function. Inside this function I have a lot of actions, but it starts with a memset for a small byte array (8 bytes), and a log which is implemented with printf.

The weird thing is that only when I delete one of those two actions - hard fault doesn't occur anymore. Does it make sense? How can it be possible that those 2 simple actions can cause this hard fault?

Thanks!

Parents
  • Hi,

    I recommend using the nrf_log module for logging. If you use deferred mode, the log processing is then shifted to idle stage.

    If you are still experiencing hardfaults after switching to the nrf_log module, then make sure that the hardfault library is used.

    Add these to your projects:

    $(SDK_ROOT)/components/libraries/hardfault/hardfault_implementation.c
    $(SDK_ROOT)/components/libraries/hardfault/nrf52/handler/hardfault_handler_gcc.c 

    And set HARDFAULT_HANDLER_ENABLED to 1 in sdk_config.h, and add DEBUG_NRF in your preprocessor definitions.

Reply
  • Hi,

    I recommend using the nrf_log module for logging. If you use deferred mode, the log processing is then shifted to idle stage.

    If you are still experiencing hardfaults after switching to the nrf_log module, then make sure that the hardfault library is used.

    Add these to your projects:

    $(SDK_ROOT)/components/libraries/hardfault/hardfault_implementation.c
    $(SDK_ROOT)/components/libraries/hardfault/nrf52/handler/hardfault_handler_gcc.c 

    And set HARDFAULT_HANDLER_ENABLED to 1 in sdk_config.h, and add DEBUG_NRF in your preprocessor definitions.

Children
  • Hi,

    Thanks for you answer!

    After disabling all logs, hard fault still occurs.

    I already have hardfault_implementation.c and hardfault_handler_gcc.c in my project, and HARDFAULT_HANDLER_ENABLED set to 1.

    However - DEBUG_NRF isn't defined in my project. Can you please explain how will it help?

    Thanks!

  • HARD FAULT at 0x00000000

     Is it always 0? I would have expected a valid PC value here, pointing to the code that is causing the hardfault.

    Did you try turning off optimization?

    I.e. in the makefile change "OPT = -O3 -g3" to "OPT = -O0 -g3".

    If you are using SES, change from "Release" build to "Debug" build.

    and a log which is implemented with printf.
    If you are using e.g. NRF_LOG other places in your code, you should use NRF_LOG instead of printf
Related