stm32f4xx_hal_sdram.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_sdram.c
  4. * @author MCD Application Team
  5. * @version V1.3.0
  6. * @date 09-March-2015
  7. * @brief SDRAM HAL module driver.
  8. * This file provides a generic firmware to drive SDRAM memories mounted
  9. * as external device.
  10. *
  11. @verbatim
  12. ==============================================================================
  13. ##### How to use this driver #####
  14. ==============================================================================
  15. [..]
  16. This driver is a generic layered driver which contains a set of APIs used to
  17. control SDRAM memories. It uses the FMC layer functions to interface
  18. with SDRAM devices.
  19. The following sequence should be followed to configure the FMC to interface
  20. with SDRAM memories:
  21. (#) Declare a SDRAM_HandleTypeDef handle structure, for example:
  22. SDRAM_HandleTypeDef hdsram
  23. (++) Fill the SDRAM_HandleTypeDef handle "Init" field with the allowed
  24. values of the structure member.
  25. (++) Fill the SDRAM_HandleTypeDef handle "Instance" field with a predefined
  26. base register instance for NOR or SDRAM device
  27. (#) Declare a FMC_SDRAM_TimingTypeDef structure; for example:
  28. FMC_SDRAM_TimingTypeDef Timing;
  29. and fill its fields with the allowed values of the structure member.
  30. (#) Initialize the SDRAM Controller by calling the function HAL_SDRAM_Init(). This function
  31. performs the following sequence:
  32. (##) MSP hardware layer configuration using the function HAL_SDRAM_MspInit()
  33. (##) Control register configuration using the FMC SDRAM interface function
  34. FMC_SDRAM_Init()
  35. (##) Timing register configuration using the FMC SDRAM interface function
  36. FMC_SDRAM_Timing_Init()
  37. (##) Program the SDRAM external device by applying its initialization sequence
  38. according to the device plugged in your hardware. This step is mandatory
  39. for accessing the SDRAM device.
  40. (#) At this stage you can perform read/write accesses from/to the memory connected
  41. to the SDRAM Bank. You can perform either polling or DMA transfer using the
  42. following APIs:
  43. (++) HAL_SDRAM_Read()/HAL_SDRAM_Write() for polling read/write access
  44. (++) HAL_SDRAM_Read_DMA()/HAL_SDRAM_Write_DMA() for DMA read/write transfer
  45. (#) You can also control the SDRAM device by calling the control APIs HAL_SDRAM_WriteOperation_Enable()/
  46. HAL_SDRAM_WriteOperation_Disable() to respectively enable/disable the SDRAM write operation or
  47. the function HAL_SDRAM_SendCommand() to send a specified command to the SDRAM
  48. device. The command to be sent must be configured with the FMC_SDRAM_CommandTypeDef
  49. structure.
  50. (#) You can continuously monitor the SDRAM device HAL state by calling the function
  51. HAL_SDRAM_GetState()
  52. @endverbatim
  53. ******************************************************************************
  54. * @attention
  55. *
  56. * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
  57. *
  58. * Redistribution and use in source and binary forms, with or without modification,
  59. * are permitted provided that the following conditions are met:
  60. * 1. Redistributions of source code must retain the above copyright notice,
  61. * this list of conditions and the following disclaimer.
  62. * 2. Redistributions in binary form must reproduce the above copyright notice,
  63. * this list of conditions and the following disclaimer in the documentation
  64. * and/or other materials provided with the distribution.
  65. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  66. * may be used to endorse or promote products derived from this software
  67. * without specific prior written permission.
  68. *
  69. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  70. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  71. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  72. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  73. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  74. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  75. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  76. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  77. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  78. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  79. *
  80. ******************************************************************************
  81. */
  82. /* Includes ------------------------------------------------------------------*/
  83. #include "stm32f4xx_hal.h"
  84. /** @addtogroup STM32F4xx_HAL_Driver
  85. * @{
  86. */
  87. /** @defgroup SDRAM SDRAM
  88. * @brief SDRAM driver modules
  89. * @{
  90. */
  91. #ifdef HAL_SDRAM_MODULE_ENABLED
  92. #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) || defined(STM32F446xx)
  93. /* Private typedef -----------------------------------------------------------*/
  94. /* Private define ------------------------------------------------------------*/
  95. /* Private macro -------------------------------------------------------------*/
  96. /* Private variables ---------------------------------------------------------*/
  97. /* Private functions ---------------------------------------------------------*/
  98. /* Exported functions --------------------------------------------------------*/
  99. /** @defgroup SDRAM_Exported_Functions SDRAM Exported Functions
  100. * @{
  101. */
  102. /** @defgroup SDRAM_Exported_Functions_Group1 Initialization and de-initialization functions
  103. * @brief Initialization and Configuration functions
  104. *
  105. @verbatim
  106. ==============================================================================
  107. ##### SDRAM Initialization and de_initialization functions #####
  108. ==============================================================================
  109. [..]
  110. This section provides functions allowing to initialize/de-initialize
  111. the SDRAM memory
  112. @endverbatim
  113. * @{
  114. */
  115. /**
  116. * @brief Performs the SDRAM device initialization sequence.
  117. * @param hsdram: pointer to a SDRAM_HandleTypeDef structure that contains
  118. * the configuration information for SDRAM module.
  119. * @param Timing: Pointer to SDRAM control timing structure
  120. * @retval HAL status
  121. */
  122. HAL_StatusTypeDef HAL_SDRAM_Init(SDRAM_HandleTypeDef *hsdram, FMC_SDRAM_TimingTypeDef *Timing)
  123. {
  124. /* Check the SDRAM handle parameter */
  125. if(hsdram == NULL)
  126. {
  127. return HAL_ERROR;
  128. }
  129. if(hsdram->State == HAL_SDRAM_STATE_RESET)
  130. {
  131. /* Allocate lock resource and initialize it */
  132. hsdram->Lock = HAL_UNLOCKED;
  133. /* Initialize the low level hardware (MSP) */
  134. HAL_SDRAM_MspInit(hsdram);
  135. }
  136. /* Initialize the SDRAM controller state */
  137. hsdram->State = HAL_SDRAM_STATE_BUSY;
  138. /* Initialize SDRAM control Interface */
  139. FMC_SDRAM_Init(hsdram->Instance, &(hsdram->Init));
  140. /* Initialize SDRAM timing Interface */
  141. FMC_SDRAM_Timing_Init(hsdram->Instance, Timing, hsdram->Init.SDBank);
  142. /* Update the SDRAM controller state */
  143. hsdram->State = HAL_SDRAM_STATE_READY;
  144. return HAL_OK;
  145. }
  146. /**
  147. * @brief Perform the SDRAM device initialization sequence.
  148. * @param hsdram: pointer to a SDRAM_HandleTypeDef structure that contains
  149. * the configuration information for SDRAM module.
  150. * @retval HAL status
  151. */
  152. HAL_StatusTypeDef HAL_SDRAM_DeInit(SDRAM_HandleTypeDef *hsdram)
  153. {
  154. /* Initialize the low level hardware (MSP) */
  155. HAL_SDRAM_MspDeInit(hsdram);
  156. /* Configure the SDRAM registers with their reset values */
  157. FMC_SDRAM_DeInit(hsdram->Instance, hsdram->Init.SDBank);
  158. /* Reset the SDRAM controller state */
  159. hsdram->State = HAL_SDRAM_STATE_RESET;
  160. /* Release Lock */
  161. __HAL_UNLOCK(hsdram);
  162. return HAL_OK;
  163. }
  164. /**
  165. * @brief SDRAM MSP Init.
  166. * @param hsdram: pointer to a SDRAM_HandleTypeDef structure that contains
  167. * the configuration information for SDRAM module.
  168. * @retval None
  169. */
  170. __weak void HAL_SDRAM_MspInit(SDRAM_HandleTypeDef *hsdram)
  171. {
  172. /* NOTE: This function Should not be modified, when the callback is needed,
  173. the HAL_SDRAM_MspInit could be implemented in the user file
  174. */
  175. }
  176. /**
  177. * @brief SDRAM MSP DeInit.
  178. * @param hsdram: pointer to a SDRAM_HandleTypeDef structure that contains
  179. * the configuration information for SDRAM module.
  180. * @retval None
  181. */
  182. __weak void HAL_SDRAM_MspDeInit(SDRAM_HandleTypeDef *hsdram)
  183. {
  184. /* NOTE: This function Should not be modified, when the callback is needed,
  185. the HAL_SDRAM_MspDeInit could be implemented in the user file
  186. */
  187. }
  188. /**
  189. * @brief This function handles SDRAM refresh error interrupt request.
  190. * @param hsdram: pointer to a SDRAM_HandleTypeDef structure that contains
  191. * the configuration information for SDRAM module.
  192. * @retval HAL status
  193. */
  194. void HAL_SDRAM_IRQHandler(SDRAM_HandleTypeDef *hsdram)
  195. {
  196. /* Check SDRAM interrupt Rising edge flag */
  197. if(__FMC_SDRAM_GET_FLAG(hsdram->Instance, FMC_SDRAM_FLAG_REFRESH_IT))
  198. {
  199. /* SDRAM refresh error interrupt callback */
  200. HAL_SDRAM_RefreshErrorCallback(hsdram);
  201. /* Clear SDRAM refresh error interrupt pending bit */
  202. __FMC_SDRAM_CLEAR_FLAG(hsdram->Instance, FMC_SDRAM_FLAG_REFRESH_ERROR);
  203. }
  204. }
  205. /**
  206. * @brief SDRAM Refresh error callback.
  207. * @param hsdram: pointer to a SDRAM_HandleTypeDef structure that contains
  208. * the configuration information for SDRAM module.
  209. * @retval None
  210. */
  211. __weak void HAL_SDRAM_RefreshErrorCallback(SDRAM_HandleTypeDef *hsdram)
  212. {
  213. /* NOTE: This function Should not be modified, when the callback is needed,
  214. the HAL_SDRAM_RefreshErrorCallback could be implemented in the user file
  215. */
  216. }
  217. /**
  218. * @brief DMA transfer complete callback.
  219. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  220. * the configuration information for the specified DMA module.
  221. * @retval None
  222. */
  223. __weak void HAL_SDRAM_DMA_XferCpltCallback(DMA_HandleTypeDef *hdma)
  224. {
  225. /* NOTE: This function Should not be modified, when the callback is needed,
  226. the HAL_SDRAM_DMA_XferCpltCallback could be implemented in the user file
  227. */
  228. }
  229. /**
  230. * @brief DMA transfer complete error callback.
  231. * @param hdma: DMA handle
  232. * @retval None
  233. */
  234. __weak void HAL_SDRAM_DMA_XferErrorCallback(DMA_HandleTypeDef *hdma)
  235. {
  236. /* NOTE: This function Should not be modified, when the callback is needed,
  237. the HAL_SDRAM_DMA_XferErrorCallback could be implemented in the user file
  238. */
  239. }
  240. /**
  241. * @}
  242. */
  243. /** @defgroup SDRAM_Exported_Functions_Group2 Input and Output functions
  244. * @brief Input Output and memory control functions
  245. *
  246. @verbatim
  247. ==============================================================================
  248. ##### SDRAM Input and Output functions #####
  249. ==============================================================================
  250. [..]
  251. This section provides functions allowing to use and control the SDRAM memory
  252. @endverbatim
  253. * @{
  254. */
  255. /**
  256. * @brief Reads 8-bit data buffer from the SDRAM memory.
  257. * @param hsdram: pointer to a SDRAM_HandleTypeDef structure that contains
  258. * the configuration information for SDRAM module.
  259. * @param pAddress: Pointer to read start address
  260. * @param pDstBuffer: Pointer to destination buffer
  261. * @param BufferSize: Size of the buffer to read from memory
  262. * @retval HAL status
  263. */
  264. HAL_StatusTypeDef HAL_SDRAM_Read_8b(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint8_t *pDstBuffer, uint32_t BufferSize)
  265. {
  266. __IO uint8_t *pSdramAddress = (uint8_t *)pAddress;
  267. /* Process Locked */
  268. __HAL_LOCK(hsdram);
  269. /* Check the SDRAM controller state */
  270. if(hsdram->State == HAL_SDRAM_STATE_BUSY)
  271. {
  272. return HAL_BUSY;
  273. }
  274. else if(hsdram->State == HAL_SDRAM_STATE_PRECHARGED)
  275. {
  276. return HAL_ERROR;
  277. }
  278. /* Read data from source */
  279. for(; BufferSize != 0; BufferSize--)
  280. {
  281. *pDstBuffer = *(__IO uint8_t *)pSdramAddress;
  282. pDstBuffer++;
  283. pSdramAddress++;
  284. }
  285. /* Process Unlocked */
  286. __HAL_UNLOCK(hsdram);
  287. return HAL_OK;
  288. }
  289. /**
  290. * @brief Writes 8-bit data buffer to SDRAM memory.
  291. * @param hsdram: pointer to a SDRAM_HandleTypeDef structure that contains
  292. * the configuration information for SDRAM module.
  293. * @param pAddress: Pointer to write start address
  294. * @param pSrcBuffer: Pointer to source buffer to write
  295. * @param BufferSize: Size of the buffer to write to memory
  296. * @retval HAL status
  297. */
  298. HAL_StatusTypeDef HAL_SDRAM_Write_8b(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint8_t *pSrcBuffer, uint32_t BufferSize)
  299. {
  300. __IO uint8_t *pSdramAddress = (uint8_t *)pAddress;
  301. uint32_t tmp = 0;
  302. /* Process Locked */
  303. __HAL_LOCK(hsdram);
  304. /* Check the SDRAM controller state */
  305. tmp = hsdram->State;
  306. if(tmp == HAL_SDRAM_STATE_BUSY)
  307. {
  308. return HAL_BUSY;
  309. }
  310. else if((tmp == HAL_SDRAM_STATE_PRECHARGED) || (tmp == HAL_SDRAM_STATE_WRITE_PROTECTED))
  311. {
  312. return HAL_ERROR;
  313. }
  314. /* Write data to memory */
  315. for(; BufferSize != 0; BufferSize--)
  316. {
  317. *(__IO uint8_t *)pSdramAddress = *pSrcBuffer;
  318. pSrcBuffer++;
  319. pSdramAddress++;
  320. }
  321. /* Process Unlocked */
  322. __HAL_UNLOCK(hsdram);
  323. return HAL_OK;
  324. }
  325. /**
  326. * @brief Reads 16-bit data buffer from the SDRAM memory.
  327. * @param hsdram: pointer to a SDRAM_HandleTypeDef structure that contains
  328. * the configuration information for SDRAM module.
  329. * @param pAddress: Pointer to read start address
  330. * @param pDstBuffer: Pointer to destination buffer
  331. * @param BufferSize: Size of the buffer to read from memory
  332. * @retval HAL status
  333. */
  334. HAL_StatusTypeDef HAL_SDRAM_Read_16b(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint16_t *pDstBuffer, uint32_t BufferSize)
  335. {
  336. __IO uint16_t *pSdramAddress = (uint16_t *)pAddress;
  337. /* Process Locked */
  338. __HAL_LOCK(hsdram);
  339. /* Check the SDRAM controller state */
  340. if(hsdram->State == HAL_SDRAM_STATE_BUSY)
  341. {
  342. return HAL_BUSY;
  343. }
  344. else if(hsdram->State == HAL_SDRAM_STATE_PRECHARGED)
  345. {
  346. return HAL_ERROR;
  347. }
  348. /* Read data from source */
  349. for(; BufferSize != 0; BufferSize--)
  350. {
  351. *pDstBuffer = *(__IO uint16_t *)pSdramAddress;
  352. pDstBuffer++;
  353. pSdramAddress++;
  354. }
  355. /* Process Unlocked */
  356. __HAL_UNLOCK(hsdram);
  357. return HAL_OK;
  358. }
  359. /**
  360. * @brief Writes 16-bit data buffer to SDRAM memory.
  361. * @param hsdram: pointer to a SDRAM_HandleTypeDef structure that contains
  362. * the configuration information for SDRAM module.
  363. * @param pAddress: Pointer to write start address
  364. * @param pSrcBuffer: Pointer to source buffer to write
  365. * @param BufferSize: Size of the buffer to write to memory
  366. * @retval HAL status
  367. */
  368. HAL_StatusTypeDef HAL_SDRAM_Write_16b(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint16_t *pSrcBuffer, uint32_t BufferSize)
  369. {
  370. __IO uint16_t *pSdramAddress = (uint16_t *)pAddress;
  371. uint32_t tmp = 0;
  372. /* Process Locked */
  373. __HAL_LOCK(hsdram);
  374. /* Check the SDRAM controller state */
  375. tmp = hsdram->State;
  376. if(tmp == HAL_SDRAM_STATE_BUSY)
  377. {
  378. return HAL_BUSY;
  379. }
  380. else if((tmp == HAL_SDRAM_STATE_PRECHARGED) || (tmp == HAL_SDRAM_STATE_WRITE_PROTECTED))
  381. {
  382. return HAL_ERROR;
  383. }
  384. /* Write data to memory */
  385. for(; BufferSize != 0; BufferSize--)
  386. {
  387. *(__IO uint16_t *)pSdramAddress = *pSrcBuffer;
  388. pSrcBuffer++;
  389. pSdramAddress++;
  390. }
  391. /* Process Unlocked */
  392. __HAL_UNLOCK(hsdram);
  393. return HAL_OK;
  394. }
  395. /**
  396. * @brief Reads 32-bit data buffer from the SDRAM memory.
  397. * @param hsdram: pointer to a SDRAM_HandleTypeDef structure that contains
  398. * the configuration information for SDRAM module.
  399. * @param pAddress: Pointer to read start address
  400. * @param pDstBuffer: Pointer to destination buffer
  401. * @param BufferSize: Size of the buffer to read from memory
  402. * @retval HAL status
  403. */
  404. HAL_StatusTypeDef HAL_SDRAM_Read_32b(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint32_t *pDstBuffer, uint32_t BufferSize)
  405. {
  406. __IO uint32_t *pSdramAddress = (uint32_t *)pAddress;
  407. /* Process Locked */
  408. __HAL_LOCK(hsdram);
  409. /* Check the SDRAM controller state */
  410. if(hsdram->State == HAL_SDRAM_STATE_BUSY)
  411. {
  412. return HAL_BUSY;
  413. }
  414. else if(hsdram->State == HAL_SDRAM_STATE_PRECHARGED)
  415. {
  416. return HAL_ERROR;
  417. }
  418. /* Read data from source */
  419. for(; BufferSize != 0; BufferSize--)
  420. {
  421. *pDstBuffer = *(__IO uint32_t *)pSdramAddress;
  422. pDstBuffer++;
  423. pSdramAddress++;
  424. }
  425. /* Process Unlocked */
  426. __HAL_UNLOCK(hsdram);
  427. return HAL_OK;
  428. }
  429. /**
  430. * @brief Writes 32-bit data buffer to SDRAM memory.
  431. * @param hsdram: pointer to a SDRAM_HandleTypeDef structure that contains
  432. * the configuration information for SDRAM module.
  433. * @param pAddress: Pointer to write start address
  434. * @param pSrcBuffer: Pointer to source buffer to write
  435. * @param BufferSize: Size of the buffer to write to memory
  436. * @retval HAL status
  437. */
  438. HAL_StatusTypeDef HAL_SDRAM_Write_32b(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint32_t *pSrcBuffer, uint32_t BufferSize)
  439. {
  440. __IO uint32_t *pSdramAddress = (uint32_t *)pAddress;
  441. uint32_t tmp = 0;
  442. /* Process Locked */
  443. __HAL_LOCK(hsdram);
  444. /* Check the SDRAM controller state */
  445. tmp = hsdram->State;
  446. if(tmp == HAL_SDRAM_STATE_BUSY)
  447. {
  448. return HAL_BUSY;
  449. }
  450. else if((tmp == HAL_SDRAM_STATE_PRECHARGED) || (tmp == HAL_SDRAM_STATE_WRITE_PROTECTED))
  451. {
  452. return HAL_ERROR;
  453. }
  454. /* Write data to memory */
  455. for(; BufferSize != 0; BufferSize--)
  456. {
  457. *(__IO uint32_t *)pSdramAddress = *pSrcBuffer;
  458. pSrcBuffer++;
  459. pSdramAddress++;
  460. }
  461. /* Process Unlocked */
  462. __HAL_UNLOCK(hsdram);
  463. return HAL_OK;
  464. }
  465. /**
  466. * @brief Reads a Words data from the SDRAM memory using DMA transfer.
  467. * @param hsdram: pointer to a SDRAM_HandleTypeDef structure that contains
  468. * the configuration information for SDRAM module.
  469. * @param pAddress: Pointer to read start address
  470. * @param pDstBuffer: Pointer to destination buffer
  471. * @param BufferSize: Size of the buffer to read from memory
  472. * @retval HAL status
  473. */
  474. HAL_StatusTypeDef HAL_SDRAM_Read_DMA(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint32_t *pDstBuffer, uint32_t BufferSize)
  475. {
  476. uint32_t tmp = 0;
  477. /* Process Locked */
  478. __HAL_LOCK(hsdram);
  479. /* Check the SDRAM controller state */
  480. tmp = hsdram->State;
  481. if(tmp == HAL_SDRAM_STATE_BUSY)
  482. {
  483. return HAL_BUSY;
  484. }
  485. else if(tmp == HAL_SDRAM_STATE_PRECHARGED)
  486. {
  487. return HAL_ERROR;
  488. }
  489. /* Configure DMA user callbacks */
  490. hsdram->hdma->XferCpltCallback = HAL_SDRAM_DMA_XferCpltCallback;
  491. hsdram->hdma->XferErrorCallback = HAL_SDRAM_DMA_XferErrorCallback;
  492. /* Enable the DMA Stream */
  493. HAL_DMA_Start_IT(hsdram->hdma, (uint32_t)pAddress, (uint32_t)pDstBuffer, (uint32_t)BufferSize);
  494. /* Process Unlocked */
  495. __HAL_UNLOCK(hsdram);
  496. return HAL_OK;
  497. }
  498. /**
  499. * @brief Writes a Words data buffer to SDRAM memory using DMA transfer.
  500. * @param hsdram: pointer to a SDRAM_HandleTypeDef structure that contains
  501. * the configuration information for SDRAM module.
  502. * @param pAddress: Pointer to write start address
  503. * @param pSrcBuffer: Pointer to source buffer to write
  504. * @param BufferSize: Size of the buffer to write to memory
  505. * @retval HAL status
  506. */
  507. HAL_StatusTypeDef HAL_SDRAM_Write_DMA(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint32_t *pSrcBuffer, uint32_t BufferSize)
  508. {
  509. uint32_t tmp = 0;
  510. /* Process Locked */
  511. __HAL_LOCK(hsdram);
  512. /* Check the SDRAM controller state */
  513. tmp = hsdram->State;
  514. if(tmp == HAL_SDRAM_STATE_BUSY)
  515. {
  516. return HAL_BUSY;
  517. }
  518. else if((tmp == HAL_SDRAM_STATE_PRECHARGED) || (tmp == HAL_SDRAM_STATE_WRITE_PROTECTED))
  519. {
  520. return HAL_ERROR;
  521. }
  522. /* Configure DMA user callbacks */
  523. hsdram->hdma->XferCpltCallback = HAL_SDRAM_DMA_XferCpltCallback;
  524. hsdram->hdma->XferErrorCallback = HAL_SDRAM_DMA_XferErrorCallback;
  525. /* Enable the DMA Stream */
  526. HAL_DMA_Start_IT(hsdram->hdma, (uint32_t)pSrcBuffer, (uint32_t)pAddress, (uint32_t)BufferSize);
  527. /* Process Unlocked */
  528. __HAL_UNLOCK(hsdram);
  529. return HAL_OK;
  530. }
  531. /**
  532. * @}
  533. */
  534. /** @defgroup SDRAM_Exported_Functions_Group3 Control functions
  535. * @brief management functions
  536. *
  537. @verbatim
  538. ==============================================================================
  539. ##### SDRAM Control functions #####
  540. ==============================================================================
  541. [..]
  542. This subsection provides a set of functions allowing to control dynamically
  543. the SDRAM interface.
  544. @endverbatim
  545. * @{
  546. */
  547. /**
  548. * @brief Enables dynamically SDRAM write protection.
  549. * @param hsdram: pointer to a SDRAM_HandleTypeDef structure that contains
  550. * the configuration information for SDRAM module.
  551. * @retval HAL status
  552. */
  553. HAL_StatusTypeDef HAL_SDRAM_WriteProtection_Enable(SDRAM_HandleTypeDef *hsdram)
  554. {
  555. /* Check the SDRAM controller state */
  556. if(hsdram->State == HAL_SDRAM_STATE_BUSY)
  557. {
  558. return HAL_BUSY;
  559. }
  560. /* Update the SDRAM state */
  561. hsdram->State = HAL_SDRAM_STATE_BUSY;
  562. /* Enable write protection */
  563. FMC_SDRAM_WriteProtection_Enable(hsdram->Instance, hsdram->Init.SDBank);
  564. /* Update the SDRAM state */
  565. hsdram->State = HAL_SDRAM_STATE_WRITE_PROTECTED;
  566. return HAL_OK;
  567. }
  568. /**
  569. * @brief Disables dynamically SDRAM write protection.
  570. * @param hsdram: pointer to a SDRAM_HandleTypeDef structure that contains
  571. * the configuration information for SDRAM module.
  572. * @retval HAL status
  573. */
  574. HAL_StatusTypeDef HAL_SDRAM_WriteProtection_Disable(SDRAM_HandleTypeDef *hsdram)
  575. {
  576. /* Check the SDRAM controller state */
  577. if(hsdram->State == HAL_SDRAM_STATE_BUSY)
  578. {
  579. return HAL_BUSY;
  580. }
  581. /* Update the SDRAM state */
  582. hsdram->State = HAL_SDRAM_STATE_BUSY;
  583. /* Disable write protection */
  584. FMC_SDRAM_WriteProtection_Disable(hsdram->Instance, hsdram->Init.SDBank);
  585. /* Update the SDRAM state */
  586. hsdram->State = HAL_SDRAM_STATE_READY;
  587. return HAL_OK;
  588. }
  589. /**
  590. * @brief Sends Command to the SDRAM bank.
  591. * @param hsdram: pointer to a SDRAM_HandleTypeDef structure that contains
  592. * the configuration information for SDRAM module.
  593. * @param Command: SDRAM command structure
  594. * @param Timeout: Timeout duration
  595. * @retval HAL status
  596. */
  597. HAL_StatusTypeDef HAL_SDRAM_SendCommand(SDRAM_HandleTypeDef *hsdram, FMC_SDRAM_CommandTypeDef *Command, uint32_t Timeout)
  598. {
  599. /* Check the SDRAM controller state */
  600. if(hsdram->State == HAL_SDRAM_STATE_BUSY)
  601. {
  602. return HAL_BUSY;
  603. }
  604. /* Update the SDRAM state */
  605. hsdram->State = HAL_SDRAM_STATE_BUSY;
  606. /* Send SDRAM command */
  607. FMC_SDRAM_SendCommand(hsdram->Instance, Command, Timeout);
  608. /* Update the SDRAM controller state */
  609. if(Command->CommandMode == FMC_SDRAM_CMD_PALL)
  610. {
  611. hsdram->State = HAL_SDRAM_STATE_PRECHARGED;
  612. }
  613. else
  614. {
  615. hsdram->State = HAL_SDRAM_STATE_READY;
  616. }
  617. return HAL_OK;
  618. }
  619. /**
  620. * @brief Programs the SDRAM Memory Refresh rate.
  621. * @param hsdram: pointer to a SDRAM_HandleTypeDef structure that contains
  622. * the configuration information for SDRAM module.
  623. * @param RefreshRate: The SDRAM refresh rate value
  624. * @retval HAL status
  625. */
  626. HAL_StatusTypeDef HAL_SDRAM_ProgramRefreshRate(SDRAM_HandleTypeDef *hsdram, uint32_t RefreshRate)
  627. {
  628. /* Check the SDRAM controller state */
  629. if(hsdram->State == HAL_SDRAM_STATE_BUSY)
  630. {
  631. return HAL_BUSY;
  632. }
  633. /* Update the SDRAM state */
  634. hsdram->State = HAL_SDRAM_STATE_BUSY;
  635. /* Program the refresh rate */
  636. FMC_SDRAM_ProgramRefreshRate(hsdram->Instance ,RefreshRate);
  637. /* Update the SDRAM state */
  638. hsdram->State = HAL_SDRAM_STATE_READY;
  639. return HAL_OK;
  640. }
  641. /**
  642. * @brief Sets the Number of consecutive SDRAM Memory auto Refresh commands.
  643. * @param hsdram: pointer to a SDRAM_HandleTypeDef structure that contains
  644. * the configuration information for SDRAM module.
  645. * @param AutoRefreshNumber: The SDRAM auto Refresh number
  646. * @retval HAL status
  647. */
  648. HAL_StatusTypeDef HAL_SDRAM_SetAutoRefreshNumber(SDRAM_HandleTypeDef *hsdram, uint32_t AutoRefreshNumber)
  649. {
  650. /* Check the SDRAM controller state */
  651. if(hsdram->State == HAL_SDRAM_STATE_BUSY)
  652. {
  653. return HAL_BUSY;
  654. }
  655. /* Update the SDRAM state */
  656. hsdram->State = HAL_SDRAM_STATE_BUSY;
  657. /* Set the Auto-Refresh number */
  658. FMC_SDRAM_SetAutoRefreshNumber(hsdram->Instance ,AutoRefreshNumber);
  659. /* Update the SDRAM state */
  660. hsdram->State = HAL_SDRAM_STATE_READY;
  661. return HAL_OK;
  662. }
  663. /**
  664. * @brief Returns the SDRAM memory current mode.
  665. * @param hsdram: pointer to a SDRAM_HandleTypeDef structure that contains
  666. * the configuration information for SDRAM module.
  667. * @retval The SDRAM memory mode.
  668. */
  669. uint32_t HAL_SDRAM_GetModeStatus(SDRAM_HandleTypeDef *hsdram)
  670. {
  671. /* Return the SDRAM memory current mode */
  672. return(FMC_SDRAM_GetModeStatus(hsdram->Instance, hsdram->Init.SDBank));
  673. }
  674. /**
  675. * @}
  676. */
  677. /** @defgroup SDRAM_Exported_Functions_Group4 State functions
  678. * @brief Peripheral State functions
  679. *
  680. @verbatim
  681. ==============================================================================
  682. ##### SDRAM State functions #####
  683. ==============================================================================
  684. [..]
  685. This subsection permits to get in run-time the status of the SDRAM controller
  686. and the data flow.
  687. @endverbatim
  688. * @{
  689. */
  690. /**
  691. * @brief Returns the SDRAM state.
  692. * @param hsdram: pointer to a SDRAM_HandleTypeDef structure that contains
  693. * the configuration information for SDRAM module.
  694. * @retval HAL state
  695. */
  696. HAL_SDRAM_StateTypeDef HAL_SDRAM_GetState(SDRAM_HandleTypeDef *hsdram)
  697. {
  698. return hsdram->State;
  699. }
  700. /**
  701. * @}
  702. */
  703. /**
  704. * @}
  705. */
  706. #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F446xx */
  707. #endif /* HAL_SDRAM_MODULE_ENABLED */
  708. /**
  709. * @}
  710. */
  711. /**
  712. * @}
  713. */
  714. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/