This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

NRF52832 erasing flash using fstorage confusion

I am new to NRF chips and trying to read, write, and erase to flash using nrf_fstorage. I have looked at the example project flash_fstorage and I am able to successfully read and write to flash but am having trouble with erasing specific pages as there is no example.

For example I set my flash address space to 0x3e000 - 0x3FFFF. 

NRF_FSTORAGE_DEF(nrf_fstorage_t fstorage) =
{
    /* Set a handler for fstorage events. */
    .evt_handler = fstorage_evt_handler,

    /* These below are the boundaries of the flash space assigned to this instance of fstorage.
     * You must set these manually, even at runtime, before nrf_fstorage_init() is called.
     * The function nrf5_flash_end_addr_get() can be used to retrieve the last address on the
     * last page of flash available to write data. */
    .start_addr = 0x3e000,
    .end_addr   = 0x3ffff,
};

In main, I setup and write "hello world" to 0x3e000 and "hello sunnn" to 0x3e200. 

static char     s1[] = "hello world";
static char     s2[] = "hello sunnn";

static char     read_s1[sizeof(s1)];
static char     read_s2[sizeof(s2)];
    
ret_code_t rc;
nrf_fstorage_api_t * p_fs_api;
p_fs_api = &nrf_fstorage_sd;
rc = nrf_fstorage_init(&fstorage, p_fs_api, NULL);
APP_ERROR_CHECK(rc);
    
rc = nrf_fstorage_write(&fstorage, 0x3e000, &s1, sizeof(s1), NULL);
APP_ERROR_CHECK(rc);
wait_for_flash_ready(&fstorage);
    
rc = nrf_fstorage_write(&fstorage, 0x3e200, &s2, sizeof(s2), NULL);
APP_ERROR_CHECK(rc);
wait_for_flash_ready(&fstorage);

Now I try to erase "hello world" using the code below however both strings in flash get cleared.

 rc = nrf_fstorage_erase(&fstorage, 0x3e000, 1, NULL);
APP_ERROR_CHECK(rc);
wait_for_flash_ready(&fstorage);

If I try the either line of code below to erase only "hello sunnn" I gfet the error: addr_is_page_aligned(p_fs, page_addr) check failed in nrf_fstorage_erase() with value 0x10.

// This line
rc = nrf_fstorage_erase(&fstorage, 0x3e000, 3, NULL);
// Or this line
rc = nrf_fstorage_erase(&fstorage, 0x3e200, 1, NULL);

What am I doing wrong?

Below is an image of how I understand it but it is clearly not correct.

  • A page is 0x1000. So `rf_fstorage_erase(&fstorage, 0x3e000, 1, NULL);` will erase 0x3e000 - 0x3eFFF.

  • Hi,

    There are two separate issues in your two different calls to nrf_fstorage_erase().

    First one:

    rc = nrf_fstorage_erase(&fstorage, 0x3e000, 3, NULL);

    Here the start address is the start of a page (the first one), so that is good. But you specify to erase 3 pages, while only 2 pages are reserved for fstorage (see the NRF_FSTORAGE_DEF).

    Second:

    rc = nrf_fstorage_erase(&fstorage, 0x3e200, 1, NULL);

    Here the start address is not page aligned, and that is not legal. You can only delete full pages (this is due to the nature of flash memory).

    The solution if you want to delete the first page is to do as suggested (here the first page is deleted):

    rc = nrf_fstorage_erase(&fstorage, 0x3e000, 1, NULL);

  • Ah, I didn't look close enough and thought a page was 0x0100 but that makes sense. Thanks for clearing that up.

Related