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