nrf51822教程系列 向nrf51822 flash中写入数据(flash write )
前言 寄存器介绍 0 .1?Non-Volatile Memory Controller (NVMC) Functional description 0.2?Factory Information Configuration Registers (FICR) Functional description 1 ?确定写入flash 的位置 ? ? ?通过FICR 寄存器,读取nrf51822的flash page size 和 the number of pages ? ? uint32_t * addr; ? uint32_t pg_size; uint32_t pg_num; pg_size = NRF_FICR->CODEPAGESIZE; pg_num = NRF_FICR->CODESIZE - 1; // Use last page in flash? ? ? ? // Start address: ? ? ? ? addr = (uint32_t *)(pg_size * pg_num); 2 ?擦除flash page(写入数据前,先把该page数据擦除) ? ? // Erase page: flash_page_erase(addr); /** @brief Function for erasing a page in flash. * * @param page_address Address of the first word in the page to be erased. */ static 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. } }3 ?数据写入flash ? ? flash_word_write(++addr,(uint32_t)patwr); /** @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. */ static 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. } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |