drv_sdram.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * File : drv_sdram.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2015, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2015-08-03 xiaonong The first version for STM32F7
  23. */
  24. #include "drv_sdram.h"
  25. static SDRAM_HandleTypeDef sdramHandle;
  26. static FMC_SDRAM_TimingTypeDef Timing;
  27. static FMC_SDRAM_CommandTypeDef Command;
  28. /**
  29. * @brief Initializes SDRAM MSP.
  30. * @param hsdram: SDRAM handle
  31. * @param Params
  32. * @retval None
  33. */
  34. static void SDRAM_MspInit(SDRAM_HandleTypeDef *hsdram, void *Params)
  35. {
  36. static DMA_HandleTypeDef dma_handle;
  37. GPIO_InitTypeDef gpio_init_structure;
  38. /* Enable FMC clock */
  39. __HAL_RCC_FMC_CLK_ENABLE();
  40. /* Enable chosen DMAx clock */
  41. SDRAM_DMA_CLK_ENABLE();
  42. /* Enable GPIOs clock */
  43. __HAL_RCC_GPIOC_CLK_ENABLE();
  44. __HAL_RCC_GPIOD_CLK_ENABLE();
  45. __HAL_RCC_GPIOE_CLK_ENABLE();
  46. __HAL_RCC_GPIOF_CLK_ENABLE();
  47. __HAL_RCC_GPIOG_CLK_ENABLE();
  48. __HAL_RCC_GPIOH_CLK_ENABLE();
  49. /* Common GPIO configuration */
  50. gpio_init_structure.Mode = GPIO_MODE_AF_PP;
  51. gpio_init_structure.Pull = GPIO_PULLUP;
  52. gpio_init_structure.Speed = GPIO_SPEED_FAST;
  53. gpio_init_structure.Alternate = GPIO_AF12_FMC;
  54. /* GPIOC configuration */
  55. gpio_init_structure.Pin = GPIO_PIN_3;
  56. HAL_GPIO_Init(GPIOC, &gpio_init_structure);
  57. /* GPIOD configuration */
  58. gpio_init_structure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_3 | GPIO_PIN_8 | GPIO_PIN_9 |
  59. GPIO_PIN_10 | GPIO_PIN_14 | GPIO_PIN_15;
  60. HAL_GPIO_Init(GPIOD, &gpio_init_structure);
  61. /* GPIOE configuration */
  62. gpio_init_structure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_7| GPIO_PIN_8 | GPIO_PIN_9 |\
  63. GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 |\
  64. GPIO_PIN_15;
  65. HAL_GPIO_Init(GPIOE, &gpio_init_structure);
  66. /* GPIOF configuration */
  67. gpio_init_structure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2| GPIO_PIN_3 | GPIO_PIN_4 |\
  68. GPIO_PIN_5 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 |\
  69. GPIO_PIN_15;
  70. HAL_GPIO_Init(GPIOF, &gpio_init_structure);
  71. /* GPIOG configuration */
  72. gpio_init_structure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_4| GPIO_PIN_5 | GPIO_PIN_8 |\
  73. GPIO_PIN_15;
  74. HAL_GPIO_Init(GPIOG, &gpio_init_structure);
  75. /* GPIOH configuration */
  76. gpio_init_structure.Pin = GPIO_PIN_3 | GPIO_PIN_5;
  77. HAL_GPIO_Init(GPIOH, &gpio_init_structure);
  78. /* Configure common DMA parameters */
  79. dma_handle.Init.Channel = SDRAM_DMA_CHANNEL;
  80. dma_handle.Init.Direction = DMA_MEMORY_TO_MEMORY;
  81. dma_handle.Init.PeriphInc = DMA_PINC_ENABLE;
  82. dma_handle.Init.MemInc = DMA_MINC_ENABLE;
  83. dma_handle.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
  84. dma_handle.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
  85. dma_handle.Init.Mode = DMA_NORMAL;
  86. dma_handle.Init.Priority = DMA_PRIORITY_HIGH;
  87. dma_handle.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  88. dma_handle.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
  89. dma_handle.Init.MemBurst = DMA_MBURST_SINGLE;
  90. dma_handle.Init.PeriphBurst = DMA_PBURST_SINGLE;
  91. dma_handle.Instance = SDRAM_DMA_STREAM;
  92. /* Associate the DMA handle */
  93. __HAL_LINKDMA(hsdram, hdma, dma_handle);
  94. /* Deinitialize the stream for new transfer */
  95. HAL_DMA_DeInit(&dma_handle);
  96. /* Configure the DMA stream */
  97. HAL_DMA_Init(&dma_handle);
  98. /* NVIC configuration for DMA transfer complete interrupt */
  99. HAL_NVIC_SetPriority(SDRAM_DMA_IRQn, 5, 0);
  100. HAL_NVIC_EnableIRQ(SDRAM_DMA_IRQn);
  101. }
  102. /**
  103. * @brief DeInitializes SDRAM MSP.
  104. * @param hsdram: SDRAM handle
  105. * @param Params
  106. * @retval None
  107. */
  108. static void SDRAM_MspDeInit(SDRAM_HandleTypeDef *hsdram, void *Params)
  109. {
  110. static DMA_HandleTypeDef dma_handle;
  111. /* Disable NVIC configuration for DMA interrupt */
  112. HAL_NVIC_DisableIRQ(SDRAM_DMA_IRQn);
  113. /* Deinitialize the stream for new transfer */
  114. dma_handle.Instance = SDRAM_DMA_STREAM;
  115. HAL_DMA_DeInit(&dma_handle);
  116. /* GPIO pins clock, FMC clock and DMA clock can be shut down in the applications
  117. by surcharging this __weak function */
  118. }
  119. /**
  120. * @brief Programs the SDRAM device.
  121. * @param RefreshCount: SDRAM refresh counter value
  122. * @retval None
  123. */
  124. static void SDRAM_InitializationSequence(uint32_t RefreshCount)
  125. {
  126. __IO uint32_t tmpmrd = 0;
  127. /* Step 1: Configure a clock configuration enable command */
  128. Command.CommandMode = FMC_SDRAM_CMD_CLK_ENABLE;
  129. Command.CommandTarget = FMC_SDRAM_CMD_TARGET_BANK1;
  130. Command.AutoRefreshNumber = 1;
  131. Command.ModeRegisterDefinition = 0;
  132. /* Send the command */
  133. HAL_SDRAM_SendCommand(&sdramHandle, &Command, SDRAM_TIMEOUT);
  134. /* Step 2: Insert 100 us minimum delay */
  135. /* Inserted delay is equal to 1 ms due to systick time base unit (ms) */
  136. // HAL_Delay(1);
  137. /* interrupt is not enable, just to delay some time. */
  138. for (tmpmrd = 0; tmpmrd < 0xfffff; tmpmrd ++)
  139. ;
  140. /* Step 3: Configure a PALL (precharge all) command */
  141. Command.CommandMode = FMC_SDRAM_CMD_PALL;
  142. Command.CommandTarget = FMC_SDRAM_CMD_TARGET_BANK1;
  143. Command.AutoRefreshNumber = 1;
  144. Command.ModeRegisterDefinition = 0;
  145. /* Send the command */
  146. HAL_SDRAM_SendCommand(&sdramHandle, &Command, SDRAM_TIMEOUT);
  147. /* Step 4: Configure an Auto Refresh command */
  148. Command.CommandMode = FMC_SDRAM_CMD_AUTOREFRESH_MODE;
  149. Command.CommandTarget = FMC_SDRAM_CMD_TARGET_BANK1;
  150. Command.AutoRefreshNumber = 8;
  151. Command.ModeRegisterDefinition = 0;
  152. /* Send the command */
  153. HAL_SDRAM_SendCommand(&sdramHandle, &Command, SDRAM_TIMEOUT);
  154. /* Step 5: Program the external memory mode register */
  155. tmpmrd = (uint32_t)SDRAM_MODEREG_BURST_LENGTH_1 |\
  156. SDRAM_MODEREG_BURST_TYPE_SEQUENTIAL |\
  157. SDRAM_MODEREG_CAS_LATENCY_2 |\
  158. SDRAM_MODEREG_OPERATING_MODE_STANDARD |\
  159. SDRAM_MODEREG_WRITEBURST_MODE_SINGLE;
  160. Command.CommandMode = FMC_SDRAM_CMD_LOAD_MODE;
  161. Command.CommandTarget = FMC_SDRAM_CMD_TARGET_BANK1;
  162. Command.AutoRefreshNumber = 1;
  163. Command.ModeRegisterDefinition = tmpmrd;
  164. /* Send the command */
  165. HAL_SDRAM_SendCommand(&sdramHandle, &Command, SDRAM_TIMEOUT);
  166. /* Step 6: Set the refresh rate counter */
  167. /* Set the device refresh rate */
  168. HAL_SDRAM_ProgramRefreshRate(&sdramHandle, RefreshCount);
  169. }
  170. /**
  171. * @brief Reads an amount of data from the SDRAM memory in polling mode.
  172. * @param uwStartAddress: Read start address
  173. * @param pData: Pointer to data to be read
  174. * @param uwDataSize: Size of read data from the memory
  175. * @retval SDRAM status
  176. */
  177. rt_err_t SDRAM_ReadData(uint32_t Address, uint32_t *Data, uint32_t DataSize)
  178. {
  179. if(HAL_SDRAM_Read_32b(&sdramHandle, (uint32_t *)Address, Data, DataSize) != HAL_OK)
  180. {
  181. return RT_ERROR;
  182. }
  183. else
  184. {
  185. return RT_EOK;
  186. }
  187. }
  188. /**
  189. * @brief Reads an amount of data from the SDRAM memory in DMA mode.
  190. * @param uwStartAddress: Read start address
  191. * @param pData: Pointer to data to be read
  192. * @param uwDataSize: Size of read data from the memory
  193. * @retval SDRAM status
  194. */
  195. rt_err_t SDRAM_ReadDataDMA(uint32_t Address, uint32_t *Data, uint32_t DataSize)
  196. {
  197. if(HAL_SDRAM_Read_DMA(&sdramHandle, (uint32_t *)Address, Data, DataSize) != HAL_OK)
  198. {
  199. return RT_ERROR;
  200. }
  201. else
  202. {
  203. return RT_EOK;
  204. }
  205. }
  206. /**
  207. * @brief Writes an amount of data to the SDRAM memory in polling mode.
  208. * @param uwStartAddress: Write start address
  209. * @param pData: Pointer to data to be written
  210. * @param uwDataSize: Size of written data from the memory
  211. * @retval SDRAM status
  212. */
  213. rt_err_t SDRAM_WriteData(uint32_t Address, uint32_t *Data, uint32_t DataSize)
  214. {
  215. if(HAL_SDRAM_Write_32b(&sdramHandle, (uint32_t *)Address, Data, DataSize) != HAL_OK)
  216. {
  217. return RT_ERROR;
  218. }
  219. else
  220. {
  221. return RT_EOK;
  222. }
  223. }
  224. /**
  225. * @brief Writes an amount of data to the SDRAM memory in DMA mode.
  226. * @param Address: Write start address
  227. * @param Data: Pointer to data to be written
  228. * @param DataSize: Size of written data from the memory
  229. * @retval SDRAM status
  230. */
  231. rt_err_t SDRAM_WriteDataDMA(uint32_t Address, uint32_t *Data, uint32_t DataSize)
  232. {
  233. if(HAL_SDRAM_Write_DMA(&sdramHandle, (uint32_t *)Address, Data, DataSize) != HAL_OK)
  234. {
  235. return RT_ERROR;
  236. }
  237. else
  238. {
  239. return RT_EOK;
  240. }
  241. }
  242. /**
  243. * @brief Initializes the SDRAM device.
  244. * @retval SDRAM status
  245. */
  246. rt_err_t sdram_hw_init(void)
  247. {
  248. static uint8_t sdramstatus = RT_ERROR;
  249. /* SDRAM device configuration */
  250. sdramHandle.Instance = FMC_SDRAM_DEVICE;
  251. /* Timing configuration for 100Mhz as SD clock frequency (System clock is up to 200Mhz) */
  252. Timing.LoadToActiveDelay = 2;
  253. Timing.ExitSelfRefreshDelay = 7;
  254. Timing.SelfRefreshTime = 4;
  255. Timing.RowCycleDelay = 7;
  256. Timing.WriteRecoveryTime = 2;
  257. Timing.RPDelay = 2;
  258. Timing.RCDDelay = 2;
  259. sdramHandle.Init.SDBank = FMC_SDRAM_BANK1;
  260. sdramHandle.Init.ColumnBitsNumber = FMC_SDRAM_COLUMN_BITS_NUM_8;
  261. sdramHandle.Init.RowBitsNumber = FMC_SDRAM_ROW_BITS_NUM_12;
  262. sdramHandle.Init.MemoryDataWidth = SDRAM_MEMORY_WIDTH;
  263. sdramHandle.Init.InternalBankNumber = FMC_SDRAM_INTERN_BANKS_NUM_4;
  264. sdramHandle.Init.CASLatency = FMC_SDRAM_CAS_LATENCY_2;
  265. sdramHandle.Init.WriteProtection = FMC_SDRAM_WRITE_PROTECTION_DISABLE;
  266. sdramHandle.Init.SDClockPeriod = SDCLOCK_PERIOD;
  267. sdramHandle.Init.ReadBurst = FMC_SDRAM_RBURST_ENABLE;
  268. sdramHandle.Init.ReadPipeDelay = FMC_SDRAM_RPIPE_DELAY_0;
  269. /* SDRAM controller initialization */
  270. SDRAM_MspInit(&sdramHandle, NULL); /* __weak function can be rewritten by the application */
  271. if(HAL_SDRAM_Init(&sdramHandle, &Timing) != HAL_OK)
  272. {
  273. sdramstatus = RT_ERROR;
  274. }
  275. else
  276. {
  277. sdramstatus = RT_EOK;
  278. }
  279. /* SDRAM initialization sequence */
  280. SDRAM_InitializationSequence(REFRESH_COUNT);
  281. return sdramstatus;
  282. }
  283. /**
  284. * @brief DeInitializes the SDRAM device.
  285. * @retval SDRAM status
  286. */
  287. rt_err_t sdram_hw_deinit(void)
  288. {
  289. static uint8_t sdramstatus = RT_ERROR;
  290. /* SDRAM device de-initialization */
  291. sdramHandle.Instance = FMC_SDRAM_DEVICE;
  292. if(HAL_SDRAM_DeInit(&sdramHandle) != HAL_OK)
  293. {
  294. sdramstatus = RT_ERROR;
  295. }
  296. else
  297. {
  298. sdramstatus = RT_EOK;
  299. }
  300. /* SDRAM controller de-initialization */
  301. SDRAM_MspDeInit(&sdramHandle, NULL);
  302. return sdramstatus;
  303. }
  304. /**
  305. * @brief Handles SDRAM DMA transfer interrupt request.
  306. * @retval None
  307. */
  308. void SDRAM_DMA_IRQHandler(void)
  309. {
  310. HAL_DMA_IRQHandler(sdramHandle.hdma);
  311. }
  312. static int rt_sdram_hw_init(void)
  313. {
  314. return (int)sdram_hw_init();
  315. }
  316. INIT_BOARD_EXPORT(rt_sdram_hw_init);