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

Problem with Flash Reading using nrf_fstorage_read() when updated value

Hi,

I am using nRF52840 and SDK 17.0.2. I used to write data using nrf_fstorage_write() at some address of Flash and I want to read the same data using nrf_fstorage_read(). I am reading the data which I written to that location correctly for the first time. When I want to update the data at that same address I used to write but unable to read the data which is written by me. Sometimes getting the data as previous value and sometimes data is 0.

I used the function wait_for_flash_ready() after writing the data before reading.

I also used the function nrf_fstorage_erase() before data being written to that address for the updated data and wait_for_flash_ready() also before and after the fstorage write function. Can you suggest me how to read the updated data.

For your reference I am adding the code snippet 

1.

ret_code_t rc;

uint32_t m_data = 0x12345678,r_data;

rc=nrf_fstorage_write(&fstorage, 0XF6000, &m_data, sizeof(m_data), NULL);

APP_ERROR_CHECK(rc);

wait_for_flash_ready(&fstorage);

rc=nrf_fstorage_read(&fstorage, 0XF6000, &r_data, sizeof(r_data), NULL);

I am able to read 0x12345678 this time

2.

ret_code_t rc;

uint32_t m_data = 0x11112222,r_data;

rc=nrf_fstorage_write(&fstorage, 0XF6000, &m_data, sizeof(m_data), NULL);

APP_ERROR_CHECK(rc);

wait_for_flash_ready(&fstorage);

rc=nrf_fstorage_read(&fstorage, 0XF6000, &r_data, sizeof(r_data), NULL);

This time I am able to write the data to that address when I want to read the data not getting the exact value.

I aslo used the function nrf_fstorage_erase(& fstorage, 0xF6000, sizeof(m_data), NULL); before write function

3. I want to write and read the data which is getting continuously from the sensor. How can I do that. Please suggest me

Thank you in advance

  • When I want to update the data at that same address I used to write but unable to read the data which is written by me. Sometimes getting the data as previous value and sometimes data is 0.

     NVMC does not allow you to update the written bits without erasing the page first and fstorage does not have the feature to update byes (like FDS, which does an implicit page copy and erase page) without explicitly erasing the page. Please look at this thread for more info.

    How the flash write works is that first you start with a clean page where all the bits of that page are set to '1'. A write to a byte then means that the NVMC will change the necessary bits from '1' to '0 to reflect the data written to it. Note that NVMC cannot change the bits from '0' to '1' without erasing the whole page.

    So your first write is successful because you are writing  0x12345678 to a clean word where all bits were initially '1' and the new write sets the relavant bits to '0' to make the data at that location equal to 0x12345678.

    But when you write data 0x11112222 to the same location which initially has below binary 

    0001 0010 0011 0100 0101 0110 0111 1000             <- Initial value
    0001 0001 0001 0001 0010 0010 0010 0010            <- Request to write new value
    _______________________________________            <- '1'->'0' successful but '0' to '1' unsuccessful
    0001 0000 0001 0000 0000 0010 0010 0000            <- new contents after second write

    What you end up after the second write is 0x10100220. You should use FDS if you want the capability of updating the same address with new sensor data, but i should tell you that each update will involve a page erase internally using FDS and that is an time expensive process.

Related