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.