bsp_fsmc_sram.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * File Name : FSMC.c
  5. * Description : This file provides code for the configuration
  6. * of the FSMC peripheral.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * Copyright (c) 2024 STMicroelectronics.
  11. * All rights reserved.
  12. *
  13. * This software is licensed under terms that can be found in the LICENSE file
  14. * in the root directory of this software component.
  15. * If no LICENSE file comes with this software, it is provided AS-IS.
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "bsp_fsmc_sram.h"
  22. /* USER CODE BEGIN 0 */
  23. /* USER CODE END 0 */
  24. SRAM_HandleTypeDef hsram1;
  25. /* FSMC initialization function */
  26. void MX_FSMC_SRAM_Init(void)
  27. {
  28. /* USER CODE BEGIN FSMC_Init 0 */
  29. /* USER CODE END FSMC_Init 0 */
  30. FSMC_NORSRAM_TimingTypeDef Timing = {0};
  31. /* USER CODE BEGIN FSMC_Init 1 */
  32. /* USER CODE END FSMC_Init 1 */
  33. /** Perform the SRAM1 memory initialization sequence
  34. */
  35. hsram1.Instance = FSMC_NORSRAM_DEVICE;
  36. hsram1.Extended = FSMC_NORSRAM_EXTENDED_DEVICE;
  37. /* hsram1.Init */
  38. hsram1.Init.NSBank = FSMC_NORSRAM_BANK3;
  39. hsram1.Init.DataAddressMux = FSMC_DATA_ADDRESS_MUX_DISABLE;
  40. hsram1.Init.MemoryType = FSMC_MEMORY_TYPE_SRAM;
  41. hsram1.Init.MemoryDataWidth = FSMC_NORSRAM_MEM_BUS_WIDTH_16;
  42. hsram1.Init.BurstAccessMode = FSMC_BURST_ACCESS_MODE_DISABLE;
  43. hsram1.Init.AsynchronousWait = FSMC_ASYNCHRONOUS_WAIT_DISABLE;
  44. hsram1.Init.WaitSignalPolarity = FSMC_WAIT_SIGNAL_POLARITY_LOW;
  45. hsram1.Init.WrapMode = FSMC_WRAP_MODE_DISABLE;
  46. hsram1.Init.WaitSignalActive = FSMC_WAIT_TIMING_BEFORE_WS;
  47. hsram1.Init.WriteOperation = FSMC_WRITE_OPERATION_ENABLE;
  48. hsram1.Init.WaitSignal = FSMC_WAIT_SIGNAL_DISABLE;
  49. hsram1.Init.ExtendedMode = FSMC_EXTENDED_MODE_DISABLE;
  50. hsram1.Init.WriteBurst = FSMC_WRITE_BURST_DISABLE;
  51. /* Timing */
  52. Timing.AddressSetupTime = 2;
  53. Timing.AddressHoldTime = 0;
  54. Timing.DataSetupTime = 0;
  55. Timing.BusTurnAroundDuration = 0;
  56. Timing.CLKDivision = 15;
  57. Timing.DataLatency = 15;
  58. Timing.AccessMode = FSMC_ACCESS_MODE_A;
  59. /* ExtTiming */
  60. if (HAL_SRAM_Init(&hsram1, &Timing, NULL) != HAL_OK)
  61. {
  62. return ;
  63. }
  64. /* USER CODE BEGIN FSMC_Init 2 */
  65. /* USER CODE END FSMC_Init 2 */
  66. }
  67. static uint32_t FSMC_Initialized = 0;
  68. static void HAL_FSMC_MspInit(void){
  69. /* USER CODE BEGIN FSMC_MspInit 0 */
  70. /* USER CODE END FSMC_MspInit 0 */
  71. GPIO_InitTypeDef GPIO_InitStruct = {0};
  72. if (FSMC_Initialized) {
  73. return;
  74. }
  75. FSMC_Initialized = 1;
  76. /* Peripheral clock enable */
  77. __HAL_RCC_FSMC_CLK_ENABLE();
  78. __HAL_RCC_GPIOD_CLK_ENABLE();
  79. __HAL_RCC_GPIOE_CLK_ENABLE();
  80. __HAL_RCC_GPIOF_CLK_ENABLE();
  81. __HAL_RCC_GPIOG_CLK_ENABLE();
  82. /** FSMC GPIO Configuration
  83. PF0 ------> FSMC_A0
  84. PF1 ------> FSMC_A1
  85. PF2 ------> FSMC_A2
  86. PF3 ------> FSMC_A3
  87. PF4 ------> FSMC_A4
  88. PF5 ------> FSMC_A5
  89. PF12 ------> FSMC_A6
  90. PF13 ------> FSMC_A7
  91. PF14 ------> FSMC_A8
  92. PF15 ------> FSMC_A9
  93. PG0 ------> FSMC_A10
  94. PG1 ------> FSMC_A11
  95. PE7 ------> FSMC_D4
  96. PE8 ------> FSMC_D5
  97. PE9 ------> FSMC_D6
  98. PE10 ------> FSMC_D7
  99. PE11 ------> FSMC_D8
  100. PE12 ------> FSMC_D9
  101. PE13 ------> FSMC_D10
  102. PE14 ------> FSMC_D11
  103. PE15 ------> FSMC_D12
  104. PD8 ------> FSMC_D13
  105. PD9 ------> FSMC_D14
  106. PD10 ------> FSMC_D15
  107. PD11 ------> FSMC_A16
  108. PD12 ------> FSMC_A17
  109. PD13 ------> FSMC_A18
  110. PD14 ------> FSMC_D0
  111. PD15 ------> FSMC_D1
  112. PG2 ------> FSMC_A12
  113. PG3 ------> FSMC_A13
  114. PG4 ------> FSMC_A14
  115. PG5 ------> FSMC_A15
  116. PD0 ------> FSMC_D2
  117. PD1 ------> FSMC_D3
  118. PD4 ------> FSMC_NOE
  119. PD5 ------> FSMC_NWE
  120. PG10 ------> FSMC_NE3
  121. PE0 ------> FSMC_NBL0
  122. PE1 ------> FSMC_NBL1
  123. */
  124. /* GPIO_InitStruct */
  125. GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3
  126. |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_12|GPIO_PIN_13
  127. |GPIO_PIN_14|GPIO_PIN_15;
  128. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  129. GPIO_InitStruct.Pull = GPIO_NOPULL;
  130. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  131. GPIO_InitStruct.Alternate = GPIO_AF12_FSMC;
  132. HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
  133. /* GPIO_InitStruct */
  134. GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3
  135. |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_10;
  136. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  137. GPIO_InitStruct.Pull = GPIO_NOPULL;
  138. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  139. GPIO_InitStruct.Alternate = GPIO_AF12_FSMC;
  140. HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
  141. /* GPIO_InitStruct */
  142. GPIO_InitStruct.Pin = GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10
  143. |GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14
  144. |GPIO_PIN_15|GPIO_PIN_0|GPIO_PIN_1;
  145. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  146. GPIO_InitStruct.Pull = GPIO_NOPULL;
  147. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  148. GPIO_InitStruct.Alternate = GPIO_AF12_FSMC;
  149. HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
  150. /* GPIO_InitStruct */
  151. GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11
  152. |GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15
  153. |GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_5;
  154. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  155. GPIO_InitStruct.Pull = GPIO_NOPULL;
  156. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  157. GPIO_InitStruct.Alternate = GPIO_AF12_FSMC;
  158. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  159. /* USER CODE BEGIN FSMC_MspInit 1 */
  160. /* USER CODE END FSMC_MspInit 1 */
  161. }
  162. void HAL_SRAM_MspInit(SRAM_HandleTypeDef* sramHandle){
  163. /* USER CODE BEGIN SRAM_MspInit 0 */
  164. /* USER CODE END SRAM_MspInit 0 */
  165. HAL_FSMC_MspInit();
  166. /* USER CODE BEGIN SRAM_MspInit 1 */
  167. /* USER CODE END SRAM_MspInit 1 */
  168. }
  169. static uint32_t FSMC_DeInitialized = 0;
  170. static void HAL_FSMC_MspDeInit(void){
  171. /* USER CODE BEGIN FSMC_MspDeInit 0 */
  172. /* USER CODE END FSMC_MspDeInit 0 */
  173. if (FSMC_DeInitialized) {
  174. return;
  175. }
  176. FSMC_DeInitialized = 1;
  177. /* Peripheral clock enable */
  178. __HAL_RCC_FSMC_CLK_DISABLE();
  179. /** FSMC GPIO Configuration
  180. PF0 ------> FSMC_A0
  181. PF1 ------> FSMC_A1
  182. PF2 ------> FSMC_A2
  183. PF3 ------> FSMC_A3
  184. PF4 ------> FSMC_A4
  185. PF5 ------> FSMC_A5
  186. PF12 ------> FSMC_A6
  187. PF13 ------> FSMC_A7
  188. PF14 ------> FSMC_A8
  189. PF15 ------> FSMC_A9
  190. PG0 ------> FSMC_A10
  191. PG1 ------> FSMC_A11
  192. PE7 ------> FSMC_D4
  193. PE8 ------> FSMC_D5
  194. PE9 ------> FSMC_D6
  195. PE10 ------> FSMC_D7
  196. PE11 ------> FSMC_D8
  197. PE12 ------> FSMC_D9
  198. PE13 ------> FSMC_D10
  199. PE14 ------> FSMC_D11
  200. PE15 ------> FSMC_D12
  201. PD8 ------> FSMC_D13
  202. PD9 ------> FSMC_D14
  203. PD10 ------> FSMC_D15
  204. PD11 ------> FSMC_A16
  205. PD12 ------> FSMC_A17
  206. PD13 ------> FSMC_A18
  207. PD14 ------> FSMC_D0
  208. PD15 ------> FSMC_D1
  209. PG2 ------> FSMC_A12
  210. PG3 ------> FSMC_A13
  211. PG4 ------> FSMC_A14
  212. PG5 ------> FSMC_A15
  213. PD0 ------> FSMC_D2
  214. PD1 ------> FSMC_D3
  215. PD4 ------> FSMC_NOE
  216. PD5 ------> FSMC_NWE
  217. PG10 ------> FSMC_NE3
  218. PE0 ------>FSMC_NBL0
  219. PE1 ------>FSMC_NBL1
  220. */
  221. HAL_GPIO_DeInit(GPIOF, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3
  222. |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_12|GPIO_PIN_13
  223. |GPIO_PIN_14|GPIO_PIN_15);
  224. HAL_GPIO_DeInit(GPIOG, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3
  225. |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_10);
  226. HAL_GPIO_DeInit(GPIOE, GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10
  227. |GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14
  228. |GPIO_PIN_15);
  229. HAL_GPIO_DeInit(GPIOD, GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11
  230. |GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15
  231. |GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_5);
  232. /* USER CODE BEGIN FSMC_MspDeInit 1 */
  233. /* USER CODE END FSMC_MspDeInit 1 */
  234. }
  235. void HAL_SRAM_MspDeInit(SRAM_HandleTypeDef* sramHandle){
  236. /* USER CODE BEGIN SRAM_MspDeInit 0 */
  237. /* USER CODE END SRAM_MspDeInit 0 */
  238. HAL_FSMC_MspDeInit();
  239. /* USER CODE BEGIN SRAM_MspDeInit 1 */
  240. /* USER CODE END SRAM_MspDeInit 1 */
  241. }
  242. /**
  243. * @brief 以“字”为单位向sdram写入数据
  244. * @param pBuffer: 指向数据的指针
  245. * @param uwWriteAddress: 要写入的SRAM内部地址
  246. * @param uwBufferSize: 要写入数据大小
  247. * @retval None.
  248. */
  249. void SRAM_WriteBuffer(uint32_t* pBuffer, uint32_t uwWriteAddress, uint32_t uwBufferSize)
  250. {
  251. __IO uint32_t write_pointer = (uint32_t)uwWriteAddress;
  252. /* 循环写入数据 */
  253. for (; uwBufferSize != 0; uwBufferSize--)
  254. {
  255. /* 发送数据到SRAM */
  256. *(uint32_t *) (Bank1_SRAM3_ADDR + write_pointer) = *pBuffer++;
  257. /* 地址自增*/
  258. write_pointer += 4;
  259. }
  260. }
  261. /**
  262. * @brief 从SRAM中读取数据
  263. * @param pBuffer: 指向存储数据的buffer
  264. * @param ReadAddress: 要读取数据的地十
  265. * @param uwBufferSize: 要读取的数据大小
  266. * @retval None.
  267. */
  268. void SRAM_ReadBuffer(uint32_t* pBuffer, uint32_t uwReadAddress, uint32_t uwBufferSize)
  269. {
  270. __IO uint32_t write_pointer = (uint32_t)uwReadAddress;
  271. /*读取数据 */
  272. for(; uwBufferSize != 0x00; uwBufferSize--)
  273. {
  274. *pBuffer++ = *(__IO uint32_t *)(Bank1_SRAM3_ADDR + write_pointer );
  275. /* 地址自增*/
  276. write_pointer += 4;
  277. }
  278. }
  279. /**
  280. * @brief 测试SRAM是否正常
  281. * @param None
  282. * @retval 正常返回1,异常返回0
  283. */
  284. uint8_t SRAM_Test(void)
  285. {
  286. /*写入数据计数器*/
  287. uint32_t counter=0;
  288. /* 8位的数据 */
  289. uint8_t ubWritedata_8b = 0, ubReaddata_8b = 0;
  290. /* 16位的数据 */
  291. uint16_t uhWritedata_16b = 0, uhReaddata_16b = 0;
  292. /*按8位格式读写数据,并校验*/
  293. /* 把SRAM数据全部重置为0 ,IS62WV51216_SIZE是以8位为单位的 */
  294. for (counter = 0x00; counter < IS62WV51216_SIZE; counter++)
  295. {
  296. *(__IO uint8_t*) (Bank1_SRAM3_ADDR + counter) = (uint8_t)0x0;
  297. }
  298. /* 向整个SRAM写入数据 8位 */
  299. for (counter = 0; counter < IS62WV51216_SIZE; counter++)
  300. {
  301. *(__IO uint8_t*) (Bank1_SRAM3_ADDR + counter) = (uint8_t)(ubWritedata_8b + counter);
  302. }
  303. /* 读取 SRAM 数据并检测*/
  304. for(counter = 0; counter<IS62WV51216_SIZE;counter++ )
  305. {
  306. ubReaddata_8b = *(__IO uint8_t*)(Bank1_SRAM3_ADDR + counter); //从该地址读出数据
  307. if(ubReaddata_8b != (uint8_t)(ubWritedata_8b + counter)) //检测数据,若不相等,跳出函数,返回检测失败结果。
  308. {
  309. return 0;
  310. }
  311. }
  312. /*按16位格式读写数据,并检测*/
  313. /* 把SRAM数据全部重置为0 */
  314. for (counter = 0x00; counter < IS62WV51216_SIZE/2; counter++)
  315. {
  316. *(__IO uint16_t*) (Bank1_SRAM3_ADDR + 2*counter) = (uint16_t)0x00;
  317. }
  318. /* 向整个SRAM写入数据 16位 */
  319. for (counter = 0; counter < IS62WV51216_SIZE/2; counter++)
  320. {
  321. *(__IO uint16_t*) (Bank1_SRAM3_ADDR + 2*counter) = (uint16_t)(uhWritedata_16b + counter);
  322. }
  323. /* 读取 SRAM 数据并检测*/
  324. for(counter = 0; counter<IS62WV51216_SIZE/2;counter++ )
  325. {
  326. uhReaddata_16b = *(__IO uint16_t*)(Bank1_SRAM3_ADDR + 2*counter); //从该地址读出数据
  327. if(uhReaddata_16b != (uint16_t)(uhWritedata_16b + counter)) //检测数据,若不相等,跳出函数,返回检测失败结果。
  328. {
  329. return 0;
  330. }
  331. }
  332. /*检测正常,return 1 */
  333. return 1;
  334. }
  335. /**
  336. * @}
  337. */