Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

nRF52 Hardfault implementation doubts

Hello,

I'm working with nRF5 SDK v16.0.0.

The documentation on Hardfault handler implementation clearly suggests to define DEBUG_NRF in order to get an automatic breakpoint on HF, before the call to HardFault_process

What I see in the code it is this instead:

#if defined(DEBUG)

    NRF_BREAKPOINT_COND;

 #endif // defined (DEBUG)

#endif // __CORTEX_M == 0x04

    HardFault_process((HardFault_stack_t *)p_stack_address);
}

What's the difference between DEBUG and DEBUG_NRF? I don't see any explicit relation between the two, and as such I think there could be a mistake on the documentation.

DEBUG is used in the app_error libraries as well.

Moreover, the signature header of HardFault_process states:

/**
 * @brief Function for processing HardFault exceptions.
 *
 * An application that needs to process HardFault exceptions should provide an implementation of this function.
 * It will be called from the HardFault handler.
 * If no implementation is provided, the library uses a default one, which just restarts the MCU.
 *
 * @note If the DEBUG_NRF macro is defined, the software breakpoint is set just before the call
 *       to this function.
 *
 * @param p_stack Pointer to the stack bottom.
 *                This pointer might be NULL if the HardFault was called when the main stack was
 *                the active stack and a stack overrun is detected.
 *                In such a situation, the stack pointer is reinitialized to the default position,
 *                and the stack content is lost.
 */
void HardFault_process(HardFault_stack_t * p_stack);

Can you point me to the code which is handling the case of main stack + stack overrun, so that p_stack is set to NULL? I did not find anything as such.

Moving back to macro definitions, it would be great to have a document where the usage DEBUG_NRF, DEBUG and other switches used in the code to enable debug behaviour is described.

If such a document already exists, please let me know!

Thanks a lot,

Kind Regards,

D

  • What's the difference between DEBUG and DEBUG_NRF

     You seems to be right, the initial idea was to keep DEBUG_NRF for NRF  libraries and drivers related debug capabilities, like asserts etc. And DEBUG was supposed to be for common assert handling. But unfortunately, it looks  like this two are merging in logical use. 

  • Hi Susheel.

    Thanks for your reply.

    So coming back to my first request:

    • is there any good summary of what and how those switches enable when they are defined? I think it would be very useful to engineers during development.
    • I'd like to understand in the code where p_stack is set to NULL in the code...? Is it in the assembly function implementing the low level HardFault_Handler?

    Thanks!

    D

  • Hi Dave,

    davege said:

    • is there any good summary of what and how those switches enable when they are defined? I think it would be very useful to engineers during development.
    • I'd like to understand in the code

     Unfortunately, there are no document to describe them. 
    DEBUG_NRF is only valid for the macro ASSERT(x). If DEBUG_NRF is defined to 1, then all calls to ASSERT(x) would assert in app_error_fault_handler. This  is somewhat same to APP_ERROR_CHECK (but the latter is not depending on DEBUG_NRF). There are lots of examples where WEAK implementation of app_error_fault_handler has been overridden in the main.c to let the application handle errors as they suit them.

     

    davege said:
    I'd like to understand in the code where p_stack is set to NULL in the code...? Is it in the assembly function implementing the low level HardFault_Handler?

    Yes, you are right, there is a low-level implementation of the HardFault exception.  Please take a look in SDK_17.0.0_9d13099\components\libraries\hardfault\nrf52\handler\hardfault_handler_gcc.c to understand how p_stack value is pushed as argument. If you are using different compiler, then please look into the relevant implementation in the given path.

Related