I tested on nrf5_sdk_17.1.0_ddde560,Tested on nrf5_sdk_17.1.0_ddde560\examples\ble_peripheral\ble_app_hrs_freertos project。Call the testfun function to test the test。
The location where I called the function execution is shown in the image
#include "nrf_fstorage.h" #include "nrf_sdh.h" #include "nrf_sdh_ble.h" #include "nrf_fstorage_sd.h" #include "nrf_soc.h" #include "app_error.h" #define EE_DEVICE_ADDR (0xE0000) #define EE_DEVICE_ADDR_BP (0xE1000) void wait_for_flash_ready(nrf_fstorage_t const * p_fstorage); static void fstorage_evt_handler(nrf_fstorage_evt_t * p_evt); static void print_flash_info(nrf_fstorage_t * p_fstorage); void WritetoFlash(uint32_t Address, uint8_t *pdata, uint16_t Len); 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 = 0xE0000, .end_addr = 0xE3000, }; void NordicFlashInit(void) { 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); print_flash_info(&fstorage); } void DeinitFlash(void) { nrf_fstorage_uninit(&fstorage, NULL); } void EraseflashStorage(void) { ret_code_t ret_code; ret_code = nrf_fstorage_erase(&fstorage, 0xE0000, 1, NULL); APP_ERROR_CHECK(ret_code); } void EraseflashStorageBk(void) { ret_code_t ret_code; ret_code = nrf_fstorage_erase(&fstorage, 0xE1000, 1, NULL); APP_ERROR_CHECK(ret_code); } uint8_t TestFun(void) { uint8_t buffer[] = {1,2,3,4}; NordicFlashInit(); EraseflashStorage(); EraseflashStorageBk(); WritetoFlash(EE_DEVICE_ADDR, (uint8_t*)&buffer, 4); WritetoFlash(EE_DEVICE_ADDR_BP, (uint8_t*)&buffer, 4); return 0; } static void print_flash_info(nrf_fstorage_t * p_fstorage) { #if ENABLE_PRINTF_DEUBG printf("\r========| flash info |========\n"); printf("\rerase unit: \t%d bytes\n", p_fstorage->p_flash_info->erase_unit); printf("\rprogram unit: \t%d bytes\n", p_fstorage->p_flash_info->program_unit); printf("\r==============================\n"); #endif } static void fstorage_evt_handler(nrf_fstorage_evt_t * p_evt) { if (p_evt->result != NRF_SUCCESS) { #if ENABLE_PRINTF_DEUBG printf("--> Event received: ERROR while executing an fstorage operation."); #endif return; } switch (p_evt->id) { case NRF_FSTORAGE_EVT_WRITE_RESULT: { #if ENABLE_PRINTF_DEUBG printf("--> Event received: wrote %d bytes at address 0x%x.", p_evt->len, p_evt->addr); #endif } break; case NRF_FSTORAGE_EVT_ERASE_RESULT: { #if ENABLE_PRINTF_DEUBG printf("--> Event received: erased %d page from address 0x%x.", p_evt->len, p_evt->addr); #endif } break; default: break; } } void wait_for_flash_ready(nrf_fstorage_t const * p_fstorage) { /* While fstorage is busy, sleep and wait for an event. */ while (nrf_fstorage_is_busy(p_fstorage)) { } } void WritetoFlash(uint32_t Address, uint8_t *pdata, uint16_t Len) { ret_code_t rc; rc = nrf_fstorage_write(&fstorage, Address, pdata, Len, NULL); APP_ERROR_CHECK(rc); wait_for_flash_ready(&fstorage); }