drv_sdram.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2016-08-20 xuzhuoyi The first version for STM32F42x
  9. * 2017-04-07 lizhen9880 Use SDRAM BANK1
  10. */
  11. /* Includes ------------------------------------------------------------------*/
  12. #include "drv_sdram.h"
  13. #include "stm32f4xx_ll_fmc.h"
  14. #include <rtdevice.h>
  15. #include "board.h"
  16. #include "drv_mpu.h"
  17. SDRAM_HandleTypeDef hsdram1;
  18. FMC_SDRAM_CommandTypeDef command;
  19. /**
  20. * @brief SDRAM MSP Initialization
  21. * This function configures the hardware resources used in this example:
  22. * - Peripheral's clock enable
  23. * - Peripheral's GPIO Configuration
  24. * @param hsdram: SDRAM handle pointer
  25. * @retval None
  26. */
  27. void HAL_SDRAM_MspInit(SDRAM_HandleTypeDef *hsdram)
  28. {
  29. GPIO_InitTypeDef GPIO_Init_Structure;
  30. /*##-1- Enable peripherals and GPIO Clocks #################################*/
  31. /* Enable GPIO clocks */
  32. __HAL_RCC_GPIOB_CLK_ENABLE();
  33. __HAL_RCC_GPIOC_CLK_ENABLE();
  34. __HAL_RCC_GPIOD_CLK_ENABLE();
  35. __HAL_RCC_GPIOE_CLK_ENABLE();
  36. __HAL_RCC_GPIOF_CLK_ENABLE();
  37. __HAL_RCC_GPIOG_CLK_ENABLE();
  38. /* Enable FMC clock */
  39. __HAL_RCC_FMC_CLK_ENABLE();
  40. /*##-2- Configure peripheral GPIO ##########################################*/
  41. /*-- GPIOs Configuration -----------------------------------------------------*/
  42. /*
  43. +-------------------+--------------------+--------------------+--------------------+
  44. + SDRAM pins assignment +
  45. +-------------------+--------------------+--------------------+--------------------+
  46. | PD0 <-> FMC_D2 | PE0 <-> FMC_NBL0 | PF0 <-> FMC_A0 | PG0 <-> FMC_A10 |
  47. | PD1 <-> FMC_D3 | PE1 <-> FMC_NBL1 | PF1 <-> FMC_A1 | PG1 <-> FMC_A11 |
  48. | PD8 <-> FMC_D13 | PE7 <-> FMC_D4 | PF2 <-> FMC_A2 | PG2 <-> FMC_A12 |
  49. | PD9 <-> FMC_D14 | PE8 <-> FMC_D5 | PF3 <-> FMC_A3 | PG4 <-> FMC_BA0 |
  50. | PD10 <-> FMC_D15 | PE9 <-> FMC_D6 | PF4 <-> FMC_A4 | PG5 <-> FMC_BA1 |
  51. | PD14 <-> FMC_D0 | PE10 <-> FMC_D7 | PF5 <-> FMC_A5 | PG8 <-> FMC_SDCLK |
  52. | PD15 <-> FMC_D1 | PE11 <-> FMC_D8 | PF11 <-> FMC_NRAS | PG15 <-> FMC_NCAS |
  53. +-------------------| PE12 <-> FMC_D9 | PF12 <-> FMC_A6 |--------------------+
  54. | PE13 <-> FMC_D10 | PF13 <-> FMC_A7 |
  55. | PE14 <-> FMC_D11 | PF14 <-> FMC_A8 |
  56. | PE15 <-> FMC_D12 | PF15 <-> FMC_A9 |
  57. +-------------------+--------------------+--------------------+
  58. | PC3 <-> FMC_SDCKE0|
  59. | PC2 <-> FMC_SDNE0 |
  60. | PC0 <-> FMC_SDNWE |
  61. +-------------------+
  62. */
  63. /* Common GPIO configuration */
  64. GPIO_Init_Structure.Mode = GPIO_MODE_AF_PP;
  65. GPIO_Init_Structure.Speed = GPIO_SPEED_HIGH;
  66. GPIO_Init_Structure.Pull = GPIO_PULLUP;
  67. GPIO_Init_Structure.Alternate = GPIO_AF12_FMC;
  68. /* GPIOC configuration */
  69. GPIO_Init_Structure.Pin = GPIO_PIN_0 | GPIO_PIN_2 | GPIO_PIN_3;
  70. HAL_GPIO_Init(GPIOC, &GPIO_Init_Structure);
  71. /* GPIOD configuration */
  72. GPIO_Init_Structure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_8 |
  73. GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_14 |
  74. GPIO_PIN_15;
  75. HAL_GPIO_Init(GPIOD, &GPIO_Init_Structure);
  76. /* GPIOE configuration */
  77. GPIO_Init_Structure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_7 |
  78. GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 |
  79. GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13 |
  80. GPIO_PIN_14 | GPIO_PIN_15;
  81. HAL_GPIO_Init(GPIOE, &GPIO_Init_Structure);
  82. /* GPIOF configuration */
  83. GPIO_Init_Structure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 |
  84. GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 |
  85. GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13 |
  86. GPIO_PIN_14 | GPIO_PIN_15;
  87. HAL_GPIO_Init(GPIOF, &GPIO_Init_Structure);
  88. /* GPIOG configuration */
  89. GPIO_Init_Structure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_4 |
  90. GPIO_PIN_5 | GPIO_PIN_8 | GPIO_PIN_15;
  91. HAL_GPIO_Init(GPIOG, &GPIO_Init_Structure);
  92. }
  93. /**
  94. * @brief SDRAM MSP De-Initialization
  95. * This function frees the hardware resources used in this example:
  96. * - Disable the Peripheral's clock
  97. * - Revert GPIO configuration to their default state
  98. * @param hsdram: SDRAM handle pointer
  99. * @retval None
  100. */
  101. void HAL_SDRAM_MspDeInit(SDRAM_HandleTypeDef *hsdram)
  102. {
  103. /*## Disable peripherals and GPIO Clocks ###################################*/
  104. HAL_GPIO_DeInit(GPIOC, GPIO_PIN_0 | GPIO_PIN_2 | GPIO_PIN_3);
  105. HAL_GPIO_DeInit(GPIOD, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_8 |\
  106. GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_14 |\
  107. GPIO_PIN_15);
  108. HAL_GPIO_DeInit(GPIOE, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_7 |\
  109. GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 |\
  110. GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13 |\
  111. GPIO_PIN_14 | GPIO_PIN_15);
  112. HAL_GPIO_DeInit(GPIOF, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 |\
  113. GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 |\
  114. GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13 |\
  115. GPIO_PIN_14 | GPIO_PIN_15);
  116. HAL_GPIO_DeInit(GPIOG, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_4 |\
  117. GPIO_PIN_5 | GPIO_PIN_8 | GPIO_PIN_15);
  118. }
  119. /**
  120. * @brief Perform the SDRAM exernal memory inialization sequence
  121. * @param hsdram: SDRAM handle
  122. * @param Command: Pointer to SDRAM command structure
  123. * @retval None
  124. */
  125. static void SDRAM_Initialization_Sequence(SDRAM_HandleTypeDef *hsdram, FMC_SDRAM_CommandTypeDef *Command)
  126. {
  127. __IO uint32_t tmpmrd =0;
  128. /* Step 3: Configure a clock configuration enable command */
  129. Command->CommandMode = FMC_SDRAM_CMD_CLK_ENABLE;
  130. Command->CommandTarget = FMC_SDRAM_CMD_TARGET_BANK1;
  131. Command->AutoRefreshNumber = 1;
  132. Command->ModeRegisterDefinition = 0;
  133. /* Send the command */
  134. HAL_SDRAM_SendCommand(hsdram, Command, 0x1000);
  135. /* Step 4: Insert 100 ms delay */
  136. /* interrupt is not enable, just to delay some time. */
  137. for (tmpmrd = 0; tmpmrd < 0xfffff; tmpmrd ++)
  138. ;
  139. /* Step 5: Configure a PALL (precharge all) command */
  140. Command->CommandMode = FMC_SDRAM_CMD_PALL;
  141. Command->CommandTarget = FMC_SDRAM_CMD_TARGET_BANK1;
  142. Command->AutoRefreshNumber = 1;
  143. Command->ModeRegisterDefinition = 0;
  144. /* Send the command */
  145. HAL_SDRAM_SendCommand(hsdram, Command, 0x1000);
  146. /* Step 6 : Configure a Auto-Refresh command */
  147. Command->CommandMode = FMC_SDRAM_CMD_AUTOREFRESH_MODE;
  148. Command->CommandTarget = FMC_SDRAM_CMD_TARGET_BANK1;
  149. Command->AutoRefreshNumber = 8;
  150. Command->ModeRegisterDefinition = 0;
  151. /* Send the command */
  152. HAL_SDRAM_SendCommand(hsdram, Command, 0x1000);
  153. /* Step 7: Program the external memory mode register */
  154. tmpmrd = (uint32_t)SDRAM_MODEREG_BURST_LENGTH_1 |
  155. SDRAM_MODEREG_BURST_TYPE_SEQUENTIAL |
  156. SDRAM_MODEREG_CAS_LATENCY_3 |
  157. SDRAM_MODEREG_OPERATING_MODE_STANDARD |
  158. SDRAM_MODEREG_WRITEBURST_MODE_SINGLE;
  159. Command->CommandMode = FMC_SDRAM_CMD_LOAD_MODE;
  160. Command->CommandTarget = FMC_SDRAM_CMD_TARGET_BANK1;
  161. Command->AutoRefreshNumber = 1;
  162. Command->ModeRegisterDefinition = tmpmrd;
  163. /* Send the command */
  164. HAL_SDRAM_SendCommand(hsdram, Command, 0x1000);
  165. /* Step 8: Set the refresh rate counter */
  166. /* (15.62 us x Freq) - 20 */
  167. /* Set the device refresh counter */
  168. HAL_SDRAM_ProgramRefreshRate(hsdram, REFRESH_COUNT);
  169. }
  170. /**
  171. * @brief Configures the FMC and GPIOs to interface with the SDRAM memory.
  172. * This function must be called before any read/write operation
  173. * on the SDRAM.
  174. * @param None
  175. * @retval None
  176. */
  177. void SDRAM_Init(void)
  178. {
  179. FMC_SDRAM_TimingTypeDef SDRAM_Timing;
  180. /*##-1- Configure the SDRAM device #########################################*/
  181. /* SDRAM device configuration */
  182. hsdram1.Instance = FMC_SDRAM_DEVICE;
  183. /* Timing configuration for 90 MHz of SD clock frequency (180MHz/2) */
  184. /* TMRD: 2 Clock cycles */
  185. SDRAM_Timing.LoadToActiveDelay = 2;
  186. /* TXSR: min=70ns (6x11.90ns) */
  187. SDRAM_Timing.ExitSelfRefreshDelay = 8;
  188. /* TRAS: min=42ns (4x11.90ns) max=120k (ns) */
  189. SDRAM_Timing.SelfRefreshTime = 6;
  190. /* TRC: min=63 (6x11.90ns) */
  191. SDRAM_Timing.RowCycleDelay = 6;
  192. /* TWR: 2 Clock cycles */
  193. SDRAM_Timing.WriteRecoveryTime = 2;
  194. /* TRP: 15ns => 2x11.90ns */
  195. SDRAM_Timing.RPDelay = 2;
  196. /* TRCD: 15ns => 2x11.90ns */
  197. SDRAM_Timing.RCDDelay = 2;
  198. hsdram1.Init.SDBank = FMC_SDRAM_BANK1;
  199. hsdram1.Init.ColumnBitsNumber = FMC_SDRAM_COLUMN_BITS_NUM_9;
  200. hsdram1.Init.RowBitsNumber = FMC_SDRAM_ROW_BITS_NUM_13;
  201. hsdram1.Init.MemoryDataWidth = SDRAM_MEMORY_WIDTH;
  202. hsdram1.Init.InternalBankNumber = FMC_SDRAM_INTERN_BANKS_NUM_4;
  203. hsdram1.Init.CASLatency = FMC_SDRAM_CAS_LATENCY_3;
  204. hsdram1.Init.WriteProtection = FMC_SDRAM_WRITE_PROTECTION_DISABLE;
  205. hsdram1.Init.SDClockPeriod = SDCLOCK_PERIOD;
  206. hsdram1.Init.ReadBurst = FMC_SDRAM_RBURST_ENABLE;
  207. hsdram1.Init.ReadPipeDelay = FMC_SDRAM_RPIPE_DELAY_1;
  208. /* Initialize the SDRAM controller */
  209. if(HAL_SDRAM_Init(&hsdram1, &SDRAM_Timing) != HAL_OK)
  210. {
  211. /* Initialization Error */
  212. Error_Handler();
  213. }
  214. /* Program the SDRAM external device */
  215. SDRAM_Initialization_Sequence(&hsdram1, &command);
  216. }
  217. rt_err_t sdram_hw_init(void)
  218. {
  219. SDRAM_Init();
  220. return RT_EOK;
  221. }
  222. static int rt_sdram_hw_init(void)
  223. {
  224. return (int)sdram_hw_init();
  225. }
  226. INIT_BOARD_EXPORT(rt_sdram_hw_init);