一、實驗內容:
通過串口發送單個字符到NRF51822,NRF51822 接收到字符后將其寫入到FLASH 的最后一頁,之后將其讀出並通過串口打印出數據。
二、nRF51822芯片內部flash知識:
EN-nRF51D 開發板使用NRF51822 芯片為nRF51822-QFAA,如下圖所示,共有256KBFLASH,256 頁,頁大小為1024 字節。
NRF51822 內部FLASH 寫流程如下:
三、代碼解析:
main:
1 int main(void) 2 { 3 ... 4 5 串口初始化 6 7 printf("Flashwrite example\r\n"); 8 patold = 0; 9 pg_size = NRF_FICR->CODEPAGESIZE; 10 pg_num = NRF_FICR->CODESIZE - 1; // Use last page in flash 11 12 while (true) 13 { 14 // Start address: 15 addr = (uint32_t *)(pg_size * pg_num);//計算地址並擦除一頁 16 // Erase page: 17 flash_page_erase(addr); 18 i = 0; 19 20 do 21 { 22 printf("Enter char to write to flash\r\n"); 23 24 // Read char from uart, and write it to flash: 25 do 26 { 27 err_code = app_uart_get(&patwr); 28 } 29 while(err_code == NRF_ERROR_NOT_FOUND);//從串口獲得1byte數據 30 31 if (patold != patwr)//將1byte寫入flash 32 { 33 patold = patwr; 34 flash_word_write(++addr, (uint32_t)patwr); 35 i += 4; 36 printf("'%c' was write to flash\r\n", patwr); 37 } 38 // Read pattern from flash and send it back: 39 patrd = (uint8_t) * addr; 40 printf("'%c' was read from flash\r\n", patrd); 41 } 42 while (i < pg_size); 43 } 44 }
@nRF51822基礎實驗系列:
[nRF51822] 8、基礎實驗代碼解析大全 · 實驗11 - PPI
[nRF51822] 9、基礎實驗代碼解析大全 · 實驗12 - ADC
[nRF51822] 10、基礎實驗代碼解析大全 · 實驗15 - RTC
@beautifulzzzz - 物聯網&普適計算實踐者
e-mail:beautifulzzzz@qq.com
i-blog:blog.beautifulzzzz.com