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

how to save data in flash memory?

I am using SDK v16, SEGGER to program on my nrf52832 SOC  for our project. I need to save a few variables to the flash memory. These variables will hold the status of the device and they keep on getting updated once a day at least so that when the network regains its power, the components in it will read the data in flash memory to continue working

  • Hi,

    For that purpose, I would suggest to use the Flash Data Storage (FDS) module from the SDK. Your use case sounds like exactly the use case that FDS was made for.

    Regards,
    Terje

  • Are you using softdevice? If so the routines are async. Be aware the flash has about a 10,000 write limit.

    Assuming you're using softdevice you have to first block of a page of memory in your flash_placement.xml at the appropriate space. Check out the memory map documentation for your device to pick it.

    <ProgramSection load="No" keep="Yes" name=".appSettings" start="$(FLASH_APPSETTINGS_START)" size="$(FLASH_APPSETTINGS_END)-$(FLASH_APPSETTINGS_START)" />

    The values of FLASH_APPSETTINGS_END and FLASH_APPSETTINGS_START go into your preprocessor definition in Project settings.

    I think most use fstorage to do the flash operations as under the hood it takes care of the async method for softdevice (which is a queue). Follow the project in sdk examples to get started. Basically its:

    NRF_FSTORAGE_DEF(nrf_fstorage_t fstorage) = {

      .evt_handler = fstorage_evt_handler,
      .start_addr = FLASH_APPSETTINGS_START,
      .end_addr   = FLASH_APPSETTINGS_END,
    };

    inline void CONFIG_Init (void) {

      (void) nrf_fstorage_init(&fstorage, &nrf_fstorage_sd, NULL);
    }

    void writer (void) {

      (void) nrf_fstorage_erase(&fstorage, fstorage.start_addr, 1, NULL);

       (void) nrf_fstorage_write(&fstorage, fstorage.start_addr, &config, sizeof(config), NULL);

    }

    In your main loop decide what to do while the op is working, like stop accepting new commands, reset a ble char flag to let the app know it can continue etc.

    while(1) { if (nrf_fstorage_is_busy(&fstorage)) { ... }

Related