Reset reason to safely delete bonds of nRF52832

Hello,

We have a product which is a custom nRF52832 board, nRF5 SDK (didn't have time to upgrade to nRF Connect SDK), s132, BLE. 

We used to call delete_bonds() function in security failure event. This fault on our behalf of course, caused many problems, since security fail can happen for many reasons, the device erased bonding information often. Then clients had to delete bonds from the mobile device to be able to reconnect to nRF52832, and this behaviour is not user friendly.

On another ticket, I got the reply that we shouldn't call delete bonds anywhere, and to allow repairing. 

But, since we couldn't reproduce the error of the clients, and we want to be safe, we want to use the Reset button, as a "factory reset", which will reset the device and delete the bonds. 

I used this code:

int32_t reset_reason = NRF_POWER->RESETREAS;
NRF_LOG_INFO("Reset reason = 0x%x.\n", reset_reason);
NRF_POWER->RESETREAS = NRF_POWER->RESETREAS;

to retrieve the reason of the reset (after reset), and then if reset = 0x1 (pin reset) I called delete_bonds function.

as I found here: 

RESETREAS and here https://devzone.nordicsemi.com/f/nordic-q-a/48446/how-to-detect-cause-of-reset

The problem is that I tested all of the above and it worked fine, using debugger (connected the board on a DK) and having enabled NRF_LOG_ENABLED and NRF_LOG_BACKEND_RTT_ENABLED. 

When i disabled them and tried to test the board not connected to the debugger, the reset reason I get for soft-reset (error from the code) is 0x4 and sometimes it is 0x0. Also from pin-reset I get 0x0, which is supposed to be 0x1

The register doesn't work without the debugger? What could be wrong in this situation? 

Thank you in advance, and I would really appreciate if you can help me on how to achieve to safely rcognise pin-rest, to perform delete bonds only in this case of device reset. 

Best regards, 

Dimitra

Parents
  • Hi Dimitra

    Disabling the debugger shouldn't change the reset reasons if the application is running as it should, so I'm guessing that when you disable the logging, something is failing in your application causing this soft-reset you're seeing. Maybe something in your application is trying to use logging, and that the application crashes when you disable it. Have you also commented out the calls to the logging after disabling the logs on your end.

    Best regards,

    Simon

  • Okay, I will describe what I do for logging:

    First, main function starts with these lines:

    //Initialization
      log_init();                             //Initialize logs.
      int32_t reset_reason = NRF_POWER->RESETREAS;
      NRF_LOG_INFO("Reset reason = 0x%x.\n", reset_reason);
      NRF_POWER->RESETREAS = NRF_POWER->RESETREAS;
    
      fds_module_init();                      //Initialize Flash Storage Module.
    
      gpio_init();                            //Initialize general purpose input/output.
    
      gpiote_init();                          //Initialize general purpose input/output task and events.
    
      dfu_async_svci_init();                  //Initialize async svci for device firmware update bootloader.
      
      .... // more initializations 
    }


    Then, in the main file, I use a custom function that works like APP_ERROR_CHECK, and what it does is that it takes the error code that is the return value from almost all of the functions from libraries softdevice etc, and save it in flash along with timestamp and some other information. 

    I have also redefined the weak  function app_error_fault_handler , which does the same, takes error code, line and file info saves them in flash and then performs soft-reset, which is done by calling this function:

    void hardfault_handler(void)
    {
      __disable_irq();
    
      NRF_LOG_FINAL_FLUSH();
      
      NVIC_SystemReset();
    }


    I have also changed something in app_error.h file. 
    /**@brief Macro for calling error handler function.
     *
     * @param[in] ERR_CODE Error code supplied to the error handler.
     */
    #ifndef DEBUG   //NOTE changed here ifdef to ifndef 
    #define APP_ERROR_HANDLER(ERR_CODE)                                    \
        do                                                                 \
        {                                                                  \
            app_error_handler((ERR_CODE), __LINE__, (uint8_t*) __FILE__);  \
        } while (0)
    #else
    #define APP_ERROR_HANDLER(ERR_CODE)                                    \
        do                                                                 \
        {                                                                  \
            app_error_handler_bare((ERR_CODE));                            \
        } while (0)
    #endif

    see the NOTE. I changed it so that when in Release mode, I can still get line and file information of the error to save them in flash. 

    Finally, I call NRF_LOG_INFO in many lines in my code, which I don't comment them out when I create the hex file and operate the device without debugger. 

    1) Could the functionality I made with app_error.h, and fault handlers cause the fault ? 
    2) Should I not use NRF_LOG_INFO (should I comment these lines) when I make release code to work without debugger, and not even initiliaze logging with log_init() function in main function?
    3) Should I don't call NRF_LOG_FINAL_FLUSH(); in handler that starts the reset ? 


    Thank you very much for your help in advance,
    Best regards,
    Dimitra

Reply
  • Okay, I will describe what I do for logging:

    First, main function starts with these lines:

    //Initialization
      log_init();                             //Initialize logs.
      int32_t reset_reason = NRF_POWER->RESETREAS;
      NRF_LOG_INFO("Reset reason = 0x%x.\n", reset_reason);
      NRF_POWER->RESETREAS = NRF_POWER->RESETREAS;
    
      fds_module_init();                      //Initialize Flash Storage Module.
    
      gpio_init();                            //Initialize general purpose input/output.
    
      gpiote_init();                          //Initialize general purpose input/output task and events.
    
      dfu_async_svci_init();                  //Initialize async svci for device firmware update bootloader.
      
      .... // more initializations 
    }


    Then, in the main file, I use a custom function that works like APP_ERROR_CHECK, and what it does is that it takes the error code that is the return value from almost all of the functions from libraries softdevice etc, and save it in flash along with timestamp and some other information. 

    I have also redefined the weak  function app_error_fault_handler , which does the same, takes error code, line and file info saves them in flash and then performs soft-reset, which is done by calling this function:

    void hardfault_handler(void)
    {
      __disable_irq();
    
      NRF_LOG_FINAL_FLUSH();
      
      NVIC_SystemReset();
    }


    I have also changed something in app_error.h file. 
    /**@brief Macro for calling error handler function.
     *
     * @param[in] ERR_CODE Error code supplied to the error handler.
     */
    #ifndef DEBUG   //NOTE changed here ifdef to ifndef 
    #define APP_ERROR_HANDLER(ERR_CODE)                                    \
        do                                                                 \
        {                                                                  \
            app_error_handler((ERR_CODE), __LINE__, (uint8_t*) __FILE__);  \
        } while (0)
    #else
    #define APP_ERROR_HANDLER(ERR_CODE)                                    \
        do                                                                 \
        {                                                                  \
            app_error_handler_bare((ERR_CODE));                            \
        } while (0)
    #endif

    see the NOTE. I changed it so that when in Release mode, I can still get line and file information of the error to save them in flash. 

    Finally, I call NRF_LOG_INFO in many lines in my code, which I don't comment them out when I create the hex file and operate the device without debugger. 

    1) Could the functionality I made with app_error.h, and fault handlers cause the fault ? 
    2) Should I not use NRF_LOG_INFO (should I comment these lines) when I make release code to work without debugger, and not even initiliaze logging with log_init() function in main function?
    3) Should I don't call NRF_LOG_FINAL_FLUSH(); in handler that starts the reset ? 


    Thank you very much for your help in advance,
    Best regards,
    Dimitra

Children
No Data
Related