fsmc.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /**
  2. ******************************************************************************
  3. * File Name : FSMC.c
  4. * Description : This file provides code for the configuration
  5. * of the FSMC peripheral.
  6. ******************************************************************************
  7. *
  8. * COPYRIGHT(c) 2016 STMicroelectronics
  9. *
  10. * Redistribution and use in source and binary forms, with or without modification,
  11. * are permitted provided that the following conditions are met:
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  24. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. ******************************************************************************
  33. */
  34. /* Includes ------------------------------------------------------------------*/
  35. #include "fsmc.h"
  36. #include "gpio.h"
  37. /* USER CODE BEGIN 0 */
  38. /* USER CODE END 0 */
  39. SRAM_HandleTypeDef hsram1;
  40. /* FSMC initialization function */
  41. void MX_FSMC_Init(void)
  42. {
  43. FSMC_NORSRAM_TimingTypeDef Timing;
  44. /** Perform the SRAM1 memory initialization sequence
  45. */
  46. hsram1.Instance = FSMC_NORSRAM_DEVICE;
  47. hsram1.Extended = FSMC_NORSRAM_EXTENDED_DEVICE;
  48. /* hsram1.Init */
  49. hsram1.Init.NSBank = FSMC_NORSRAM_BANK1;
  50. hsram1.Init.DataAddressMux = FSMC_DATA_ADDRESS_MUX_ENABLE;
  51. hsram1.Init.MemoryType = FSMC_MEMORY_TYPE_PSRAM;
  52. hsram1.Init.MemoryDataWidth = FSMC_NORSRAM_MEM_BUS_WIDTH_16;
  53. hsram1.Init.BurstAccessMode = FSMC_BURST_ACCESS_MODE_DISABLE;
  54. hsram1.Init.WaitSignalPolarity = FSMC_WAIT_SIGNAL_POLARITY_LOW;
  55. hsram1.Init.WrapMode = FSMC_WRAP_MODE_DISABLE;
  56. hsram1.Init.WaitSignalActive = FSMC_WAIT_TIMING_BEFORE_WS;
  57. hsram1.Init.WriteOperation = FSMC_WRITE_OPERATION_ENABLE;
  58. hsram1.Init.WaitSignal = FSMC_WAIT_SIGNAL_DISABLE;
  59. hsram1.Init.ExtendedMode = FSMC_EXTENDED_MODE_DISABLE;
  60. hsram1.Init.AsynchronousWait = FSMC_ASYNCHRONOUS_WAIT_DISABLE;
  61. hsram1.Init.WriteBurst = FSMC_WRITE_BURST_DISABLE;
  62. /* Timing */
  63. // 1 clock to read the address, + 3 for the synchroniser
  64. Timing.AddressSetupTime = 4;
  65. Timing.AddressHoldTime = 1;
  66. // 3 for synchroniser, 1 to skip hold time, 1 to process read, 1 to output
  67. Timing.DataSetupTime = 6;
  68. // Allow a clock for us to release signals, plus 3 for the synchroniser to
  69. // realise the cycle has ended. Need to avoid both devices acting as outputs
  70. // on the multiplexed lines at the same time.
  71. Timing.BusTurnAroundDuration = 4;
  72. Timing.CLKDivision = 16; // Ignored for async
  73. Timing.DataLatency = 17; // Ignored for async
  74. Timing.AccessMode = FSMC_ACCESS_MODE_A;
  75. /* ExtTiming */
  76. HAL_SRAM_Init(&hsram1, &Timing, NULL);
  77. }
  78. static int FSMC_Initialized = 0;
  79. static void HAL_FSMC_MspInit(void){
  80. /* USER CODE BEGIN FSMC_MspInit 0 */
  81. /* USER CODE END FSMC_MspInit 0 */
  82. GPIO_InitTypeDef GPIO_InitStruct;
  83. if (FSMC_Initialized) {
  84. return;
  85. }
  86. FSMC_Initialized = 1;
  87. /* Peripheral clock enable */
  88. __FSMC_CLK_ENABLE();
  89. /** FSMC GPIO Configuration
  90. PE7 ------> FSMC_DA4
  91. PE8 ------> FSMC_DA5
  92. PE9 ------> FSMC_DA6
  93. PE10 ------> FSMC_DA7
  94. PE11 ------> FSMC_DA8
  95. PE12 ------> FSMC_DA9
  96. PE13 ------> FSMC_DA10
  97. PE14 ------> FSMC_DA11
  98. PE15 ------> FSMC_DA12
  99. PD14 ------> FSMC_DA0
  100. PD15 ------> FSMC_DA1
  101. PD0 ------> FSMC_DA2
  102. PD1 ------> FSMC_DA3
  103. PD4 ------> FSMC_NOE
  104. PD5 ------> FSMC_NWE
  105. PD7 ------> FSMC_NE1
  106. PD8 ------> FSMC_DA13
  107. PD9 ------> FSMC_DA14
  108. PD10 ------> FSMC_DA15
  109. PB7 ------> FSMC_NL
  110. PE0 ------> FSMC_NBL0
  111. PE1 ------> FSMC_NBL1
  112. */
  113. /* GPIO_InitStruct */
  114. GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
  115. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  116. GPIO_InitStruct.Pull = GPIO_NOPULL;
  117. GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
  118. GPIO_InitStruct.Alternate = GPIO_AF12_FSMC;
  119. HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
  120. /* GPIO_InitStruct */
  121. GPIO_InitStruct.Pin = GPIO_PIN_14|GPIO_PIN_15|GPIO_PIN_0|GPIO_PIN_1
  122. |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10;
  123. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  124. GPIO_InitStruct.Pull = GPIO_NOPULL;
  125. GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
  126. GPIO_InitStruct.Alternate = GPIO_AF12_FSMC;
  127. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  128. /* GPIO_InitStruct */
  129. GPIO_InitStruct.Pin = GPIO_PIN_7;
  130. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  131. GPIO_InitStruct.Pull = GPIO_NOPULL;
  132. GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
  133. GPIO_InitStruct.Alternate = GPIO_AF12_FSMC;
  134. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  135. /* USER CODE BEGIN FSMC_MspInit 1 */
  136. /* USER CODE END FSMC_MspInit 1 */
  137. }
  138. void HAL_SRAM_MspInit(SRAM_HandleTypeDef* hsram){
  139. /* USER CODE BEGIN SRAM_MspInit 0 */
  140. /* USER CODE END SRAM_MspInit 0 */
  141. HAL_FSMC_MspInit();
  142. /* USER CODE BEGIN SRAM_MspInit 1 */
  143. /* USER CODE END SRAM_MspInit 1 */
  144. }
  145. static int FSMC_DeInitialized = 0;
  146. static void HAL_FSMC_MspDeInit(void){
  147. /* USER CODE BEGIN FSMC_MspDeInit 0 */
  148. /* USER CODE END FSMC_MspDeInit 0 */
  149. if (FSMC_DeInitialized) {
  150. return;
  151. }
  152. FSMC_DeInitialized = 1;
  153. /* Peripheral clock enable */
  154. __FSMC_CLK_DISABLE();
  155. /** FSMC GPIO Configuration
  156. PE7 ------> FSMC_DA4
  157. PE8 ------> FSMC_DA5
  158. PE9 ------> FSMC_DA6
  159. PE10 ------> FSMC_DA7
  160. PE11 ------> FSMC_DA8
  161. PE12 ------> FSMC_DA9
  162. PE13 ------> FSMC_DA10
  163. PE14 ------> FSMC_DA11
  164. PE15 ------> FSMC_DA12
  165. PD14 ------> FSMC_DA0
  166. PD15 ------> FSMC_DA1
  167. PD0 ------> FSMC_DA2
  168. PD1 ------> FSMC_DA3
  169. PD4 ------> FSMC_NOE
  170. PD5 ------> FSMC_NWE
  171. PD7 ------> FSMC_NE1
  172. PD8 ------> FSMC_DA13
  173. PD9 ------> FSMC_DA14
  174. PD10 ------> FSMC_DA15
  175. PB7 ------> FSMC_NL
  176. PE0 ------> FSMC_NBL0
  177. PE1 ------> FSMC_NBL1
  178. */
  179. HAL_GPIO_DeInit(GPIOE, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15);
  180. HAL_GPIO_DeInit(GPIOD, GPIO_PIN_14|GPIO_PIN_15|GPIO_PIN_0|GPIO_PIN_1
  181. |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10);
  182. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_7);
  183. /* USER CODE BEGIN FSMC_MspDeInit 1 */
  184. /* USER CODE END FSMC_MspDeInit 1 */
  185. }
  186. void HAL_SRAM_MspDeInit(SRAM_HandleTypeDef* hsram){
  187. /* USER CODE BEGIN SRAM_MspDeInit 0 */
  188. /* USER CODE END SRAM_MspDeInit 0 */
  189. HAL_FSMC_MspDeInit();
  190. /* USER CODE BEGIN SRAM_MspDeInit 1 */
  191. /* USER CODE END SRAM_MspDeInit 1 */
  192. }
  193. /**
  194. * @}
  195. */
  196. /**
  197. * @}
  198. */
  199. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/