Best approach to save data to flash while using Softdevice

Hello,

I am using  nrf52840, SDK 17.0.2, softdevice s140 and also implemented OTA DFU in my application.

I want to save some data lets say admin code in flash. Once I burn the firmware to device it should be like below

 uint8_t ADMIN_CODE[6]  = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};

During my application is running I want to change it to lets say ADMIN_CODE[6]  = {0x12, 0x56,0xFF,0xAA,0xFA,0xCD}. Then this becomes my admin code and when I power off and back on my ADMIN_CODE[6] should not go back to {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF} but instead should remain same ie: {0x12, 0x56,0xFF,0xAA,0xFA,0xCD} And when I do reset then it should go back to  {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}.

I saw some questions like this already on forum, some suggested to use flash write using NVMC or UICR and some suggests to use pstorage or FDS. I am confused what is the best approach in my scenario. Also if you can guide to some example code of right approach, that would be great!

Parents
  • Thank you Jimmywong and Hungbui,

    I am now able to save data to flash using fds and it is doing same as intended when I power off and then on. However, when I do reset, data(admin code) is not reset to default value.

    I guess I need to find the reset handler function and there I can make current value of admin code invalid and update it to its default value.

    The only problem is I cannot find reset handler. Can you please point me to some resource, where it actually explain that what happens when we press reset button on dev kit and if there is some handler function where my program goes once I press reset button.

    Thanks again

  • Hi AqibK, 

    What do you mean by "when I do reset, data(admin code) is not reset to default value" do you mean you want to "factory reset" when you press the reset button ? Meaning all data is reverted to 0xFF  ? 

    This is not be done automatically. And when you reset, the chip simply re-boot. There is no factory reset. You would need to implement that your self and define a way to tell the chip to do a factory reset (not by pressing reset button). 

  • No I don't want to do factory reset. I just want to reset one variable (which is stored in flash using fds) when I press reset button.

  • It's not automatically done when you do a reset. 
    If you want to do that you need to implement the code on your own, for example when you booting up you can check the RESETREAS register if it's booting up from a pin reset or not. Do something like this: 

    if (NRF_POWER->RESETREAS & POWER_RESETREAS_RESETPIN_Msk)

    {

    //It was booting up by pin reset

    }

Reply Children
Related