具体移植可以参考例程E:\nRF5_SDK\nRF5_SDK_15.2.0_9412b96\examples\peripheral\flash_fstorage
static void fstorage_evt_handler(nrf_fstorage_evt_t * p_evt)
{
if (p_evt->result != NRF_SUCCESS)
{
// NRF_LOG_INFO("--> Event received: ERROR while executing an fstorage operation.");
return;
}
switch (p_evt->id)
{
case NRF_FSTORAGE_EVT_WRITE_RESULT:
{
// NRF_LOG_INFO("--> 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("--> Event received: erased %d page from address 0x%x.",
// p_evt->len, p_evt->addr);
} break;
default:
break;
}
}
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 = FSTORAGE_START_ADDR,
.end_addr = FSTORAGE_END_ADDR,
};
/**@brief Helper function to obtain the last address on the last page of the on-chip flash that
* can be used to write user data.
*/
static uint32_t nrf5_flash_end_addr_get()
{
uint32_t const bootloader_addr = NRF_UICR->NRFFW[0];
uint32_t const page_sz = NRF_FICR->CODEPAGESIZE;
uint32_t const code_sz = NRF_FICR->CODESIZE;
return (bootloader_addr != 0xFFFFFFFF ?
bootloader_addr : (code_sz * page_sz));
}
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))
{
// power_manage();
}
}
static void print_flash_info(nrf_fstorage_t * p_fstorage)
{
// NRF_LOG_INFO("========| flash info |========");
// NRF_LOG_INFO("erase unit: \t%d bytes", p_fstorage->p_flash_info->erase_unit);
// NRF_LOG_INFO("program unit: \t%d bytes", p_fstorage->p_flash_info->program_unit);
// NRF_LOG_INFO("==============================");
}
void ble_flash_init(void)
{
ret_code_t err_code;
nrf_fstorage_api_t * p_fs_api;
p_fs_api = &nrf_fstorage_sd;
err_code = nrf_fstorage_init(&fstorage, p_fs_api, NULL);
APP_ERROR_CHECK(err_code);
print_flash_info(&fstorage);
/* It is possible to set the start and end addresses of an fstorage instance at runtime.
* They can be set multiple times, should it be needed. The helper function below can
* be used to determine the last address on the last page of flash memory available to
* store data. */
(void) nrf5_flash_end_addr_get();
// ble_flash_erase_page(FSTORAGE_START_ADDR);
}
void ble_flash_erase_page(uint32_t fs_addr)
{
ret_code_t err_code;
uint32_t start_addr = FSTORAGE_END_ADDR - fs_addr;
if(start_addr != 4096)
{
fs_addr = FSTORAGE_START_ADDR;
}
/* Let's write to flash. */
// NRF_LOG_INFO("Writing \"%x\" to flash.", p_data);
err_code = nrf_fstorage_erase(&fstorage, fs_addr, 1, NULL);//erase one page
APP_ERROR_CHECK(err_code);
wait_for_flash_ready(&fstorage);
}
void ble_flash_write(uint32_t fs_addr,uint32_t *p_data,uint32_t length)
{
ret_code_t err_code;
uint32_t start_addr = FSTORAGE_END_ADDR - fs_addr;
if(start_addr != 4096)
{
fs_addr = FSTORAGE_START_ADDR;
}
/* Let's write to flash. */
// NRF_LOG_INFO("Writing \"%x\" to flash.", p_data);
err_code = nrf_fstorage_erase(&fstorage, fs_addr, 1, NULL);//erase one page
APP_ERROR_CHECK(err_code);
wait_for_flash_ready(&fstorage);
err_code = nrf_fstorage_write(&fstorage, fs_addr, p_data, length, NULL);
APP_ERROR_CHECK(err_code);
wait_for_flash_ready(&fstorage);
// NRF_LOG_INFO("Done.");
}
void ble_flash_read(uint32_t fs_addr,uint32_t *p_data,uint32_t length)
{
ret_code_t err_code;
/* Let's write to flash. */
// NRF_LOG_INFO("Writing \"%x\" to flash.", p_data);
err_code = nrf_fstorage_read(&fstorage, fs_addr, p_data, length);
APP_ERROR_CHECK(err_code);
wait_for_flash_ready(&fstorage);
// NRF_LOG_INFO("Done.");
}
这里需要注意的几点:
1、写flash的时候,必须先操除,然后再写,不然会出错,因为flash的bit只能写0,不能写1.
2、操除与写的操作直接影响到flash的使用寿命,读则没影响,芯片可以操除多少次可以看芯片说明书
3、注意这里的写和读都是32bit的,如你写入0x123456,在芯片的存入是这样的 56 34 12 00,所以你可以将8bit的转成32bit的再存,高低位可以自己控制
4、读数据的时候,我们可以不用nrf_fstorage_read(&fstorage, fs_addr, p_data, length);函数,有的时候读的数据多了,它会直接报错,所以我们直接用memcpy()函数,直接读地址,将地址里的数copy出来,这样就不会报错了。
5、sdk_config.h 中需要修改:#define NRF_FSTORAGE_ENABLED 1