stm32f4xx_hal_sdram.c 27 KB

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