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

Problems with using Flash and fstorage

I am playing with flash and fstorage lib. I finally got it up and running - I can write and read to flash without having fatal errors. But it's not working as expected. I write "hello world" to 0x3E000 and right after I read some crappy string from that address.

So I tried the flash_fstorage example, compiled and run and I can see the same behaviour. See the output from the logs:

What is going wrong? Do I have my flash somehow corrupted?

Thank you.

EDIT 1:

I tested with another board. I erased the memory (make erase) and then flashed and run the flash_fstorage. The result is the same, only the string that I read back is different: "hL(o world".

EDIT 2:

nrf_fstorage_write() fires a NRF_FSTORAGE_EVT_WRITE_RESULT event which I can handle with evt_handler() from NRF_STORAGE_DEF. But nrf_fstorage_read() DOES NOT fire any event of this type - the evt_handler is not called. Why?

This is my evt_handler implementation:

  switch (p_evt->id) {
    case NRF_FSTORAGE_EVT_WRITE_RESULT:
      NRF_LOG_INFO("--> storage Event received: wrote %d bytes at address 0x%x.", p_evt->len, p_evt->addr);
    break;
    case NRF_FSTORAGE_EVT_ERASE_RESULT:
      NRF_LOG_INFO("--> storage Event received: erased %d page from address 0x%x.", p_evt->len, p_evt->addr);
    break;
    case NRF_FSTORAGE_EVT_READ_RESULT:
      NRF_LOG_INFO("--> storage Event received: erased %d page from address 0x%x.", p_evt->len, p_evt->addr);
    break;
    default:
      NRF_LOG_INFO("--> storage Event received: unknown action %d, %d page @ address 0x%x.", p_evt->id, p_evt->len, p_evt->addr);
    break;
  }

  • Dusan,

    You will need to keep an image of your Flash page in RAM (like an array, or structure).  Then, when you write to a section of that Flash page you will need to:

    1. First, update the image in RAM with the new data.

    2. Erase your whole Flash page.

    3. Write the updated RAM image to Flash.

    Like Kristin pointed out, this is how Flash works.  You may also create your own middleware "write_to_Flash()" function that does all of these 3 steps in one call.

    Cheers,

    Gil

Related