加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

STM32访问外部存储器-NOR-Flash

发布时间:2020-12-15 18:00:23 所属栏目:百科 来源:网络整理
导读:作者: emouse 转自:http://www.voidcn.com/article/p-patqdlkh-qm.html 基本说明 STM32访问外部存储器是需要配置FSMC的相关函数,在STM32固件库函数说明的中文翻译版中并没有这部分的说明,因此需要参考库函数的相关说明和库中自带的例程。 以下内容来自AN

作者:emouse

转自:http://www.voidcn.com/article/p-patqdlkh-qm.html


基本说明

STM32访问外部存储器是需要配置FSMC的相关函数,在STM32固件库函数说明的中文翻译版中并没有这部分的说明,因此需要参考库函数的相关说明和库中自带的例程。

以下内容来自AN2784应用笔记:

2 与非总线复用模式的异步16位NOR闪存接口

2.1

FSMC配置

控制一个NOR闪存存储器,需要FSMC提供下述功能:

选择合适的存储块映射NOR闪存存储器:共有4个独立的存储块可以用于与NOR闪存、SRAM和PSRAM存储器接口,每个存储块都有一个专用的片选管脚。

使用或禁止地址/数据总线的复用功能。

选择所用的存储器类型:NOR闪存、SRAM或PSRAM。

定义外部存储器的数据总线宽度:8或16位。

使用或关闭同步NOR闪存存储器的突发访问模式。

配置等待信号的使用:开启或关闭,极性设置,时序配置。

使用或关闭扩展模式:扩展模式用于访问那些具有不同读写操作时序的存储器。

因为NOR闪存/SRAM控制器可以支持异步和同步存储器,用户只须根据存储器的参数配置使用到的参数。

FSMC提供了一些可编程的参数,可以正确地与外部存储器接口。依存储器类型的不同,有些参数是不需要的。

当使用一个外部异步存储器时,用户必须按照存储器的数据手册给出的时序数据,计算和设置下列参数:

ADDSET:地址建立时间

ADDHOLD:地址保持时间

DATAST:数据建立时间

ACCMOD:访问模式 这个参数允许 FSMC可以灵活地访问多种异步的静态存储器。共有4种扩展模式允许以不同的时序分别读写存储器。 在扩展模式下,FSMC_BTR用于配置读操作,FSMC_BWR用于配置写操作。(译注:如果读时序与写时序相同,只须使用FSMC_BTR即可。)

如果使用了同步的存储器,用户必须计算和设置下述参数:

CLKDIV:时钟分频系数

DATLAT:数据延时

如果存储器支持的话,NOR闪存的读操作可以是同步的,而写操作仍然是异步的。

当对一个同步的NOR闪存编程时,存储器会自动地在同步与异步之间切换;因此,必须正确地设置所有的参数

?

?

?

程序分析

[cpp] view plain copy print ?
  1. ?/*--?FSMC?Configuration?----------------------------------------------------*/??
  2. ??p.FSMC_AddressSetupTime?=?0x05;?????/*ADDSET??地址建立时间*/??
  3. ??p.FSMC_AddressHoldTime?=?0x00;????/*ADDHOLD?地址保持时间*/??
  4. ??p.FSMC_DataSetupTime?=?0x07;?????/*DATAST?数据建立时间*/??
  5. ??p.FSMC_BusTurnAroundDuration?=?0x00;???/*BUSTURN?总线返转时间*/??
  6. ??p.FSMC_CLKDivision?=?0x00;??????/*CLKDIV?时钟分频*/??
  7. ??p.FSMC_DataLatency?=?0x00;?????/*DATLAT?数据保持时间*/??
  8. ??p.FSMC_AccessMode?=?FSMC_AccessMode_B;???/*访问模式*/??
  9. /*NOR/SRAM的存储块,共4个选项*/??
  10. ??FSMC_NORSRAMInitStructure.FSMC_Bank?=?FSMC_Bank1_NORSRAM2;??????
  11. /*是否选择地址和数据复用数据线*/??
  12. ??FSMC_NORSRAMInitStructure.FSMC_DataAddressMux?=?FSMC_DataAddressMux_Disable;????
  13. /*连接到相应存储块的外部存储器类型*/??
  14. ??FSMC_NORSRAMInitStructure.FSMC_MemoryType?=?FSMC_MemoryType_NOR;?????
  15. /*存储器数据总线宽度*/??
  16. ??FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth?=?FSMC_MemoryDataWidth_16b;????
  17. /*使能或关闭同步NOR闪存存储器的突发访问模式设置是否使用迸发访问模式(应该就是连续读写模式吧)*/??
  18. ??FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode?=?FSMC_BurstAccessMode_Disable;?????
  19. /*设置WAIT信号的有效电平*/??
  20. ??FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity?=?FSMC_WaitSignalPolarity_Low;?????
  21. ?/*设置是否使用环回模式*/??
  22. ??FSMC_NORSRAMInitStructure.FSMC_WrapMode?=?FSMC_WrapMode_Disable;??????
  23. /*设置WAIT信号有效时机*/??
  24. ??FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive?=?FSMC_WaitSignalActive_BeforeWaitState;???
  25. /*设定是否使能写操作*/??
  26. ??FSMC_NORSRAMInitStructure.FSMC_WriteOperation?=?FSMC_WriteOperation_Enable;????
  27. /*设定是否使用WAIT信号*/??
  28. ??FSMC_NORSRAMInitStructure.FSMC_WaitSignal?=?FSMC_WaitSignal_Disable;?????
  29. /*使能或关闭扩展模式,扩展模式用于访问具有不同读写操作时序的存储器,设定是否使用单独的写时序*/???????
  30. ??FSMC_NORSRAMInitStructure.FSMC_ExtendedMode?=?FSMC_ExtendedMode_Disable;????
  31. /*设定是否使用异步等待信号*/??
  32. ??FSMC_NORSRAMInitStructure.FSMC_AsyncWait?=?FSMC_AsyncWait_Disable;????
  33. /*设定是否使用迸发写模式*/????
  34. ??FSMC_NORSRAMInitStructure.FSMC_WriteBurst?=?FSMC_WriteBurst_Disable;????
  35. ?/*设定读写时序*/??
  36. ??FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct?=?&p;????//???????
  37. ??FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct?=?&p;????//??
  38. ???????????????????
  39. ??FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);????????//??
  40. ??/*?Enable?FSMC?Bank1_NOR?Bank?*/??
  41. ??FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM2,?ENABLE);????????//??
  42. }??
?

实际例程

以下例程来自??stm3210e_eval_fsmc_nor.c具体信息参加固件库中源文件。

[c-sharp] view plaincopyprint?
  1. /**?
  2. ??******************************************************************************?
  3. ??*?@file????stm3210e_eval_fsmc_nor.c?
  4. ??*?@author??MCD?Application?Team?
  5. ??*?@version?V4.3.0?
  6. ??*?@date????10/15/2010?
  7. ??*?@brief???This?file?provides?a?set?of?functions?needed?to?drive?the?M29W128FL,??
  8. ??*??????????M29W128GL?and?S29GL128P?NOR?memories?mounted?on?STM3210E-EVAL?board.?
  9. ??******************************************************************************?
  10. ??*?@copy?
  11. ??*?
  12. ??*?THE?PRESENT?FIRMWARE?WHICH?IS?FOR?GUIDANCE?ONLY?AIMS?AT?PROVIDING?CUSTOMERS?
  13. ??*?WITH?CODING?INFORMATION?REGARDING?THEIR?PRODUCTS?IN?ORDER?FOR?THEM?TO?SAVE?
  14. ??*?TIME.?AS?A?RESULT,?STMICROELECTRONICS?SHALL?NOT?BE?HELD?LIABLE?FOR?ANY?
  15. ??*?DIRECT,?INDIRECT?OR?CONSEQUENTIAL?DAMAGES?WITH?RESPECT?TO?ANY?CLAIMS?ARISING?
  16. ??*?FROM?THE?CONTENT?OF?SUCH?FIRMWARE?AND/OR?THE?USE?MADE?BY?CUSTOMERS?OF?THE?
  17. ??*?CODING?INFORMATION?CONTAINED?HEREIN?IN?CONNECTION?WITH?THEIR?PRODUCTS.?
  18. ??*?
  19. ??*?<h2><center>??COPYRIGHT?2010?STMicroelectronics</center></h2>?
  20. ??*/???
  21. /*?Includes?------------------------------------------------------------------*/??
  22. #include?"stm3210e_eval_fsmc_nor.h"??
  23. /**?@addtogroup?Utilities?
  24. ??*?@{?
  25. ??*/??
  26. ????
  27. /**?@addtogroup?STM32_EVAL?
  28. ??*?@{?
  29. ??*/???
  30. /**?@addtogroup?STM3210E_EVAL?
  31. ??*?@{?
  32. ??*/??
  33. ????
  34. /**?@addtogroup?STM3210E_EVAL_FSMC_NOR?
  35. ??*?@brief??????This?file?provides?a?set?of?functions?needed?to?drive?the?M29W128FL,??
  36. ??*?????????????M29W128GL?and?S29GL128P?NOR?memories?mounted?on?STM3210E-EVAL?board.?
  37. ??*?@{?
  38. ??*/???
  39. /**?@defgroup?STM3210E_EVAL_FSMC_NOR_Private_Types?
  40. ??*?@{?
  41. ??*/???
  42. /**?
  43. ??*?@}?
  44. ??*/???
  45. /**?@defgroup?STM3210E_EVAL_FSMC_NOR_Private_Defines?
  46. ??*?@{?
  47. ??*/???
  48. /**??
  49. ??*?@brief??FSMC?Bank?1?NOR/SRAM2???
  50. ??*/??
  51. #define?Bank1_NOR2_ADDR???????((uint32_t)0x64000000)??
  52. /*?Delay?definition?*/?????
  53. #define?BlockErase_Timeout????((uint32_t)0x00A00000)??
  54. #define?ChipErase_Timeout?????((uint32_t)0x30000000)???
  55. #define?Program_Timeout???????((uint32_t)0x00001400)???????
  56. /**?
  57. ??*?@}?
  58. ??*/???
  59. ??
  60. /**?@defgroup?STM3210E_EVAL_FSMC_NOR_Private_Macros?
  61. ??*?@{?
  62. ??*/??
  63. #define?ADDR_SHIFT(A)?(Bank1_NOR2_ADDR?+?(2?*?(A)))??
  64. #define?NOR_WRITE(Address,?Data)??(*(__IO?uint16_t?*)(Address)?=?(Data))????
  65. /**?
  66. ??*?@}?
  67. ??*/???
  68. ????
  69. /**?@defgroup?STM3210E_EVAL_FSMC_NOR_Private_Variables?
  70. ??*?@{?
  71. ??*/???
  72. /**?
  73. ??*?@}?
  74. ??*/???
  75. ??
  76. /**?@defgroupSTM3210E_EVAL_FSMC_NOR_Private_Function_Prototypes?
  77. ??*?@{?
  78. ??*/???
  79. /**?
  80. ??*?@}?
  81. ??*/???
  82. ??
  83. /**?@defgroup?STM3210E_EVAL_FSMC_NOR_Private_Functions?
  84. ??*?@{?
  85. ??*/??
  86. /**?
  87. ??*?@brief??Configures?the?FSMC?and?GPIOs?to?interface?with?the?NOR?memory.?
  88. ??*?????????This?function?must?be?called?before?any?write/read?operation?
  89. ??*?????????on?the?NOR.?
  90. ??*?@param??None?
  91. ??*?@retval?None?
  92. ??*/??
  93. void?NOR_Init(void)??
  94. {??
  95. ??FSMC_NORSRAMInitTypeDef??FSMC_NORSRAMInitStructure;??
  96. ??FSMC_NORSRAMTimingInitTypeDef??p;??
  97. ??GPIO_InitTypeDef?GPIO_InitStructure;??
  98. ??RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD?|?RCC_APB2Periph_GPIOE?|???
  99. ?????????????????????????RCC_APB2Periph_GPIOF?|?RCC_APB2Periph_GPIOG,?ENABLE);??
  100. ??/*--?GPIO?Configuration?------------------------------------------------------*/??
  101. ??/*!<?NOR?Data?lines?configuration?*/??
  102. ??GPIO_InitStructure.GPIO_Pin?=?GPIO_Pin_0?|?GPIO_Pin_1?|?GPIO_Pin_8?|?GPIO_Pin_9?|??
  103. ????????????????????????????????GPIO_Pin_10?|?GPIO_Pin_14?|?GPIO_Pin_15;??
  104. ??GPIO_InitStructure.GPIO_Mode?=?GPIO_Mode_AF_PP;??
  105. ??GPIO_InitStructure.GPIO_Speed?=?GPIO_Speed_50MHz;??
  106. ??GPIO_Init(GPIOD,?&GPIO_InitStructure);??
  107. ??GPIO_InitStructure.GPIO_Pin?=?GPIO_Pin_7?|?GPIO_Pin_8?|?GPIO_Pin_9?|?GPIO_Pin_10?|??
  108. ????????????????????????????????GPIO_Pin_11?|?GPIO_Pin_12?|?GPIO_Pin_13?|??
  109. ????????????????????????????????GPIO_Pin_14?|?GPIO_Pin_15;??
  110. ??GPIO_Init(GPIOE,?&GPIO_InitStructure);??
  111. ??/*!<?NOR?Address?lines?configuration?*/??
  112. ??GPIO_InitStructure.GPIO_Pin?=?GPIO_Pin_0?|?GPIO_Pin_1?|?GPIO_Pin_2?|?GPIO_Pin_3?|??
  113. ????????????????????????????????GPIO_Pin_4?|?GPIO_Pin_5?|?GPIO_Pin_12?|?GPIO_Pin_13?|??
  114. ????????????????????????????????GPIO_Pin_14?|?GPIO_Pin_15;??
  115. ??GPIO_Init(GPIOF,?&GPIO_InitStructure);??
  116. ??GPIO_InitStructure.GPIO_Pin?=?GPIO_Pin_0?|?GPIO_Pin_1?|?GPIO_Pin_2?|??
  117. ????????????????????????????????GPIO_Pin_3?|?GPIO_Pin_4?|?GPIO_Pin_5;??
  118. ??GPIO_Init(GPIOG,?&GPIO_InitStructure);??
  119. ??GPIO_InitStructure.GPIO_Pin?=?GPIO_Pin_11?|?GPIO_Pin_12?|?GPIO_Pin_13;??
  120. ??GPIO_Init(GPIOD,?&GPIO_InitStructure);??
  121. ??GPIO_InitStructure.GPIO_Pin?=?GPIO_Pin_3?|?GPIO_Pin_4?|?GPIO_Pin_5?|?GPIO_Pin_6;??
  122. ??GPIO_Init(GPIOE,?&GPIO_InitStructure);??
  123. ??/*!<?NOE?and?NWE?configuration?*/??
  124. ??GPIO_InitStructure.GPIO_Pin?=?GPIO_Pin_4?|?GPIO_Pin_5;??
  125. ??GPIO_Init(GPIOD,?&GPIO_InitStructure);??
  126. ??/*!<?NE2?configuration?*/??
  127. ??GPIO_InitStructure.GPIO_Pin?=?GPIO_Pin_9;??
  128. ??GPIO_Init(GPIOG,?&GPIO_InitStructure);??
  129. ??/*!<?Configure?PD6?for?NOR?memory?Ready/Busy?signal?*/??
  130. ??GPIO_InitStructure.GPIO_Pin?=?GPIO_Pin_6;??
  131. ??GPIO_InitStructure.GPIO_Mode?=?GPIO_Mode_IN_FLOATING;??
  132. ??GPIO_Init(GPIOD,?&GPIO_InitStructure);??
  133. ????
  134. ??/*--?FSMC?Configuration?----------------------------------------------------*/??
  135. ??p.FSMC_AddressSetupTime?=?0x02;??
  136. ??p.FSMC_AddressHoldTime?=?0x00;??
  137. ??p.FSMC_DataSetupTime?=?0x05;??
  138. ??p.FSMC_BusTurnAroundDuration?=?0x00;??
  139. ??p.FSMC_CLKDivision?=?0x00;??
  140. ??p.FSMC_DataLatency?=?0x00;??
  141. ??p.FSMC_AccessMode?=?FSMC_AccessMode_B;??
  142. ??FSMC_NORSRAMInitStructure.FSMC_Bank?=?FSMC_Bank1_NORSRAM2;??
  143. ??FSMC_NORSRAMInitStructure.FSMC_DataAddressMux?=?FSMC_DataAddressMux_Disable;??
  144. ??FSMC_NORSRAMInitStructure.FSMC_MemoryType?=?FSMC_MemoryType_NOR;??
  145. ??FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth?=?FSMC_MemoryDataWidth_16b;??
  146. ??FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode?=?FSMC_BurstAccessMode_Disable;??
  147. ??FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait?=?FSMC_AsynchronousWait_Disable;????
  148. ??FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity?=?FSMC_WaitSignalPolarity_Low;??
  149. ??FSMC_NORSRAMInitStructure.FSMC_WrapMode?=?FSMC_WrapMode_Disable;??
  150. ??FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive?=?FSMC_WaitSignalActive_BeforeWaitState;??
  151. ??FSMC_NORSRAMInitStructure.FSMC_WriteOperation?=?FSMC_WriteOperation_Enable;??
  152. ??FSMC_NORSRAMInitStructure.FSMC_WaitSignal?=?FSMC_WaitSignal_Disable;??
  153. ??FSMC_NORSRAMInitStructure.FSMC_ExtendedMode?=?FSMC_ExtendedMode_Disable;??
  154. ??FSMC_NORSRAMInitStructure.FSMC_WriteBurst?=?FSMC_WriteBurst_Disable;??
  155. ??FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct?=?&p;??
  156. ??FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct?=?&p;??
  157. ??FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);??
  158. ??/*!<?Enable?FSMC?Bank1_NOR?Bank?*/??
  159. ??FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM2,?ENABLE);??
  160. }??
  161. /**?
  162. ??*?@brief??Reads?NOR?memory's?Manufacturer?and?Device?Code.?
  163. ??*?@param??NOR_ID:?pointer?to?a?NOR_IDTypeDef?structure?which?will?hold?the??
  164. ??*?????????Manufacturer?and?Device?Code.???
  165. ??*?@retval?None?
  166. ??*/??
  167. void?NOR_ReadID(NOR_IDTypeDef*?NOR_ID)??
  168. {??
  169. ??NOR_WRITE(ADDR_SHIFT(0x0555),?0x00AA);??
  170. ??NOR_WRITE(ADDR_SHIFT(0x02AA),?0x0055);??
  171. ??NOR_WRITE(ADDR_SHIFT(0x0555),?0x0090);??
  172. ??NOR_ID->Manufacturer_Code?=?*(__IO?uint16_t?*)?ADDR_SHIFT(0x0000);??
  173. ??NOR_ID->Device_Code1?=?*(__IO?uint16_t?*)?ADDR_SHIFT(0x0001);??
  174. ??NOR_ID->Device_Code2?=?*(__IO?uint16_t?*)?ADDR_SHIFT(0x000E);??
  175. ??NOR_ID->Device_Code3?=?*(__IO?uint16_t?*)?ADDR_SHIFT(0x000F);??
  176. }??
  177. /**?
  178. ??*?@brief??Erases?the?specified?Nor?memory?block.?
  179. ??*?@param??BlockAddr:?address?of?the?block?to?erase.?
  180. ??*?@retval?NOR_Status:?The?returned?value?can?be:?NOR_SUCCESS,?NOR_ERROR?
  181. ??*?????????or?NOR_TIMEOUT?
  182. ??*/??
  183. NOR_Status?NOR_EraseBlock(uint32_t?BlockAddr)??
  184. {??
  185. ??NOR_WRITE(ADDR_SHIFT(0x0555),?0x0080);??
  186. ??NOR_WRITE(ADDR_SHIFT(0x0555),?0x00AA);??
  187. ??NOR_WRITE(ADDR_SHIFT(0x02AA),?0x0055);??
  188. ??NOR_WRITE((Bank1_NOR2_ADDR?+?BlockAddr),?0x30);??
  189. ??return?(NOR_GetStatus(BlockErase_Timeout));??
  190. }??
  191. /**?
  192. ??*?@brief??Erases?the?entire?chip.?
  193. ??*?@param??None???????????????????????
  194. ??*?@retval?NOR_Status:?The?returned?value?can?be:?NOR_SUCCESS,?NOR_ERROR?
  195. ??*?????????or?NOR_TIMEOUT?
  196. ??*/??
  197. NOR_Status?NOR_EraseChip(void)??
  198. {??
  199. ??NOR_WRITE(ADDR_SHIFT(0x0555),?0x0055);??
  200. ??NOR_WRITE(ADDR_SHIFT(0x0555),?0x0010);??
  201. ??return?(NOR_GetStatus(ChipErase_Timeout));??
  202. }??
  203. /**?
  204. ??*?@brief??Writes?a?half-word?to?the?NOR?memory.?
  205. ??*?@param??WriteAddr:?NOR?memory?internal?address?to?write?to.?
  206. ??*?@param??Data:?Data?to?write.??
  207. ??*?@retval?NOR_Status:?The?returned?value?can?be:?NOR_SUCCESS,?NOR_ERROR?
  208. ??*?????????or?NOR_TIMEOUT?
  209. ??*/??
  210. NOR_Status?NOR_WriteHalfWord(uint32_t?WriteAddr,?uint16_t?Data)??
  211. {??
  212. ??NOR_WRITE(ADDR_SHIFT(0x0555),?0x00A0);??
  213. ??NOR_WRITE((Bank1_NOR2_ADDR?+?WriteAddr),?Data);??
  214. ??return?(NOR_GetStatus(Program_Timeout));??
  215. }??
  216. /**?
  217. ??*?@brief??Writes?a?half-word?buffer?to?the?FSMC?NOR?memory.??
  218. ??*?@param??pBuffer:?pointer?to?buffer.??
  219. ??*?@param??WriteAddr:?NOR?memory?internal?address?from?which?the?data?will?be??
  220. ??*?????????written.?
  221. ??*?@param??NumHalfwordToWrite:?number?of?Half?words?to?write.??
  222. ??*?@retval?NOR_Status:?The?returned?value?can?be:?NOR_SUCCESS,?NOR_ERROR?
  223. ??*?????????or?NOR_TIMEOUT?
  224. ??*/??
  225. NOR_Status?NOR_WriteBuffer(uint16_t*?pBuffer,?uint32_t?WriteAddr,?uint32_t?NumHalfwordToWrite)??
  226. {??
  227. ??NOR_Status?status?=?NOR_ONGOING;???
  228. ??do??
  229. ??{??
  230. ????/*!<?Transfer?data?to?the?memory?*/??
  231. ????status?=?NOR_WriteHalfWord(WriteAddr,?*pBuffer++);??
  232. ????WriteAddr?=?WriteAddr?+?2;??
  233. ????NumHalfwordToWrite--;??
  234. ??}??
  235. ??while((status?==?NOR_SUCCESS)?&&?(NumHalfwordToWrite?!=?0));??
  236. ????
  237. ??return?(status);???
  238. }??
  239. /**?
  240. ??*?@brief??Writes?a?half-word?buffer?to?the?FSMC?NOR?memory.?This?function??
  241. ??*?????????must?be?used?only?with?S29GL128P?NOR?memory.?
  242. ??*?@param??pBuffer:?pointer?to?buffer.??
  243. ??*?@param??WriteAddr:?NOR?memory?internal?address?from?which?the?data?will?be??
  244. ??*?????????written.?
  245. ??*?@param??NumHalfwordToWrite:?number?of?Half?words?to?write.?
  246. ??*?????????The?maximum?allowed?value?is?32?Half?words?(64?bytes).?
  247. ??*?@retval?NOR_Status:?The?returned?value?can?be:?NOR_SUCCESS,?NOR_ERROR?
  248. ??*?????????or?NOR_TIMEOUT?
  249. ??*/??
  250. NOR_Status?NOR_ProgramBuffer(uint16_t*?pBuffer,?uint32_t?NumHalfwordToWrite)??
  251. {??
  252. ??uint32_t?lastloadedaddress?=?0x00;??
  253. ??uint32_t?currentaddress?=?0x00;??
  254. ??uint32_t?endaddress?=?0x00;??
  255. ??/*!<?Initialize?variables?*/??
  256. ??currentaddress?=?WriteAddr;??
  257. ??endaddress?=?WriteAddr?+?NumHalfwordToWrite?-?1;??
  258. ??lastloadedaddress?=?WriteAddr;??
  259. ??/*!<?Issue?unlock?command?sequence?*/??
  260. ??NOR_WRITE(ADDR_SHIFT(0x00555),?0x0055);????
  261. ??/*!<?Write?Write?Buffer?Load?Command?*/??
  262. ??NOR_WRITE(ADDR_SHIFT(WriteAddr),?0x0025);??
  263. ??NOR_WRITE(ADDR_SHIFT(WriteAddr),?(NumHalfwordToWrite?-?1));??
  264. ??/*!<?Load?Data?into?NOR?Buffer?*/??
  265. ??while(currentaddress?<=?endaddress)??
  266. ??{??
  267. ????/*!<?Store?last?loaded?address?&?data?value?(for?polling)?*/??
  268. ????lastloadedaddress?=?currentaddress;??
  269. ???
  270. ????NOR_WRITE(ADDR_SHIFT(currentaddress),?*pBuffer++);??
  271. ????currentaddress?+=?1;???
  272. ??}??
  273. ??NOR_WRITE(ADDR_SHIFT(lastloadedaddress),?0x29);??
  274. ????
  275. ??return(NOR_GetStatus(Program_Timeout));??
  276. }??
  277. /**?
  278. ??*?@brief??Reads?a?half-word?from?the?NOR?memory.??
  279. ??*?@param??ReadAddr:?NOR?memory?internal?address?to?read?from.?
  280. ??*?@retval?Half-word?read?from?the?NOR?memory?
  281. ??*/??
  282. uint16_t?NOR_ReadHalfWord(uint32_t?ReadAddr)??
  283. {??
  284. ??NOR_WRITE(ADDR_SHIFT(0x00555),?0x00AA);???
  285. ??NOR_WRITE(ADDR_SHIFT(0x002AA),?0x0055);????
  286. ??NOR_WRITE((Bank1_NOR2_ADDR?+?ReadAddr),?0x00F0?);??
  287. ??return?(*(__IO?uint16_t?*)((Bank1_NOR2_ADDR?+?ReadAddr)));??
  288. }??
  289. /**?
  290. ??*?@brief??Reads?a?block?of?data?from?the?FSMC?NOR?memory.?
  291. ??*?@param??pBuffer:?pointer?to?the?buffer?that?receives?the?data?read?from?the??
  292. ??*?????????NOR?memory.?
  293. ??*?@param??ReadAddr:?NOR?memory?internal?address?to?read?from.?
  294. ??*?@param??NumHalfwordToRead?:?number?of?Half?word?to?read.?
  295. ??*?@retval?None?
  296. ??*/??
  297. void?NOR_ReadBuffer(uint16_t*?pBuffer,?uint32_t?ReadAddr,?uint32_t?NumHalfwordToRead)??
  298. {??
  299. ??NOR_WRITE(ADDR_SHIFT(0x0555),?0x0055);??
  300. ??NOR_WRITE((Bank1_NOR2_ADDR?+?ReadAddr),?0x00F0);??
  301. ??for(;?NumHalfwordToRead?!=?0x00;?NumHalfwordToRead--)?/*!<?while?there?is?data?to?read?*/??
  302. ??{??
  303. ????/*!<?Read?a?Halfword?from?the?NOR?*/??
  304. ????*pBuffer++?=?*(__IO?uint16_t?*)((Bank1_NOR2_ADDR?+?ReadAddr));??
  305. ????ReadAddr?=?ReadAddr?+?2;???
  306. ??}????
  307. }??
  308. /**?
  309. ??*?@brief??Returns?the?NOR?memory?to?Read?mode.?
  310. ??*?@param??None?
  311. ??*?@retval?NOR_SUCCESS?
  312. ??*/??
  313. NOR_Status?NOR_ReturnToReadMode(void)??
  314. {??
  315. ??NOR_WRITE(Bank1_NOR2_ADDR,?0x00F0);??
  316. ??return?(NOR_SUCCESS);??
  317. }??
  318. /**?
  319. ??*?@brief??Returns?the?NOR?memory?to?Read?mode?and?resets?the?errors?in?the?NOR??
  320. ??*?????????memory?Status?Register.???
  321. ??*?@param??None?
  322. ??*?@retval?NOR_SUCCESS?
  323. ??*/??
  324. NOR_Status?NOR_Reset(void)??
  325. {??
  326. ??NOR_WRITE(ADDR_SHIFT(0x00555),?0x00AA);???
  327. ??NOR_WRITE(ADDR_SHIFT(0x002AA),?0x0055);???
  328. ??NOR_WRITE(Bank1_NOR2_ADDR,?0x00F0);???
  329. ??return?(NOR_SUCCESS);??
  330. }??
  331. /**?
  332. ??*?@brief??Returns?the?NOR?operation?status.?
  333. ??*?@param??Timeout:?NOR?progamming?Timeout?
  334. ??*?@retval?NOR_Status:?The?returned?value?can?be:?NOR_SUCCESS,?NOR_ERROR?
  335. ??*?????????or?NOR_TIMEOUT?
  336. ??*/??
  337. NOR_Status?NOR_GetStatus(uint32_t?Timeout)??
  338. {???
  339. ??uint16_t?val1?=?0x00,?val2?=?0x00;??
  340. ??NOR_Status?status?=?NOR_ONGOING;???
  341. ??uint32_t?timeout?=?Timeout;??
  342. ??/*!<?Poll?on?NOR?memory?Ready/Busy?signal?----------------------------------*/??
  343. ??while((GPIO_ReadInputDataBit(GPIOD,?GPIO_Pin_6)?!=?RESET)?&&?(timeout?>?0))???
  344. ??{??
  345. ????timeout--;??
  346. ??}??
  347. ??timeout?=?Timeout;??
  348. ????
  349. ??while((GPIO_ReadInputDataBit(GPIOD,?GPIO_Pin_6)?==?RESET)?&&?(timeout?>?0))?????
  350. ??{??
  351. ????timeout--;??
  352. ??}??
  353. ????
  354. ??/*!<?Get?the?NOR?memory?operation?status?-----------------------------------*/??
  355. ??while((Timeout?!=?0x00)?&&?(status?!=?NOR_SUCCESS))??
  356. ??{??
  357. ????Timeout--;??
  358. ????/*!<?Read?DQ6?and?DQ5?*/??
  359. ????val1?=?*(__IO?uint16_t?*)(Bank1_NOR2_ADDR);??
  360. ????val2?=?*(__IO?uint16_t?*)(Bank1_NOR2_ADDR);??
  361. ????/*!<?If?DQ6?did?not?toggle?between?the?two?reads?then?return?NOR_Success?*/??
  362. ????if((val1?&?0x0040)?==?(val2?&?0x0040))???
  363. ????{??
  364. ??????return?NOR_SUCCESS;??
  365. ????}??
  366. ????if((val1?&?0x0020)?!=?0x0020)??
  367. ????{??
  368. ??????status?=?NOR_ONGOING;??
  369. ????}??
  370. ????val1?=?*(__IO?uint16_t?*)(Bank1_NOR2_ADDR);??
  371. ????val2?=?*(__IO?uint16_t?*)(Bank1_NOR2_ADDR);??
  372. ??????
  373. ????if((val1?&?0x0040)?==?(val2?&?0x0040))???
  374. ????{??
  375. ??????return?NOR_SUCCESS;??
  376. ????}??
  377. ????else?if((val1?&?0x0020)?==?0x0020)??
  378. ????{??
  379. ??????return?NOR_ERROR;??
  380. ????}??
  381. ??}??
  382. ??if(Timeout?==?0x00)??
  383. ??{??
  384. ????status?=?NOR_TIMEOUT;??
  385. ??}???
  386. ??/*!<?Return?the?operation?status?*/??
  387. ??return?(status);??
  388. }??
  389. /**?
  390. ??*?@}?
  391. ??*/??
  392. /**?
  393. ??*?@}?
  394. ??*/??
  395. /**?
  396. ??*?@}?
  397. ??*/??
  398. /**?
  399. ??*?@}?
  400. ??*/??
  401. /**?
  402. ??*?@}?
  403. ??*/????
  404. /*******************?(C)?COPYRIGHT?2010?STMicroelectronics?*****END?OF?FILE****/

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读