[nRF51822] 11、基础实验代码解析大全 · 实验16 - 内部FLASH读写

 

 一、实验内容:

  通过串口发送单个字符到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] 7、基础实验代码解析大全(前十)

[nRF51822] 8、基础实验代码解析大全 · 实验11 - PPI

[nRF51822] 9、基础实验代码解析大全 · 实验12 - ADC

[nRF51822] 10、基础实验代码解析大全 · 实验15 - RTC


 

 

@beautifulzzzz - 物联网&普适计算实践者
e-mail:beautifulzzzz@qq.com 
i-blog:blog.beautifulzzzz.com 

 

posted @ 2016-08-10 01:08  beautifulzzzz  阅读(4860)  评论(0编辑  收藏  举报