drv_sdram.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. /* Step 3: Configure a PALL (precharge all) command */
  138. Command.CommandMode = FMC_SDRAM_CMD_PALL;
  139. Command.CommandTarget = FMC_SDRAM_CMD_TARGET_BANK1;
  140. Command.AutoRefreshNumber = 1;
  141. Command.ModeRegisterDefinition = 0;
  142. /* Send the command */
  143. HAL_SDRAM_SendCommand(&sdramHandle, &Command, SDRAM_TIMEOUT);
  144. /* Step 4: Configure an Auto Refresh command */
  145. Command.CommandMode = FMC_SDRAM_CMD_AUTOREFRESH_MODE;
  146. Command.CommandTarget = FMC_SDRAM_CMD_TARGET_BANK1;
  147. Command.AutoRefreshNumber = 8;
  148. Command.ModeRegisterDefinition = 0;
  149. /* Send the command */
  150. HAL_SDRAM_SendCommand(&sdramHandle, &Command, SDRAM_TIMEOUT);
  151. /* Step 5: Program the external memory mode register */
  152. tmpmrd = (uint32_t)SDRAM_MODEREG_BURST_LENGTH_1 |\
  153. SDRAM_MODEREG_BURST_TYPE_SEQUENTIAL |\
  154. SDRAM_MODEREG_CAS_LATENCY_2 |\
  155. SDRAM_MODEREG_OPERATING_MODE_STANDARD |\
  156. SDRAM_MODEREG_WRITEBURST_MODE_SINGLE;
  157. Command.CommandMode = FMC_SDRAM_CMD_LOAD_MODE;
  158. Command.CommandTarget = FMC_SDRAM_CMD_TARGET_BANK1;
  159. Command.AutoRefreshNumber = 1;
  160. Command.ModeRegisterDefinition = tmpmrd;
  161. /* Send the command */
  162. HAL_SDRAM_SendCommand(&sdramHandle, &Command, SDRAM_TIMEOUT);
  163. /* Step 6: Set the refresh rate counter */
  164. /* Set the device refresh rate */
  165. HAL_SDRAM_ProgramRefreshRate(&sdramHandle, RefreshCount);
  166. }
  167. /**
  168. * @brief Reads an amount of data from the SDRAM memory in polling mode.
  169. * @param uwStartAddress: Read start address
  170. * @param pData: Pointer to data to be read
  171. * @param uwDataSize: Size of read data from the memory
  172. * @retval SDRAM status
  173. */
  174. rt_err_t SDRAM_ReadData(uint32_t Address, uint32_t *Data, uint32_t DataSize)
  175. {
  176. if(HAL_SDRAM_Read_32b(&sdramHandle, (uint32_t *)Address, Data, DataSize) != HAL_OK)
  177. {
  178. return RT_ERROR;
  179. }
  180. else
  181. {
  182. return RT_EOK;
  183. }
  184. }
  185. /**
  186. * @brief Reads an amount of data from the SDRAM memory in DMA mode.
  187. * @param uwStartAddress: Read start address
  188. * @param pData: Pointer to data to be read
  189. * @param uwDataSize: Size of read data from the memory
  190. * @retval SDRAM status
  191. */
  192. rt_err_t SDRAM_ReadDataDMA(uint32_t Address, uint32_t *Data, uint32_t DataSize)
  193. {
  194. if(HAL_SDRAM_Read_DMA(&sdramHandle, (uint32_t *)Address, Data, DataSize) != HAL_OK)
  195. {
  196. return RT_ERROR;
  197. }
  198. else
  199. {
  200. return RT_EOK;
  201. }
  202. }
  203. /**
  204. * @brief Writes an amount of data to the SDRAM memory in polling mode.
  205. * @param uwStartAddress: Write start address
  206. * @param pData: Pointer to data to be written
  207. * @param uwDataSize: Size of written data from the memory
  208. * @retval SDRAM status
  209. */
  210. rt_err_t SDRAM_WriteData(uint32_t Address, uint32_t *Data, uint32_t DataSize)
  211. {
  212. if(HAL_SDRAM_Write_32b(&sdramHandle, (uint32_t *)Address, Data, DataSize) != HAL_OK)
  213. {
  214. return RT_ERROR;
  215. }
  216. else
  217. {
  218. return RT_EOK;
  219. }
  220. }
  221. /**
  222. * @brief Writes an amount of data to the SDRAM memory in DMA mode.
  223. * @param Address: Write start address
  224. * @param Data: Pointer to data to be written
  225. * @param DataSize: Size of written data from the memory
  226. * @retval SDRAM status
  227. */
  228. rt_err_t SDRAM_WriteDataDMA(uint32_t Address, uint32_t *Data, uint32_t DataSize)
  229. {
  230. if(HAL_SDRAM_Write_DMA(&sdramHandle, (uint32_t *)Address, Data, DataSize) != HAL_OK)
  231. {
  232. return RT_ERROR;
  233. }
  234. else
  235. {
  236. return RT_EOK;
  237. }
  238. }
  239. /**
  240. * @brief Initializes the SDRAM device.
  241. * @retval SDRAM status
  242. */
  243. rt_err_t sdram_hw_init(void)
  244. {
  245. static uint8_t sdramstatus = RT_ERROR;
  246. /* SDRAM device configuration */
  247. sdramHandle.Instance = FMC_SDRAM_DEVICE;
  248. /* Timing configuration for 100Mhz as SD clock frequency (System clock is up to 200Mhz) */
  249. Timing.LoadToActiveDelay = 2;
  250. Timing.ExitSelfRefreshDelay = 7;
  251. Timing.SelfRefreshTime = 4;
  252. Timing.RowCycleDelay = 7;
  253. Timing.WriteRecoveryTime = 2;
  254. Timing.RPDelay = 2;
  255. Timing.RCDDelay = 2;
  256. sdramHandle.Init.SDBank = FMC_SDRAM_BANK1;
  257. sdramHandle.Init.ColumnBitsNumber = FMC_SDRAM_COLUMN_BITS_NUM_8;
  258. sdramHandle.Init.RowBitsNumber = FMC_SDRAM_ROW_BITS_NUM_12;
  259. sdramHandle.Init.MemoryDataWidth = SDRAM_MEMORY_WIDTH;
  260. sdramHandle.Init.InternalBankNumber = FMC_SDRAM_INTERN_BANKS_NUM_4;
  261. sdramHandle.Init.CASLatency = FMC_SDRAM_CAS_LATENCY_2;
  262. sdramHandle.Init.WriteProtection = FMC_SDRAM_WRITE_PROTECTION_DISABLE;
  263. sdramHandle.Init.SDClockPeriod = SDCLOCK_PERIOD;
  264. sdramHandle.Init.ReadBurst = FMC_SDRAM_RBURST_ENABLE;
  265. sdramHandle.Init.ReadPipeDelay = FMC_SDRAM_RPIPE_DELAY_0;
  266. /* SDRAM controller initialization */
  267. SDRAM_MspInit(&sdramHandle, NULL); /* __weak function can be rewritten by the application */
  268. if(HAL_SDRAM_Init(&sdramHandle, &Timing) != HAL_OK)
  269. {
  270. sdramstatus = RT_ERROR;
  271. }
  272. else
  273. {
  274. sdramstatus = RT_EOK;
  275. }
  276. /* SDRAM initialization sequence */
  277. SDRAM_InitializationSequence(REFRESH_COUNT);
  278. return sdramstatus;
  279. }
  280. /**
  281. * @brief DeInitializes the SDRAM device.
  282. * @retval SDRAM status
  283. */
  284. rt_err_t sdram_hw_deinit(void)
  285. {
  286. static uint8_t sdramstatus = RT_ERROR;
  287. /* SDRAM device de-initialization */
  288. sdramHandle.Instance = FMC_SDRAM_DEVICE;
  289. if(HAL_SDRAM_DeInit(&sdramHandle) != HAL_OK)
  290. {
  291. sdramstatus = RT_ERROR;
  292. }
  293. else
  294. {
  295. sdramstatus = RT_EOK;
  296. }
  297. /* SDRAM controller de-initialization */
  298. SDRAM_MspDeInit(&sdramHandle, NULL);
  299. return sdramstatus;
  300. }
  301. /**
  302. * @brief Handles SDRAM DMA transfer interrupt request.
  303. * @retval None
  304. */
  305. void SDRAM_DMA_IRQHandler(void)
  306. {
  307. HAL_DMA_IRQHandler(sdramHandle.hdma);
  308. }
  309. #ifdef RT_USING_FINSH
  310. #include <finsh.h>
  311. int sdram_test(void)
  312. {
  313. uint32_t i;
  314. volatile uint32_t *wr_ptr;
  315. volatile uint8_t *char_wr_ptr;
  316. volatile uint16_t *short_wr_ptr;
  317. /* initialize memory */
  318. rt_kprintf("SDRAM初始化...\r\n");
  319. wr_ptr = (uint32_t *)SDRAM_DEVICE_ADDR;
  320. char_wr_ptr = (uint8_t *)wr_ptr;
  321. /* 进行8位数据写测试前先清除数据*/
  322. rt_kprintf("清除SDRAM数据...\r\n");
  323. for (i = 0; i < SDRAM_DEVICE_SIZE / 4; i++)
  324. {
  325. *wr_ptr++ = 0x00; //写入0x00
  326. }
  327. /* 8 bit write */
  328. rt_kprintf("写入8位数据...\r\n");
  329. for (i = 0; i < SDRAM_DEVICE_SIZE / 4; i++)
  330. {
  331. *char_wr_ptr++ = 0x11;
  332. *char_wr_ptr++ = 0x22;
  333. *char_wr_ptr++ = 0x33;
  334. *char_wr_ptr++ = 0x44;
  335. }
  336. /* 校验写入的数据*/
  337. rt_kprintf("校验数据...\r\n");
  338. wr_ptr = (uint32_t *)SDRAM_DEVICE_ADDR;
  339. for (i = 0; i < SDRAM_DEVICE_SIZE / 8; i++)
  340. {
  341. if (*wr_ptr != 0x44332211) /* be aware of endianess */
  342. {
  343. /* byte comparison failure */
  344. rt_kprintf("校验失败,测试完毕!\r\n");
  345. return 1; /* fatal error */
  346. }
  347. wr_ptr++;
  348. }
  349. /* byte comparison succeed. */
  350. rt_kprintf("继续测试16位数据写入...\r\n");
  351. wr_ptr = (uint32_t *)SDRAM_DEVICE_ADDR;
  352. short_wr_ptr = (uint16_t *)wr_ptr;
  353. /* Clear content before 16 bit access test */
  354. rt_kprintf("清除SDRAM中的数据...\r\n");
  355. for (i = 0; i < SDRAM_DEVICE_SIZE / 4; i++)
  356. {
  357. *wr_ptr++ = 0;
  358. }
  359. /* 16 bit write */
  360. rt_kprintf("写入16位数据...\r\n");
  361. for (i = 0; i < (SDRAM_DEVICE_SIZE / 4); i++)
  362. {
  363. *short_wr_ptr++ = 0x5AA5;
  364. *short_wr_ptr++ = 0xAA55;
  365. }
  366. /* Verifying */
  367. wr_ptr = (uint32_t *)SDRAM_DEVICE_ADDR;
  368. //wr_ptr -= SDRAM_BASE_ADDR/4;
  369. for (i = 0; i < SDRAM_DEVICE_SIZE / 4; i++)
  370. {
  371. if (*wr_ptr != 0xAA555AA5) /* be aware of endianess */
  372. {
  373. /* 16-bit half word failure */
  374. rt_kprintf("校验失败,测试完毕!\r\n");
  375. return 1; /* fatal error */
  376. }
  377. wr_ptr++;
  378. }
  379. /* 16-bit half word comparison succeed. */
  380. rt_kprintf("校验成功,测试完毕!\r\n");
  381. return 0;
  382. }
  383. FINSH_FUNCTION_EXPORT(sdram_test, SDRAM read write test)
  384. #endif