stm32f4xx_hal_sdram.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  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. *** Callback registration ***
  51. =============================================
  52. [..]
  53. The compilation define USE_HAL_SDRAM_REGISTER_CALLBACKS when set to 1
  54. allows the user to configure dynamically the driver callbacks.
  55. Use Functions @ref HAL_SDRAM_RegisterCallback() to register a user callback,
  56. it allows to register following callbacks:
  57. (+) MspInitCallback : SDRAM MspInit.
  58. (+) MspDeInitCallback : SDRAM MspDeInit.
  59. This function takes as parameters the HAL peripheral handle, the Callback ID
  60. and a pointer to the user callback function.
  61. Use function @ref HAL_SDRAM_UnRegisterCallback() to reset a callback to the default
  62. weak (surcharged) function. It allows to reset following callbacks:
  63. (+) MspInitCallback : SDRAM MspInit.
  64. (+) MspDeInitCallback : SDRAM MspDeInit.
  65. This function) takes as parameters the HAL peripheral handle and the Callback ID.
  66. By default, after the @ref HAL_SDRAM_Init and if the state is HAL_SDRAM_STATE_RESET
  67. all callbacks are reset to the corresponding legacy weak (surcharged) functions.
  68. Exception done for MspInit and MspDeInit callbacks that are respectively
  69. reset to the legacy weak (surcharged) functions in the @ref HAL_SDRAM_Init
  70. and @ref HAL_SDRAM_DeInit only when these callbacks are null (not registered beforehand).
  71. If not, MspInit or MspDeInit are not null, the @ref HAL_SDRAM_Init and @ref HAL_SDRAM_DeInit
  72. keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
  73. Callbacks can be registered/unregistered in READY state only.
  74. Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
  75. in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
  76. during the Init/DeInit.
  77. In that case first register the MspInit/MspDeInit user callbacks
  78. using @ref HAL_SDRAM_RegisterCallback before calling @ref HAL_SDRAM_DeInit
  79. or @ref HAL_SDRAM_Init function.
  80. When The compilation define USE_HAL_SDRAM_REGISTER_CALLBACKS is set to 0 or
  81. not defined, the callback registering feature is not available
  82. and weak (surcharged) callbacks are used.
  83. @endverbatim
  84. ******************************************************************************
  85. * @attention
  86. *
  87. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  88. * All rights reserved.</center></h2>
  89. *
  90. * This software component is licensed by ST under BSD 3-Clause license,
  91. * the "License"; You may not use this file except in compliance with the
  92. * License. You may obtain a copy of the License at:
  93. * opensource.org/licenses/BSD-3-Clause
  94. *
  95. ******************************************************************************
  96. */
  97. /* Includes ------------------------------------------------------------------*/
  98. #include "stm32f4xx_hal.h"
  99. /** @addtogroup STM32F4xx_HAL_Driver
  100. * @{
  101. */
  102. /** @defgroup SDRAM SDRAM
  103. * @brief SDRAM driver modules
  104. * @{
  105. */
  106. #ifdef HAL_SDRAM_MODULE_ENABLED
  107. #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) ||\
  108. defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx)
  109. /* Private typedef -----------------------------------------------------------*/
  110. /* Private define ------------------------------------------------------------*/
  111. /* Private macro -------------------------------------------------------------*/
  112. /* Private variables ---------------------------------------------------------*/
  113. /* Private functions ---------------------------------------------------------*/
  114. /* Exported functions --------------------------------------------------------*/
  115. /** @defgroup SDRAM_Exported_Functions SDRAM Exported Functions
  116. * @{
  117. */
  118. /** @defgroup SDRAM_Exported_Functions_Group1 Initialization and de-initialization functions
  119. * @brief Initialization and Configuration functions
  120. *
  121. @verbatim
  122. ==============================================================================
  123. ##### SDRAM Initialization and de_initialization functions #####
  124. ==============================================================================
  125. [..]
  126. This section provides functions allowing to initialize/de-initialize
  127. the SDRAM memory
  128. @endverbatim
  129. * @{
  130. */
  131. /**
  132. * @brief Performs the SDRAM device initialization sequence.
  133. * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
  134. * the configuration information for SDRAM module.
  135. * @param Timing Pointer to SDRAM control timing structure
  136. * @retval HAL status
  137. */
  138. HAL_StatusTypeDef HAL_SDRAM_Init(SDRAM_HandleTypeDef *hsdram, FMC_SDRAM_TimingTypeDef *Timing)
  139. {
  140. /* Check the SDRAM handle parameter */
  141. if(hsdram == NULL)
  142. {
  143. return HAL_ERROR;
  144. }
  145. if(hsdram->State == HAL_SDRAM_STATE_RESET)
  146. {
  147. /* Allocate lock resource and initialize it */
  148. hsdram->Lock = HAL_UNLOCKED;
  149. #if (USE_HAL_SDRAM_REGISTER_CALLBACKS == 1)
  150. if(hsdram->MspInitCallback == NULL)
  151. {
  152. hsdram->MspInitCallback = HAL_SDRAM_MspInit;
  153. }
  154. hsdram->RefreshErrorCallback = HAL_SDRAM_RefreshErrorCallback;
  155. hsdram->DmaXferCpltCallback = HAL_SDRAM_DMA_XferCpltCallback;
  156. hsdram->DmaXferErrorCallback = HAL_SDRAM_DMA_XferErrorCallback;
  157. /* Init the low level hardware */
  158. hsdram->MspInitCallback(hsdram);
  159. #else
  160. /* Initialize the low level hardware (MSP) */
  161. HAL_SDRAM_MspInit(hsdram);
  162. #endif
  163. }
  164. /* Initialize the SDRAM controller state */
  165. hsdram->State = HAL_SDRAM_STATE_BUSY;
  166. /* Initialize SDRAM control Interface */
  167. FMC_SDRAM_Init(hsdram->Instance, &(hsdram->Init));
  168. /* Initialize SDRAM timing Interface */
  169. FMC_SDRAM_Timing_Init(hsdram->Instance, Timing, hsdram->Init.SDBank);
  170. /* Update the SDRAM controller state */
  171. hsdram->State = HAL_SDRAM_STATE_READY;
  172. return HAL_OK;
  173. }
  174. /**
  175. * @brief Perform the SDRAM device initialization sequence.
  176. * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
  177. * the configuration information for SDRAM module.
  178. * @retval HAL status
  179. */
  180. HAL_StatusTypeDef HAL_SDRAM_DeInit(SDRAM_HandleTypeDef *hsdram)
  181. {
  182. #if (USE_HAL_SDRAM_REGISTER_CALLBACKS == 1)
  183. if(hsdram->MspDeInitCallback == NULL)
  184. {
  185. hsdram->MspDeInitCallback = HAL_SDRAM_MspDeInit;
  186. }
  187. /* DeInit the low level hardware */
  188. hsdram->MspDeInitCallback(hsdram);
  189. #else
  190. /* Initialize the low level hardware (MSP) */
  191. HAL_SDRAM_MspDeInit(hsdram);
  192. #endif
  193. /* Configure the SDRAM registers with their reset values */
  194. FMC_SDRAM_DeInit(hsdram->Instance, hsdram->Init.SDBank);
  195. /* Reset the SDRAM controller state */
  196. hsdram->State = HAL_SDRAM_STATE_RESET;
  197. /* Release Lock */
  198. __HAL_UNLOCK(hsdram);
  199. return HAL_OK;
  200. }
  201. /**
  202. * @brief SDRAM MSP Init.
  203. * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
  204. * the configuration information for SDRAM module.
  205. * @retval None
  206. */
  207. __weak void HAL_SDRAM_MspInit(SDRAM_HandleTypeDef *hsdram)
  208. {
  209. /* Prevent unused argument(s) compilation warning */
  210. UNUSED(hsdram);
  211. /* NOTE: This function Should not be modified, when the callback is needed,
  212. the HAL_SDRAM_MspInit could be implemented in the user file
  213. */
  214. }
  215. /**
  216. * @brief SDRAM MSP DeInit.
  217. * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
  218. * the configuration information for SDRAM module.
  219. * @retval None
  220. */
  221. __weak void HAL_SDRAM_MspDeInit(SDRAM_HandleTypeDef *hsdram)
  222. {
  223. /* Prevent unused argument(s) compilation warning */
  224. UNUSED(hsdram);
  225. /* NOTE: This function Should not be modified, when the callback is needed,
  226. the HAL_SDRAM_MspDeInit could be implemented in the user file
  227. */
  228. }
  229. /**
  230. * @brief This function handles SDRAM refresh error interrupt request.
  231. * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
  232. * the configuration information for SDRAM module.
  233. * @retval HAL status
  234. */
  235. void HAL_SDRAM_IRQHandler(SDRAM_HandleTypeDef *hsdram)
  236. {
  237. /* Check SDRAM interrupt Rising edge flag */
  238. if(__FMC_SDRAM_GET_FLAG(hsdram->Instance, FMC_SDRAM_FLAG_REFRESH_IT))
  239. {
  240. /* SDRAM refresh error interrupt callback */
  241. #if (USE_HAL_SDRAM_REGISTER_CALLBACKS == 1)
  242. hsdram->RefreshErrorCallback(hsdram);
  243. #else
  244. HAL_SDRAM_RefreshErrorCallback(hsdram);
  245. #endif
  246. /* Clear SDRAM refresh error interrupt pending bit */
  247. __FMC_SDRAM_CLEAR_FLAG(hsdram->Instance, FMC_SDRAM_FLAG_REFRESH_ERROR);
  248. }
  249. }
  250. /**
  251. * @brief SDRAM Refresh error callback.
  252. * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
  253. * the configuration information for SDRAM module.
  254. * @retval None
  255. */
  256. __weak void HAL_SDRAM_RefreshErrorCallback(SDRAM_HandleTypeDef *hsdram)
  257. {
  258. /* Prevent unused argument(s) compilation warning */
  259. UNUSED(hsdram);
  260. /* NOTE: This function Should not be modified, when the callback is needed,
  261. the HAL_SDRAM_RefreshErrorCallback could be implemented in the user file
  262. */
  263. }
  264. /**
  265. * @brief DMA transfer complete callback.
  266. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  267. * the configuration information for the specified DMA module.
  268. * @retval None
  269. */
  270. __weak void HAL_SDRAM_DMA_XferCpltCallback(DMA_HandleTypeDef *hdma)
  271. {
  272. /* Prevent unused argument(s) compilation warning */
  273. UNUSED(hdma);
  274. /* NOTE: This function Should not be modified, when the callback is needed,
  275. the HAL_SDRAM_DMA_XferCpltCallback could be implemented in the user file
  276. */
  277. }
  278. /**
  279. * @brief DMA transfer complete error callback.
  280. * @param hdma DMA handle
  281. * @retval None
  282. */
  283. __weak void HAL_SDRAM_DMA_XferErrorCallback(DMA_HandleTypeDef *hdma)
  284. {
  285. /* Prevent unused argument(s) compilation warning */
  286. UNUSED(hdma);
  287. /* NOTE: This function Should not be modified, when the callback is needed,
  288. the HAL_SDRAM_DMA_XferErrorCallback could be implemented in the user file
  289. */
  290. }
  291. /**
  292. * @}
  293. */
  294. /** @defgroup SDRAM_Exported_Functions_Group2 Input and Output functions
  295. * @brief Input Output and memory control functions
  296. *
  297. @verbatim
  298. ==============================================================================
  299. ##### SDRAM Input and Output functions #####
  300. ==============================================================================
  301. [..]
  302. This section provides functions allowing to use and control the SDRAM memory
  303. @endverbatim
  304. * @{
  305. */
  306. /**
  307. * @brief Reads 8-bit data buffer from the SDRAM memory.
  308. * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
  309. * the configuration information for SDRAM module.
  310. * @param pAddress Pointer to read start address
  311. * @param pDstBuffer Pointer to destination buffer
  312. * @param BufferSize Size of the buffer to read from memory
  313. * @retval HAL status
  314. */
  315. HAL_StatusTypeDef HAL_SDRAM_Read_8b(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint8_t *pDstBuffer, uint32_t BufferSize)
  316. {
  317. __IO uint8_t *pSdramAddress = (uint8_t *)pAddress;
  318. /* Process Locked */
  319. __HAL_LOCK(hsdram);
  320. /* Check the SDRAM controller state */
  321. if(hsdram->State == HAL_SDRAM_STATE_BUSY)
  322. {
  323. return HAL_BUSY;
  324. }
  325. else if(hsdram->State == HAL_SDRAM_STATE_PRECHARGED)
  326. {
  327. return HAL_ERROR;
  328. }
  329. /* Read data from source */
  330. for(; BufferSize != 0U; BufferSize--)
  331. {
  332. *pDstBuffer = *(__IO uint8_t *)pSdramAddress;
  333. pDstBuffer++;
  334. pSdramAddress++;
  335. }
  336. /* Process Unlocked */
  337. __HAL_UNLOCK(hsdram);
  338. return HAL_OK;
  339. }
  340. /**
  341. * @brief Writes 8-bit data buffer to SDRAM memory.
  342. * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
  343. * the configuration information for SDRAM module.
  344. * @param pAddress Pointer to write start address
  345. * @param pSrcBuffer Pointer to source buffer to write
  346. * @param BufferSize Size of the buffer to write to memory
  347. * @retval HAL status
  348. */
  349. HAL_StatusTypeDef HAL_SDRAM_Write_8b(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint8_t *pSrcBuffer, uint32_t BufferSize)
  350. {
  351. __IO uint8_t *pSdramAddress = (uint8_t *)pAddress;
  352. uint32_t tmp = 0U;
  353. /* Process Locked */
  354. __HAL_LOCK(hsdram);
  355. /* Check the SDRAM controller state */
  356. tmp = hsdram->State;
  357. if(tmp == HAL_SDRAM_STATE_BUSY)
  358. {
  359. return HAL_BUSY;
  360. }
  361. else if((tmp == HAL_SDRAM_STATE_PRECHARGED) || (tmp == HAL_SDRAM_STATE_WRITE_PROTECTED))
  362. {
  363. return HAL_ERROR;
  364. }
  365. /* Write data to memory */
  366. for(; BufferSize != 0U; BufferSize--)
  367. {
  368. *(__IO uint8_t *)pSdramAddress = *pSrcBuffer;
  369. pSrcBuffer++;
  370. pSdramAddress++;
  371. }
  372. /* Process Unlocked */
  373. __HAL_UNLOCK(hsdram);
  374. return HAL_OK;
  375. }
  376. /**
  377. * @brief Reads 16-bit data buffer from the SDRAM memory.
  378. * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
  379. * the configuration information for SDRAM module.
  380. * @param pAddress Pointer to read start address
  381. * @param pDstBuffer Pointer to destination buffer
  382. * @param BufferSize Size of the buffer to read from memory
  383. * @retval HAL status
  384. */
  385. HAL_StatusTypeDef HAL_SDRAM_Read_16b(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint16_t *pDstBuffer, uint32_t BufferSize)
  386. {
  387. __IO uint16_t *pSdramAddress = (uint16_t *)pAddress;
  388. /* Process Locked */
  389. __HAL_LOCK(hsdram);
  390. /* Check the SDRAM controller state */
  391. if(hsdram->State == HAL_SDRAM_STATE_BUSY)
  392. {
  393. return HAL_BUSY;
  394. }
  395. else if(hsdram->State == HAL_SDRAM_STATE_PRECHARGED)
  396. {
  397. return HAL_ERROR;
  398. }
  399. /* Read data from source */
  400. for(; BufferSize != 0U; BufferSize--)
  401. {
  402. *pDstBuffer = *(__IO uint16_t *)pSdramAddress;
  403. pDstBuffer++;
  404. pSdramAddress++;
  405. }
  406. /* Process Unlocked */
  407. __HAL_UNLOCK(hsdram);
  408. return HAL_OK;
  409. }
  410. /**
  411. * @brief Writes 16-bit data buffer to SDRAM memory.
  412. * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
  413. * the configuration information for SDRAM module.
  414. * @param pAddress Pointer to write start address
  415. * @param pSrcBuffer Pointer to source buffer to write
  416. * @param BufferSize Size of the buffer to write to memory
  417. * @retval HAL status
  418. */
  419. HAL_StatusTypeDef HAL_SDRAM_Write_16b(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint16_t *pSrcBuffer, uint32_t BufferSize)
  420. {
  421. __IO uint16_t *pSdramAddress = (uint16_t *)pAddress;
  422. uint32_t tmp = 0U;
  423. /* Process Locked */
  424. __HAL_LOCK(hsdram);
  425. /* Check the SDRAM controller state */
  426. tmp = hsdram->State;
  427. if(tmp == HAL_SDRAM_STATE_BUSY)
  428. {
  429. return HAL_BUSY;
  430. }
  431. else if((tmp == HAL_SDRAM_STATE_PRECHARGED) || (tmp == HAL_SDRAM_STATE_WRITE_PROTECTED))
  432. {
  433. return HAL_ERROR;
  434. }
  435. /* Write data to memory */
  436. for(; BufferSize != 0U; BufferSize--)
  437. {
  438. *(__IO uint16_t *)pSdramAddress = *pSrcBuffer;
  439. pSrcBuffer++;
  440. pSdramAddress++;
  441. }
  442. /* Process Unlocked */
  443. __HAL_UNLOCK(hsdram);
  444. return HAL_OK;
  445. }
  446. /**
  447. * @brief Reads 32-bit data buffer from the SDRAM memory.
  448. * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
  449. * the configuration information for SDRAM module.
  450. * @param pAddress Pointer to read start address
  451. * @param pDstBuffer Pointer to destination buffer
  452. * @param BufferSize Size of the buffer to read from memory
  453. * @retval HAL status
  454. */
  455. HAL_StatusTypeDef HAL_SDRAM_Read_32b(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint32_t *pDstBuffer, uint32_t BufferSize)
  456. {
  457. __IO uint32_t *pSdramAddress = (uint32_t *)pAddress;
  458. /* Process Locked */
  459. __HAL_LOCK(hsdram);
  460. /* Check the SDRAM controller state */
  461. if(hsdram->State == HAL_SDRAM_STATE_BUSY)
  462. {
  463. return HAL_BUSY;
  464. }
  465. else if(hsdram->State == HAL_SDRAM_STATE_PRECHARGED)
  466. {
  467. return HAL_ERROR;
  468. }
  469. /* Read data from source */
  470. for(; BufferSize != 0U; BufferSize--)
  471. {
  472. *pDstBuffer = *(__IO uint32_t *)pSdramAddress;
  473. pDstBuffer++;
  474. pSdramAddress++;
  475. }
  476. /* Process Unlocked */
  477. __HAL_UNLOCK(hsdram);
  478. return HAL_OK;
  479. }
  480. /**
  481. * @brief Writes 32-bit data buffer to SDRAM memory.
  482. * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
  483. * the configuration information for SDRAM module.
  484. * @param pAddress Pointer to write start address
  485. * @param pSrcBuffer Pointer to source buffer to write
  486. * @param BufferSize Size of the buffer to write to memory
  487. * @retval HAL status
  488. */
  489. HAL_StatusTypeDef HAL_SDRAM_Write_32b(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint32_t *pSrcBuffer, uint32_t BufferSize)
  490. {
  491. __IO uint32_t *pSdramAddress = (uint32_t *)pAddress;
  492. uint32_t tmp = 0U;
  493. /* Process Locked */
  494. __HAL_LOCK(hsdram);
  495. /* Check the SDRAM controller state */
  496. tmp = hsdram->State;
  497. if(tmp == HAL_SDRAM_STATE_BUSY)
  498. {
  499. return HAL_BUSY;
  500. }
  501. else if((tmp == HAL_SDRAM_STATE_PRECHARGED) || (tmp == HAL_SDRAM_STATE_WRITE_PROTECTED))
  502. {
  503. return HAL_ERROR;
  504. }
  505. /* Write data to memory */
  506. for(; BufferSize != 0U; BufferSize--)
  507. {
  508. *(__IO uint32_t *)pSdramAddress = *pSrcBuffer;
  509. pSrcBuffer++;
  510. pSdramAddress++;
  511. }
  512. /* Process Unlocked */
  513. __HAL_UNLOCK(hsdram);
  514. return HAL_OK;
  515. }
  516. /**
  517. * @brief Reads a Words data from the SDRAM memory using DMA transfer.
  518. * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
  519. * the configuration information for SDRAM module.
  520. * @param pAddress Pointer to read start address
  521. * @param pDstBuffer Pointer to destination buffer
  522. * @param BufferSize Size of the buffer to read from memory
  523. * @retval HAL status
  524. */
  525. HAL_StatusTypeDef HAL_SDRAM_Read_DMA(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint32_t *pDstBuffer, uint32_t BufferSize)
  526. {
  527. uint32_t tmp = 0U;
  528. /* Process Locked */
  529. __HAL_LOCK(hsdram);
  530. /* Check the SDRAM controller state */
  531. tmp = hsdram->State;
  532. if(tmp == HAL_SDRAM_STATE_BUSY)
  533. {
  534. return HAL_BUSY;
  535. }
  536. else if(tmp == HAL_SDRAM_STATE_PRECHARGED)
  537. {
  538. return HAL_ERROR;
  539. }
  540. /* Configure DMA user callbacks */
  541. hsdram->hdma->XferCpltCallback = HAL_SDRAM_DMA_XferCpltCallback;
  542. hsdram->hdma->XferErrorCallback = HAL_SDRAM_DMA_XferErrorCallback;
  543. /* Enable the DMA Stream */
  544. HAL_DMA_Start_IT(hsdram->hdma, (uint32_t)pAddress, (uint32_t)pDstBuffer, (uint32_t)BufferSize);
  545. /* Process Unlocked */
  546. __HAL_UNLOCK(hsdram);
  547. return HAL_OK;
  548. }
  549. /**
  550. * @brief Writes a Words data buffer to SDRAM memory using DMA transfer.
  551. * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
  552. * the configuration information for SDRAM module.
  553. * @param pAddress Pointer to write start address
  554. * @param pSrcBuffer Pointer to source buffer to write
  555. * @param BufferSize Size of the buffer to write to memory
  556. * @retval HAL status
  557. */
  558. HAL_StatusTypeDef HAL_SDRAM_Write_DMA(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint32_t *pSrcBuffer, uint32_t BufferSize)
  559. {
  560. uint32_t tmp = 0U;
  561. /* Process Locked */
  562. __HAL_LOCK(hsdram);
  563. /* Check the SDRAM controller state */
  564. tmp = hsdram->State;
  565. if(tmp == HAL_SDRAM_STATE_BUSY)
  566. {
  567. return HAL_BUSY;
  568. }
  569. else if((tmp == HAL_SDRAM_STATE_PRECHARGED) || (tmp == HAL_SDRAM_STATE_WRITE_PROTECTED))
  570. {
  571. return HAL_ERROR;
  572. }
  573. /* Configure DMA user callbacks */
  574. hsdram->hdma->XferCpltCallback = HAL_SDRAM_DMA_XferCpltCallback;
  575. hsdram->hdma->XferErrorCallback = HAL_SDRAM_DMA_XferErrorCallback;
  576. /* Enable the DMA Stream */
  577. HAL_DMA_Start_IT(hsdram->hdma, (uint32_t)pSrcBuffer, (uint32_t)pAddress, (uint32_t)BufferSize);
  578. /* Process Unlocked */
  579. __HAL_UNLOCK(hsdram);
  580. return HAL_OK;
  581. }
  582. #if (USE_HAL_SDRAM_REGISTER_CALLBACKS == 1)
  583. /**
  584. * @brief Register a User SDRAM Callback
  585. * To be used instead of the weak (surcharged) predefined callback
  586. * @param hsdram : SDRAM handle
  587. * @param CallbackId : ID of the callback to be registered
  588. * This parameter can be one of the following values:
  589. * @arg @ref HAL_SDRAM_MSP_INIT_CB_ID SDRAM MspInit callback ID
  590. * @arg @ref HAL_SDRAM_MSP_DEINIT_CB_ID SDRAM MspDeInit callback ID
  591. * @arg @ref HAL_SDRAM_REFRESH_ERR_CB_ID SDRAM Refresh Error callback ID
  592. * @param pCallback : pointer to the Callback function
  593. * @retval status
  594. */
  595. HAL_StatusTypeDef HAL_SDRAM_RegisterCallback (SDRAM_HandleTypeDef *hsdram, HAL_SDRAM_CallbackIDTypeDef CallbackId, pSDRAM_CallbackTypeDef pCallback)
  596. {
  597. HAL_StatusTypeDef status = HAL_OK;
  598. HAL_SDRAM_StateTypeDef state;
  599. if(pCallback == NULL)
  600. {
  601. return HAL_ERROR;
  602. }
  603. /* Process locked */
  604. __HAL_LOCK(hsdram);
  605. state = hsdram->State;
  606. if((state == HAL_SDRAM_STATE_READY) || (state == HAL_SDRAM_STATE_WRITE_PROTECTED))
  607. {
  608. switch (CallbackId)
  609. {
  610. case HAL_SDRAM_MSP_INIT_CB_ID :
  611. hsdram->MspInitCallback = pCallback;
  612. break;
  613. case HAL_SDRAM_MSP_DEINIT_CB_ID :
  614. hsdram->MspDeInitCallback = pCallback;
  615. break;
  616. case HAL_SDRAM_REFRESH_ERR_CB_ID :
  617. hsdram->RefreshErrorCallback = pCallback;
  618. break;
  619. default :
  620. /* update return status */
  621. status = HAL_ERROR;
  622. break;
  623. }
  624. }
  625. else if(hsdram->State == HAL_SDRAM_STATE_RESET)
  626. {
  627. switch (CallbackId)
  628. {
  629. case HAL_SDRAM_MSP_INIT_CB_ID :
  630. hsdram->MspInitCallback = pCallback;
  631. break;
  632. case HAL_SDRAM_MSP_DEINIT_CB_ID :
  633. hsdram->MspDeInitCallback = pCallback;
  634. break;
  635. default :
  636. /* update return status */
  637. status = HAL_ERROR;
  638. break;
  639. }
  640. }
  641. else
  642. {
  643. /* update return status */
  644. status = HAL_ERROR;
  645. }
  646. /* Release Lock */
  647. __HAL_UNLOCK(hsdram);
  648. return status;
  649. }
  650. /**
  651. * @brief Unregister a User SDRAM Callback
  652. * SDRAM Callback is redirected to the weak (surcharged) predefined callback
  653. * @param hsdram : SDRAM handle
  654. * @param CallbackId : ID of the callback to be unregistered
  655. * This parameter can be one of the following values:
  656. * @arg @ref HAL_SDRAM_MSP_INIT_CB_ID SDRAM MspInit callback ID
  657. * @arg @ref HAL_SDRAM_MSP_DEINIT_CB_ID SDRAM MspDeInit callback ID
  658. * @arg @ref HAL_SDRAM_REFRESH_ERR_CB_ID SDRAM Refresh Error callback ID
  659. * @arg @ref HAL_SDRAM_DMA_XFER_CPLT_CB_ID SDRAM DMA Xfer Complete callback ID
  660. * @arg @ref HAL_SDRAM_DMA_XFER_ERR_CB_ID SDRAM DMA Xfer Error callback ID
  661. * @retval status
  662. */
  663. HAL_StatusTypeDef HAL_SDRAM_UnRegisterCallback (SDRAM_HandleTypeDef *hsdram, HAL_SDRAM_CallbackIDTypeDef CallbackId)
  664. {
  665. HAL_StatusTypeDef status = HAL_OK;
  666. HAL_SDRAM_StateTypeDef state;
  667. /* Process locked */
  668. __HAL_LOCK(hsdram);
  669. state = hsdram->State;
  670. if((state == HAL_SDRAM_STATE_READY) || (state == HAL_SDRAM_STATE_WRITE_PROTECTED))
  671. {
  672. switch (CallbackId)
  673. {
  674. case HAL_SDRAM_MSP_INIT_CB_ID :
  675. hsdram->MspInitCallback = HAL_SDRAM_MspInit;
  676. break;
  677. case HAL_SDRAM_MSP_DEINIT_CB_ID :
  678. hsdram->MspDeInitCallback = HAL_SDRAM_MspDeInit;
  679. break;
  680. case HAL_SDRAM_REFRESH_ERR_CB_ID :
  681. hsdram->RefreshErrorCallback = HAL_SDRAM_RefreshErrorCallback;
  682. break;
  683. case HAL_SDRAM_DMA_XFER_CPLT_CB_ID :
  684. hsdram->DmaXferCpltCallback = HAL_SDRAM_DMA_XferCpltCallback;
  685. break;
  686. case HAL_SDRAM_DMA_XFER_ERR_CB_ID :
  687. hsdram->DmaXferErrorCallback = HAL_SDRAM_DMA_XferErrorCallback;
  688. break;
  689. default :
  690. /* update return status */
  691. status = HAL_ERROR;
  692. break;
  693. }
  694. }
  695. else if(hsdram->State == HAL_SDRAM_STATE_RESET)
  696. {
  697. switch (CallbackId)
  698. {
  699. case HAL_SDRAM_MSP_INIT_CB_ID :
  700. hsdram->MspInitCallback = HAL_SDRAM_MspInit;
  701. break;
  702. case HAL_SDRAM_MSP_DEINIT_CB_ID :
  703. hsdram->MspDeInitCallback = HAL_SDRAM_MspDeInit;
  704. break;
  705. default :
  706. /* update return status */
  707. status = HAL_ERROR;
  708. break;
  709. }
  710. }
  711. else
  712. {
  713. /* update return status */
  714. status = HAL_ERROR;
  715. }
  716. /* Release Lock */
  717. __HAL_UNLOCK(hsdram);
  718. return status;
  719. }
  720. /**
  721. * @brief Register a User SDRAM Callback for DMA transfers
  722. * To be used instead of the weak (surcharged) predefined callback
  723. * @param hsdram : SDRAM handle
  724. * @param CallbackId : ID of the callback to be registered
  725. * This parameter can be one of the following values:
  726. * @arg @ref HAL_SDRAM_DMA_XFER_CPLT_CB_ID SDRAM DMA Xfer Complete callback ID
  727. * @arg @ref HAL_SDRAM_DMA_XFER_ERR_CB_ID SDRAM DMA Xfer Error callback ID
  728. * @param pCallback : pointer to the Callback function
  729. * @retval status
  730. */
  731. HAL_StatusTypeDef HAL_SDRAM_RegisterDmaCallback(SDRAM_HandleTypeDef *hsdram, HAL_SDRAM_CallbackIDTypeDef CallbackId, pSDRAM_DmaCallbackTypeDef pCallback)
  732. {
  733. HAL_StatusTypeDef status = HAL_OK;
  734. HAL_SDRAM_StateTypeDef state;
  735. if(pCallback == NULL)
  736. {
  737. return HAL_ERROR;
  738. }
  739. /* Process locked */
  740. __HAL_LOCK(hsdram);
  741. state = hsdram->State;
  742. if((state == HAL_SDRAM_STATE_READY) || (state == HAL_SDRAM_STATE_WRITE_PROTECTED))
  743. {
  744. switch (CallbackId)
  745. {
  746. case HAL_SDRAM_DMA_XFER_CPLT_CB_ID :
  747. hsdram->DmaXferCpltCallback = pCallback;
  748. break;
  749. case HAL_SDRAM_DMA_XFER_ERR_CB_ID :
  750. hsdram->DmaXferErrorCallback = pCallback;
  751. break;
  752. default :
  753. /* update return status */
  754. status = HAL_ERROR;
  755. break;
  756. }
  757. }
  758. else
  759. {
  760. /* update return status */
  761. status = HAL_ERROR;
  762. }
  763. /* Release Lock */
  764. __HAL_UNLOCK(hsdram);
  765. return status;
  766. }
  767. #endif
  768. /**
  769. * @}
  770. */
  771. /** @defgroup SDRAM_Exported_Functions_Group3 Control functions
  772. * @brief management functions
  773. *
  774. @verbatim
  775. ==============================================================================
  776. ##### SDRAM Control functions #####
  777. ==============================================================================
  778. [..]
  779. This subsection provides a set of functions allowing to control dynamically
  780. the SDRAM interface.
  781. @endverbatim
  782. * @{
  783. */
  784. /**
  785. * @brief Enables dynamically SDRAM write protection.
  786. * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
  787. * the configuration information for SDRAM module.
  788. * @retval HAL status
  789. */
  790. HAL_StatusTypeDef HAL_SDRAM_WriteProtection_Enable(SDRAM_HandleTypeDef *hsdram)
  791. {
  792. /* Check the SDRAM controller state */
  793. if(hsdram->State == HAL_SDRAM_STATE_BUSY)
  794. {
  795. return HAL_BUSY;
  796. }
  797. /* Update the SDRAM state */
  798. hsdram->State = HAL_SDRAM_STATE_BUSY;
  799. /* Enable write protection */
  800. FMC_SDRAM_WriteProtection_Enable(hsdram->Instance, hsdram->Init.SDBank);
  801. /* Update the SDRAM state */
  802. hsdram->State = HAL_SDRAM_STATE_WRITE_PROTECTED;
  803. return HAL_OK;
  804. }
  805. /**
  806. * @brief Disables dynamically SDRAM write protection.
  807. * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
  808. * the configuration information for SDRAM module.
  809. * @retval HAL status
  810. */
  811. HAL_StatusTypeDef HAL_SDRAM_WriteProtection_Disable(SDRAM_HandleTypeDef *hsdram)
  812. {
  813. /* Check the SDRAM controller state */
  814. if(hsdram->State == HAL_SDRAM_STATE_BUSY)
  815. {
  816. return HAL_BUSY;
  817. }
  818. /* Update the SDRAM state */
  819. hsdram->State = HAL_SDRAM_STATE_BUSY;
  820. /* Disable write protection */
  821. FMC_SDRAM_WriteProtection_Disable(hsdram->Instance, hsdram->Init.SDBank);
  822. /* Update the SDRAM state */
  823. hsdram->State = HAL_SDRAM_STATE_READY;
  824. return HAL_OK;
  825. }
  826. /**
  827. * @brief Sends Command to the SDRAM bank.
  828. * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
  829. * the configuration information for SDRAM module.
  830. * @param Command SDRAM command structure
  831. * @param Timeout Timeout duration
  832. * @retval HAL status
  833. */
  834. HAL_StatusTypeDef HAL_SDRAM_SendCommand(SDRAM_HandleTypeDef *hsdram, FMC_SDRAM_CommandTypeDef *Command, uint32_t Timeout)
  835. {
  836. /* Check the SDRAM controller state */
  837. if(hsdram->State == HAL_SDRAM_STATE_BUSY)
  838. {
  839. return HAL_BUSY;
  840. }
  841. /* Update the SDRAM state */
  842. hsdram->State = HAL_SDRAM_STATE_BUSY;
  843. /* Send SDRAM command */
  844. FMC_SDRAM_SendCommand(hsdram->Instance, Command, Timeout);
  845. /* Update the SDRAM controller state */
  846. if(Command->CommandMode == FMC_SDRAM_CMD_PALL)
  847. {
  848. hsdram->State = HAL_SDRAM_STATE_PRECHARGED;
  849. }
  850. else
  851. {
  852. hsdram->State = HAL_SDRAM_STATE_READY;
  853. }
  854. return HAL_OK;
  855. }
  856. /**
  857. * @brief Programs the SDRAM Memory Refresh rate.
  858. * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
  859. * the configuration information for SDRAM module.
  860. * @param RefreshRate The SDRAM refresh rate value
  861. * @retval HAL status
  862. */
  863. HAL_StatusTypeDef HAL_SDRAM_ProgramRefreshRate(SDRAM_HandleTypeDef *hsdram, uint32_t RefreshRate)
  864. {
  865. /* Check the SDRAM controller state */
  866. if(hsdram->State == HAL_SDRAM_STATE_BUSY)
  867. {
  868. return HAL_BUSY;
  869. }
  870. /* Update the SDRAM state */
  871. hsdram->State = HAL_SDRAM_STATE_BUSY;
  872. /* Program the refresh rate */
  873. FMC_SDRAM_ProgramRefreshRate(hsdram->Instance ,RefreshRate);
  874. /* Update the SDRAM state */
  875. hsdram->State = HAL_SDRAM_STATE_READY;
  876. return HAL_OK;
  877. }
  878. /**
  879. * @brief Sets the Number of consecutive SDRAM Memory auto Refresh commands.
  880. * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
  881. * the configuration information for SDRAM module.
  882. * @param AutoRefreshNumber The SDRAM auto Refresh number
  883. * @retval HAL status
  884. */
  885. HAL_StatusTypeDef HAL_SDRAM_SetAutoRefreshNumber(SDRAM_HandleTypeDef *hsdram, uint32_t AutoRefreshNumber)
  886. {
  887. /* Check the SDRAM controller state */
  888. if(hsdram->State == HAL_SDRAM_STATE_BUSY)
  889. {
  890. return HAL_BUSY;
  891. }
  892. /* Update the SDRAM state */
  893. hsdram->State = HAL_SDRAM_STATE_BUSY;
  894. /* Set the Auto-Refresh number */
  895. FMC_SDRAM_SetAutoRefreshNumber(hsdram->Instance ,AutoRefreshNumber);
  896. /* Update the SDRAM state */
  897. hsdram->State = HAL_SDRAM_STATE_READY;
  898. return HAL_OK;
  899. }
  900. /**
  901. * @brief Returns the SDRAM memory current mode.
  902. * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
  903. * the configuration information for SDRAM module.
  904. * @retval The SDRAM memory mode.
  905. */
  906. uint32_t HAL_SDRAM_GetModeStatus(SDRAM_HandleTypeDef *hsdram)
  907. {
  908. /* Return the SDRAM memory current mode */
  909. return(FMC_SDRAM_GetModeStatus(hsdram->Instance, hsdram->Init.SDBank));
  910. }
  911. /**
  912. * @}
  913. */
  914. /** @defgroup SDRAM_Exported_Functions_Group4 State functions
  915. * @brief Peripheral State functions
  916. *
  917. @verbatim
  918. ==============================================================================
  919. ##### SDRAM State functions #####
  920. ==============================================================================
  921. [..]
  922. This subsection permits to get in run-time the status of the SDRAM controller
  923. and the data flow.
  924. @endverbatim
  925. * @{
  926. */
  927. /**
  928. * @brief Returns the SDRAM state.
  929. * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
  930. * the configuration information for SDRAM module.
  931. * @retval HAL state
  932. */
  933. HAL_SDRAM_StateTypeDef HAL_SDRAM_GetState(SDRAM_HandleTypeDef *hsdram)
  934. {
  935. return hsdram->State;
  936. }
  937. /**
  938. * @}
  939. */
  940. /**
  941. * @}
  942. */
  943. #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F446xx || STM32F469xx || STM32F479xx */
  944. #endif /* HAL_SDRAM_MODULE_ENABLED */
  945. /**
  946. * @}
  947. */
  948. /**
  949. * @}
  950. */
  951. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/