[菜农助学交流] 烈火-M0 4K DATAFLASH当做1K 32Bit EEPROM用,非常方便!

[复制链接]
 楼主| plc_avr 发表于 2011-11-26 13:45 | 显示全部楼层 |阅读模式
  M0有 4K DATAFLASH,可以当做1K 32Bit EEPROM用,非常方便!
  前几天说共享,说到要做到,呵呵,现将资料整理一下上传,可以不用再外扩EEPROM了,谁用谁知道。注意在用DATA FLASH前,请先将DATA FLASH使能,要不然没办法存数据进去。
设置见截图:

运行图片:

源码:
  1. #include <stdio.h>
  2. #include "NUC1xx.h"
  3. #include "Driver\DrvSYS.h"
  4. #include "Driver\DrvGPIO.h"
  5. #include "Driver\DrvUART.h"
  6. #include "Driver\DrvFMC.h"

  7. uint32_t temp1,temp2,temp3;

  8. #define DATA_Flash_Start_ADD 0x0001F000
  9. #define PAGE_SIZE 128 //512 byte

  10. void DATA_FLASH_Write(uint32_t add,uint32_t data);
  11. uint32_t DATA_FLASH_Read(uint32_t add);

  12. //============================================================================
  13. // DATA FLASH当做EEPROM用。4k 8BIT当做1K 32位用,这样读写更方便!---烈火狂龙
  14. // u32addr : 0-1024   写入地址
  15. // u32data : 0-0xFFFFFFFF 写入数据
  16. //============================================================================
  17. void DATA_FLASH_Write(uint32_t u32addr,uint32_t u32data)
  18. {
  19. uint32_t data_buff[128]={0};
  20. uint32_t i=0;

  21. __set_PRIMASK(1);//关闭全局中断,不关中断就等着出错吧,呵呵。

  22. UNLOCKREG();
  23. DrvFMC_EnableISP();

  24. for(i=0;i<AGE_SIZE;i++)
  25. DrvFMC_Read(DATA_Flash_Start_ADD+i*4+ u32addr/128*512, &data_buff);//读出一页

  26. DrvFMC_Erase(DATA_Flash_Start_ADD+u32addr/128*512);//擦除一页

  27. data_buff[u32addr%128]=u32data; //修改要写入的数据

  28. for(i=0; i<AGE_SIZE; i++) //写入数据
  29. DrvFMC_Write(DATA_Flash_Start_ADD+i*4+ u32addr/128*512, data_buff);


  30. DrvFMC_DisableISP();
  31. LOCKREG();
  32. __set_PRIMASK(0);//打开全局中断

  33. }

  34. //============================================================================
  35. // u32addr : 0-1024   读取地址
  36. //============================================================================
  37. uint32_t DATA_FLASH_Read(uint32_t u32add)
  38. {
  39. uint32_t u32data;
  40. __set_PRIMASK(1);//关闭全局中断,不关中断就等着出错吧,呵呵。
  41. UNLOCKREG();
  42. DrvFMC_EnableISP();
  43. DrvFMC_Read(u32add*4+DATA_Flash_Start_ADD, &u32data);
  44. DrvFMC_DisableISP();
  45. LOCKREG();
  46. __set_PRIMASK(0);//打开全局中断
  47. return u32data;
  48. }



  49. //============================================================================
  50. //                主程序
  51. //============================================================================
  52. int32_t main (void)
  53. {
  54. STR_UART_T sParam;

  55. /* Unlock the protected registers */
  56. UNLOCKREG();
  57. /* Enable the 12MHz oscillator oscillation */
  58. DrvSYS_SetOscCtrl(E_SYS_XTL12M, 1);

  59. /* Waiting for 12M Xtal stalble */
  60. DrvSYS_Delay(5000);

  61. /* HCLK clock source. 0: external 12MHz; 4:internal 22MHz RC oscillator */
  62. DrvSYS_SelectHCLKSource(0);
  63. /*lock the protected registers */
  64. LOCKREG();

  65. DrvSYS_SetClockDivider(E_SYS_HCLK_DIV, 0); /* HCLK clock frequency = HCLK clock source / (HCLK_N + 1) */

  66. /* Set UART Pin */
  67. DrvGPIO_InitFunction(E_FUNC_UART0);

  68. /* UART Setting */
  69. sParam.u32BaudRate = 115200;
  70. sParam.u8cDataBits = DRVUART_DATABITS_8;
  71. sParam.u8cStopBits = DRVUART_STOPBITS_1;
  72. sParam.u8cParity = DRVUART_PARITY_NONE;
  73. sParam.u8cRxTriggerLevel= DRVUART_FIFO_1BYTES;

  74. /* Set UART Configuration */
  75. if(DrvUART_Open(UART_PORT0,&sParam) != E_SUCCESS)
  76. {
  77. printf("UART0 open failed\n");
  78. return FALSE;
  79. }

  80. printf("================================================\n\r");
  81. printf(" 菜农新唐M0助学板范例 \n\r");
  82. printf(" 实验十: DATA FLASH读写例程  \n\r");
  83. printf(" 烈火狂龙 论坛ID: plc_avr \n\r");
  84. printf("================================================\n\r");

  85. if(DATA_FLASH_Read(0)==0xFFFFFFFF)
  86. {
  87. DATA_FLASH_Write(0,0);
  88. DATA_FLASH_Write(1,1);
  89. DATA_FLASH_Write(2,2);
  90. }
  91. else
  92. {
  93. temp1 = DATA_FLASH_Read(0);
  94. printf("当前DATA FLASH 地址0数据: %ld\n", temp1);
  95. temp2 = DATA_FLASH_Read(1);
  96. printf("当前DATA FLASH 地址1数据: %ld\n", temp2);
  97. temp3 = DATA_FLASH_Read(2);
  98. printf("当前DATA FLASH 地址2数据: %ld\n", temp3);

  99. DATA_FLASH_Write(0,temp1+1);
  100. DATA_FLASH_Write(1,temp2+1);
  101. DATA_FLASH_Write(2,temp3+1);
  102. }
  103. printf("演示结束,按板子上面复位,可以看到DATA FLASH数据累加并保存\n");

  104. while(1)
  105. {

  106. }

  107. }
  108. //============================================================================
  109. // END
  110. //============================================================================



工程包:

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
 楼主| plc_avr 发表于 2011-11-26 13:47 | 显示全部楼层
自已做沙发.......;P
 楼主| plc_avr 发表于 2011-11-26 13:56 | 显示全部楼层
如果要写连续的地址存储,请自行修改写入函数,要不然页擦写的次数多了,会影响使用寿命。
dong_abc 发表于 2011-11-26 16:34 | 显示全部楼层
还有这个功能,我也试试。
hotpower 发表于 2011-11-26 18:52 | 显示全部楼层
flash和eeprom还是有差异的,不过不频繁的写,flash还是不错的
sh007 发表于 2011-11-26 20:21 | 显示全部楼层
这个不错 得学习一下!
Cortex-M0 发表于 2011-11-27 04:40 | 显示全部楼层
烈火中勇生~~~
Swallow_0322 发表于 2011-11-27 07:33 | 显示全部楼层
顶烈火!
zhouyc168 发表于 2011-11-27 09:58 | 显示全部楼层
如果要写连续的地址存储,请自行修改写入函数,要不然页擦写的次数多了,会影响使用寿命。
Ryanhsiung 发表于 2011-11-28 12:59 | 显示全部楼层
好东东,顶一下
Ryanhsiung 发表于 2011-11-28 14:23 | 显示全部楼层
本帖最后由 Ryanhsiung 于 2011-11-28 15:09 编辑

仔细一下,写的不是很好
注:写的不正规,不易移植及应用
weshiluwei6 发表于 2011-11-28 15:11 | 显示全部楼层
支持火哥 牛人啊
 楼主| plc_avr 发表于 2011-11-28 16:23 | 显示全部楼层
仔细一下,写的不是很好
注:写的不正规,不易移植及应用
Ryanhsiung 发表于 2011-11-28 14:23

想要移值也不是难事,我的实际工程原先就是分开好的,后来做例程时头文件和C合并了。省得大家看的累。哈哈。
 楼主| plc_avr 发表于 2011-11-28 16:29 | 显示全部楼层
DATA_FLASH_RW.c

  1. //============================================================================
  2. // DATA FLASH当做EEPROM用。4k 8BIT当做1K 32位用,这样读写更方便!
  3. // u32addr : 0-1024   写入地址
  4. // u32data : 0-0xFFFFFFFF 写入数据
  5. //============================================================================
  6. void DATA_FLASH_Write(uint32_t u32addr,uint32_t u32data)
  7. {
  8.            uint32_t data_buff[128]={0};
  9.        uint32_t i=0;
  10.            
  11.            __set_PRIMASK(1);//关闭全局中断

  12.            UNLOCKREG();
  13.        DrvFMC_EnableISP();

  14.            for(i=0;i<AGE_SIZE;i++)
  15.               DrvFMC_Read(DATA_Flash_Start_ADD+i*4+ u32addr/128*512, &data_buff);//读出一



  16.        DrvFMC_Erase(DATA_Flash_Start_ADD+u32addr/128*512);//擦除一页

  17.            data_buff[u32addr%128]=u32data;  //修改要写入的数据

  18.            for(i=0; i<AGE_SIZE; i++)                //写入数据
  19.               DrvFMC_Write(DATA_Flash_Start_ADD+i*4+ u32addr/128*512, data_buff);

  20.       
  21.        DrvFMC_DisableISP();
  22.            LOCKREG();
  23.            __set_PRIMASK(0);//打开全局中断

  24. }

  25. //============================================================================
  26. // u32addr : 0-1024   读取地址
  27. //============================================================================
  28. uint32_t DATA_FLASH_Read(uint32_t u32add)
  29. {
  30.        uint32_t u32data;
  31.            __set_PRIMASK(1);//关闭全局中断
  32.        UNLOCKREG();
  33.        DrvFMC_EnableISP();
  34.        DrvFMC_Read(u32add*4+DATA_Flash_Start_ADD, &u32data);
  35.        DrvFMC_DisableISP();
  36.            LOCKREG();
  37.            __set_PRIMASK(0);//打开全局中断
  38.        return u32data;
  39. }
DATA_FLASH_RW.h

  1. #ifndef        __DATA_FLASH_RW_H
  2. #define        __DATA_FLASH_RW_H

  3. #define DATA_Flash_Start_ADD   0x0001F000
  4. #define PAGE_SIZE   128        //512 byte

  5. void DATA_FLASH_Write(uint32_t add,uint32_t data);
  6. uint32_t DATA_FLASH_Read(uint32_t add);

  7. #endif

文件打包:

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
Ryanhsiung 发表于 2011-11-28 18:01 | 显示全部楼层
本帖最后由 Ryanhsiung 于 2011-11-28 18:05 编辑

这是我写的你看一下,指教一下

https://bbs.21ic.com/icview-289376-1-1.html

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×

评分

参与人数 1威望 +4 收起 理由
dwh000 + 4 程序包邮bug!

查看全部评分

 楼主| plc_avr 发表于 2011-11-28 21:00 | 显示全部楼层
写的不错,呵呵,向你学习.
autopccopy 发表于 2011-12-4 10:08 | 显示全部楼层
flash和eeprom还是有差异的,不过不频繁的写,flash还是不错的
hotpower 发表于 2011-11-26 18:52


官方起码保证10W次吧,理论上20W次是没问题的。。。;P
haolaishi 发表于 2011-12-4 22:07 | 显示全部楼层
mcs8098 发表于 2011-12-20 12:42 | 显示全部楼层
COOL!
lixupengarm 发表于 2011-12-30 23:34 | 显示全部楼层
关注下!!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:烈火DIY Mini四轴飞行器群:234879071  http://fire-dragon.taobao.com/

42

主题

1633

帖子

23

粉丝
快速回复 在线客服 返回列表 返回顶部