read write operation from flash memory nrf52840

I want to perform read and write operation from flash memory in nrf52840 through below api's

uint32_t flash_address = 0x0003D1E0;

 sd_flash_write((uint32_t *)flash_address, data_to_write, 1);         write operation

memcpy(&data_to_read, (uint32_t *)flash_address, 1);                  read operation

my issue is -

1. Is there any API to do for flash read operation.

2. when I try to write using the write operation command it fails.

3. which location should i used for storing some data in flash currently using 0x0003D1E0 this location.

4. There are multiple API's for flash operation which one should i prefer.

nrf_fstorage_write,

fds_record_write,

sd_flash_write

  • Hi,

    1. Is there any API to do for flash read operation.

    No, there is no SoftDevice API to read flash. Write and erase operations will halt the CPU when the operation is completed, so the APIs are provided to allow the Softdevice to schedule the operations between other tasks. Read operations can be done at any time without halting the CPU, and therefore does not need a special API.

    2. when I try to write using the write operation command it fails.

    Can you elaborate how it fails? Do the API return any errors, are the new value not written correctly, etc?

    3. which location should i used for storing some data in flash currently using 0x0003D1E0 this location.

    You can use any location that does not overlap with the application code or other static flash content. The address also needs to be erased before it can be written. The answer to this question depends on your application. You can see the size of the application in the build logs or similar. You can also read out the full flash content using nrfjprog or nRF Connect Programmer application to see which address ranges are occupied.

    4. There are multiple API's for flash operation which one should i prefer.

    nrf_fstorage_write,

    fds_record_write,

    sd_flash_write

    Again, this depends on your application needs. If a Softdevice is enabled, the SoftDevice API must be used to perform write/erase operations. Further, fstorage is one abstraction layer above this, and provides backends for both SoftDevice (which uses the SoftDevice APIs) and NVMC backend that access the NVMC peripheral directly. FDS is one abstraction layer above this again, and uses fstorage in the lower layers for flash writes. FDS provides additional functionality, like minimalistic file system, basic wear leveling, etc.

    If you are planning to update the value stored in flash at a regular interval, I would highly recommend using FDS. If using SD API directly, you either need to erase the full flash page between each write operation, or keep track of the current next free address where flash is erased.

    Best regards,
    Jørgen

Related