芯片: NRF52810 协议栈: s112 v6.0.0
1. 擦除flash操作:
/** @brief Function for erasing a page in flash.
*
* @param page_address Address of the first word in the page to be erased.
*/
void flash_page_erase(uint32_t * page_address)
{
// Turn on flash erase enable and wait until the NVMC is ready:
NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Een << NVMC_CONFIG_WEN_Pos);
while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
{
// Do nothing.
}
// Erase page:
NRF_NVMC->ERASEPAGE = (uint32_t)page_address;
while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
{
// Do nothing.
}
// Turn off flash erase enable and wait until the NVMC is ready:
NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos);
while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
{
// Do nothing.
}
}
2. 写flash操作:
/** @brief Function for filling a page in flash with a value.
*
* @param[in] address Address of the first word in the page to be filled.
* @param[in] value Value to be written to flash.
*/
void flash_word_write(uint32_t * address, uint32_t value)
{
// Turn on flash write enable and wait until the NVMC is ready:
NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos);
while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
{
// Do nothing.
}
*address = value;
while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
{
// Do nothing.
}
// Turn off flash write enable and wait until the NVMC is ready:
NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos);
while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
{
// Do nothing.
}
}
3.读取flash操作 直接读取相应地址内容。
注意事项:
flash擦除是以页为单位擦除的,地址寻找可以按照下述方法寻找:
uint32_t pg_size = NRF_FICR->CODEPAGESIZE;
uint32_t pg_num = NRF_FICR->CODESIZE - 1;// Use last page in flash
flash的擦除和写操作, 不要在nus中断内部操作,这样子会出现 NRF_FAULT_ID_SD_ASSERT 这个错误, 具体应该是指协议栈内部执行出了问题,但是因为协议栈是通过二进制文件提供的,所以不能找到为什么这么操作就会出问题。这个问题建议需要的时候置个标志位,然后在其他的循环体进行操作就可以了。
参考1: https://blog.csdn.net/rdm123456/article/details/79262639
参考2: http://eleaction01.spaces.eepw.com.cn/articles/article/item/146247