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

    1. I don't think it's a good idea to change app_error.h with custom features.

    2. When you don't initialize logging you should indeed not call logging functions like NRF_LOG_INFO() in your application.

    3. If logs aren't deferred, then this call has no use and will be empty, but I don't think it will be a problem calling it before a reset, since that is what it's used for (macro for flushing log data before a reset).

    Best regards,

    Simon

  • 1. I don't think it's a good idea to change app_error.h with custom features.

    But, all we do is calling the function app_error_handler() and not app_error_handler_bare() when in release mode, so that we have access to information such as line and file of an error that might occur. 
    So far, we haven't noticed any strange behaviour regarding this, do you think we should undo it?

    2. When you don't initialize logging you should indeed not call logging functions like NRF_LOG_INFO() in your application.

    We do initialize it and call NRF_LOG_INFO. We just don't have NRF_LOG_ENABLED enable (it is 0) in the production version. This I believe works fine, and the these calls are just empty when the NRF_LOG_ENABLED define is zero. Please confirm that this is true, otherwise we must comment any print we have in the code (print such as NRF_LOG_INFO) for debugging purposes.. 

    #define NRF_LOG_INTERNAL_MODULE(level, level_id, ...)                                    \
        if (NRF_LOG_ENABLED && (NRF_LOG_LEVEL >= level) &&                                   \
            (level <= NRF_LOG_DEFAULT_LEVEL))                                                \
        {                                                                                    \
            if (NRF_LOG_FILTER >= level)                                                     \
            {                                                                                \
                LOG_INTERNAL(LOG_SEVERITY_MOD_ID(level_id), __VA_ARGS__);                    \
            }                                                                                \
        }

    as i see in the file nrf_log_internal.h , if the NRF_LOG_ENABLED is zero, then the call of the function does nothing.

    So, to turn back to our problem, we still cannot find out why the register RESETREAS doesn't have the expected values when the external debugger (from DK) is not connected. 

    I will explain again, that, after a button reset on our custom board, we expect that this register will have the value 0x01. 
    When we check this with our board connected to the DK to use the external debugger, it does have this value. The button is connected to p0.21 (pin reset) and this "CONFIG_GPIO_AS_PINRESET" is included in the preprocessor's definitions. 

    When a soft-reset happens, for example from a function that returns an error code other than NRF_SUCCESS, the reset reason from the register is 0x4 as expected. When the button is pressed, however, 0x1 is the value with the debugger, and 0x0 is the value without the debugger. 
    Also, after some more tests, I see that the reason might be 0x1 after double press or more of the button. We cannot understand why this strange behaviour happens. 

    Finally, this is very important to us, because we want to use the reset reason in order to delete bonds. (when reset reason is 0x1 we will call delete_bonds function) . But we do not want this to happen any other time in case that the register does not work without the debugger. 

    Hope my description helps, and I really look forward for your assistance. 

    Best regards, 
    Dimitra

    Edit

    I tested many times trying to find what is going on and I want to make something clear, that maybe will help you understand what I am doing wrong or what is going on...

    1. When I connect my custom board with the devkit to use it as an external debugger, I also enable NRF_LOG_ENABLED and NRF_LOG_BACKEND_RTT_ENABLED and also I comment the call of the function dfu_async_svci_init() in main in order to see the debugging messages in Segger Embedded Studio. 
    In this case, the reset reasons I read from the register are correct, and the RED led I have on the board blinks when reason is 0x1, which is when I press the pin-reset button. 

    2. When I disable NRF_LOG_ENABLED and NRF_LOG_BACKEND_RTT_ENABLED and uncomment the dfu_async_svci_init(), and download the application through segger, it also works when the board is connected to the debugger (devkit) and when it is not ! (red led blilnks which indicates to me that the reason is 0x1). 

    3. When I keep the logs disabled and create the hex file, which includes softdevice and bootloader, and download this on the board through the nRF Connect App, then the reset reason doesnot work as expected and when I press the button when the board is not connected to the devkit debugger!  the RED led doesnot turn on to indicate to me that the reason is 0x01 and the another led blinks which means that the reason is 0x00. 

    So is it the bootloader's fault that the behaviour has been changed? 

    Or is the debugger? In all cases, it works as expected when the board is connected to the debugger of the devkit.... But it doesn't when I have bootloader+application and no debugger, which is the real case.. 

    EDIT 2

    After more tests, I find out that when my board is powered by a battery, it works as expected!! (reset register gives 1 for pin reset)
    When it is powered by the Power Profiler Kit II (in ampere mode, power output enabled), the reset register gives 0 for pin reset! 
    All the previous tests were executed with the PPK2, so probably this was the problem from the beginning. But why? How can this be the cause ? 

  • Hi Dimitra, 

    DimitraN said:
    So is it the bootloader's fault that the behaviour has been changed? 

    Yes, the bootloader clears the resetreason at nRF5_SDK_17.1.0_ddde560\components\libraries\bootloader\nrf_bootloader.c:crc_on_valid_app_required:nrf_power_resetreas_clear

    Put a breakpoint here and see if your execution hits here. If it does, comment this clear function so that your application can read the RESETREAS and then the application should clear this register.

Reply
  • Hi Dimitra, 

    DimitraN said:
    So is it the bootloader's fault that the behaviour has been changed? 

    Yes, the bootloader clears the resetreason at nRF5_SDK_17.1.0_ddde560\components\libraries\bootloader\nrf_bootloader.c:crc_on_valid_app_required:nrf_power_resetreas_clear

    Put a breakpoint here and see if your execution hits here. If it does, comment this clear function so that your application can read the RESETREAS and then the application should clear this register.

Children
  • As I said here:

    EDIT 2

    After more tests, I find out that when my board is powered by a battery, it works as expected!! (reset register gives 1 for pin reset)
    When it is powered by the Power Profiler Kit II (in ampere mode, power output enabled), the reset register gives 0 for pin reset!

    with bootloader , disabled NRF_LOG_ENABLED, and RTT defines, release mode, and connected to battery it works as expected. The register gives 0x01 when pin-reset is pressed. 

    When I connect it with another battery - and PPK2 in series to measure the consumption, it gives 0x00 when I press pin-reset. 

    I really cannot understand why. But since the normal use with battery works, I keep it. 

Related