Create release.yml
[betaflight.git] / lib / main / STM32H7 / Drivers / STM32H7xx_HAL_Driver / Src / stm32h7xx_hal_sdram.c
blob8d71bc2b199216af581f2bfc15c0648e2a13ae1a
1 /**
2 ******************************************************************************
3 * @file stm32h7xx_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.
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:
20 (#) Declare a SDRAM_HandleTypeDef handle structure, for example:
21 SDRAM_HandleTypeDef hsdram
23 (++) Fill the SDRAM_HandleTypeDef handle "Init" field with the allowed
24 values of the structure member.
26 (++) Fill the SDRAM_HandleTypeDef handle "Instance" field with a predefined
27 base register instance for NOR or SDRAM device
29 (#) Declare a FMC_SDRAM_TimingTypeDef structure; for example:
30 FMC_SDRAM_TimingTypeDef Timing;
31 and fill its fields with the allowed values of the structure member.
33 (#) Initialize the SDRAM Controller by calling the function HAL_SDRAM_Init(). This function
34 performs the following sequence:
36 (##) MSP hardware layer configuration using the function HAL_SDRAM_MspInit()
37 (##) Control register configuration using the FMC SDRAM interface function
38 FMC_SDRAM_Init()
39 (##) Timing register configuration using the FMC SDRAM interface function
40 FMC_SDRAM_Timing_Init()
41 (##) Program the SDRAM external device by applying its initialization sequence
42 according to the device plugged in your hardware. This step is mandatory
43 for accessing the SDRAM device.
45 (#) At this stage you can perform read/write accesses from/to the memory connected
46 to the SDRAM Bank. You can perform either polling or DMA transfer using the
47 following APIs:
48 (++) HAL_SDRAM_Read()/HAL_SDRAM_Write() for polling read/write access
49 (++) HAL_SDRAM_Read_DMA()/HAL_SDRAM_Write_DMA() for DMA read/write transfer
51 (#) You can also control the SDRAM device by calling the control APIs HAL_SDRAM_WriteOperation_Enable()/
52 HAL_SDRAM_WriteOperation_Disable() to respectively enable/disable the SDRAM write operation or
53 the function HAL_SDRAM_SendCommand() to send a specified command to the SDRAM
54 device. The command to be sent must be configured with the FMC_SDRAM_CommandTypeDef
55 structure.
57 (#) You can continuously monitor the SDRAM device HAL state by calling the function
58 HAL_SDRAM_GetState()
60 *** Callback registration ***
61 =============================================
62 [..]
63 The compilation define USE_HAL_SDRAM_REGISTER_CALLBACKS when set to 1
64 allows the user to configure dynamically the driver callbacks.
66 Use Functions @ref HAL_SDRAM_RegisterCallback() to register a user callback,
67 it allows to register following callbacks:
68 (+) MspInitCallback : SDRAM MspInit.
69 (+) MspDeInitCallback : SDRAM MspDeInit.
70 This function takes as parameters the HAL peripheral handle, the Callback ID
71 and a pointer to the user callback function.
73 Use function @ref HAL_SDRAM_UnRegisterCallback() to reset a callback to the default
74 weak (surcharged) function. It allows to reset following callbacks:
75 (+) MspInitCallback : SDRAM MspInit.
76 (+) MspDeInitCallback : SDRAM MspDeInit.
77 This function) takes as parameters the HAL peripheral handle and the Callback ID.
79 By default, after the @ref HAL_SDRAM_Init and if the state is HAL_SDRAM_STATE_RESET
80 all callbacks are reset to the corresponding legacy weak (surcharged) functions.
81 Exception done for MspInit and MspDeInit callbacks that are respectively
82 reset to the legacy weak (surcharged) functions in the @ref HAL_SDRAM_Init
83 and @ref HAL_SDRAM_DeInit only when these callbacks are null (not registered beforehand).
84 If not, MspInit or MspDeInit are not null, the @ref HAL_SDRAM_Init and @ref HAL_SDRAM_DeInit
85 keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
87 Callbacks can be registered/unregistered in READY state only.
88 Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
89 in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
90 during the Init/DeInit.
91 In that case first register the MspInit/MspDeInit user callbacks
92 using @ref HAL_SDRAM_RegisterCallback before calling @ref HAL_SDRAM_DeInit
93 or @ref HAL_SDRAM_Init function.
95 When The compilation define USE_HAL_SDRAM_REGISTER_CALLBACKS is set to 0 or
96 not defined, the callback registering feature is not available
97 and weak (surcharged) callbacks are used.
99 @endverbatim
100 ******************************************************************************
101 * @attention
103 * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
104 * All rights reserved.</center></h2>
106 * This software component is licensed by ST under BSD 3-Clause license,
107 * the "License"; You may not use this file except in compliance with the
108 * License. You may obtain a copy of the License at:
109 * opensource.org/licenses/BSD-3-Clause
111 ******************************************************************************
114 /* Includes ------------------------------------------------------------------*/
115 #include "stm32h7xx_hal.h"
118 /** @addtogroup STM32H7xx_HAL_Driver
119 * @{
122 #ifdef HAL_SDRAM_MODULE_ENABLED
124 /** @defgroup SDRAM SDRAM
125 * @brief SDRAM driver modules
126 * @{
130 @cond 0
132 /* Private typedef -----------------------------------------------------------*/
133 /* Private define ------------------------------------------------------------*/
134 /* Private macro -------------------------------------------------------------*/
135 /* Private variables ---------------------------------------------------------*/
136 /* Private function prototypes -----------------------------------------------*/
137 static void SDRAM_DMACplt (MDMA_HandleTypeDef *hmdma);
138 static void SDRAM_DMACpltProt(MDMA_HandleTypeDef *hmdma);
139 static void SDRAM_DMAError (MDMA_HandleTypeDef *hmdma);
141 @endcond
144 /* Exported functions --------------------------------------------------------*/
145 /** @defgroup SDRAM_Exported_Functions SDRAM Exported Functions
146 * @{
149 /** @defgroup SDRAM_Exported_Functions_Group1 Initialization and de-initialization functions
150 * @brief Initialization and Configuration functions
152 @verbatim
153 ==============================================================================
154 ##### SDRAM Initialization and de_initialization functions #####
155 ==============================================================================
156 [..]
157 This section provides functions allowing to initialize/de-initialize
158 the SDRAM memory
160 @endverbatim
161 * @{
165 * @brief Performs the SDRAM device initialization sequence.
166 * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
167 * the configuration information for SDRAM module.
168 * @param Timing Pointer to SDRAM control timing structure
169 * @retval HAL status
171 HAL_StatusTypeDef HAL_SDRAM_Init(SDRAM_HandleTypeDef *hsdram, FMC_SDRAM_TimingTypeDef *Timing)
173 /* Check the SDRAM handle parameter */
174 if (hsdram == NULL)
176 return HAL_ERROR;
179 if (hsdram->State == HAL_SDRAM_STATE_RESET)
181 /* Allocate lock resource and initialize it */
182 hsdram->Lock = HAL_UNLOCKED;
183 #if (USE_HAL_SDRAM_REGISTER_CALLBACKS == 1)
184 if(hsdram->MspInitCallback == NULL)
186 hsdram->MspInitCallback = HAL_SDRAM_MspInit;
188 hsdram->RefreshErrorCallback = HAL_SDRAM_RefreshErrorCallback;
189 hsdram->DmaXferCpltCallback = HAL_SDRAM_DMA_XferCpltCallback;
190 hsdram->DmaXferErrorCallback = HAL_SDRAM_DMA_XferErrorCallback;
192 /* Init the low level hardware */
193 hsdram->MspInitCallback(hsdram);
194 #else
195 /* Initialize the low level hardware (MSP) */
196 HAL_SDRAM_MspInit(hsdram);
197 #endif
200 /* Initialize the SDRAM controller state */
201 hsdram->State = HAL_SDRAM_STATE_BUSY;
203 /* Initialize SDRAM control Interface */
204 (void)FMC_SDRAM_Init(hsdram->Instance, &(hsdram->Init));
206 /* Initialize SDRAM timing Interface */
207 (void)FMC_SDRAM_Timing_Init(hsdram->Instance, Timing, hsdram->Init.SDBank);
209 /* Enable FMC Peripheral */
210 __FMC_ENABLE();
211 /* Update the SDRAM controller state */
212 hsdram->State = HAL_SDRAM_STATE_READY;
214 return HAL_OK;
218 * @brief Perform the SDRAM device initialization sequence.
219 * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
220 * the configuration information for SDRAM module.
221 * @retval HAL status
223 HAL_StatusTypeDef HAL_SDRAM_DeInit(SDRAM_HandleTypeDef *hsdram)
225 #if (USE_HAL_SDRAM_REGISTER_CALLBACKS == 1)
226 if(hsdram->MspDeInitCallback == NULL)
228 hsdram->MspDeInitCallback = HAL_SDRAM_MspDeInit;
231 /* DeInit the low level hardware */
232 hsdram->MspDeInitCallback(hsdram);
233 #else
234 /* Initialize the low level hardware (MSP) */
235 HAL_SDRAM_MspDeInit(hsdram);
236 #endif
238 /* Configure the SDRAM registers with their reset values */
239 (void)FMC_SDRAM_DeInit(hsdram->Instance, hsdram->Init.SDBank);
241 /* Reset the SDRAM controller state */
242 hsdram->State = HAL_SDRAM_STATE_RESET;
244 /* Release Lock */
245 __HAL_UNLOCK(hsdram);
247 return HAL_OK;
251 * @brief SDRAM MSP Init.
252 * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
253 * the configuration information for SDRAM module.
254 * @retval None
256 __weak void HAL_SDRAM_MspInit(SDRAM_HandleTypeDef *hsdram)
258 /* Prevent unused argument(s) compilation warning */
259 UNUSED(hsdram);
261 /* NOTE: This function Should not be modified, when the callback is needed,
262 the HAL_SDRAM_MspInit could be implemented in the user file
267 * @brief SDRAM MSP DeInit.
268 * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
269 * the configuration information for SDRAM module.
270 * @retval None
272 __weak void HAL_SDRAM_MspDeInit(SDRAM_HandleTypeDef *hsdram)
274 /* Prevent unused argument(s) compilation warning */
275 UNUSED(hsdram);
277 /* NOTE: This function Should not be modified, when the callback is needed,
278 the HAL_SDRAM_MspDeInit could be implemented in the user file
283 * @brief This function handles SDRAM refresh error interrupt request.
284 * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
285 * the configuration information for SDRAM module.
286 * @retval HAL status
288 void HAL_SDRAM_IRQHandler(SDRAM_HandleTypeDef *hsdram)
290 /* Check SDRAM interrupt Rising edge flag */
291 if (__FMC_SDRAM_GET_FLAG(hsdram->Instance, FMC_SDRAM_FLAG_REFRESH_IT))
293 /* SDRAM refresh error interrupt callback */
294 #if (USE_HAL_SDRAM_REGISTER_CALLBACKS == 1)
295 hsdram->RefreshErrorCallback(hsdram);
296 #else
297 HAL_SDRAM_RefreshErrorCallback(hsdram);
298 #endif
300 /* Clear SDRAM refresh error interrupt pending bit */
301 __FMC_SDRAM_CLEAR_FLAG(hsdram->Instance, FMC_SDRAM_FLAG_REFRESH_ERROR);
306 * @brief SDRAM Refresh error callback.
307 * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
308 * the configuration information for SDRAM module.
309 * @retval None
311 __weak void HAL_SDRAM_RefreshErrorCallback(SDRAM_HandleTypeDef *hsdram)
313 /* Prevent unused argument(s) compilation warning */
314 UNUSED(hsdram);
316 /* NOTE: This function Should not be modified, when the callback is needed,
317 the HAL_SDRAM_RefreshErrorCallback could be implemented in the user file
322 * @brief DMA transfer complete callback.
323 * @param hmdma pointer to a DMA_HandleTypeDef structure that contains
324 * the configuration information for the specified DMA module.
325 * @retval None
327 __weak void HAL_SDRAM_DMA_XferCpltCallback(MDMA_HandleTypeDef *hmdma)
329 /* Prevent unused argument(s) compilation warning */
330 UNUSED(hmdma);
332 /* NOTE: This function Should not be modified, when the callback is needed,
333 the HAL_SDRAM_DMA_XferCpltCallback could be implemented in the user file
338 * @brief DMA transfer complete error callback.
339 * @param hmdma DMA handle
340 * @retval None
342 __weak void HAL_SDRAM_DMA_XferErrorCallback(MDMA_HandleTypeDef *hmdma)
344 /* Prevent unused argument(s) compilation warning */
345 UNUSED(hmdma);
347 /* NOTE: This function Should not be modified, when the callback is needed,
348 the HAL_SDRAM_DMA_XferErrorCallback could be implemented in the user file
353 * @}
356 /** @defgroup SDRAM_Exported_Functions_Group2 Input and Output functions
357 * @brief Input Output and memory control functions
359 @verbatim
360 ==============================================================================
361 ##### SDRAM Input and Output functions #####
362 ==============================================================================
363 [..]
364 This section provides functions allowing to use and control the SDRAM memory
366 @endverbatim
367 * @{
371 * @brief Reads 8-bit data buffer from the 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 read start address
375 * @param pDstBuffer Pointer to destination buffer
376 * @param BufferSize Size of the buffer to read from memory
377 * @retval HAL status
379 HAL_StatusTypeDef HAL_SDRAM_Read_8b(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint8_t *pDstBuffer, uint32_t BufferSize)
381 uint32_t size;
382 __IO uint8_t *pSdramAddress = (uint8_t *)pAddress;
383 uint8_t * pdestbuff = pDstBuffer;
384 HAL_SDRAM_StateTypeDef state = hsdram->State;
386 /* Check the SDRAM controller state */
387 if (state == HAL_SDRAM_STATE_BUSY)
389 return HAL_BUSY;
391 else if ((state == HAL_SDRAM_STATE_READY) || (state == HAL_SDRAM_STATE_WRITE_PROTECTED))
393 /* Process Locked */
394 __HAL_LOCK(hsdram);
396 /* Update the SDRAM controller state */
397 hsdram->State = HAL_SDRAM_STATE_BUSY;
399 /* Read data from source */
400 for (size = BufferSize; size != 0U; size--)
402 *pdestbuff = *(__IO uint8_t *)pSdramAddress;
403 pdestbuff++;
404 pSdramAddress++;
407 /* Update the SDRAM controller state */
408 hsdram->State = state;
410 /* Process Unlocked */
411 __HAL_UNLOCK(hsdram);
413 else
415 return HAL_ERROR;
418 return HAL_OK;
422 * @brief Writes 8-bit data buffer to SDRAM memory.
423 * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
424 * the configuration information for SDRAM module.
425 * @param pAddress Pointer to write start address
426 * @param pSrcBuffer Pointer to source buffer to write
427 * @param BufferSize Size of the buffer to write to memory
428 * @retval HAL status
430 HAL_StatusTypeDef HAL_SDRAM_Write_8b(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint8_t *pSrcBuffer, uint32_t BufferSize)
432 uint32_t size;
433 __IO uint8_t *pSdramAddress = (uint8_t *)pAddress;
434 uint8_t * psrcbuff = pSrcBuffer;
436 /* Check the SDRAM controller state */
437 if (hsdram->State == HAL_SDRAM_STATE_BUSY)
439 return HAL_BUSY;
441 else if (hsdram->State == HAL_SDRAM_STATE_READY)
443 /* Process Locked */
444 __HAL_LOCK(hsdram);
446 /* Update the SDRAM controller state */
447 hsdram->State = HAL_SDRAM_STATE_BUSY;
449 /* Write data to memory */
450 for (size = BufferSize; size != 0U; size--)
452 *(__IO uint8_t *)pSdramAddress = *psrcbuff;
453 psrcbuff++;
454 pSdramAddress++;
457 /* Update the SDRAM controller state */
458 hsdram->State = HAL_SDRAM_STATE_READY;
460 /* Process Unlocked */
461 __HAL_UNLOCK(hsdram);
463 else
465 return HAL_ERROR;
468 return HAL_OK;
472 * @brief Reads 16-bit data buffer from the SDRAM memory.
473 * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
474 * the configuration information for SDRAM module.
475 * @param pAddress Pointer to read start address
476 * @param pDstBuffer Pointer to destination buffer
477 * @param BufferSize Size of the buffer to read from memory
478 * @retval HAL status
480 HAL_StatusTypeDef HAL_SDRAM_Read_16b(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint16_t *pDstBuffer, uint32_t BufferSize)
482 uint32_t size;
483 __IO uint32_t *pSdramAddress = pAddress;
484 uint16_t *pdestbuff = pDstBuffer;
485 HAL_SDRAM_StateTypeDef state = hsdram->State;
487 /* Check the SDRAM controller state */
488 if (state == HAL_SDRAM_STATE_BUSY)
490 return HAL_BUSY;
492 else if ((state == HAL_SDRAM_STATE_READY) || (state == HAL_SDRAM_STATE_WRITE_PROTECTED))
494 /* Process Locked */
495 __HAL_LOCK(hsdram);
497 /* Update the SDRAM controller state */
498 hsdram->State = HAL_SDRAM_STATE_BUSY;
500 /* Read data from memory */
501 for (size = BufferSize; size >= 2U ; size-=2U)
503 *pdestbuff = (uint16_t)((*pSdramAddress) & 0x0000FFFFU);
504 pdestbuff++;
505 *pdestbuff = (uint16_t)(((*pSdramAddress) & 0xFFFF0000U) >> 16U);
506 pdestbuff++;
507 pSdramAddress++;
510 /* Read last 16-bits if size is not 32-bits multiple */
511 if ((BufferSize % 2U)!= 0U)
513 *pdestbuff = (uint16_t)((*pSdramAddress) & 0x0000FFFFU);
516 /* Update the SDRAM controller state */
517 hsdram->State = state;
519 /* Process Unlocked */
520 __HAL_UNLOCK(hsdram);
522 else
524 return HAL_ERROR;
527 return HAL_OK;
531 * @brief Writes 16-bit data buffer to SDRAM memory.
532 * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
533 * the configuration information for SDRAM module.
534 * @param pAddress Pointer to write start address
535 * @param pSrcBuffer Pointer to source buffer to write
536 * @param BufferSize Size of the buffer to write to memory
537 * @retval HAL status
539 HAL_StatusTypeDef HAL_SDRAM_Write_16b(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint16_t *pSrcBuffer, uint32_t BufferSize)
541 uint32_t size;
542 __IO uint32_t *psdramaddress = pAddress;
543 uint16_t * psrcbuff = pSrcBuffer;
545 /* Check the SDRAM controller state */
546 if (hsdram->State == HAL_SDRAM_STATE_BUSY)
548 return HAL_BUSY;
550 else if (hsdram->State == HAL_SDRAM_STATE_READY)
552 /* Process Locked */
553 __HAL_LOCK(hsdram);
555 /* Update the SDRAM controller state */
556 hsdram->State = HAL_SDRAM_STATE_BUSY;
558 /* Write data to memory */
559 for (size = BufferSize; size >= 2U ; size-=2U)
561 *psdramaddress = (uint32_t)(*psrcbuff);
562 psrcbuff++;
563 *psdramaddress |= ((uint32_t)(*psrcbuff) << 16U);
564 psrcbuff++;
565 psdramaddress++;
568 /* Write last 16-bits if size is not 32-bits multiple */
569 if ((BufferSize % 2U)!= 0U)
571 *psdramaddress = ((uint32_t)(*psrcbuff) & 0x0000FFFFU) | ((*psdramaddress) & 0xFFFF0000U);
574 /* Update the SDRAM controller state */
575 hsdram->State = HAL_SDRAM_STATE_READY;
577 /* Process Unlocked */
578 __HAL_UNLOCK(hsdram);
580 else
582 return HAL_ERROR;
585 return HAL_OK;
589 * @brief Reads 32-bit data buffer from the SDRAM memory.
590 * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
591 * the configuration information for SDRAM module.
592 * @param pAddress Pointer to read start address
593 * @param pDstBuffer Pointer to destination buffer
594 * @param BufferSize Size of the buffer to read from memory
595 * @retval HAL status
597 HAL_StatusTypeDef HAL_SDRAM_Read_32b(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint32_t *pDstBuffer, uint32_t BufferSize)
599 uint32_t size;
600 __IO uint32_t *pSdramAddress = (uint32_t *)pAddress;
601 uint32_t * pdestbuff = pDstBuffer;
602 HAL_SDRAM_StateTypeDef state = hsdram->State;
604 /* Check the SDRAM controller state */
605 if (state == HAL_SDRAM_STATE_BUSY)
607 return HAL_BUSY;
609 else if ((state == HAL_SDRAM_STATE_READY) || (state == HAL_SDRAM_STATE_WRITE_PROTECTED))
611 /* Process Locked */
612 __HAL_LOCK(hsdram);
614 /* Update the SDRAM controller state */
615 hsdram->State = HAL_SDRAM_STATE_BUSY;
617 /* Read data from source */
618 for (size = BufferSize; size != 0U; size--)
620 *pdestbuff = *(__IO uint32_t *)pSdramAddress;
621 pdestbuff++;
622 pSdramAddress++;
625 /* Update the SDRAM controller state */
626 hsdram->State = state;
628 /* Process Unlocked */
629 __HAL_UNLOCK(hsdram);
631 else
633 return HAL_ERROR;
636 return HAL_OK;
640 * @brief Writes 32-bit data buffer to SDRAM memory.
641 * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
642 * the configuration information for SDRAM module.
643 * @param pAddress Pointer to write start address
644 * @param pSrcBuffer Pointer to source buffer to write
645 * @param BufferSize Size of the buffer to write to memory
646 * @retval HAL status
648 HAL_StatusTypeDef HAL_SDRAM_Write_32b(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint32_t *pSrcBuffer, uint32_t BufferSize)
650 uint32_t size;
651 __IO uint32_t *pSdramAddress = pAddress;
652 uint32_t * psrcbuff = pSrcBuffer;
654 /* Check the SDRAM controller state */
655 if (hsdram->State == HAL_SDRAM_STATE_BUSY)
657 return HAL_BUSY;
659 else if (hsdram->State == HAL_SDRAM_STATE_READY)
661 /* Process Locked */
662 __HAL_LOCK(hsdram);
664 /* Update the SDRAM controller state */
665 hsdram->State = HAL_SDRAM_STATE_BUSY;
667 /* Write data to memory */
668 for (size = BufferSize; size != 0U; size--)
670 *pSdramAddress = *psrcbuff;
671 psrcbuff++;
672 pSdramAddress++;
675 /* Update the SDRAM controller state */
676 hsdram->State = HAL_SDRAM_STATE_READY;
678 /* Process Unlocked */
679 __HAL_UNLOCK(hsdram);
681 else
683 return HAL_ERROR;
686 return HAL_OK;
690 * @brief Reads a Words data from the SDRAM memory using DMA transfer.
691 * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
692 * the configuration information for SDRAM module.
693 * @param pAddress Pointer to read start address
694 * @param pDstBuffer Pointer to destination buffer
695 * @param BufferSize Size of the buffer to read from memory
696 * @retval HAL status
698 HAL_StatusTypeDef HAL_SDRAM_Read_DMA(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint32_t *pDstBuffer, uint32_t BufferSize)
700 HAL_StatusTypeDef status;
701 HAL_SDRAM_StateTypeDef state = hsdram->State;
703 /* Check the SDRAM controller state */
704 if (state == HAL_SDRAM_STATE_BUSY)
706 return HAL_BUSY;
708 else if ((state == HAL_SDRAM_STATE_READY) || (state == HAL_SDRAM_STATE_WRITE_PROTECTED))
710 /* Process Locked */
711 __HAL_LOCK(hsdram);
713 /* Update the SDRAM controller state */
714 hsdram->State = HAL_SDRAM_STATE_BUSY;
716 /* Configure DMA user callbacks */
717 if (state == HAL_SDRAM_STATE_READY)
719 hsdram->hmdma->XferCpltCallback = SDRAM_DMACplt;
721 else
723 hsdram->hmdma->XferCpltCallback = SDRAM_DMACpltProt;
725 hsdram->hmdma->XferErrorCallback = SDRAM_DMAError;
727 /* Enable the DMA Stream */
728 status = HAL_MDMA_Start_IT(hsdram->hmdma, (uint32_t)pAddress, (uint32_t)pDstBuffer, (uint32_t)(BufferSize * 4U), 1);
730 /* Process Unlocked */
731 __HAL_UNLOCK(hsdram);
733 else
735 return HAL_ERROR;
738 return status;
742 * @brief Writes a Words data buffer to SDRAM memory using DMA transfer.
743 * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
744 * the configuration information for SDRAM module.
745 * @param pAddress Pointer to write start address
746 * @param pSrcBuffer Pointer to source buffer to write
747 * @param BufferSize Size of the buffer to write to memory
748 * @retval HAL status
750 HAL_StatusTypeDef HAL_SDRAM_Write_DMA(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint32_t *pSrcBuffer, uint32_t BufferSize)
752 HAL_StatusTypeDef status;
754 /* Check the SDRAM controller state */
755 if (hsdram->State == HAL_SDRAM_STATE_BUSY)
757 return HAL_BUSY;
759 else if (hsdram->State == HAL_SDRAM_STATE_READY)
761 /* Process Locked */
762 __HAL_LOCK(hsdram);
764 /* Update the SDRAM controller state */
765 hsdram->State = HAL_SDRAM_STATE_BUSY;
767 /* Configure DMA user callbacks */
768 hsdram->hmdma->XferCpltCallback = SDRAM_DMACplt;
769 hsdram->hmdma->XferErrorCallback = SDRAM_DMAError;
771 /* Enable the DMA Stream */
772 status = HAL_MDMA_Start_IT(hsdram->hmdma, (uint32_t)pSrcBuffer, (uint32_t)pAddress, (uint32_t)(BufferSize * 4U), 1);
774 /* Process Unlocked */
775 __HAL_UNLOCK(hsdram);
777 else
779 return HAL_ERROR;
782 return status;
785 #if (USE_HAL_SDRAM_REGISTER_CALLBACKS == 1)
787 * @brief Register a User SDRAM Callback
788 * To be used instead of the weak (surcharged) predefined callback
789 * @param hsdram : SDRAM handle
790 * @param CallbackId : ID of the callback to be registered
791 * This parameter can be one of the following values:
792 * @arg @ref HAL_SDRAM_MSP_INIT_CB_ID SDRAM MspInit callback ID
793 * @arg @ref HAL_SDRAM_MSP_DEINIT_CB_ID SDRAM MspDeInit callback ID
794 * @arg @ref HAL_SDRAM_REFRESH_ERR_CB_ID SDRAM Refresh Error callback ID
795 * @param pCallback : pointer to the Callback function
796 * @retval status
798 HAL_StatusTypeDef HAL_SDRAM_RegisterCallback (SDRAM_HandleTypeDef *hsdram, HAL_SDRAM_CallbackIDTypeDef CallbackId, pSDRAM_CallbackTypeDef pCallback)
800 HAL_StatusTypeDef status = HAL_OK;
801 HAL_SDRAM_StateTypeDef state;
803 if(pCallback == NULL)
805 return HAL_ERROR;
808 /* Process locked */
809 __HAL_LOCK(hsdram);
811 state = hsdram->State;
812 if((state == HAL_SDRAM_STATE_READY) || (state == HAL_SDRAM_STATE_WRITE_PROTECTED))
814 switch (CallbackId)
816 case HAL_SDRAM_MSP_INIT_CB_ID :
817 hsdram->MspInitCallback = pCallback;
818 break;
819 case HAL_SDRAM_MSP_DEINIT_CB_ID :
820 hsdram->MspDeInitCallback = pCallback;
821 break;
822 case HAL_SDRAM_REFRESH_ERR_CB_ID :
823 hsdram->RefreshErrorCallback = pCallback;
824 break;
825 default :
826 /* update return status */
827 status = HAL_ERROR;
828 break;
831 else if(hsdram->State == HAL_SDRAM_STATE_RESET)
833 switch (CallbackId)
835 case HAL_SDRAM_MSP_INIT_CB_ID :
836 hsdram->MspInitCallback = pCallback;
837 break;
838 case HAL_SDRAM_MSP_DEINIT_CB_ID :
839 hsdram->MspDeInitCallback = pCallback;
840 break;
841 default :
842 /* update return status */
843 status = HAL_ERROR;
844 break;
847 else
849 /* update return status */
850 status = HAL_ERROR;
853 /* Release Lock */
854 __HAL_UNLOCK(hsdram);
855 return status;
859 * @brief Unregister a User SDRAM Callback
860 * SDRAM Callback is redirected to the weak (surcharged) predefined callback
861 * @param hsdram : SDRAM handle
862 * @param CallbackId : ID of the callback to be unregistered
863 * This parameter can be one of the following values:
864 * @arg @ref HAL_SDRAM_MSP_INIT_CB_ID SDRAM MspInit callback ID
865 * @arg @ref HAL_SDRAM_MSP_DEINIT_CB_ID SDRAM MspDeInit callback ID
866 * @arg @ref HAL_SDRAM_REFRESH_ERR_CB_ID SDRAM Refresh Error callback ID
867 * @arg @ref HAL_SDRAM_DMA_XFER_CPLT_CB_ID SDRAM DMA Xfer Complete callback ID
868 * @arg @ref HAL_SDRAM_DMA_XFER_ERR_CB_ID SDRAM DMA Xfer Error callback ID
869 * @retval status
871 HAL_StatusTypeDef HAL_SDRAM_UnRegisterCallback (SDRAM_HandleTypeDef *hsdram, HAL_SDRAM_CallbackIDTypeDef CallbackId)
873 HAL_StatusTypeDef status = HAL_OK;
874 HAL_SDRAM_StateTypeDef state;
876 /* Process locked */
877 __HAL_LOCK(hsdram);
879 state = hsdram->State;
880 if((state == HAL_SDRAM_STATE_READY) || (state == HAL_SDRAM_STATE_WRITE_PROTECTED))
882 switch (CallbackId)
884 case HAL_SDRAM_MSP_INIT_CB_ID :
885 hsdram->MspInitCallback = HAL_SDRAM_MspInit;
886 break;
887 case HAL_SDRAM_MSP_DEINIT_CB_ID :
888 hsdram->MspDeInitCallback = HAL_SDRAM_MspDeInit;
889 break;
890 case HAL_SDRAM_REFRESH_ERR_CB_ID :
891 hsdram->RefreshErrorCallback = HAL_SDRAM_RefreshErrorCallback;
892 break;
893 case HAL_SDRAM_DMA_XFER_CPLT_CB_ID :
894 hsdram->DmaXferCpltCallback = HAL_SDRAM_DMA_XferCpltCallback;
895 break;
896 case HAL_SDRAM_DMA_XFER_ERR_CB_ID :
897 hsdram->DmaXferErrorCallback = HAL_SDRAM_DMA_XferErrorCallback;
898 break;
899 default :
900 /* update return status */
901 status = HAL_ERROR;
902 break;
905 else if(hsdram->State == HAL_SDRAM_STATE_RESET)
907 switch (CallbackId)
909 case HAL_SDRAM_MSP_INIT_CB_ID :
910 hsdram->MspInitCallback = HAL_SDRAM_MspInit;
911 break;
912 case HAL_SDRAM_MSP_DEINIT_CB_ID :
913 hsdram->MspDeInitCallback = HAL_SDRAM_MspDeInit;
914 break;
915 default :
916 /* update return status */
917 status = HAL_ERROR;
918 break;
921 else
923 /* update return status */
924 status = HAL_ERROR;
927 /* Release Lock */
928 __HAL_UNLOCK(hsdram);
929 return status;
933 * @brief Register a User SDRAM Callback for DMA transfers
934 * To be used instead of the weak (surcharged) predefined callback
935 * @param hsdram : SDRAM handle
936 * @param CallbackId : ID of the callback to be registered
937 * This parameter can be one of the following values:
938 * @arg @ref HAL_SDRAM_DMA_XFER_CPLT_CB_ID SDRAM DMA Xfer Complete callback ID
939 * @arg @ref HAL_SDRAM_DMA_XFER_ERR_CB_ID SDRAM DMA Xfer Error callback ID
940 * @param pCallback : pointer to the Callback function
941 * @retval status
943 HAL_StatusTypeDef HAL_SDRAM_RegisterDmaCallback(SDRAM_HandleTypeDef *hsdram, HAL_SDRAM_CallbackIDTypeDef CallbackId, pSDRAM_DmaCallbackTypeDef pCallback)
945 HAL_StatusTypeDef status = HAL_OK;
946 HAL_SDRAM_StateTypeDef state;
948 if(pCallback == NULL)
950 return HAL_ERROR;
953 /* Process locked */
954 __HAL_LOCK(hsdram);
956 state = hsdram->State;
957 if((state == HAL_SDRAM_STATE_READY) || (state == HAL_SDRAM_STATE_WRITE_PROTECTED))
959 switch (CallbackId)
961 case HAL_SDRAM_DMA_XFER_CPLT_CB_ID :
962 hsdram->DmaXferCpltCallback = pCallback;
963 break;
964 case HAL_SDRAM_DMA_XFER_ERR_CB_ID :
965 hsdram->DmaXferErrorCallback = pCallback;
966 break;
967 default :
968 /* update return status */
969 status = HAL_ERROR;
970 break;
973 else
975 /* update return status */
976 status = HAL_ERROR;
979 /* Release Lock */
980 __HAL_UNLOCK(hsdram);
981 return status;
983 #endif
986 * @}
989 /** @defgroup SDRAM_Exported_Functions_Group3 Control functions
990 * @brief management functions
992 @verbatim
993 ==============================================================================
994 ##### SDRAM Control functions #####
995 ==============================================================================
996 [..]
997 This subsection provides a set of functions allowing to control dynamically
998 the SDRAM interface.
1000 @endverbatim
1001 * @{
1005 * @brief Enables dynamically SDRAM write protection.
1006 * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
1007 * the configuration information for SDRAM module.
1008 * @retval HAL status
1010 HAL_StatusTypeDef HAL_SDRAM_WriteProtection_Enable(SDRAM_HandleTypeDef *hsdram)
1012 /* Check the SDRAM controller state */
1013 if (hsdram->State == HAL_SDRAM_STATE_BUSY)
1015 return HAL_BUSY;
1017 else if (hsdram->State == HAL_SDRAM_STATE_READY)
1019 /* Update the SDRAM state */
1020 hsdram->State = HAL_SDRAM_STATE_BUSY;
1022 /* Enable write protection */
1023 (void)FMC_SDRAM_WriteProtection_Enable(hsdram->Instance, hsdram->Init.SDBank);
1025 /* Update the SDRAM state */
1026 hsdram->State = HAL_SDRAM_STATE_WRITE_PROTECTED;
1028 else
1030 return HAL_ERROR;
1033 return HAL_OK;
1037 * @brief Disables dynamically SDRAM write protection.
1038 * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
1039 * the configuration information for SDRAM module.
1040 * @retval HAL status
1042 HAL_StatusTypeDef HAL_SDRAM_WriteProtection_Disable(SDRAM_HandleTypeDef *hsdram)
1044 HAL_SDRAM_StateTypeDef state = hsdram->State;
1046 /* Check the SDRAM controller state */
1047 if (state == HAL_SDRAM_STATE_BUSY)
1049 return HAL_BUSY;
1051 else if (state == HAL_SDRAM_STATE_WRITE_PROTECTED)
1053 /* Update the SDRAM state */
1054 hsdram->State = HAL_SDRAM_STATE_BUSY;
1056 /* Disable write protection */
1057 (void)FMC_SDRAM_WriteProtection_Disable(hsdram->Instance, hsdram->Init.SDBank);
1059 /* Update the SDRAM state */
1060 hsdram->State = HAL_SDRAM_STATE_READY;
1062 else
1064 return HAL_ERROR;
1067 return HAL_OK;
1071 * @brief Sends Command to the SDRAM bank.
1072 * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
1073 * the configuration information for SDRAM module.
1074 * @param Command SDRAM command structure
1075 * @param Timeout Timeout duration
1076 * @retval HAL status
1078 HAL_StatusTypeDef HAL_SDRAM_SendCommand(SDRAM_HandleTypeDef *hsdram, FMC_SDRAM_CommandTypeDef *Command, uint32_t Timeout)
1080 HAL_SDRAM_StateTypeDef state = hsdram->State;
1082 /* Check the SDRAM controller state */
1083 if (state == HAL_SDRAM_STATE_BUSY)
1085 return HAL_BUSY;
1087 else if((state == HAL_SDRAM_STATE_READY) || (state == HAL_SDRAM_STATE_PRECHARGED))
1089 /* Update the SDRAM state */
1090 hsdram->State = HAL_SDRAM_STATE_BUSY;
1092 /* Send SDRAM command */
1093 (void)FMC_SDRAM_SendCommand(hsdram->Instance, Command, Timeout);
1095 /* Update the SDRAM controller state state */
1096 if (Command->CommandMode == FMC_SDRAM_CMD_PALL)
1098 hsdram->State = HAL_SDRAM_STATE_PRECHARGED;
1100 else
1102 hsdram->State = HAL_SDRAM_STATE_READY;
1105 else
1107 return HAL_ERROR;
1110 return HAL_OK;
1114 * @brief Programs the SDRAM Memory Refresh rate.
1115 * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
1116 * the configuration information for SDRAM module.
1117 * @param RefreshRate The SDRAM refresh rate value
1118 * @retval HAL status
1120 HAL_StatusTypeDef HAL_SDRAM_ProgramRefreshRate(SDRAM_HandleTypeDef *hsdram, uint32_t RefreshRate)
1122 /* Check the SDRAM controller state */
1123 if (hsdram->State == HAL_SDRAM_STATE_BUSY)
1125 return HAL_BUSY;
1127 else if (hsdram->State == HAL_SDRAM_STATE_READY)
1129 /* Update the SDRAM state */
1130 hsdram->State = HAL_SDRAM_STATE_BUSY;
1132 /* Program the refresh rate */
1133 (void)FMC_SDRAM_ProgramRefreshRate(hsdram->Instance, RefreshRate);
1135 /* Update the SDRAM state */
1136 hsdram->State = HAL_SDRAM_STATE_READY;
1138 else
1140 return HAL_ERROR;
1143 return HAL_OK;
1147 * @brief Sets the Number of consecutive SDRAM Memory auto Refresh commands.
1148 * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
1149 * the configuration information for SDRAM module.
1150 * @param AutoRefreshNumber The SDRAM auto Refresh number
1151 * @retval HAL status
1153 HAL_StatusTypeDef HAL_SDRAM_SetAutoRefreshNumber(SDRAM_HandleTypeDef *hsdram, uint32_t AutoRefreshNumber)
1155 /* Check the SDRAM controller state */
1156 if (hsdram->State == HAL_SDRAM_STATE_BUSY)
1158 return HAL_BUSY;
1160 else if (hsdram->State == HAL_SDRAM_STATE_READY)
1162 /* Update the SDRAM state */
1163 hsdram->State = HAL_SDRAM_STATE_BUSY;
1165 /* Set the Auto-Refresh number */
1166 (void)FMC_SDRAM_SetAutoRefreshNumber(hsdram->Instance, AutoRefreshNumber);
1168 /* Update the SDRAM state */
1169 hsdram->State = HAL_SDRAM_STATE_READY;
1171 else
1173 return HAL_ERROR;
1176 return HAL_OK;
1180 * @brief Returns the SDRAM memory current mode.
1181 * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
1182 * the configuration information for SDRAM module.
1183 * @retval The SDRAM memory mode.
1185 uint32_t HAL_SDRAM_GetModeStatus(SDRAM_HandleTypeDef *hsdram)
1187 /* Return the SDRAM memory current mode */
1188 return (FMC_SDRAM_GetModeStatus(hsdram->Instance, hsdram->Init.SDBank));
1192 * @}
1195 /** @defgroup SDRAM_Exported_Functions_Group4 State functions
1196 * @brief Peripheral State functions
1198 @verbatim
1199 ==============================================================================
1200 ##### SDRAM State functions #####
1201 ==============================================================================
1202 [..]
1203 This subsection permits to get in run-time the status of the SDRAM controller
1204 and the data flow.
1206 @endverbatim
1207 * @{
1211 * @brief Returns the SDRAM state.
1212 * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
1213 * the configuration information for SDRAM module.
1214 * @retval HAL state
1216 HAL_SDRAM_StateTypeDef HAL_SDRAM_GetState(SDRAM_HandleTypeDef *hsdram)
1218 return hsdram->State;
1222 * @}
1226 * @}
1230 @cond 0
1233 * @brief MDMA SDRAM process complete callback.
1234 * @param hmdma : MDMA handle
1235 * @retval None
1237 static void SDRAM_DMACplt(MDMA_HandleTypeDef *hmdma)
1239 SDRAM_HandleTypeDef* hsdram = ( SDRAM_HandleTypeDef* )(hmdma->Parent);
1241 /* Disable the MDMA channel */
1242 __HAL_MDMA_DISABLE(hmdma);
1244 /* Update the SDRAM controller state */
1245 hsdram->State = HAL_SDRAM_STATE_READY;
1247 #if (USE_HAL_SDRAM_REGISTER_CALLBACKS == 1)
1248 hsdram->DmaXferCpltCallback(hmdma);
1249 #else
1250 HAL_SDRAM_DMA_XferCpltCallback(hmdma);
1251 #endif
1255 * @brief MDMA SRAM process complete callback.
1256 * @param hmdma : MDMA handle
1257 * @retval None
1259 static void SDRAM_DMACpltProt(MDMA_HandleTypeDef *hmdma)
1261 SDRAM_HandleTypeDef* hsdram = ( SDRAM_HandleTypeDef* )(hmdma->Parent);
1263 /* Disable the MDMA channel */
1264 __HAL_MDMA_DISABLE(hmdma);
1266 /* Update the SDRAM controller state */
1267 hsdram->State = HAL_SDRAM_STATE_WRITE_PROTECTED;
1269 #if (USE_HAL_SDRAM_REGISTER_CALLBACKS == 1)
1270 hsdram->DmaXferCpltCallback(hmdma);
1271 #else
1272 HAL_SDRAM_DMA_XferCpltCallback(hmdma);
1273 #endif
1277 * @brief MDMA SDRAM error callback.
1278 * @param hmdma : MDMA handle
1279 * @retval None
1281 static void SDRAM_DMAError(MDMA_HandleTypeDef *hmdma)
1283 SDRAM_HandleTypeDef* hsdram = ( SDRAM_HandleTypeDef* )(hmdma->Parent);
1285 /* Disable the MDMA channel */
1286 __HAL_MDMA_DISABLE(hmdma);
1288 /* Update the SDRAM controller state */
1289 hsdram->State = HAL_SDRAM_STATE_ERROR;
1291 #if (USE_HAL_SDRAM_REGISTER_CALLBACKS == 1)
1292 hsdram->DmaXferErrorCallback(hmdma);
1293 #else
1294 HAL_SDRAM_DMA_XferErrorCallback(hmdma);
1295 #endif
1298 @endcond
1302 * @}
1305 #endif /* HAL_SDRAM_MODULE_ENABLED */
1308 * @}
1312 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/