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

Flash storage

Dear sir/madame

Hopefully someone at this forum can bring a clarifying light to me.

I am struggling with storing a variable in flash (nRF 52840 and SDK 17.0.2).

 

If I define the variable to be stored as static then I can store and read, but if it not defined as static, I am able to store, but data read is different.

Here is the code with static variable

void storeTestADDR1(){

               static uint32_t myData = 0xAA;

                // uint32_t myData = 0xCC;;

               ret_code_t rc;  

               rc = nrf_fstorage_write(&fstorage, FLASH_ADDR1, &myData, sizeof(myData), NULL);

               APP_ERROR_CHECK(rc);

}

 

Gives this printout when reading content at ADDR1 location:  

[00:00:00.514,831] <info> app: Stored data at flash ADDR1 is: AA

 

 

Here is the code with non-static variable

void storeTestADDR1(){

               //static uint32_t myData = 0xAA;

                 uint32_t myData = 0xCC;

               ret_code_t rc;  

               rc = nrf_fstorage_write(&fstorage, FLASH_ADDR1, &myData, sizeof(myData), NULL);

               APP_ERROR_CHECK(rc);

}

 

Gives this printout when reading content at ADDR1 location:  

[00:00:00.616,088] <info> app: Stored data at flash ADDR1 is: 293ED

 

 

Here is the code for reading:

void read_data_from_flash_addr1(){

  uint32_t* p = (uint32_t *)FLASH_ADDR1;

  NRF_LOG_INFO("Stored data at flash ADDR1 is: %x", p[0]);

}

 

 

Kind regards

Svein

Parents
  • Hi Svein,

    I assume you are testing with the Softdevice enabled? The fstorage API becomes non-blocking in that case which basically means the flash operation (erase or write) won't  occur immediately but at some point after returning from the function call. The application is then later notified through the fstorage user callback when the scheduled flash operation is completed.

    And it's because of this asynchronous behavior you must ensure the source buffer is kept in 'static' memory until the flash write is actually executed. This is also noted in the fstorage API:

    The data to be written to flash must be kept in memory until the operation has terminated and an event is received. (nrf_fstorage_write)

    Kind regards,

    Vidar

Reply
  • Hi Svein,

    I assume you are testing with the Softdevice enabled? The fstorage API becomes non-blocking in that case which basically means the flash operation (erase or write) won't  occur immediately but at some point after returning from the function call. The application is then later notified through the fstorage user callback when the scheduled flash operation is completed.

    And it's because of this asynchronous behavior you must ensure the source buffer is kept in 'static' memory until the flash write is actually executed. This is also noted in the fstorage API:

    The data to be written to flash must be kept in memory until the operation has terminated and an event is received. (nrf_fstorage_write)

    Kind regards,

    Vidar

Children
No Data
Related