STM32H735xx HAL User Manual
stm32h7xx_hal_sram.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32h7xx_hal_sram.c
00004   * @author  MCD Application Team
00005   * @brief   SRAM HAL module driver.
00006   *          This file provides a generic firmware to drive SRAM memories
00007   *          mounted as external device.
00008   *
00009   ******************************************************************************
00010   * @attention
00011   *
00012   * Copyright (c) 2017 STMicroelectronics.
00013   * All rights reserved.
00014   *
00015   * This software is licensed under terms that can be found in the LICENSE file
00016   * in the root directory of this software component.
00017   * If no LICENSE file comes with this software, it is provided AS-IS.
00018   *
00019   ******************************************************************************
00020   @verbatim
00021   ==============================================================================
00022                           ##### How to use this driver #####
00023   ==============================================================================
00024   [..]
00025     This driver is a generic layered driver which contains a set of APIs used to
00026     control SRAM memories. It uses the FMC layer functions to interface
00027     with SRAM devices.
00028     The following sequence should be followed to configure the FMC to interface
00029     with SRAM/PSRAM memories:
00030 
00031    (#) Declare a SRAM_HandleTypeDef handle structure, for example:
00032           SRAM_HandleTypeDef  hsram; and:
00033 
00034        (++) Fill the SRAM_HandleTypeDef handle "Init" field with the allowed
00035             values of the structure member.
00036 
00037        (++) Fill the SRAM_HandleTypeDef handle "Instance" field with a predefined
00038             base register instance for NOR or SRAM device
00039 
00040        (++) Fill the SRAM_HandleTypeDef handle "Extended" field with a predefined
00041             base register instance for NOR or SRAM extended mode
00042 
00043    (#) Declare two FMC_NORSRAM_TimingTypeDef structures, for both normal and extended
00044        mode timings; for example:
00045           FMC_NORSRAM_TimingTypeDef  Timing and FMC_NORSRAM_TimingTypeDef  ExTiming;
00046       and fill its fields with the allowed values of the structure member.
00047 
00048    (#) Initialize the SRAM Controller by calling the function HAL_SRAM_Init(). This function
00049        performs the following sequence:
00050 
00051        (##) MSP hardware layer configuration using the function HAL_SRAM_MspInit()
00052        (##) Control register configuration using the FMC NORSRAM interface function
00053             FMC_NORSRAM_Init()
00054        (##) Timing register configuration using the FMC NORSRAM interface function
00055             FMC_NORSRAM_Timing_Init()
00056        (##) Extended mode Timing register configuration using the FMC NORSRAM interface function
00057             FMC_NORSRAM_Extended_Timing_Init()
00058        (##) Enable the SRAM device using the macro __FMC_NORSRAM_ENABLE()
00059 
00060    (#) At this stage you can perform read/write accesses from/to the memory connected
00061        to the NOR/SRAM Bank. You can perform either polling or DMA transfer using the
00062        following APIs:
00063        (++) HAL_SRAM_Read()/HAL_SRAM_Write() for polling read/write access
00064        (++) HAL_SRAM_Read_DMA()/HAL_SRAM_Write_DMA() for DMA read/write transfer
00065 
00066    (#) You can also control the SRAM device by calling the control APIs HAL_SRAM_WriteOperation_Enable()/
00067        HAL_SRAM_WriteOperation_Disable() to respectively enable/disable the SRAM write operation
00068 
00069    (#) You can continuously monitor the SRAM device HAL state by calling the function
00070        HAL_SRAM_GetState()
00071 
00072        *** Callback registration ***
00073     =============================================
00074     [..]
00075       The compilation define  USE_HAL_SRAM_REGISTER_CALLBACKS when set to 1
00076       allows the user to configure dynamically the driver callbacks.
00077 
00078       Use Functions HAL_SRAM_RegisterCallback() to register a user callback,
00079       it allows to register following callbacks:
00080         (+) MspInitCallback    : SRAM MspInit.
00081         (+) MspDeInitCallback  : SRAM MspDeInit.
00082       This function takes as parameters the HAL peripheral handle, the Callback ID
00083       and a pointer to the user callback function.
00084 
00085       Use function HAL_SRAM_UnRegisterCallback() to reset a callback to the default
00086       weak (surcharged) function. It allows to reset following callbacks:
00087         (+) MspInitCallback    : SRAM MspInit.
00088         (+) MspDeInitCallback  : SRAM MspDeInit.
00089       This function) takes as parameters the HAL peripheral handle and the Callback ID.
00090 
00091       By default, after the HAL_SRAM_Init and if the state is HAL_SRAM_STATE_RESET
00092       all callbacks are reset to the corresponding legacy weak (surcharged) functions.
00093       Exception done for MspInit and MspDeInit callbacks that are respectively
00094       reset to the legacy weak (surcharged) functions in the HAL_SRAM_Init
00095       and  HAL_SRAM_DeInit only when these callbacks are null (not registered beforehand).
00096       If not, MspInit or MspDeInit are not null, the HAL_SRAM_Init and HAL_SRAM_DeInit
00097       keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
00098 
00099       Callbacks can be registered/unregistered in READY state only.
00100       Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
00101       in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
00102       during the Init/DeInit.
00103       In that case first register the MspInit/MspDeInit user callbacks
00104       using HAL_SRAM_RegisterCallback before calling HAL_SRAM_DeInit
00105       or HAL_SRAM_Init function.
00106 
00107       When The compilation define USE_HAL_SRAM_REGISTER_CALLBACKS is set to 0 or
00108       not defined, the callback registering feature is not available
00109       and weak (surcharged) callbacks are used.
00110 
00111   @endverbatim
00112   ******************************************************************************
00113   */
00114 
00115 /* Includes ------------------------------------------------------------------*/
00116 #include "stm32h7xx_hal.h"
00117 
00118 
00119 /** @addtogroup STM32H7xx_HAL_Driver
00120   * @{
00121   */
00122 
00123 #ifdef HAL_SRAM_MODULE_ENABLED
00124 
00125 /** @defgroup SRAM SRAM
00126   * @brief SRAM driver modules
00127   * @{
00128   */
00129 
00130 /* Private typedef -----------------------------------------------------------*/
00131 /* Private define ------------------------------------------------------------*/
00132 /* Private macro -------------------------------------------------------------*/
00133 /* Private variables ---------------------------------------------------------*/
00134 /* Private function prototypes -----------------------------------------------*/
00135 static void SRAM_DMACplt(MDMA_HandleTypeDef *hmdma);
00136 static void SRAM_DMACpltProt(MDMA_HandleTypeDef *hmdma);
00137 static void SRAM_DMAError(MDMA_HandleTypeDef *hmdma);
00138 
00139 /* Exported functions --------------------------------------------------------*/
00140 
00141 /** @defgroup SRAM_Exported_Functions SRAM Exported Functions
00142   * @{
00143   */
00144 
00145 /** @defgroup SRAM_Exported_Functions_Group1 Initialization and de-initialization functions
00146   * @brief    Initialization and Configuration functions.
00147   *
00148   @verbatim
00149   ==============================================================================
00150            ##### SRAM Initialization and de_initialization functions #####
00151   ==============================================================================
00152     [..]  This section provides functions allowing to initialize/de-initialize
00153           the SRAM memory
00154 
00155 @endverbatim
00156   * @{
00157   */
00158 
00159 /**
00160   * @brief  Performs the SRAM device initialization sequence
00161   * @param  hsram pointer to a SRAM_HandleTypeDef structure that contains
00162   *                the configuration information for SRAM module.
00163   * @param  Timing Pointer to SRAM control timing structure
00164   * @param  ExtTiming Pointer to SRAM extended mode timing structure
00165   * @retval HAL status
00166   */
00167 HAL_StatusTypeDef HAL_SRAM_Init(SRAM_HandleTypeDef *hsram, FMC_NORSRAM_TimingTypeDef *Timing,
00168                                 FMC_NORSRAM_TimingTypeDef *ExtTiming)
00169 {
00170   /* Check the SRAM handle parameter */
00171   if (hsram == NULL)
00172   {
00173     return HAL_ERROR;
00174   }
00175 
00176   if (hsram->State == HAL_SRAM_STATE_RESET)
00177   {
00178     /* Allocate lock resource and initialize it */
00179     hsram->Lock = HAL_UNLOCKED;
00180 
00181 #if (USE_HAL_SRAM_REGISTER_CALLBACKS == 1)
00182     if (hsram->MspInitCallback == NULL)
00183     {
00184       hsram->MspInitCallback = HAL_SRAM_MspInit;
00185     }
00186     hsram->DmaXferCpltCallback = HAL_SRAM_DMA_XferCpltCallback;
00187     hsram->DmaXferErrorCallback = HAL_SRAM_DMA_XferErrorCallback;
00188 
00189     /* Init the low level hardware */
00190     hsram->MspInitCallback(hsram);
00191 #else
00192     /* Initialize the low level hardware (MSP) */
00193     HAL_SRAM_MspInit(hsram);
00194 #endif /* USE_HAL_SRAM_REGISTER_CALLBACKS */
00195   }
00196 
00197   /* Initialize SRAM control Interface */
00198   (void)FMC_NORSRAM_Init(hsram->Instance, &(hsram->Init));
00199 
00200   /* Initialize SRAM timing Interface */
00201   (void)FMC_NORSRAM_Timing_Init(hsram->Instance, Timing, hsram->Init.NSBank);
00202 
00203   /* Initialize SRAM extended mode timing Interface */
00204   (void)FMC_NORSRAM_Extended_Timing_Init(hsram->Extended, ExtTiming, hsram->Init.NSBank,
00205                                          hsram->Init.ExtendedMode);
00206 
00207   /* Enable the NORSRAM device */
00208   __FMC_NORSRAM_ENABLE(hsram->Instance, hsram->Init.NSBank);
00209 
00210   /* Enable FMC Peripheral */
00211   __FMC_ENABLE();
00212 
00213   /* Initialize the SRAM controller state */
00214   hsram->State = HAL_SRAM_STATE_READY;
00215 
00216   return HAL_OK;
00217 }
00218 
00219 /**
00220   * @brief  Performs the SRAM device De-initialization sequence.
00221   * @param  hsram pointer to a SRAM_HandleTypeDef structure that contains
00222   *                the configuration information for SRAM module.
00223   * @retval HAL status
00224   */
00225 HAL_StatusTypeDef HAL_SRAM_DeInit(SRAM_HandleTypeDef *hsram)
00226 {
00227 #if (USE_HAL_SRAM_REGISTER_CALLBACKS == 1)
00228   if (hsram->MspDeInitCallback == NULL)
00229   {
00230     hsram->MspDeInitCallback = HAL_SRAM_MspDeInit;
00231   }
00232 
00233   /* DeInit the low level hardware */
00234   hsram->MspDeInitCallback(hsram);
00235 #else
00236   /* De-Initialize the low level hardware (MSP) */
00237   HAL_SRAM_MspDeInit(hsram);
00238 #endif /* USE_HAL_SRAM_REGISTER_CALLBACKS */
00239 
00240   /* Configure the SRAM registers with their reset values */
00241   (void)FMC_NORSRAM_DeInit(hsram->Instance, hsram->Extended, hsram->Init.NSBank);
00242 
00243   /* Reset the SRAM controller state */
00244   hsram->State = HAL_SRAM_STATE_RESET;
00245 
00246   /* Release Lock */
00247   __HAL_UNLOCK(hsram);
00248 
00249   return HAL_OK;
00250 }
00251 
00252 /**
00253   * @brief  SRAM MSP Init.
00254   * @param  hsram pointer to a SRAM_HandleTypeDef structure that contains
00255   *                the configuration information for SRAM module.
00256   * @retval None
00257   */
00258 __weak void HAL_SRAM_MspInit(SRAM_HandleTypeDef *hsram)
00259 {
00260   /* Prevent unused argument(s) compilation warning */
00261   UNUSED(hsram);
00262 
00263   /* NOTE : This function Should not be modified, when the callback is needed,
00264             the HAL_SRAM_MspInit could be implemented in the user file
00265    */
00266 }
00267 
00268 /**
00269   * @brief  SRAM MSP DeInit.
00270   * @param  hsram pointer to a SRAM_HandleTypeDef structure that contains
00271   *                the configuration information for SRAM module.
00272   * @retval None
00273   */
00274 __weak void HAL_SRAM_MspDeInit(SRAM_HandleTypeDef *hsram)
00275 {
00276   /* Prevent unused argument(s) compilation warning */
00277   UNUSED(hsram);
00278 
00279   /* NOTE : This function Should not be modified, when the callback is needed,
00280             the HAL_SRAM_MspDeInit could be implemented in the user file
00281    */
00282 }
00283 
00284 /**
00285   * @brief  DMA transfer complete callback.
00286   * @param  hmdma pointer to a SRAM_HandleTypeDef structure that contains
00287   *                the configuration information for SRAM module.
00288   * @retval None
00289   */
00290 __weak void HAL_SRAM_DMA_XferCpltCallback(MDMA_HandleTypeDef *hmdma)
00291 {
00292   /* Prevent unused argument(s) compilation warning */
00293   UNUSED(hmdma);
00294 
00295   /* NOTE : This function Should not be modified, when the callback is needed,
00296             the HAL_SRAM_DMA_XferCpltCallback could be implemented in the user file
00297    */
00298 }
00299 
00300 /**
00301   * @brief  DMA transfer complete error callback.
00302   * @param  hmdma pointer to a SRAM_HandleTypeDef structure that contains
00303   *                the configuration information for SRAM module.
00304   * @retval None
00305   */
00306 __weak void HAL_SRAM_DMA_XferErrorCallback(MDMA_HandleTypeDef *hmdma)
00307 {
00308   /* Prevent unused argument(s) compilation warning */
00309   UNUSED(hmdma);
00310 
00311   /* NOTE : This function Should not be modified, when the callback is needed,
00312             the HAL_SRAM_DMA_XferErrorCallback could be implemented in the user file
00313    */
00314 }
00315 
00316 /**
00317   * @}
00318   */
00319 
00320 /** @defgroup SRAM_Exported_Functions_Group2 Input Output and memory control functions
00321   * @brief    Input Output and memory control functions
00322   *
00323   @verbatim
00324   ==============================================================================
00325                   ##### SRAM Input and Output functions #####
00326   ==============================================================================
00327   [..]
00328     This section provides functions allowing to use and control the SRAM memory
00329 
00330 @endverbatim
00331   * @{
00332   */
00333 
00334 /**
00335   * @brief  Reads 8-bit buffer from SRAM memory.
00336   * @param  hsram pointer to a SRAM_HandleTypeDef structure that contains
00337   *                the configuration information for SRAM module.
00338   * @param  pAddress Pointer to read start address
00339   * @param  pDstBuffer Pointer to destination buffer
00340   * @param  BufferSize Size of the buffer to read from memory
00341   * @retval HAL status
00342   */
00343 HAL_StatusTypeDef HAL_SRAM_Read_8b(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint8_t *pDstBuffer,
00344                                    uint32_t BufferSize)
00345 {
00346   uint32_t size;
00347   __IO uint8_t *psramaddress = (uint8_t *)pAddress;
00348   uint8_t *pdestbuff = pDstBuffer;
00349   HAL_SRAM_StateTypeDef state = hsram->State;
00350 
00351   /* Check the SRAM controller state */
00352   if ((state == HAL_SRAM_STATE_READY) || (state == HAL_SRAM_STATE_PROTECTED))
00353   {
00354     /* Process Locked */
00355     __HAL_LOCK(hsram);
00356 
00357     /* Update the SRAM controller state */
00358     hsram->State = HAL_SRAM_STATE_BUSY;
00359 
00360     /* Read data from memory */
00361     for (size = BufferSize; size != 0U; size--)
00362     {
00363       *pdestbuff = *psramaddress;
00364       pdestbuff++;
00365       psramaddress++;
00366     }
00367 
00368     /* Update the SRAM controller state */
00369     hsram->State = state;
00370 
00371     /* Process unlocked */
00372     __HAL_UNLOCK(hsram);
00373   }
00374   else
00375   {
00376     return HAL_ERROR;
00377   }
00378 
00379   return HAL_OK;
00380 }
00381 
00382 /**
00383   * @brief  Writes 8-bit buffer to SRAM memory.
00384   * @param  hsram pointer to a SRAM_HandleTypeDef structure that contains
00385   *                the configuration information for SRAM module.
00386   * @param  pAddress Pointer to write start address
00387   * @param  pSrcBuffer Pointer to source buffer to write
00388   * @param  BufferSize Size of the buffer to write to memory
00389   * @retval HAL status
00390   */
00391 HAL_StatusTypeDef HAL_SRAM_Write_8b(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint8_t *pSrcBuffer,
00392                                     uint32_t BufferSize)
00393 {
00394   uint32_t size;
00395   __IO uint8_t *psramaddress = (uint8_t *)pAddress;
00396   uint8_t *psrcbuff = pSrcBuffer;
00397 
00398   /* Check the SRAM controller state */
00399   if (hsram->State == HAL_SRAM_STATE_READY)
00400   {
00401     /* Process Locked */
00402     __HAL_LOCK(hsram);
00403 
00404     /* Update the SRAM controller state */
00405     hsram->State = HAL_SRAM_STATE_BUSY;
00406 
00407     /* Write data to memory */
00408     for (size = BufferSize; size != 0U; size--)
00409     {
00410       *psramaddress = *psrcbuff;
00411       psrcbuff++;
00412       psramaddress++;
00413     }
00414 
00415     /* Update the SRAM controller state */
00416     hsram->State = HAL_SRAM_STATE_READY;
00417 
00418     /* Process unlocked */
00419     __HAL_UNLOCK(hsram);
00420   }
00421   else
00422   {
00423     return HAL_ERROR;
00424   }
00425 
00426   return HAL_OK;
00427 }
00428 
00429 /**
00430   * @brief  Reads 16-bit buffer from SRAM memory.
00431   * @param  hsram pointer to a SRAM_HandleTypeDef structure that contains
00432   *                the configuration information for SRAM module.
00433   * @param  pAddress Pointer to read start address
00434   * @param  pDstBuffer Pointer to destination buffer
00435   * @param  BufferSize Size of the buffer to read from memory
00436   * @retval HAL status
00437   */
00438 HAL_StatusTypeDef HAL_SRAM_Read_16b(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint16_t *pDstBuffer,
00439                                     uint32_t BufferSize)
00440 {
00441   uint32_t size;
00442   __IO uint32_t *psramaddress = pAddress;
00443   uint16_t *pdestbuff = pDstBuffer;
00444   uint8_t limit;
00445   HAL_SRAM_StateTypeDef state = hsram->State;
00446 
00447   /* Check the SRAM controller state */
00448   if ((state == HAL_SRAM_STATE_READY) || (state == HAL_SRAM_STATE_PROTECTED))
00449   {
00450     /* Process Locked */
00451     __HAL_LOCK(hsram);
00452 
00453     /* Update the SRAM controller state */
00454     hsram->State = HAL_SRAM_STATE_BUSY;
00455 
00456     /* Check if the size is a 32-bits multiple */
00457     limit = (((BufferSize % 2U) != 0U) ? 1U : 0U);
00458 
00459     /* Read data from memory */
00460     for (size = BufferSize; size != limit; size -= 2U)
00461     {
00462       *pdestbuff = (uint16_t)((*psramaddress) & 0x0000FFFFU);
00463       pdestbuff++;
00464       *pdestbuff = (uint16_t)(((*psramaddress) & 0xFFFF0000U) >> 16U);
00465       pdestbuff++;
00466       psramaddress++;
00467     }
00468 
00469     /* Read last 16-bits if size is not 32-bits multiple */
00470     if (limit != 0U)
00471     {
00472       *pdestbuff = (uint16_t)((*psramaddress) & 0x0000FFFFU);
00473     }
00474 
00475     /* Update the SRAM controller state */
00476     hsram->State = state;
00477 
00478     /* Process unlocked */
00479     __HAL_UNLOCK(hsram);
00480   }
00481   else
00482   {
00483     return HAL_ERROR;
00484   }
00485 
00486   return HAL_OK;
00487 }
00488 
00489 /**
00490   * @brief  Writes 16-bit buffer to SRAM memory.
00491   * @param  hsram pointer to a SRAM_HandleTypeDef structure that contains
00492   *                the configuration information for SRAM module.
00493   * @param  pAddress Pointer to write start address
00494   * @param  pSrcBuffer Pointer to source buffer to write
00495   * @param  BufferSize Size of the buffer to write to memory
00496   * @retval HAL status
00497   */
00498 HAL_StatusTypeDef HAL_SRAM_Write_16b(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint16_t *pSrcBuffer,
00499                                      uint32_t BufferSize)
00500 {
00501   uint32_t size;
00502   __IO uint32_t *psramaddress = pAddress;
00503   uint16_t *psrcbuff = pSrcBuffer;
00504   uint8_t limit;
00505 
00506   /* Check the SRAM controller state */
00507   if (hsram->State == HAL_SRAM_STATE_READY)
00508   {
00509     /* Process Locked */
00510     __HAL_LOCK(hsram);
00511 
00512     /* Update the SRAM controller state */
00513     hsram->State = HAL_SRAM_STATE_BUSY;
00514 
00515     /* Check if the size is a 32-bits multiple */
00516     limit = (((BufferSize % 2U) != 0U) ? 1U : 0U);
00517 
00518     /* Write data to memory */
00519     for (size = BufferSize; size != limit; size -= 2U)
00520     {
00521       *psramaddress = (uint32_t)(*psrcbuff);
00522       psrcbuff++;
00523       *psramaddress |= ((uint32_t)(*psrcbuff) << 16U);
00524       psrcbuff++;
00525       psramaddress++;
00526     }
00527 
00528     /* Write last 16-bits if size is not 32-bits multiple */
00529     if (limit != 0U)
00530     {
00531       *psramaddress = ((uint32_t)(*psrcbuff) & 0x0000FFFFU) | ((*psramaddress) & 0xFFFF0000U);
00532     }
00533 
00534     /* Update the SRAM controller state */
00535     hsram->State = HAL_SRAM_STATE_READY;
00536 
00537     /* Process unlocked */
00538     __HAL_UNLOCK(hsram);
00539   }
00540   else
00541   {
00542     return HAL_ERROR;
00543   }
00544 
00545   return HAL_OK;
00546 }
00547 
00548 /**
00549   * @brief  Reads 32-bit buffer from SRAM memory.
00550   * @param  hsram pointer to a SRAM_HandleTypeDef structure that contains
00551   *                the configuration information for SRAM module.
00552   * @param  pAddress Pointer to read start address
00553   * @param  pDstBuffer Pointer to destination buffer
00554   * @param  BufferSize Size of the buffer to read from memory
00555   * @retval HAL status
00556   */
00557 HAL_StatusTypeDef HAL_SRAM_Read_32b(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint32_t *pDstBuffer,
00558                                     uint32_t BufferSize)
00559 {
00560   uint32_t size;
00561   __IO uint32_t *psramaddress = pAddress;
00562   uint32_t *pdestbuff = pDstBuffer;
00563   HAL_SRAM_StateTypeDef state = hsram->State;
00564 
00565   /* Check the SRAM controller state */
00566   if ((state == HAL_SRAM_STATE_READY) || (state == HAL_SRAM_STATE_PROTECTED))
00567   {
00568     /* Process Locked */
00569     __HAL_LOCK(hsram);
00570 
00571     /* Update the SRAM controller state */
00572     hsram->State = HAL_SRAM_STATE_BUSY;
00573 
00574     /* Read data from memory */
00575     for (size = BufferSize; size != 0U; size--)
00576     {
00577       *pdestbuff = *psramaddress;
00578       pdestbuff++;
00579       psramaddress++;
00580     }
00581 
00582     /* Update the SRAM controller state */
00583     hsram->State = state;
00584 
00585     /* Process unlocked */
00586     __HAL_UNLOCK(hsram);
00587   }
00588   else
00589   {
00590     return HAL_ERROR;
00591   }
00592 
00593   return HAL_OK;
00594 }
00595 
00596 /**
00597   * @brief  Writes 32-bit buffer to SRAM memory.
00598   * @param  hsram pointer to a SRAM_HandleTypeDef structure that contains
00599   *                the configuration information for SRAM module.
00600   * @param  pAddress Pointer to write start address
00601   * @param  pSrcBuffer Pointer to source buffer to write
00602   * @param  BufferSize Size of the buffer to write to memory
00603   * @retval HAL status
00604   */
00605 HAL_StatusTypeDef HAL_SRAM_Write_32b(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint32_t *pSrcBuffer,
00606                                      uint32_t BufferSize)
00607 {
00608   uint32_t size;
00609   __IO uint32_t *psramaddress = pAddress;
00610   uint32_t *psrcbuff = pSrcBuffer;
00611 
00612   /* Check the SRAM controller state */
00613   if (hsram->State == HAL_SRAM_STATE_READY)
00614   {
00615     /* Process Locked */
00616     __HAL_LOCK(hsram);
00617 
00618     /* Update the SRAM controller state */
00619     hsram->State = HAL_SRAM_STATE_BUSY;
00620 
00621     /* Write data to memory */
00622     for (size = BufferSize; size != 0U; size--)
00623     {
00624       *psramaddress = *psrcbuff;
00625       psrcbuff++;
00626       psramaddress++;
00627     }
00628 
00629     /* Update the SRAM controller state */
00630     hsram->State = HAL_SRAM_STATE_READY;
00631 
00632     /* Process unlocked */
00633     __HAL_UNLOCK(hsram);
00634   }
00635   else
00636   {
00637     return HAL_ERROR;
00638   }
00639 
00640   return HAL_OK;
00641 }
00642 
00643 /**
00644   * @brief  Reads a Words data from the SRAM memory using DMA transfer.
00645   * @param  hsram pointer to a SRAM_HandleTypeDef structure that contains
00646   *                the configuration information for SRAM module.
00647   * @param  pAddress Pointer to read start address
00648   * @param  pDstBuffer Pointer to destination buffer
00649   * @param  BufferSize Size of the buffer to read from memory
00650   * @retval HAL status
00651   */
00652 HAL_StatusTypeDef HAL_SRAM_Read_DMA(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint32_t *pDstBuffer,
00653                                     uint32_t BufferSize)
00654 {
00655   HAL_StatusTypeDef status;
00656   HAL_SRAM_StateTypeDef state = hsram->State;
00657 
00658   /* Check the SRAM controller state */
00659   if ((state == HAL_SRAM_STATE_READY) || (state == HAL_SRAM_STATE_PROTECTED))
00660   {
00661     /* Process Locked */
00662     __HAL_LOCK(hsram);
00663 
00664     /* Update the SRAM controller state */
00665     hsram->State = HAL_SRAM_STATE_BUSY;
00666 
00667     /* Configure DMA user callbacks */
00668     if (state == HAL_SRAM_STATE_READY)
00669     {
00670       hsram->hmdma->XferCpltCallback = SRAM_DMACplt;
00671     }
00672     else
00673     {
00674       hsram->hmdma->XferCpltCallback = SRAM_DMACpltProt;
00675     }
00676     hsram->hmdma->XferErrorCallback = SRAM_DMAError;
00677 
00678     /* Enable the DMA Stream */
00679     status = HAL_MDMA_Start_IT(hsram->hmdma, (uint32_t)pAddress, (uint32_t)pDstBuffer, (uint32_t)(BufferSize * 4U), 1);
00680 
00681     /* Process unlocked */
00682     __HAL_UNLOCK(hsram);
00683   }
00684   else
00685   {
00686     status = HAL_ERROR;
00687   }
00688 
00689   return status;
00690 }
00691 
00692 /**
00693   * @brief  Writes a Words data buffer to SRAM memory using DMA transfer.
00694   * @param  hsram pointer to a SRAM_HandleTypeDef structure that contains
00695   *                the configuration information for SRAM module.
00696   * @param  pAddress Pointer to write start address
00697   * @param  pSrcBuffer Pointer to source buffer to write
00698   * @param  BufferSize Size of the buffer to write to memory
00699   * @retval HAL status
00700   */
00701 HAL_StatusTypeDef HAL_SRAM_Write_DMA(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint32_t *pSrcBuffer,
00702                                      uint32_t BufferSize)
00703 {
00704   HAL_StatusTypeDef status;
00705 
00706   /* Check the SRAM controller state */
00707   if (hsram->State == HAL_SRAM_STATE_READY)
00708   {
00709     /* Process Locked */
00710     __HAL_LOCK(hsram);
00711 
00712     /* Update the SRAM controller state */
00713     hsram->State = HAL_SRAM_STATE_BUSY;
00714 
00715     /* Configure DMA user callbacks */
00716     hsram->hmdma->XferCpltCallback = SRAM_DMACplt;
00717     hsram->hmdma->XferErrorCallback = SRAM_DMAError;
00718 
00719     /* Enable the DMA Stream */
00720     status = HAL_MDMA_Start_IT(hsram->hmdma, (uint32_t)pSrcBuffer, (uint32_t)pAddress, (uint32_t)(BufferSize * 4U), 1);
00721 
00722     /* Process unlocked */
00723     __HAL_UNLOCK(hsram);
00724   }
00725   else
00726   {
00727     status = HAL_ERROR;
00728   }
00729 
00730   return status;
00731 }
00732 
00733 #if (USE_HAL_SRAM_REGISTER_CALLBACKS == 1)
00734 /**
00735   * @brief  Register a User SRAM Callback
00736   *         To be used instead of the weak (surcharged) predefined callback
00737   * @param hsram : SRAM handle
00738   * @param CallbackId : ID of the callback to be registered
00739   *        This parameter can be one of the following values:
00740   *          @arg @ref HAL_SRAM_MSP_INIT_CB_ID       SRAM MspInit callback ID
00741   *          @arg @ref HAL_SRAM_MSP_DEINIT_CB_ID     SRAM MspDeInit callback ID
00742   * @param pCallback : pointer to the Callback function
00743   * @retval status
00744   */
00745 HAL_StatusTypeDef HAL_SRAM_RegisterCallback(SRAM_HandleTypeDef *hsram, HAL_SRAM_CallbackIDTypeDef CallbackId,
00746                                             pSRAM_CallbackTypeDef pCallback)
00747 {
00748   HAL_StatusTypeDef status = HAL_OK;
00749   HAL_SRAM_StateTypeDef state;
00750 
00751   if (pCallback == NULL)
00752   {
00753     return HAL_ERROR;
00754   }
00755 
00756   /* Process locked */
00757   __HAL_LOCK(hsram);
00758 
00759   state = hsram->State;
00760   if ((state == HAL_SRAM_STATE_READY) || (state == HAL_SRAM_STATE_RESET) || (state == HAL_SRAM_STATE_PROTECTED))
00761   {
00762     switch (CallbackId)
00763     {
00764       case HAL_SRAM_MSP_INIT_CB_ID :
00765         hsram->MspInitCallback = pCallback;
00766         break;
00767       case HAL_SRAM_MSP_DEINIT_CB_ID :
00768         hsram->MspDeInitCallback = pCallback;
00769         break;
00770       default :
00771         /* update return status */
00772         status =  HAL_ERROR;
00773         break;
00774     }
00775   }
00776   else
00777   {
00778     /* update return status */
00779     status =  HAL_ERROR;
00780   }
00781 
00782   /* Release Lock */
00783   __HAL_UNLOCK(hsram);
00784   return status;
00785 }
00786 
00787 /**
00788   * @brief  Unregister a User SRAM Callback
00789   *         SRAM Callback is redirected to the weak (surcharged) predefined callback
00790   * @param hsram : SRAM handle
00791   * @param CallbackId : ID of the callback to be unregistered
00792   *        This parameter can be one of the following values:
00793   *          @arg @ref HAL_SRAM_MSP_INIT_CB_ID       SRAM MspInit callback ID
00794   *          @arg @ref HAL_SRAM_MSP_DEINIT_CB_ID     SRAM MspDeInit callback ID
00795   *          @arg @ref HAL_SRAM_DMA_XFER_CPLT_CB_ID  SRAM DMA Xfer Complete callback ID
00796   *          @arg @ref HAL_SRAM_DMA_XFER_ERR_CB_ID   SRAM DMA Xfer Error callback ID
00797   * @retval status
00798   */
00799 HAL_StatusTypeDef HAL_SRAM_UnRegisterCallback(SRAM_HandleTypeDef *hsram, HAL_SRAM_CallbackIDTypeDef CallbackId)
00800 {
00801   HAL_StatusTypeDef status = HAL_OK;
00802   HAL_SRAM_StateTypeDef state;
00803 
00804   /* Process locked */
00805   __HAL_LOCK(hsram);
00806 
00807   state = hsram->State;
00808   if ((state == HAL_SRAM_STATE_READY) || (state == HAL_SRAM_STATE_PROTECTED))
00809   {
00810     switch (CallbackId)
00811     {
00812       case HAL_SRAM_MSP_INIT_CB_ID :
00813         hsram->MspInitCallback = HAL_SRAM_MspInit;
00814         break;
00815       case HAL_SRAM_MSP_DEINIT_CB_ID :
00816         hsram->MspDeInitCallback = HAL_SRAM_MspDeInit;
00817         break;
00818       case HAL_SRAM_DMA_XFER_CPLT_CB_ID :
00819         hsram->DmaXferCpltCallback = HAL_SRAM_DMA_XferCpltCallback;
00820         break;
00821       case HAL_SRAM_DMA_XFER_ERR_CB_ID :
00822         hsram->DmaXferErrorCallback = HAL_SRAM_DMA_XferErrorCallback;
00823         break;
00824       default :
00825         /* update return status */
00826         status =  HAL_ERROR;
00827         break;
00828     }
00829   }
00830   else if (state == HAL_SRAM_STATE_RESET)
00831   {
00832     switch (CallbackId)
00833     {
00834       case HAL_SRAM_MSP_INIT_CB_ID :
00835         hsram->MspInitCallback = HAL_SRAM_MspInit;
00836         break;
00837       case HAL_SRAM_MSP_DEINIT_CB_ID :
00838         hsram->MspDeInitCallback = HAL_SRAM_MspDeInit;
00839         break;
00840       default :
00841         /* update return status */
00842         status =  HAL_ERROR;
00843         break;
00844     }
00845   }
00846   else
00847   {
00848     /* update return status */
00849     status =  HAL_ERROR;
00850   }
00851 
00852   /* Release Lock */
00853   __HAL_UNLOCK(hsram);
00854   return status;
00855 }
00856 
00857 /**
00858   * @brief  Register a User SRAM Callback for DMA transfers
00859   *         To be used instead of the weak (surcharged) predefined callback
00860   * @param hsram : SRAM handle
00861   * @param CallbackId : ID of the callback to be registered
00862   *        This parameter can be one of the following values:
00863   *          @arg @ref HAL_SRAM_DMA_XFER_CPLT_CB_ID  SRAM DMA Xfer Complete callback ID
00864   *          @arg @ref HAL_SRAM_DMA_XFER_ERR_CB_ID   SRAM DMA Xfer Error callback ID
00865   * @param pCallback : pointer to the Callback function
00866   * @retval status
00867   */
00868 HAL_StatusTypeDef HAL_SRAM_RegisterDmaCallback(SRAM_HandleTypeDef *hsram, HAL_SRAM_CallbackIDTypeDef CallbackId,
00869                                                pSRAM_DmaCallbackTypeDef pCallback)
00870 {
00871   HAL_StatusTypeDef status = HAL_OK;
00872   HAL_SRAM_StateTypeDef state;
00873 
00874   if (pCallback == NULL)
00875   {
00876     return HAL_ERROR;
00877   }
00878 
00879   /* Process locked */
00880   __HAL_LOCK(hsram);
00881 
00882   state = hsram->State;
00883   if ((state == HAL_SRAM_STATE_READY) || (state == HAL_SRAM_STATE_PROTECTED))
00884   {
00885     switch (CallbackId)
00886     {
00887       case HAL_SRAM_DMA_XFER_CPLT_CB_ID :
00888         hsram->DmaXferCpltCallback = pCallback;
00889         break;
00890       case HAL_SRAM_DMA_XFER_ERR_CB_ID :
00891         hsram->DmaXferErrorCallback = pCallback;
00892         break;
00893       default :
00894         /* update return status */
00895         status =  HAL_ERROR;
00896         break;
00897     }
00898   }
00899   else
00900   {
00901     /* update return status */
00902     status =  HAL_ERROR;
00903   }
00904 
00905   /* Release Lock */
00906   __HAL_UNLOCK(hsram);
00907   return status;
00908 }
00909 #endif /* USE_HAL_SRAM_REGISTER_CALLBACKS */
00910 
00911 /**
00912   * @}
00913   */
00914 
00915 /** @defgroup SRAM_Exported_Functions_Group3 Control functions
00916   *  @brief   Control functions
00917   *
00918 @verbatim
00919   ==============================================================================
00920                         ##### SRAM Control functions #####
00921   ==============================================================================
00922   [..]
00923     This subsection provides a set of functions allowing to control dynamically
00924     the SRAM interface.
00925 
00926 @endverbatim
00927   * @{
00928   */
00929 
00930 /**
00931   * @brief  Enables dynamically SRAM write operation.
00932   * @param  hsram pointer to a SRAM_HandleTypeDef structure that contains
00933   *                the configuration information for SRAM module.
00934   * @retval HAL status
00935   */
00936 HAL_StatusTypeDef HAL_SRAM_WriteOperation_Enable(SRAM_HandleTypeDef *hsram)
00937 {
00938   /* Check the SRAM controller state */
00939   if (hsram->State == HAL_SRAM_STATE_PROTECTED)
00940   {
00941     /* Process Locked */
00942     __HAL_LOCK(hsram);
00943 
00944     /* Update the SRAM controller state */
00945     hsram->State = HAL_SRAM_STATE_BUSY;
00946 
00947     /* Enable write operation */
00948     (void)FMC_NORSRAM_WriteOperation_Enable(hsram->Instance, hsram->Init.NSBank);
00949 
00950     /* Update the SRAM controller state */
00951     hsram->State = HAL_SRAM_STATE_READY;
00952 
00953     /* Process unlocked */
00954     __HAL_UNLOCK(hsram);
00955   }
00956   else
00957   {
00958     return HAL_ERROR;
00959   }
00960 
00961   return HAL_OK;
00962 }
00963 
00964 /**
00965   * @brief  Disables dynamically SRAM write operation.
00966   * @param  hsram pointer to a SRAM_HandleTypeDef structure that contains
00967   *                the configuration information for SRAM module.
00968   * @retval HAL status
00969   */
00970 HAL_StatusTypeDef HAL_SRAM_WriteOperation_Disable(SRAM_HandleTypeDef *hsram)
00971 {
00972   /* Check the SRAM controller state */
00973   if (hsram->State == HAL_SRAM_STATE_READY)
00974   {
00975     /* Process Locked */
00976     __HAL_LOCK(hsram);
00977 
00978     /* Update the SRAM controller state */
00979     hsram->State = HAL_SRAM_STATE_BUSY;
00980 
00981     /* Disable write operation */
00982     (void)FMC_NORSRAM_WriteOperation_Disable(hsram->Instance, hsram->Init.NSBank);
00983 
00984     /* Update the SRAM controller state */
00985     hsram->State = HAL_SRAM_STATE_PROTECTED;
00986 
00987     /* Process unlocked */
00988     __HAL_UNLOCK(hsram);
00989   }
00990   else
00991   {
00992     return HAL_ERROR;
00993   }
00994 
00995   return HAL_OK;
00996 }
00997 
00998 /**
00999   * @}
01000   */
01001 
01002 /** @defgroup SRAM_Exported_Functions_Group4 Peripheral State functions
01003   *  @brief   Peripheral State functions
01004   *
01005 @verbatim
01006   ==============================================================================
01007                       ##### SRAM State functions #####
01008   ==============================================================================
01009   [..]
01010     This subsection permits to get in run-time the status of the SRAM controller
01011     and the data flow.
01012 
01013 @endverbatim
01014   * @{
01015   */
01016 
01017 /**
01018   * @brief  Returns the SRAM controller state
01019   * @param  hsram pointer to a SRAM_HandleTypeDef structure that contains
01020   *                the configuration information for SRAM module.
01021   * @retval HAL state
01022   */
01023 HAL_SRAM_StateTypeDef HAL_SRAM_GetState(SRAM_HandleTypeDef *hsram)
01024 {
01025   return hsram->State;
01026 }
01027 
01028 /**
01029   * @}
01030   */
01031 
01032 /**
01033   * @}
01034   */
01035 
01036 /**
01037   * @brief  MDMA SRAM process complete callback.
01038   * @param  hmdma : MDMA handle
01039   * @retval None
01040   */
01041 static void SRAM_DMACplt(MDMA_HandleTypeDef *hmdma)
01042 {
01043   SRAM_HandleTypeDef *hsram = (SRAM_HandleTypeDef *)(hmdma->Parent);
01044 
01045   /* Disable the MDMA channel */
01046   __HAL_MDMA_DISABLE(hmdma);
01047 
01048   /* Update the SRAM controller state */
01049   hsram->State = HAL_SRAM_STATE_READY;
01050 
01051 #if (USE_HAL_SRAM_REGISTER_CALLBACKS == 1)
01052   hsram->DmaXferCpltCallback(hmdma);
01053 #else
01054   HAL_SRAM_DMA_XferCpltCallback(hmdma);
01055 #endif /* USE_HAL_SRAM_REGISTER_CALLBACKS */
01056 }
01057 
01058 /**
01059   * @brief  MDMA SRAM process complete callback.
01060   * @param  hmdma : MDMA handle
01061   * @retval None
01062   */
01063 static void SRAM_DMACpltProt(MDMA_HandleTypeDef *hmdma)
01064 {
01065   SRAM_HandleTypeDef *hsram = (SRAM_HandleTypeDef *)(hmdma->Parent);
01066 
01067   /* Disable the MDMA channel */
01068   __HAL_MDMA_DISABLE(hmdma);
01069 
01070   /* Update the SRAM controller state */
01071   hsram->State = HAL_SRAM_STATE_PROTECTED;
01072 
01073 #if (USE_HAL_SRAM_REGISTER_CALLBACKS == 1)
01074   hsram->DmaXferCpltCallback(hmdma);
01075 #else
01076   HAL_SRAM_DMA_XferCpltCallback(hmdma);
01077 #endif /* USE_HAL_SRAM_REGISTER_CALLBACKS */
01078 }
01079 
01080 /**
01081   * @brief  MDMA SRAM error callback.
01082   * @param  hmdma : MDMA handle
01083   * @retval None
01084   */
01085 static void SRAM_DMAError(MDMA_HandleTypeDef *hmdma)
01086 {
01087   SRAM_HandleTypeDef *hsram = (SRAM_HandleTypeDef *)(hmdma->Parent);
01088 
01089   /* Disable the MDMA channel */
01090   __HAL_MDMA_DISABLE(hmdma);
01091 
01092   /* Update the SRAM controller state */
01093   hsram->State = HAL_SRAM_STATE_ERROR;
01094 
01095 #if (USE_HAL_SRAM_REGISTER_CALLBACKS == 1)
01096   hsram->DmaXferErrorCallback(hmdma);
01097 #else
01098   HAL_SRAM_DMA_XferErrorCallback(hmdma);
01099 #endif /* USE_HAL_SRAM_REGISTER_CALLBACKS */
01100 }
01101 
01102 /**
01103   * @}
01104   */
01105 
01106 #endif /* HAL_SRAM_MODULE_ENABLED */
01107 
01108 /**
01109   * @}
01110   */
01111