fsmc.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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, + 1 for synchroniser skew
  64. Timing.AddressSetupTime = 2;
  65. Timing.AddressHoldTime = 1;
  66. // 1 for synchroniser skew, 1 to skip hold time, 1 to process read, 1 to output
  67. Timing.DataSetupTime = 5;//4 doesn't work ? ?? ?
  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. // MM: GPIO_SPEED_FREQ_MEDIUM is rated up to 50MHz, which is fine as all the
  114. // fsmc timings are > 1 (ie. so clock speed / 2 is around 50MHz).
  115. /* GPIO_InitStruct */
  116. 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;
  117. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  118. GPIO_InitStruct.Pull = GPIO_NOPULL;
  119. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM; //HIGH;
  120. GPIO_InitStruct.Alternate = GPIO_AF12_FSMC;
  121. HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
  122. /* GPIO_InitStruct */
  123. GPIO_InitStruct.Pin = GPIO_PIN_14|GPIO_PIN_15|GPIO_PIN_0|GPIO_PIN_1
  124. |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10;
  125. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  126. GPIO_InitStruct.Pull = GPIO_NOPULL;
  127. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM; //HIGH;
  128. GPIO_InitStruct.Alternate = GPIO_AF12_FSMC;
  129. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  130. /* GPIO_InitStruct */
  131. GPIO_InitStruct.Pin = GPIO_PIN_7;
  132. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  133. GPIO_InitStruct.Pull = GPIO_NOPULL;
  134. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;//HIGH;
  135. GPIO_InitStruct.Alternate = GPIO_AF12_FSMC;
  136. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  137. /* USER CODE BEGIN FSMC_MspInit 1 */
  138. /* USER CODE END FSMC_MspInit 1 */
  139. }
  140. void HAL_SRAM_MspInit(SRAM_HandleTypeDef* hsram){
  141. /* USER CODE BEGIN SRAM_MspInit 0 */
  142. /* USER CODE END SRAM_MspInit 0 */
  143. HAL_FSMC_MspInit();
  144. /* USER CODE BEGIN SRAM_MspInit 1 */
  145. /* USER CODE END SRAM_MspInit 1 */
  146. }
  147. static int FSMC_DeInitialized = 0;
  148. static void HAL_FSMC_MspDeInit(void){
  149. /* USER CODE BEGIN FSMC_MspDeInit 0 */
  150. /* USER CODE END FSMC_MspDeInit 0 */
  151. if (FSMC_DeInitialized) {
  152. return;
  153. }
  154. FSMC_DeInitialized = 1;
  155. /* Peripheral clock enable */
  156. __FSMC_CLK_DISABLE();
  157. /** FSMC GPIO Configuration
  158. PE7 ------> FSMC_DA4
  159. PE8 ------> FSMC_DA5
  160. PE9 ------> FSMC_DA6
  161. PE10 ------> FSMC_DA7
  162. PE11 ------> FSMC_DA8
  163. PE12 ------> FSMC_DA9
  164. PE13 ------> FSMC_DA10
  165. PE14 ------> FSMC_DA11
  166. PE15 ------> FSMC_DA12
  167. PD14 ------> FSMC_DA0
  168. PD15 ------> FSMC_DA1
  169. PD0 ------> FSMC_DA2
  170. PD1 ------> FSMC_DA3
  171. PD4 ------> FSMC_NOE
  172. PD5 ------> FSMC_NWE
  173. PD7 ------> FSMC_NE1
  174. PD8 ------> FSMC_DA13
  175. PD9 ------> FSMC_DA14
  176. PD10 ------> FSMC_DA15
  177. PB7 ------> FSMC_NL
  178. PE0 ------> FSMC_NBL0
  179. PE1 ------> FSMC_NBL1
  180. */
  181. 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);
  182. HAL_GPIO_DeInit(GPIOD, GPIO_PIN_14|GPIO_PIN_15|GPIO_PIN_0|GPIO_PIN_1
  183. |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10);
  184. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_7);
  185. /* USER CODE BEGIN FSMC_MspDeInit 1 */
  186. /* USER CODE END FSMC_MspDeInit 1 */
  187. }
  188. void HAL_SRAM_MspDeInit(SRAM_HandleTypeDef* hsram){
  189. /* USER CODE BEGIN SRAM_MspDeInit 0 */
  190. /* USER CODE END SRAM_MspDeInit 0 */
  191. HAL_FSMC_MspDeInit();
  192. /* USER CODE BEGIN SRAM_MspDeInit 1 */
  193. /* USER CODE END SRAM_MspDeInit 1 */
  194. }
  195. /**
  196. * @}
  197. */
  198. /**
  199. * @}
  200. */
  201. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/