stm32f4xx_hal_sdram.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_sdram.c
  4. * @author MCD Application Team
  5. * @version V1.4.3
  6. * @date 11-December-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) ||\
  93. defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx)
  94. /* Private typedef -----------------------------------------------------------*/
  95. /* Private define ------------------------------------------------------------*/
  96. /* Private macro -------------------------------------------------------------*/
  97. /* Private variables ---------------------------------------------------------*/
  98. /* Private functions ---------------------------------------------------------*/
  99. /* Exported functions --------------------------------------------------------*/
  100. /** @defgroup SDRAM_Exported_Functions SDRAM Exported Functions
  101. * @{
  102. */
  103. /** @defgroup SDRAM_Exported_Functions_Group1 Initialization and de-initialization functions
  104. * @brief Initialization and Configuration functions
  105. *
  106. @verbatim
  107. ==============================================================================
  108. ##### SDRAM Initialization and de_initialization functions #####
  109. ==============================================================================
  110. [..]
  111. This section provides functions allowing to initialize/de-initialize
  112. the SDRAM memory
  113. @endverbatim
  114. * @{
  115. */
  116. /**
  117. * @brief Performs the SDRAM device initialization sequence.
  118. * @param hsdram: pointer to a SDRAM_HandleTypeDef structure that contains
  119. * the configuration information for SDRAM module.
  120. * @param Timing: Pointer to SDRAM control timing structure
  121. * @retval HAL status
  122. */
  123. HAL_StatusTypeDef HAL_SDRAM_Init(SDRAM_HandleTypeDef *hsdram, FMC_SDRAM_TimingTypeDef *Timing)
  124. {
  125. /* Check the SDRAM handle parameter */
  126. if(hsdram == NULL)
  127. {
  128. return HAL_ERROR;
  129. }
  130. if(hsdram->State == HAL_SDRAM_STATE_RESET)
  131. {
  132. /* Allocate lock resource and initialize it */
  133. hsdram->Lock = HAL_UNLOCKED;
  134. /* Initialize the low level hardware (MSP) */
  135. HAL_SDRAM_MspInit(hsdram);
  136. }
  137. /* Initialize the SDRAM controller state */
  138. hsdram->State = HAL_SDRAM_STATE_BUSY;
  139. /* Initialize SDRAM control Interface */
  140. FMC_SDRAM_Init(hsdram->Instance, &(hsdram->Init));
  141. /* Initialize SDRAM timing Interface */
  142. FMC_SDRAM_Timing_Init(hsdram->Instance, Timing, hsdram->Init.SDBank);
  143. /* Update the SDRAM controller state */
  144. hsdram->State = HAL_SDRAM_STATE_READY;
  145. return HAL_OK;
  146. }
  147. /**
  148. * @brief Perform the SDRAM device initialization sequence.
  149. * @param hsdram: pointer to a SDRAM_HandleTypeDef structure that contains
  150. * the configuration information for SDRAM module.
  151. * @retval HAL status
  152. */
  153. HAL_StatusTypeDef HAL_SDRAM_DeInit(SDRAM_HandleTypeDef *hsdram)
  154. {
  155. /* Initialize the low level hardware (MSP) */
  156. HAL_SDRAM_MspDeInit(hsdram);
  157. /* Configure the SDRAM registers with their reset values */
  158. FMC_SDRAM_DeInit(hsdram->Instance, hsdram->Init.SDBank);
  159. /* Reset the SDRAM controller state */
  160. hsdram->State = HAL_SDRAM_STATE_RESET;
  161. /* Release Lock */
  162. __HAL_UNLOCK(hsdram);
  163. return HAL_OK;
  164. }
  165. /**
  166. * @brief SDRAM MSP Init.
  167. * @param hsdram: pointer to a SDRAM_HandleTypeDef structure that contains
  168. * the configuration information for SDRAM module.
  169. * @retval None
  170. */
  171. __weak void HAL_SDRAM_MspInit(SDRAM_HandleTypeDef *hsdram)
  172. {
  173. /* Prevent unused argument(s) compilation warning */
  174. UNUSED(hsdram);
  175. /* NOTE: This function Should not be modified, when the callback is needed,
  176. the HAL_SDRAM_MspInit could be implemented in the user file
  177. */
  178. }
  179. /**
  180. * @brief SDRAM MSP DeInit.
  181. * @param hsdram: pointer to a SDRAM_HandleTypeDef structure that contains
  182. * the configuration information for SDRAM module.
  183. * @retval None
  184. */
  185. __weak void HAL_SDRAM_MspDeInit(SDRAM_HandleTypeDef *hsdram)
  186. {
  187. /* Prevent unused argument(s) compilation warning */
  188. UNUSED(hsdram);
  189. /* NOTE: This function Should not be modified, when the callback is needed,
  190. the HAL_SDRAM_MspDeInit could be implemented in the user file
  191. */
  192. }
  193. /**
  194. * @brief This function handles SDRAM refresh error interrupt request.
  195. * @param hsdram: pointer to a SDRAM_HandleTypeDef structure that contains
  196. * the configuration information for SDRAM module.
  197. * @retval HAL status
  198. */
  199. void HAL_SDRAM_IRQHandler(SDRAM_HandleTypeDef *hsdram)
  200. {
  201. /* Check SDRAM interrupt Rising edge flag */
  202. if(__FMC_SDRAM_GET_FLAG(hsdram->Instance, FMC_SDRAM_FLAG_REFRESH_IT))
  203. {
  204. /* SDRAM refresh error interrupt callback */
  205. HAL_SDRAM_RefreshErrorCallback(hsdram);
  206. /* Clear SDRAM refresh error interrupt pending bit */
  207. __FMC_SDRAM_CLEAR_FLAG(hsdram->Instance, FMC_SDRAM_FLAG_REFRESH_ERROR);
  208. }
  209. }
  210. /**
  211. * @brief SDRAM Refresh error callback.
  212. * @param hsdram: pointer to a SDRAM_HandleTypeDef structure that contains
  213. * the configuration information for SDRAM module.
  214. * @retval None
  215. */
  216. __weak void HAL_SDRAM_RefreshErrorCallback(SDRAM_HandleTypeDef *hsdram)
  217. {
  218. /* Prevent unused argument(s) compilation warning */
  219. UNUSED(hsdram);
  220. /* NOTE: This function Should not be modified, when the callback is needed,
  221. the HAL_SDRAM_RefreshErrorCallback could be implemented in the user file
  222. */
  223. }
  224. /**
  225. * @brief DMA transfer complete callback.
  226. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  227. * the configuration information for the specified DMA module.
  228. * @retval None
  229. */
  230. __weak void HAL_SDRAM_DMA_XferCpltCallback(DMA_HandleTypeDef *hdma)
  231. {
  232. /* Prevent unused argument(s) compilation warning */
  233. UNUSED(hdma);
  234. /* NOTE: This function Should not be modified, when the callback is needed,
  235. the HAL_SDRAM_DMA_XferCpltCallback could be implemented in the user file
  236. */
  237. }
  238. /**
  239. * @brief DMA transfer complete error callback.
  240. * @param hdma: DMA handle
  241. * @retval None
  242. */
  243. __weak void HAL_SDRAM_DMA_XferErrorCallback(DMA_HandleTypeDef *hdma)
  244. {
  245. /* Prevent unused argument(s) compilation warning */
  246. UNUSED(hdma);
  247. /* NOTE: This function Should not be modified, when the callback is needed,
  248. the HAL_SDRAM_DMA_XferErrorCallback could be implemented in the user file
  249. */
  250. }
  251. /**
  252. * @}
  253. */
  254. /** @defgroup SDRAM_Exported_Functions_Group2 Input and Output functions
  255. * @brief Input Output and memory control functions
  256. *
  257. @verbatim
  258. ==============================================================================
  259. ##### SDRAM Input and Output functions #####
  260. ==============================================================================
  261. [..]
  262. This section provides functions allowing to use and control the SDRAM memory
  263. @endverbatim
  264. * @{
  265. */
  266. /**
  267. * @brief Reads 8-bit data buffer from the SDRAM memory.
  268. * @param hsdram: pointer to a SDRAM_HandleTypeDef structure that contains
  269. * the configuration information for SDRAM module.
  270. * @param pAddress: Pointer to read start address
  271. * @param pDstBuffer: Pointer to destination buffer
  272. * @param BufferSize: Size of the buffer to read from memory
  273. * @retval HAL status
  274. */
  275. HAL_StatusTypeDef HAL_SDRAM_Read_8b(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint8_t *pDstBuffer, uint32_t BufferSize)
  276. {
  277. __IO uint8_t *pSdramAddress = (uint8_t *)pAddress;
  278. /* Process Locked */
  279. __HAL_LOCK(hsdram);
  280. /* Check the SDRAM controller state */
  281. if(hsdram->State == HAL_SDRAM_STATE_BUSY)
  282. {
  283. return HAL_BUSY;
  284. }
  285. else if(hsdram->State == HAL_SDRAM_STATE_PRECHARGED)
  286. {
  287. return HAL_ERROR;
  288. }
  289. /* Read data from source */
  290. for(; BufferSize != 0; BufferSize--)
  291. {
  292. *pDstBuffer = *(__IO uint8_t *)pSdramAddress;
  293. pDstBuffer++;
  294. pSdramAddress++;
  295. }
  296. /* Process Unlocked */
  297. __HAL_UNLOCK(hsdram);
  298. return HAL_OK;
  299. }
  300. /**
  301. * @brief Writes 8-bit data buffer to SDRAM memory.
  302. * @param hsdram: pointer to a SDRAM_HandleTypeDef structure that contains
  303. * the configuration information for SDRAM module.
  304. * @param pAddress: Pointer to write start address
  305. * @param pSrcBuffer: Pointer to source buffer to write
  306. * @param BufferSize: Size of the buffer to write to memory
  307. * @retval HAL status
  308. */
  309. HAL_StatusTypeDef HAL_SDRAM_Write_8b(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint8_t *pSrcBuffer, uint32_t BufferSize)
  310. {
  311. __IO uint8_t *pSdramAddress = (uint8_t *)pAddress;
  312. uint32_t tmp = 0;
  313. /* Process Locked */
  314. __HAL_LOCK(hsdram);
  315. /* Check the SDRAM controller state */
  316. tmp = hsdram->State;
  317. if(tmp == HAL_SDRAM_STATE_BUSY)
  318. {
  319. return HAL_BUSY;
  320. }
  321. else if((tmp == HAL_SDRAM_STATE_PRECHARGED) || (tmp == HAL_SDRAM_STATE_WRITE_PROTECTED))
  322. {
  323. return HAL_ERROR;
  324. }
  325. /* Write data to memory */
  326. for(; BufferSize != 0; BufferSize--)
  327. {
  328. *(__IO uint8_t *)pSdramAddress = *pSrcBuffer;
  329. pSrcBuffer++;
  330. pSdramAddress++;
  331. }
  332. /* Process Unlocked */
  333. __HAL_UNLOCK(hsdram);
  334. return HAL_OK;
  335. }
  336. /**
  337. * @brief Reads 16-bit data buffer from the SDRAM memory.
  338. * @param hsdram: pointer to a SDRAM_HandleTypeDef structure that contains
  339. * the configuration information for SDRAM module.
  340. * @param pAddress: Pointer to read start address
  341. * @param pDstBuffer: Pointer to destination buffer
  342. * @param BufferSize: Size of the buffer to read from memory
  343. * @retval HAL status
  344. */
  345. HAL_StatusTypeDef HAL_SDRAM_Read_16b(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint16_t *pDstBuffer, uint32_t BufferSize)
  346. {
  347. __IO uint16_t *pSdramAddress = (uint16_t *)pAddress;
  348. /* Process Locked */
  349. __HAL_LOCK(hsdram);
  350. /* Check the SDRAM controller state */
  351. if(hsdram->State == HAL_SDRAM_STATE_BUSY)
  352. {
  353. return HAL_BUSY;
  354. }
  355. else if(hsdram->State == HAL_SDRAM_STATE_PRECHARGED)
  356. {
  357. return HAL_ERROR;
  358. }
  359. /* Read data from source */
  360. for(; BufferSize != 0; BufferSize--)
  361. {
  362. *pDstBuffer = *(__IO uint16_t *)pSdramAddress;
  363. pDstBuffer++;
  364. pSdramAddress++;
  365. }
  366. /* Process Unlocked */
  367. __HAL_UNLOCK(hsdram);
  368. return HAL_OK;
  369. }
  370. /**
  371. * @brief Writes 16-bit data buffer to SDRAM memory.
  372. * @param hsdram: pointer to a SDRAM_HandleTypeDef structure that contains
  373. * the configuration information for SDRAM module.
  374. * @param pAddress: Pointer to write start address
  375. * @param pSrcBuffer: Pointer to source buffer to write
  376. * @param BufferSize: Size of the buffer to write to memory
  377. * @retval HAL status
  378. */
  379. HAL_StatusTypeDef HAL_SDRAM_Write_16b(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint16_t *pSrcBuffer, uint32_t BufferSize)
  380. {
  381. __IO uint16_t *pSdramAddress = (uint16_t *)pAddress;
  382. uint32_t tmp = 0;
  383. /* Process Locked */
  384. __HAL_LOCK(hsdram);
  385. /* Check the SDRAM controller state */
  386. tmp = hsdram->State;
  387. if(tmp == HAL_SDRAM_STATE_BUSY)
  388. {
  389. return HAL_BUSY;
  390. }
  391. else if((tmp == HAL_SDRAM_STATE_PRECHARGED) || (tmp == HAL_SDRAM_STATE_WRITE_PROTECTED))
  392. {
  393. return HAL_ERROR;
  394. }
  395. /* Write data to memory */
  396. for(; BufferSize != 0; BufferSize--)
  397. {
  398. *(__IO uint16_t *)pSdramAddress = *pSrcBuffer;
  399. pSrcBuffer++;
  400. pSdramAddress++;
  401. }
  402. /* Process Unlocked */
  403. __HAL_UNLOCK(hsdram);
  404. return HAL_OK;
  405. }
  406. /**
  407. * @brief Reads 32-bit data buffer from the SDRAM memory.
  408. * @param hsdram: pointer to a SDRAM_HandleTypeDef structure that contains
  409. * the configuration information for SDRAM module.
  410. * @param pAddress: Pointer to read start address
  411. * @param pDstBuffer: Pointer to destination buffer
  412. * @param BufferSize: Size of the buffer to read from memory
  413. * @retval HAL status
  414. */
  415. HAL_StatusTypeDef HAL_SDRAM_Read_32b(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint32_t *pDstBuffer, uint32_t BufferSize)
  416. {
  417. __IO uint32_t *pSdramAddress = (uint32_t *)pAddress;
  418. /* Process Locked */
  419. __HAL_LOCK(hsdram);
  420. /* Check the SDRAM controller state */
  421. if(hsdram->State == HAL_SDRAM_STATE_BUSY)
  422. {
  423. return HAL_BUSY;
  424. }
  425. else if(hsdram->State == HAL_SDRAM_STATE_PRECHARGED)
  426. {
  427. return HAL_ERROR;
  428. }
  429. /* Read data from source */
  430. for(; BufferSize != 0; BufferSize--)
  431. {
  432. *pDstBuffer = *(__IO uint32_t *)pSdramAddress;
  433. pDstBuffer++;
  434. pSdramAddress++;
  435. }
  436. /* Process Unlocked */
  437. __HAL_UNLOCK(hsdram);
  438. return HAL_OK;
  439. }
  440. /**
  441. * @brief Writes 32-bit data buffer to SDRAM memory.
  442. * @param hsdram: pointer to a SDRAM_HandleTypeDef structure that contains
  443. * the configuration information for SDRAM module.
  444. * @param pAddress: Pointer to write start address
  445. * @param pSrcBuffer: Pointer to source buffer to write
  446. * @param BufferSize: Size of the buffer to write to memory
  447. * @retval HAL status
  448. */
  449. HAL_StatusTypeDef HAL_SDRAM_Write_32b(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint32_t *pSrcBuffer, uint32_t BufferSize)
  450. {
  451. __IO uint32_t *pSdramAddress = (uint32_t *)pAddress;
  452. uint32_t tmp = 0;
  453. /* Process Locked */
  454. __HAL_LOCK(hsdram);
  455. /* Check the SDRAM controller state */
  456. tmp = hsdram->State;
  457. if(tmp == HAL_SDRAM_STATE_BUSY)
  458. {
  459. return HAL_BUSY;
  460. }
  461. else if((tmp == HAL_SDRAM_STATE_PRECHARGED) || (tmp == HAL_SDRAM_STATE_WRITE_PROTECTED))
  462. {
  463. return HAL_ERROR;
  464. }
  465. /* Write data to memory */
  466. for(; BufferSize != 0; BufferSize--)
  467. {
  468. *(__IO uint32_t *)pSdramAddress = *pSrcBuffer;
  469. pSrcBuffer++;
  470. pSdramAddress++;
  471. }
  472. /* Process Unlocked */
  473. __HAL_UNLOCK(hsdram);
  474. return HAL_OK;
  475. }
  476. /**
  477. * @brief Reads a Words data from the SDRAM memory using DMA transfer.
  478. * @param hsdram: pointer to a SDRAM_HandleTypeDef structure that contains
  479. * the configuration information for SDRAM module.
  480. * @param pAddress: Pointer to read start address
  481. * @param pDstBuffer: Pointer to destination buffer
  482. * @param BufferSize: Size of the buffer to read from memory
  483. * @retval HAL status
  484. */
  485. HAL_StatusTypeDef HAL_SDRAM_Read_DMA(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint32_t *pDstBuffer, uint32_t BufferSize)
  486. {
  487. uint32_t tmp = 0;
  488. /* Process Locked */
  489. __HAL_LOCK(hsdram);
  490. /* Check the SDRAM controller state */
  491. tmp = hsdram->State;
  492. if(tmp == HAL_SDRAM_STATE_BUSY)
  493. {
  494. return HAL_BUSY;
  495. }
  496. else if(tmp == HAL_SDRAM_STATE_PRECHARGED)
  497. {
  498. return HAL_ERROR;
  499. }
  500. /* Configure DMA user callbacks */
  501. hsdram->hdma->XferCpltCallback = HAL_SDRAM_DMA_XferCpltCallback;
  502. hsdram->hdma->XferErrorCallback = HAL_SDRAM_DMA_XferErrorCallback;
  503. /* Enable the DMA Stream */
  504. HAL_DMA_Start_IT(hsdram->hdma, (uint32_t)pAddress, (uint32_t)pDstBuffer, (uint32_t)BufferSize);
  505. /* Process Unlocked */
  506. __HAL_UNLOCK(hsdram);
  507. return HAL_OK;
  508. }
  509. /**
  510. * @brief Writes a Words data buffer to SDRAM memory using DMA transfer.
  511. * @param hsdram: pointer to a SDRAM_HandleTypeDef structure that contains
  512. * the configuration information for SDRAM module.
  513. * @param pAddress: Pointer to write start address
  514. * @param pSrcBuffer: Pointer to source buffer to write
  515. * @param BufferSize: Size of the buffer to write to memory
  516. * @retval HAL status
  517. */
  518. HAL_StatusTypeDef HAL_SDRAM_Write_DMA(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint32_t *pSrcBuffer, uint32_t BufferSize)
  519. {
  520. uint32_t tmp = 0;
  521. /* Process Locked */
  522. __HAL_LOCK(hsdram);
  523. /* Check the SDRAM controller state */
  524. tmp = hsdram->State;
  525. if(tmp == HAL_SDRAM_STATE_BUSY)
  526. {
  527. return HAL_BUSY;
  528. }
  529. else if((tmp == HAL_SDRAM_STATE_PRECHARGED) || (tmp == HAL_SDRAM_STATE_WRITE_PROTECTED))
  530. {
  531. return HAL_ERROR;
  532. }
  533. /* Configure DMA user callbacks */
  534. hsdram->hdma->XferCpltCallback = HAL_SDRAM_DMA_XferCpltCallback;
  535. hsdram->hdma->XferErrorCallback = HAL_SDRAM_DMA_XferErrorCallback;
  536. /* Enable the DMA Stream */
  537. HAL_DMA_Start_IT(hsdram->hdma, (uint32_t)pSrcBuffer, (uint32_t)pAddress, (uint32_t)BufferSize);
  538. /* Process Unlocked */
  539. __HAL_UNLOCK(hsdram);
  540. return HAL_OK;
  541. }
  542. /**
  543. * @}
  544. */
  545. /** @defgroup SDRAM_Exported_Functions_Group3 Control functions
  546. * @brief management functions
  547. *
  548. @verbatim
  549. ==============================================================================
  550. ##### SDRAM Control functions #####
  551. ==============================================================================
  552. [..]
  553. This subsection provides a set of functions allowing to control dynamically
  554. the SDRAM interface.
  555. @endverbatim
  556. * @{
  557. */
  558. /**
  559. * @brief Enables dynamically SDRAM write protection.
  560. * @param hsdram: pointer to a SDRAM_HandleTypeDef structure that contains
  561. * the configuration information for SDRAM module.
  562. * @retval HAL status
  563. */
  564. HAL_StatusTypeDef HAL_SDRAM_WriteProtection_Enable(SDRAM_HandleTypeDef *hsdram)
  565. {
  566. /* Check the SDRAM controller state */
  567. if(hsdram->State == HAL_SDRAM_STATE_BUSY)
  568. {
  569. return HAL_BUSY;
  570. }
  571. /* Update the SDRAM state */
  572. hsdram->State = HAL_SDRAM_STATE_BUSY;
  573. /* Enable write protection */
  574. FMC_SDRAM_WriteProtection_Enable(hsdram->Instance, hsdram->Init.SDBank);
  575. /* Update the SDRAM state */
  576. hsdram->State = HAL_SDRAM_STATE_WRITE_PROTECTED;
  577. return HAL_OK;
  578. }
  579. /**
  580. * @brief Disables dynamically SDRAM write protection.
  581. * @param hsdram: pointer to a SDRAM_HandleTypeDef structure that contains
  582. * the configuration information for SDRAM module.
  583. * @retval HAL status
  584. */
  585. HAL_StatusTypeDef HAL_SDRAM_WriteProtection_Disable(SDRAM_HandleTypeDef *hsdram)
  586. {
  587. /* Check the SDRAM controller state */
  588. if(hsdram->State == HAL_SDRAM_STATE_BUSY)
  589. {
  590. return HAL_BUSY;
  591. }
  592. /* Update the SDRAM state */
  593. hsdram->State = HAL_SDRAM_STATE_BUSY;
  594. /* Disable write protection */
  595. FMC_SDRAM_WriteProtection_Disable(hsdram->Instance, hsdram->Init.SDBank);
  596. /* Update the SDRAM state */
  597. hsdram->State = HAL_SDRAM_STATE_READY;
  598. return HAL_OK;
  599. }
  600. /**
  601. * @brief Sends Command to the SDRAM bank.
  602. * @param hsdram: pointer to a SDRAM_HandleTypeDef structure that contains
  603. * the configuration information for SDRAM module.
  604. * @param Command: SDRAM command structure
  605. * @param Timeout: Timeout duration
  606. * @retval HAL status
  607. */
  608. HAL_StatusTypeDef HAL_SDRAM_SendCommand(SDRAM_HandleTypeDef *hsdram, FMC_SDRAM_CommandTypeDef *Command, uint32_t Timeout)
  609. {
  610. /* Check the SDRAM controller state */
  611. if(hsdram->State == HAL_SDRAM_STATE_BUSY)
  612. {
  613. return HAL_BUSY;
  614. }
  615. /* Update the SDRAM state */
  616. hsdram->State = HAL_SDRAM_STATE_BUSY;
  617. /* Send SDRAM command */
  618. FMC_SDRAM_SendCommand(hsdram->Instance, Command, Timeout);
  619. /* Update the SDRAM controller state */
  620. if(Command->CommandMode == FMC_SDRAM_CMD_PALL)
  621. {
  622. hsdram->State = HAL_SDRAM_STATE_PRECHARGED;
  623. }
  624. else
  625. {
  626. hsdram->State = HAL_SDRAM_STATE_READY;
  627. }
  628. return HAL_OK;
  629. }
  630. /**
  631. * @brief Programs the SDRAM Memory Refresh rate.
  632. * @param hsdram: pointer to a SDRAM_HandleTypeDef structure that contains
  633. * the configuration information for SDRAM module.
  634. * @param RefreshRate: The SDRAM refresh rate value
  635. * @retval HAL status
  636. */
  637. HAL_StatusTypeDef HAL_SDRAM_ProgramRefreshRate(SDRAM_HandleTypeDef *hsdram, uint32_t RefreshRate)
  638. {
  639. /* Check the SDRAM controller state */
  640. if(hsdram->State == HAL_SDRAM_STATE_BUSY)
  641. {
  642. return HAL_BUSY;
  643. }
  644. /* Update the SDRAM state */
  645. hsdram->State = HAL_SDRAM_STATE_BUSY;
  646. /* Program the refresh rate */
  647. FMC_SDRAM_ProgramRefreshRate(hsdram->Instance ,RefreshRate);
  648. /* Update the SDRAM state */
  649. hsdram->State = HAL_SDRAM_STATE_READY;
  650. return HAL_OK;
  651. }
  652. /**
  653. * @brief Sets the Number of consecutive SDRAM Memory auto Refresh commands.
  654. * @param hsdram: pointer to a SDRAM_HandleTypeDef structure that contains
  655. * the configuration information for SDRAM module.
  656. * @param AutoRefreshNumber: The SDRAM auto Refresh number
  657. * @retval HAL status
  658. */
  659. HAL_StatusTypeDef HAL_SDRAM_SetAutoRefreshNumber(SDRAM_HandleTypeDef *hsdram, uint32_t AutoRefreshNumber)
  660. {
  661. /* Check the SDRAM controller state */
  662. if(hsdram->State == HAL_SDRAM_STATE_BUSY)
  663. {
  664. return HAL_BUSY;
  665. }
  666. /* Update the SDRAM state */
  667. hsdram->State = HAL_SDRAM_STATE_BUSY;
  668. /* Set the Auto-Refresh number */
  669. FMC_SDRAM_SetAutoRefreshNumber(hsdram->Instance ,AutoRefreshNumber);
  670. /* Update the SDRAM state */
  671. hsdram->State = HAL_SDRAM_STATE_READY;
  672. return HAL_OK;
  673. }
  674. /**
  675. * @brief Returns the SDRAM memory current mode.
  676. * @param hsdram: pointer to a SDRAM_HandleTypeDef structure that contains
  677. * the configuration information for SDRAM module.
  678. * @retval The SDRAM memory mode.
  679. */
  680. uint32_t HAL_SDRAM_GetModeStatus(SDRAM_HandleTypeDef *hsdram)
  681. {
  682. /* Return the SDRAM memory current mode */
  683. return(FMC_SDRAM_GetModeStatus(hsdram->Instance, hsdram->Init.SDBank));
  684. }
  685. /**
  686. * @}
  687. */
  688. /** @defgroup SDRAM_Exported_Functions_Group4 State functions
  689. * @brief Peripheral State functions
  690. *
  691. @verbatim
  692. ==============================================================================
  693. ##### SDRAM State functions #####
  694. ==============================================================================
  695. [..]
  696. This subsection permits to get in run-time the status of the SDRAM controller
  697. and the data flow.
  698. @endverbatim
  699. * @{
  700. */
  701. /**
  702. * @brief Returns the SDRAM state.
  703. * @param hsdram: pointer to a SDRAM_HandleTypeDef structure that contains
  704. * the configuration information for SDRAM module.
  705. * @retval HAL state
  706. */
  707. HAL_SDRAM_StateTypeDef HAL_SDRAM_GetState(SDRAM_HandleTypeDef *hsdram)
  708. {
  709. return hsdram->State;
  710. }
  711. /**
  712. * @}
  713. */
  714. /**
  715. * @}
  716. */
  717. #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F446xx || STM32F469xx || STM32F479xx */
  718. #endif /* HAL_SDRAM_MODULE_ENABLED */
  719. /**
  720. * @}
  721. */
  722. /**
  723. * @}
  724. */
  725. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/