[牛人杂谈] 使用Data Flash模拟EEPROM

[复制链接]
3491|10
 楼主| wanduzi 发表于 2017-10-28 20:34 | 显示全部楼层 |阅读模式
使用Data Flash模拟EEPROM.pdf (1020.21 KB, 下载次数: 197)



 楼主| wanduzi 发表于 2017-10-28 20:34 | 显示全部楼层
这份文件介绍如何使用Data Flash模拟EEPROM,并且利用SRAM加速资料读写的速度。内容包含原理介绍、性能数据以及使用建议。附录提供范例程序源码以及函数库。
 楼主| wanduzi 发表于 2017-10-28 20:36 | 显示全部楼层
Init_EEPROM()
  1. /**
  2. * [url=home.php?mod=space&uid=247401]@brief[/url] Initial Data Flash as EEPROM.
  3. * @param[in] data_size: The amount of user's data, unit in byte. The maximun amount is
  4. 128.
  5. * @param[in] use_pages: The amount of page which user want to use.
  6. * @retval Err_OverPageSize: The amount of user's data is over than the maximun amount.
  7. * @retval Err_OverPageAmount: The amount of page which user want to use is over than
  8. the maximun amount.
  9. * @retval 0: Success
  10. */
  11. uint32_t Init_EEPROM(uint32_t data_amount, uint32_t use_pages)
  12. {
  13. uint32_t i;
  14. /* The amount of data includes 1 byte address and 1 byte data */
  15. Amount_of_Data = data_amount;
  16. /* The amount of page which user want to use */
  17. Amount_Pages = use_pages;
  18. /* Check setting is valid or not */
  19. /* The amount of user's data is more than the maximun amount or not */
  20. if(Amount_of_Data > Max_Amount_of_Data)
  21. return Err_OverAmountData;
  22. /* For M051 Series, the max. amount of Data Flash pages is 8 */
  23. if(Amount_Pages > 8)
  24. return Err_OverPageAmount;
  25. /* Init SRAM for data */
  26. Written_Data = (uint8_t *)malloc(sizeof(uint8_t) * Amount_of_Data);
  27. /* Fill initial data 0xFF*/
  28. for(i = 0; i < Amount_of_Data; i++)
  29. {
  30. Written_Data[i] = 0xFF;
  31. }
  32. return 0;
  33. }
  34. /**
  35. * @brief Search which page has valid data and where is current cursor for the next
  36. data to write.
  37. */
  38. void Search_Valid_Page(void)
  39. {
  40. uint32_t i, temp;
  41. uint8_t addr, data;
  42. uint16_t *Page_Status_ptr;
  43. /* Enable FMC ISP function */
  44. FMC_Enable();
  45. /* Set information of each pages to Page_Status */
  46. Page_Status_ptr = (uint16_t *)malloc(sizeof(uint16_t) * Amount_Pages);
  47. for(i = 0; i < Amount_Pages; i++)
  48. {
  49. Page_Status_ptr[i] = (uint16_t)FMC_Read(DataFlash_BaseAddr +
  50. (FMC_FLASH_PAGE_SIZE * i));
  51. }
  52. /* Search which page has valid data */
  53. for(i = 0; i < Amount_Pages; i++)
  54. {
  55. if(Page_Status_ptr[i] != Status_Unwritten)
  56. Current_Valid_Page = i;
  57. }
  58. /* If Data Flash is used for first time, set counter = 0 */
  59. if(Page_Status_ptr[Current_Valid_Page] == Status_Unwritten)
  60. {
  61. /* Set counter = 0 */
  62. FMC_Write(DataFlash_BaseAddr + (FMC_FLASH_PAGE_SIZE *
  63. Current_Valid_Page), 0xFFFF0000);
  64. /* Set cursor to current Data Flash address */
  65. Current_Cursor = 2;
  66. }
  67. else
  68. {
  69. /* Search where is current cursor for the next data to write and get the
  70. data has been written */
  71. /* Check even value */
  72. temp = FMC_Read(DataFlash_BaseAddr + (FMC_FLASH_PAGE_SIZE *
  73. Current_Valid_Page));
  74. addr = (temp & Even_Addr_Mask) >> Even_Addr_Pos;
  75. data = (temp & Even_Data_Mask) >> Even_Data_Pos;
  76. /* Check Address is 0xFF (un-written) of not */
  77. if(addr == 0xFF)
  78. {
  79. /* If Address is 0xFF, then set cursor to current Data Flash
  80. address */
  81. Current_Cursor = 2;
  82. }
  83. else
  84. {
  85. /* Copy the address and data to SRAM */
  86. Written_Data[addr] = data;
  87. /* Check the whole Data Flash */
  88. for(i = 4; i < FMC_FLASH_PAGE_SIZE; i += 4)
  89. {
  90. /* Check odd value */
  91. temp = FMC_Read(DataFlash_BaseAddr +
  92. (FMC_FLASH_PAGE_SIZE * Current_Valid_Page) + i);
  93. addr = (temp & Odd_Addr_Mask) >> Odd_Addr_Pos;
  94. data = (temp & Odd_Data_Mask) >> Odd_Data_Pos;
  95. /* Check Address is 0xFF (un-written) of not */
  96. if(addr == 0xFF)
  97. {
  98. /* If Address is 0xFF, then set cursor to
  99. current Data Flash address */
  100. Current_Cursor = i;
  101. break;
  102. }
  103. else
  104. {
  105. /* Copy the address and data to SRAM */
  106. Written_Data[addr] = data;
  107. }
  108. /* Check even value */
  109. addr = (temp & Even_Addr_Mask) >> Even_Addr_Pos;
  110. data = (temp & Even_Data_Mask) >> Even_Data_Pos;
  111. /* Check Address is 0xFF (un-written) of not */
  112. if(addr == 0xFF)
  113. {
  114. /* If Address is 0xFF, then set cursor to
  115. current Data Flash address */
  116. Current_Cursor = i + 2;
  117. break;
  118. }
  119. else
  120. {
  121. /* Copy the address and data to SRAM */
  122. Written_Data[addr] = data;
  123. }
  124. }
  125. }
  126. }
  127. }
 楼主| wanduzi 发表于 2017-10-28 20:37 | 显示全部楼层
QQ截图20171028203707.png
下载学习啊。
21mengnan 发表于 2017-10-28 21:17 | 显示全部楼层
一共用了4个函数实现,那么这四个函数在哪个地方可以找到?有头文件吗
dongnanxibei 发表于 2017-10-28 21:38 | 显示全部楼层
没看懂第四个函数,是统计被写过多少次了吗,这样是软件计数的还是硬件。
heelary 发表于 2017-10-31 09:21 | 显示全部楼层
谢谢楼主分享!
 楼主| wanduzi 发表于 2017-11-12 17:50 | 显示全部楼层
这个是新唐自己提供的学习资料。
role_2099 发表于 2018-3-25 14:48 | 显示全部楼层
这个资料很好。不知道还有没有其他外设的实际应用案例分享
zhuotuzi 发表于 2018-3-25 16:11 | 显示全部楼层
FMC技术嘛,这个用的挺多的。
jiekou001 发表于 2018-3-25 16:34 | 显示全部楼层
内部的接口技术。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

156

主题

1922

帖子

3

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