2 ******************************************************************************
3 * @file stm32f7xx_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
10 ==============================================================================
11 ##### How to use this driver #####
12 ==============================================================================
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
17 The following sequence should be followed to configure the FMC to interface
20 (#) Declare a SDRAM_HandleTypeDef handle structure, for example:
21 SDRAM_HandleTypeDef hdsram
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
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
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
57 (#) You can continuously monitor the SDRAM device HAL state by calling the function
60 *** Callback registration ***
61 =============================================
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.
100 ******************************************************************************
103 * <h2><center>© 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 "stm32f7xx_hal.h"
117 /** @addtogroup STM32F7xx_HAL_Driver
121 /** @defgroup SDRAM SDRAM
122 * @brief SDRAM driver modules
125 #ifdef HAL_SDRAM_MODULE_ENABLED
127 /* Private typedef -----------------------------------------------------------*/
128 /* Private define ------------------------------------------------------------*/
129 /* Private macro -------------------------------------------------------------*/
130 /* Private variables ---------------------------------------------------------*/
131 /* Private functions ---------------------------------------------------------*/
132 /* Exported functions --------------------------------------------------------*/
133 /** @defgroup SDRAM_Exported_Functions SDRAM Exported Functions
137 /** @defgroup SDRAM_Exported_Functions_Group1 Initialization and de-initialization functions
138 * @brief Initialization and Configuration functions
141 ==============================================================================
142 ##### SDRAM Initialization and de_initialization functions #####
143 ==============================================================================
145 This section provides functions allowing to initialize/de-initialize
153 * @brief Performs the SDRAM device initialization sequence.
154 * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
155 * the configuration information for SDRAM module.
156 * @param Timing Pointer to SDRAM control timing structure
159 HAL_StatusTypeDef
HAL_SDRAM_Init(SDRAM_HandleTypeDef
*hsdram
, FMC_SDRAM_TimingTypeDef
*Timing
)
161 /* Check the SDRAM handle parameter */
167 if(hsdram
->State
== HAL_SDRAM_STATE_RESET
)
169 /* Allocate lock resource and initialize it */
170 hsdram
->Lock
= HAL_UNLOCKED
;
171 #if (USE_HAL_SDRAM_REGISTER_CALLBACKS == 1)
172 if(hsdram
->MspInitCallback
== NULL
)
174 hsdram
->MspInitCallback
= HAL_SDRAM_MspInit
;
176 hsdram
->RefreshErrorCallback
= HAL_SDRAM_RefreshErrorCallback
;
177 hsdram
->DmaXferCpltCallback
= HAL_SDRAM_DMA_XferCpltCallback
;
178 hsdram
->DmaXferErrorCallback
= HAL_SDRAM_DMA_XferErrorCallback
;
180 /* Init the low level hardware */
181 hsdram
->MspInitCallback(hsdram
);
183 /* Initialize the low level hardware (MSP) */
184 HAL_SDRAM_MspInit(hsdram
);
188 /* Initialize the SDRAM controller state */
189 hsdram
->State
= HAL_SDRAM_STATE_BUSY
;
191 /* Initialize SDRAM control Interface */
192 FMC_SDRAM_Init(hsdram
->Instance
, &(hsdram
->Init
));
194 /* Initialize SDRAM timing Interface */
195 FMC_SDRAM_Timing_Init(hsdram
->Instance
, Timing
, hsdram
->Init
.SDBank
);
197 /* Update the SDRAM controller state */
198 hsdram
->State
= HAL_SDRAM_STATE_READY
;
204 * @brief Perform the SDRAM device initialization sequence.
205 * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
206 * the configuration information for SDRAM module.
209 HAL_StatusTypeDef
HAL_SDRAM_DeInit(SDRAM_HandleTypeDef
*hsdram
)
211 #if (USE_HAL_SDRAM_REGISTER_CALLBACKS == 1)
212 if(hsdram
->MspDeInitCallback
== NULL
)
214 hsdram
->MspDeInitCallback
= HAL_SDRAM_MspDeInit
;
217 /* DeInit the low level hardware */
218 hsdram
->MspDeInitCallback(hsdram
);
220 /* Initialize the low level hardware (MSP) */
221 HAL_SDRAM_MspDeInit(hsdram
);
224 /* Configure the SDRAM registers with their reset values */
225 FMC_SDRAM_DeInit(hsdram
->Instance
, hsdram
->Init
.SDBank
);
227 /* Reset the SDRAM controller state */
228 hsdram
->State
= HAL_SDRAM_STATE_RESET
;
231 __HAL_UNLOCK(hsdram
);
237 * @brief SDRAM MSP Init.
238 * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
239 * the configuration information for SDRAM module.
242 __weak
void HAL_SDRAM_MspInit(SDRAM_HandleTypeDef
*hsdram
)
244 /* Prevent unused argument(s) compilation warning */
247 /* NOTE: This function Should not be modified, when the callback is needed,
248 the HAL_SDRAM_MspInit could be implemented in the user file
253 * @brief SDRAM MSP DeInit.
254 * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
255 * the configuration information for SDRAM module.
258 __weak
void HAL_SDRAM_MspDeInit(SDRAM_HandleTypeDef
*hsdram
)
260 /* Prevent unused argument(s) compilation warning */
263 /* NOTE: This function Should not be modified, when the callback is needed,
264 the HAL_SDRAM_MspDeInit could be implemented in the user file
269 * @brief This function handles SDRAM refresh error interrupt request.
270 * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
271 * the configuration information for SDRAM module.
274 void HAL_SDRAM_IRQHandler(SDRAM_HandleTypeDef
*hsdram
)
276 /* Check SDRAM interrupt Rising edge flag */
277 if(__FMC_SDRAM_GET_FLAG(hsdram
->Instance
, FMC_SDRAM_FLAG_REFRESH_IT
))
279 /* SDRAM refresh error interrupt callback */
280 #if (USE_HAL_SDRAM_REGISTER_CALLBACKS == 1)
281 hsdram
->RefreshErrorCallback(hsdram
);
283 HAL_SDRAM_RefreshErrorCallback(hsdram
);
286 /* Clear SDRAM refresh error interrupt pending bit */
287 __FMC_SDRAM_CLEAR_FLAG(hsdram
->Instance
, FMC_SDRAM_FLAG_REFRESH_ERROR
);
292 * @brief SDRAM Refresh error callback.
293 * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
294 * the configuration information for SDRAM module.
297 __weak
void HAL_SDRAM_RefreshErrorCallback(SDRAM_HandleTypeDef
*hsdram
)
299 /* Prevent unused argument(s) compilation warning */
302 /* NOTE: This function Should not be modified, when the callback is needed,
303 the HAL_SDRAM_RefreshErrorCallback could be implemented in the user file
308 * @brief DMA transfer complete callback.
309 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
310 * the configuration information for the specified DMA module.
313 __weak
void HAL_SDRAM_DMA_XferCpltCallback(DMA_HandleTypeDef
*hdma
)
315 /* Prevent unused argument(s) compilation warning */
318 /* NOTE: This function Should not be modified, when the callback is needed,
319 the HAL_SDRAM_DMA_XferCpltCallback could be implemented in the user file
324 * @brief DMA transfer complete error callback.
325 * @param hdma DMA handle
328 __weak
void HAL_SDRAM_DMA_XferErrorCallback(DMA_HandleTypeDef
*hdma
)
330 /* Prevent unused argument(s) compilation warning */
333 /* NOTE: This function Should not be modified, when the callback is needed,
334 the HAL_SDRAM_DMA_XferErrorCallback could be implemented in the user file
342 /** @defgroup SDRAM_Exported_Functions_Group2 Input and Output functions
343 * @brief Input Output and memory control functions
346 ==============================================================================
347 ##### SDRAM Input and Output functions #####
348 ==============================================================================
350 This section provides functions allowing to use and control the SDRAM memory
357 * @brief Reads 8-bit data buffer from the SDRAM memory.
358 * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
359 * the configuration information for SDRAM module.
360 * @param pAddress Pointer to read start address
361 * @param pDstBuffer Pointer to destination buffer
362 * @param BufferSize Size of the buffer to read from memory
365 HAL_StatusTypeDef
HAL_SDRAM_Read_8b(SDRAM_HandleTypeDef
*hsdram
, uint32_t *pAddress
, uint8_t *pDstBuffer
, uint32_t BufferSize
)
367 __IO
uint8_t *pSdramAddress
= (uint8_t *)pAddress
;
372 /* Check the SDRAM controller state */
373 if(hsdram
->State
== HAL_SDRAM_STATE_BUSY
)
377 else if(hsdram
->State
== HAL_SDRAM_STATE_PRECHARGED
)
382 /* Read data from source */
383 for(; BufferSize
!= 0; BufferSize
--)
385 *pDstBuffer
= *(__IO
uint8_t *)pSdramAddress
;
390 /* Process Unlocked */
391 __HAL_UNLOCK(hsdram
);
398 * @brief Writes 8-bit data buffer to SDRAM memory.
399 * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
400 * the configuration information for SDRAM module.
401 * @param pAddress Pointer to write start address
402 * @param pSrcBuffer Pointer to source buffer to write
403 * @param BufferSize Size of the buffer to write to memory
406 HAL_StatusTypeDef
HAL_SDRAM_Write_8b(SDRAM_HandleTypeDef
*hsdram
, uint32_t *pAddress
, uint8_t *pSrcBuffer
, uint32_t BufferSize
)
408 __IO
uint8_t *pSdramAddress
= (uint8_t *)pAddress
;
414 /* Check the SDRAM controller state */
417 if(tmp
== HAL_SDRAM_STATE_BUSY
)
421 else if((tmp
== HAL_SDRAM_STATE_PRECHARGED
) || (tmp
== HAL_SDRAM_STATE_WRITE_PROTECTED
))
426 /* Write data to memory */
427 for(; BufferSize
!= 0; BufferSize
--)
429 *(__IO
uint8_t *)pSdramAddress
= *pSrcBuffer
;
434 /* Process Unlocked */
435 __HAL_UNLOCK(hsdram
);
442 * @brief Reads 16-bit data buffer from the SDRAM memory.
443 * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
444 * the configuration information for SDRAM module.
445 * @param pAddress Pointer to read start address
446 * @param pDstBuffer Pointer to destination buffer
447 * @param BufferSize Size of the buffer to read from memory
450 HAL_StatusTypeDef
HAL_SDRAM_Read_16b(SDRAM_HandleTypeDef
*hsdram
, uint32_t *pAddress
, uint16_t *pDstBuffer
, uint32_t BufferSize
)
452 __IO
uint16_t *pSdramAddress
= (uint16_t *)pAddress
;
457 /* Check the SDRAM controller state */
458 if(hsdram
->State
== HAL_SDRAM_STATE_BUSY
)
462 else if(hsdram
->State
== HAL_SDRAM_STATE_PRECHARGED
)
467 /* Read data from source */
468 for(; BufferSize
!= 0; BufferSize
--)
470 *pDstBuffer
= *(__IO
uint16_t *)pSdramAddress
;
475 /* Process Unlocked */
476 __HAL_UNLOCK(hsdram
);
482 * @brief Writes 16-bit data buffer to SDRAM memory.
483 * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
484 * the configuration information for SDRAM module.
485 * @param pAddress Pointer to write start address
486 * @param pSrcBuffer Pointer to source buffer to write
487 * @param BufferSize Size of the buffer to write to memory
490 HAL_StatusTypeDef
HAL_SDRAM_Write_16b(SDRAM_HandleTypeDef
*hsdram
, uint32_t *pAddress
, uint16_t *pSrcBuffer
, uint32_t BufferSize
)
492 __IO
uint16_t *pSdramAddress
= (uint16_t *)pAddress
;
498 /* Check the SDRAM controller state */
501 if(tmp
== HAL_SDRAM_STATE_BUSY
)
505 else if((tmp
== HAL_SDRAM_STATE_PRECHARGED
) || (tmp
== HAL_SDRAM_STATE_WRITE_PROTECTED
))
510 /* Write data to memory */
511 for(; BufferSize
!= 0; BufferSize
--)
513 *(__IO
uint16_t *)pSdramAddress
= *pSrcBuffer
;
518 /* Process Unlocked */
519 __HAL_UNLOCK(hsdram
);
525 * @brief Reads 32-bit data buffer from the SDRAM memory.
526 * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
527 * the configuration information for SDRAM module.
528 * @param pAddress Pointer to read start address
529 * @param pDstBuffer Pointer to destination buffer
530 * @param BufferSize Size of the buffer to read from memory
533 HAL_StatusTypeDef
HAL_SDRAM_Read_32b(SDRAM_HandleTypeDef
*hsdram
, uint32_t *pAddress
, uint32_t *pDstBuffer
, uint32_t BufferSize
)
535 __IO
uint32_t *pSdramAddress
= (uint32_t *)pAddress
;
540 /* Check the SDRAM controller state */
541 if(hsdram
->State
== HAL_SDRAM_STATE_BUSY
)
545 else if(hsdram
->State
== HAL_SDRAM_STATE_PRECHARGED
)
550 /* Read data from source */
551 for(; BufferSize
!= 0; BufferSize
--)
553 *pDstBuffer
= *(__IO
uint32_t *)pSdramAddress
;
558 /* Process Unlocked */
559 __HAL_UNLOCK(hsdram
);
565 * @brief Writes 32-bit data buffer to SDRAM memory.
566 * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
567 * the configuration information for SDRAM module.
568 * @param pAddress Pointer to write start address
569 * @param pSrcBuffer Pointer to source buffer to write
570 * @param BufferSize Size of the buffer to write to memory
573 HAL_StatusTypeDef
HAL_SDRAM_Write_32b(SDRAM_HandleTypeDef
*hsdram
, uint32_t *pAddress
, uint32_t *pSrcBuffer
, uint32_t BufferSize
)
575 __IO
uint32_t *pSdramAddress
= (uint32_t *)pAddress
;
581 /* Check the SDRAM controller state */
584 if(tmp
== HAL_SDRAM_STATE_BUSY
)
588 else if((tmp
== HAL_SDRAM_STATE_PRECHARGED
) || (tmp
== HAL_SDRAM_STATE_WRITE_PROTECTED
))
593 /* Write data to memory */
594 for(; BufferSize
!= 0; BufferSize
--)
596 *(__IO
uint32_t *)pSdramAddress
= *pSrcBuffer
;
601 /* Process Unlocked */
602 __HAL_UNLOCK(hsdram
);
608 * @brief Reads a Words data from the SDRAM memory using DMA transfer.
609 * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
610 * the configuration information for SDRAM module.
611 * @param pAddress Pointer to read start address
612 * @param pDstBuffer Pointer to destination buffer
613 * @param BufferSize Size of the buffer to read from memory
616 HAL_StatusTypeDef
HAL_SDRAM_Read_DMA(SDRAM_HandleTypeDef
*hsdram
, uint32_t *pAddress
, uint32_t *pDstBuffer
, uint32_t BufferSize
)
623 /* Check the SDRAM controller state */
626 if(tmp
== HAL_SDRAM_STATE_BUSY
)
630 else if(tmp
== HAL_SDRAM_STATE_PRECHARGED
)
635 /* Configure DMA user callbacks */
636 hsdram
->hdma
->XferCpltCallback
= HAL_SDRAM_DMA_XferCpltCallback
;
637 hsdram
->hdma
->XferErrorCallback
= HAL_SDRAM_DMA_XferErrorCallback
;
639 /* Enable the DMA Stream */
640 HAL_DMA_Start_IT(hsdram
->hdma
, (uint32_t)pAddress
, (uint32_t)pDstBuffer
, (uint32_t)BufferSize
);
642 /* Process Unlocked */
643 __HAL_UNLOCK(hsdram
);
649 * @brief Writes a Words data buffer to SDRAM memory using DMA transfer.
650 * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
651 * the configuration information for SDRAM module.
652 * @param pAddress Pointer to write start address
653 * @param pSrcBuffer Pointer to source buffer to write
654 * @param BufferSize Size of the buffer to write to memory
657 HAL_StatusTypeDef
HAL_SDRAM_Write_DMA(SDRAM_HandleTypeDef
*hsdram
, uint32_t *pAddress
, uint32_t *pSrcBuffer
, uint32_t BufferSize
)
664 /* Check the SDRAM controller state */
667 if(tmp
== HAL_SDRAM_STATE_BUSY
)
671 else if((tmp
== HAL_SDRAM_STATE_PRECHARGED
) || (tmp
== HAL_SDRAM_STATE_WRITE_PROTECTED
))
676 /* Configure DMA user callbacks */
677 hsdram
->hdma
->XferCpltCallback
= HAL_SDRAM_DMA_XferCpltCallback
;
678 hsdram
->hdma
->XferErrorCallback
= HAL_SDRAM_DMA_XferErrorCallback
;
680 /* Enable the DMA Stream */
681 HAL_DMA_Start_IT(hsdram
->hdma
, (uint32_t)pSrcBuffer
, (uint32_t)pAddress
, (uint32_t)BufferSize
);
683 /* Process Unlocked */
684 __HAL_UNLOCK(hsdram
);
688 #if (USE_HAL_SDRAM_REGISTER_CALLBACKS == 1)
690 * @brief Register a User SDRAM Callback
691 * To be used instead of the weak (surcharged) predefined callback
692 * @param hsdram : SDRAM handle
693 * @param CallbackId : ID of the callback to be registered
694 * This parameter can be one of the following values:
695 * @arg @ref HAL_SDRAM_MSP_INIT_CB_ID SDRAM MspInit callback ID
696 * @arg @ref HAL_SDRAM_MSP_DEINIT_CB_ID SDRAM MspDeInit callback ID
697 * @arg @ref HAL_SDRAM_REFRESH_ERR_CB_ID SDRAM Refresh Error callback ID
698 * @param pCallback : pointer to the Callback function
701 HAL_StatusTypeDef
HAL_SDRAM_RegisterCallback (SDRAM_HandleTypeDef
*hsdram
, HAL_SDRAM_CallbackIDTypeDef CallbackId
, pSDRAM_CallbackTypeDef pCallback
)
703 HAL_StatusTypeDef status
= HAL_OK
;
704 HAL_SDRAM_StateTypeDef state
;
706 if(pCallback
== NULL
)
714 state
= hsdram
->State
;
715 if((state
== HAL_SDRAM_STATE_READY
) || (state
== HAL_SDRAM_STATE_WRITE_PROTECTED
))
719 case HAL_SDRAM_MSP_INIT_CB_ID
:
720 hsdram
->MspInitCallback
= pCallback
;
722 case HAL_SDRAM_MSP_DEINIT_CB_ID
:
723 hsdram
->MspDeInitCallback
= pCallback
;
725 case HAL_SDRAM_REFRESH_ERR_CB_ID
:
726 hsdram
->RefreshErrorCallback
= pCallback
;
729 /* update return status */
734 else if(hsdram
->State
== HAL_SDRAM_STATE_RESET
)
738 case HAL_SDRAM_MSP_INIT_CB_ID
:
739 hsdram
->MspInitCallback
= pCallback
;
741 case HAL_SDRAM_MSP_DEINIT_CB_ID
:
742 hsdram
->MspDeInitCallback
= pCallback
;
745 /* update return status */
752 /* update return status */
757 __HAL_UNLOCK(hsdram
);
761 * @brief Unregister a User SDRAM Callback
762 * SDRAM Callback is redirected to the weak (surcharged) predefined callback
763 * @param hsdram : SDRAM handle
764 * @param CallbackId : ID of the callback to be unregistered
765 * This parameter can be one of the following values:
766 * @arg @ref HAL_SDRAM_MSP_INIT_CB_ID SDRAM MspInit callback ID
767 * @arg @ref HAL_SDRAM_MSP_DEINIT_CB_ID SDRAM MspDeInit callback ID
768 * @arg @ref HAL_SDRAM_REFRESH_ERR_CB_ID SDRAM Refresh Error callback ID
769 * @arg @ref HAL_SDRAM_DMA_XFER_CPLT_CB_ID SDRAM DMA Xfer Complete callback ID
770 * @arg @ref HAL_SDRAM_DMA_XFER_ERR_CB_ID SDRAM DMA Xfer Error callback ID
773 HAL_StatusTypeDef
HAL_SDRAM_UnRegisterCallback (SDRAM_HandleTypeDef
*hsdram
, HAL_SDRAM_CallbackIDTypeDef CallbackId
)
775 HAL_StatusTypeDef status
= HAL_OK
;
776 HAL_SDRAM_StateTypeDef state
;
781 state
= hsdram
->State
;
782 if((state
== HAL_SDRAM_STATE_READY
) || (state
== HAL_SDRAM_STATE_WRITE_PROTECTED
))
786 case HAL_SDRAM_MSP_INIT_CB_ID
:
787 hsdram
->MspInitCallback
= HAL_SDRAM_MspInit
;
789 case HAL_SDRAM_MSP_DEINIT_CB_ID
:
790 hsdram
->MspDeInitCallback
= HAL_SDRAM_MspDeInit
;
792 case HAL_SDRAM_REFRESH_ERR_CB_ID
:
793 hsdram
->RefreshErrorCallback
= HAL_SDRAM_RefreshErrorCallback
;
795 case HAL_SDRAM_DMA_XFER_CPLT_CB_ID
:
796 hsdram
->DmaXferCpltCallback
= HAL_SDRAM_DMA_XferCpltCallback
;
798 case HAL_SDRAM_DMA_XFER_ERR_CB_ID
:
799 hsdram
->DmaXferErrorCallback
= HAL_SDRAM_DMA_XferErrorCallback
;
802 /* update return status */
807 else if(hsdram
->State
== HAL_SDRAM_STATE_RESET
)
811 case HAL_SDRAM_MSP_INIT_CB_ID
:
812 hsdram
->MspInitCallback
= HAL_SDRAM_MspInit
;
814 case HAL_SDRAM_MSP_DEINIT_CB_ID
:
815 hsdram
->MspDeInitCallback
= HAL_SDRAM_MspDeInit
;
818 /* update return status */
825 /* update return status */
830 __HAL_UNLOCK(hsdram
);
835 * @brief Register a User SDRAM Callback for DMA transfers
836 * To be used instead of the weak (surcharged) predefined callback
837 * @param hsdram : SDRAM handle
838 * @param CallbackId : ID of the callback to be registered
839 * This parameter can be one of the following values:
840 * @arg @ref HAL_SDRAM_DMA_XFER_CPLT_CB_ID SDRAM DMA Xfer Complete callback ID
841 * @arg @ref HAL_SDRAM_DMA_XFER_ERR_CB_ID SDRAM DMA Xfer Error callback ID
842 * @param pCallback : pointer to the Callback function
845 HAL_StatusTypeDef
HAL_SDRAM_RegisterDmaCallback(SDRAM_HandleTypeDef
*hsdram
, HAL_SDRAM_CallbackIDTypeDef CallbackId
, pSDRAM_DmaCallbackTypeDef pCallback
)
847 HAL_StatusTypeDef status
= HAL_OK
;
848 HAL_SDRAM_StateTypeDef state
;
850 if(pCallback
== NULL
)
858 state
= hsdram
->State
;
859 if((state
== HAL_SDRAM_STATE_READY
) || (state
== HAL_SDRAM_STATE_WRITE_PROTECTED
))
863 case HAL_SDRAM_DMA_XFER_CPLT_CB_ID
:
864 hsdram
->DmaXferCpltCallback
= pCallback
;
866 case HAL_SDRAM_DMA_XFER_ERR_CB_ID
:
867 hsdram
->DmaXferErrorCallback
= pCallback
;
870 /* update return status */
877 /* update return status */
882 __HAL_UNLOCK(hsdram
);
891 /** @defgroup SDRAM_Exported_Functions_Group3 Control functions
892 * @brief management functions
895 ==============================================================================
896 ##### SDRAM Control functions #####
897 ==============================================================================
899 This subsection provides a set of functions allowing to control dynamically
907 * @brief Enables dynamically SDRAM write protection.
908 * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
909 * the configuration information for SDRAM module.
912 HAL_StatusTypeDef
HAL_SDRAM_WriteProtection_Enable(SDRAM_HandleTypeDef
*hsdram
)
914 /* Check the SDRAM controller state */
915 if(hsdram
->State
== HAL_SDRAM_STATE_BUSY
)
920 /* Update the SDRAM state */
921 hsdram
->State
= HAL_SDRAM_STATE_BUSY
;
923 /* Enable write protection */
924 FMC_SDRAM_WriteProtection_Enable(hsdram
->Instance
, hsdram
->Init
.SDBank
);
926 /* Update the SDRAM state */
927 hsdram
->State
= HAL_SDRAM_STATE_WRITE_PROTECTED
;
933 * @brief Disables dynamically SDRAM write protection.
934 * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
935 * the configuration information for SDRAM module.
938 HAL_StatusTypeDef
HAL_SDRAM_WriteProtection_Disable(SDRAM_HandleTypeDef
*hsdram
)
940 /* Check the SDRAM controller state */
941 if(hsdram
->State
== HAL_SDRAM_STATE_BUSY
)
946 /* Update the SDRAM state */
947 hsdram
->State
= HAL_SDRAM_STATE_BUSY
;
949 /* Disable write protection */
950 FMC_SDRAM_WriteProtection_Disable(hsdram
->Instance
, hsdram
->Init
.SDBank
);
952 /* Update the SDRAM state */
953 hsdram
->State
= HAL_SDRAM_STATE_READY
;
959 * @brief Sends Command to the SDRAM bank.
960 * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
961 * the configuration information for SDRAM module.
962 * @param Command SDRAM command structure
963 * @param Timeout Timeout duration
966 HAL_StatusTypeDef
HAL_SDRAM_SendCommand(SDRAM_HandleTypeDef
*hsdram
, FMC_SDRAM_CommandTypeDef
*Command
, uint32_t Timeout
)
968 /* Check the SDRAM controller state */
969 if(hsdram
->State
== HAL_SDRAM_STATE_BUSY
)
974 /* Update the SDRAM state */
975 hsdram
->State
= HAL_SDRAM_STATE_BUSY
;
977 /* Send SDRAM command */
978 FMC_SDRAM_SendCommand(hsdram
->Instance
, Command
, Timeout
);
980 /* Update the SDRAM controller state state */
981 if(Command
->CommandMode
== FMC_SDRAM_CMD_PALL
)
983 hsdram
->State
= HAL_SDRAM_STATE_PRECHARGED
;
987 hsdram
->State
= HAL_SDRAM_STATE_READY
;
994 * @brief Programs the SDRAM Memory Refresh rate.
995 * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
996 * the configuration information for SDRAM module.
997 * @param RefreshRate The SDRAM refresh rate value
1000 HAL_StatusTypeDef
HAL_SDRAM_ProgramRefreshRate(SDRAM_HandleTypeDef
*hsdram
, uint32_t RefreshRate
)
1002 /* Check the SDRAM controller state */
1003 if(hsdram
->State
== HAL_SDRAM_STATE_BUSY
)
1008 /* Update the SDRAM state */
1009 hsdram
->State
= HAL_SDRAM_STATE_BUSY
;
1011 /* Program the refresh rate */
1012 FMC_SDRAM_ProgramRefreshRate(hsdram
->Instance
,RefreshRate
);
1014 /* Update the SDRAM state */
1015 hsdram
->State
= HAL_SDRAM_STATE_READY
;
1021 * @brief Sets the Number of consecutive SDRAM Memory auto Refresh commands.
1022 * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
1023 * the configuration information for SDRAM module.
1024 * @param AutoRefreshNumber The SDRAM auto Refresh number
1025 * @retval HAL status
1027 HAL_StatusTypeDef
HAL_SDRAM_SetAutoRefreshNumber(SDRAM_HandleTypeDef
*hsdram
, uint32_t AutoRefreshNumber
)
1029 /* Check the SDRAM controller state */
1030 if(hsdram
->State
== HAL_SDRAM_STATE_BUSY
)
1035 /* Update the SDRAM state */
1036 hsdram
->State
= HAL_SDRAM_STATE_BUSY
;
1038 /* Set the Auto-Refresh number */
1039 FMC_SDRAM_SetAutoRefreshNumber(hsdram
->Instance
,AutoRefreshNumber
);
1041 /* Update the SDRAM state */
1042 hsdram
->State
= HAL_SDRAM_STATE_READY
;
1048 * @brief Returns the SDRAM memory current mode.
1049 * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
1050 * the configuration information for SDRAM module.
1051 * @retval The SDRAM memory mode.
1053 uint32_t HAL_SDRAM_GetModeStatus(SDRAM_HandleTypeDef
*hsdram
)
1055 /* Return the SDRAM memory current mode */
1056 return(FMC_SDRAM_GetModeStatus(hsdram
->Instance
, hsdram
->Init
.SDBank
));
1063 /** @defgroup SDRAM_Exported_Functions_Group4 State functions
1064 * @brief Peripheral State functions
1067 ==============================================================================
1068 ##### SDRAM State functions #####
1069 ==============================================================================
1071 This subsection permits to get in run-time the status of the SDRAM controller
1079 * @brief Returns the SDRAM state.
1080 * @param hsdram pointer to a SDRAM_HandleTypeDef structure that contains
1081 * the configuration information for SDRAM module.
1084 HAL_SDRAM_StateTypeDef
HAL_SDRAM_GetState(SDRAM_HandleTypeDef
*hsdram
)
1086 return hsdram
->State
;
1096 #endif /* HAL_SDRAM_MODULE_ENABLED */
1105 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/