1 /**
2   ******************************************************************************
3   * @file    stm32f2xx_hal_sram.c
4   * @author  MCD Application Team
5   * @brief   SRAM HAL module driver.
6   *          This file provides a generic firmware to drive SRAM memories
7   *          mounted as external device.
8   *
9   ******************************************************************************
10   * @attention
11   *
12   * Copyright (c) 2016 STMicroelectronics.
13   * All rights reserved.
14   *
15   * This software is licensed under terms that can be found in the LICENSE file
16   * in the root directory of this software component.
17   * If no LICENSE file comes with this software, it is provided AS-IS.
18   *
19   ******************************************************************************
20   @verbatim
21   ==============================================================================
22                           ##### How to use this driver #####
23   ==============================================================================
24   [..]
25     This driver is a generic layered driver which contains a set of APIs used to
26     control SRAM memories. It uses the FSMC layer functions to interface
27     with SRAM devices.
28     The following sequence should be followed to configure the FSMC to interface
29     with SRAM/PSRAM memories:
30 
31    (#) Declare a SRAM_HandleTypeDef handle structure, for example:
32           SRAM_HandleTypeDef  hsram; and:
33 
34        (++) Fill the SRAM_HandleTypeDef handle "Init" field with the allowed
35             values of the structure member.
36 
37        (++) Fill the SRAM_HandleTypeDef handle "Instance" field with a predefined
38             base register instance for NOR or SRAM device
39 
40        (++) Fill the SRAM_HandleTypeDef handle "Extended" field with a predefined
41             base register instance for NOR or SRAM extended mode
42 
43    (#) Declare two FSMC_NORSRAM_TimingTypeDef structures, for both normal and extended
44        mode timings; for example:
45           FSMC_NORSRAM_TimingTypeDef  Timing and FSMC_NORSRAM_TimingTypeDef  ExTiming;
46       and fill its fields with the allowed values of the structure member.
47 
48    (#) Initialize the SRAM Controller by calling the function HAL_SRAM_Init(). This function
49        performs the following sequence:
50 
51        (##) MSP hardware layer configuration using the function HAL_SRAM_MspInit()
52        (##) Control register configuration using the FSMC NORSRAM interface function
53             FSMC_NORSRAM_Init()
54        (##) Timing register configuration using the FSMC NORSRAM interface function
55             FSMC_NORSRAM_Timing_Init()
56        (##) Extended mode Timing register configuration using the FSMC NORSRAM interface function
57             FSMC_NORSRAM_Extended_Timing_Init()
58        (##) Enable the SRAM device using the macro __FSMC_NORSRAM_ENABLE()
59 
60    (#) At this stage you can perform read/write accesses from/to the memory connected
61        to the NOR/SRAM Bank. You can perform either polling or DMA transfer using the
62        following APIs:
63        (++) HAL_SRAM_Read()/HAL_SRAM_Write() for polling read/write access
64        (++) HAL_SRAM_Read_DMA()/HAL_SRAM_Write_DMA() for DMA read/write transfer
65 
66    (#) You can also control the SRAM device by calling the control APIs HAL_SRAM_WriteOperation_Enable()/
67        HAL_SRAM_WriteOperation_Disable() to respectively enable/disable the SRAM write operation
68 
69    (#) You can continuously monitor the SRAM device HAL state by calling the function
70        HAL_SRAM_GetState()
71 
72        *** Callback registration ***
73     =============================================
74     [..]
75       The compilation define  USE_HAL_SRAM_REGISTER_CALLBACKS when set to 1
76       allows the user to configure dynamically the driver callbacks.
77 
78       Use Functions HAL_SRAM_RegisterCallback() to register a user callback,
79       it allows to register following callbacks:
80         (+) MspInitCallback    : SRAM MspInit.
81         (+) MspDeInitCallback  : SRAM MspDeInit.
82       This function takes as parameters the HAL peripheral handle, the Callback ID
83       and a pointer to the user callback function.
84 
85       Use function HAL_SRAM_UnRegisterCallback() to reset a callback to the default
86       weak (overridden) function. It allows to reset following callbacks:
87         (+) MspInitCallback    : SRAM MspInit.
88         (+) MspDeInitCallback  : SRAM MspDeInit.
89       This function) takes as parameters the HAL peripheral handle and the Callback ID.
90 
91       By default, after the HAL_SRAM_Init and if the state is HAL_SRAM_STATE_RESET
92       all callbacks are reset to the corresponding legacy weak (overridden) functions.
93       Exception done for MspInit and MspDeInit callbacks that are respectively
94       reset to the legacy weak (overridden) functions in the HAL_SRAM_Init
95       and  HAL_SRAM_DeInit only when these callbacks are null (not registered beforehand).
96       If not, MspInit or MspDeInit are not null, the HAL_SRAM_Init and HAL_SRAM_DeInit
97       keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
98 
99       Callbacks can be registered/unregistered in READY state only.
100       Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
101       in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
102       during the Init/DeInit.
103       In that case first register the MspInit/MspDeInit user callbacks
104       using HAL_SRAM_RegisterCallback before calling HAL_SRAM_DeInit
105       or HAL_SRAM_Init function.
106 
107       When The compilation define USE_HAL_SRAM_REGISTER_CALLBACKS is set to 0 or
108       not defined, the callback registering feature is not available
109       and weak (overridden) callbacks are used.
110 
111   @endverbatim
112   ******************************************************************************
113   */
114 
115 /* Includes ------------------------------------------------------------------*/
116 #include "stm32f2xx_hal.h"
117 
118 
119 /** @addtogroup STM32F2xx_HAL_Driver
120   * @{
121   */
122 
123 #ifdef HAL_SRAM_MODULE_ENABLED
124 
125 /** @defgroup SRAM SRAM
126   * @brief SRAM driver modules
127   * @{
128   */
129 
130 /* Private typedef -----------------------------------------------------------*/
131 /* Private define ------------------------------------------------------------*/
132 /* Private macro -------------------------------------------------------------*/
133 /* Private variables ---------------------------------------------------------*/
134 /* Private function prototypes -----------------------------------------------*/
135 /** @addtogroup SRAM_Private_Functions SRAM Private Functions
136   * @{
137   */
138 static void SRAM_DMACplt(DMA_HandleTypeDef *hdma);
139 static void SRAM_DMACpltProt(DMA_HandleTypeDef *hdma);
140 static void SRAM_DMAError(DMA_HandleTypeDef *hdma);
141 /**
142   * @}
143   */
144 
145 /* Exported functions --------------------------------------------------------*/
146 
147 /** @defgroup SRAM_Exported_Functions SRAM Exported Functions
148   * @{
149   */
150 
151 /** @defgroup SRAM_Exported_Functions_Group1 Initialization and de-initialization functions
152   * @brief    Initialization and Configuration functions.
153   *
154   @verbatim
155   ==============================================================================
156            ##### SRAM Initialization and de_initialization functions #####
157   ==============================================================================
158     [..]  This section provides functions allowing to initialize/de-initialize
159           the SRAM memory
160 
161 @endverbatim
162   * @{
163   */
164 
165 /**
166   * @brief  Performs the SRAM device initialization sequence
167   * @param  hsram pointer to a SRAM_HandleTypeDef structure that contains
168   *                the configuration information for SRAM module.
169   * @param  Timing Pointer to SRAM control timing structure
170   * @param  ExtTiming Pointer to SRAM extended mode timing structure
171   * @retval HAL status
172   */
HAL_SRAM_Init(SRAM_HandleTypeDef * hsram,FSMC_NORSRAM_TimingTypeDef * Timing,FSMC_NORSRAM_TimingTypeDef * ExtTiming)173 HAL_StatusTypeDef HAL_SRAM_Init(SRAM_HandleTypeDef *hsram, FSMC_NORSRAM_TimingTypeDef *Timing,
174                                 FSMC_NORSRAM_TimingTypeDef *ExtTiming)
175 {
176   /* Check the SRAM handle parameter */
177   if ((hsram == NULL) || (hsram->Init.BurstAccessMode == FSMC_BURST_ACCESS_MODE_ENABLE))
178   {
179     return HAL_ERROR;
180   }
181 
182   if (hsram->State == HAL_SRAM_STATE_RESET)
183   {
184     /* Allocate lock resource and initialize it */
185     hsram->Lock = HAL_UNLOCKED;
186 
187 #if (USE_HAL_SRAM_REGISTER_CALLBACKS == 1)
188     if (hsram->MspInitCallback == NULL)
189     {
190       hsram->MspInitCallback = HAL_SRAM_MspInit;
191     }
192     hsram->DmaXferCpltCallback = HAL_SRAM_DMA_XferCpltCallback;
193     hsram->DmaXferErrorCallback = HAL_SRAM_DMA_XferErrorCallback;
194 
195     /* Init the low level hardware */
196     hsram->MspInitCallback(hsram);
197 #else
198     /* Initialize the low level hardware (MSP) */
199     HAL_SRAM_MspInit(hsram);
200 #endif /* USE_HAL_SRAM_REGISTER_CALLBACKS */
201   }
202 
203   /* Initialize SRAM control Interface */
204   (void)FSMC_NORSRAM_Init(hsram->Instance, &(hsram->Init));
205 
206   /* Initialize SRAM timing Interface */
207   (void)FSMC_NORSRAM_Timing_Init(hsram->Instance, Timing, hsram->Init.NSBank);
208 
209   /* Initialize SRAM extended mode timing Interface */
210   (void)FSMC_NORSRAM_Extended_Timing_Init(hsram->Extended, ExtTiming, hsram->Init.NSBank,
211                                          hsram->Init.ExtendedMode);
212 
213   /* Enable the NORSRAM device */
214   __FSMC_NORSRAM_ENABLE(hsram->Instance, hsram->Init.NSBank);
215 
216   /* Initialize the SRAM controller state */
217   hsram->State = HAL_SRAM_STATE_READY;
218 
219   return HAL_OK;
220 }
221 
222 /**
223   * @brief  Performs the SRAM device De-initialization sequence.
224   * @param  hsram pointer to a SRAM_HandleTypeDef structure that contains
225   *                the configuration information for SRAM module.
226   * @retval HAL status
227   */
HAL_SRAM_DeInit(SRAM_HandleTypeDef * hsram)228 HAL_StatusTypeDef HAL_SRAM_DeInit(SRAM_HandleTypeDef *hsram)
229 {
230 #if (USE_HAL_SRAM_REGISTER_CALLBACKS == 1)
231   if (hsram->MspDeInitCallback == NULL)
232   {
233     hsram->MspDeInitCallback = HAL_SRAM_MspDeInit;
234   }
235 
236   /* DeInit the low level hardware */
237   hsram->MspDeInitCallback(hsram);
238 #else
239   /* De-Initialize the low level hardware (MSP) */
240   HAL_SRAM_MspDeInit(hsram);
241 #endif /* USE_HAL_SRAM_REGISTER_CALLBACKS */
242 
243   /* Configure the SRAM registers with their reset values */
244   (void)FSMC_NORSRAM_DeInit(hsram->Instance, hsram->Extended, hsram->Init.NSBank);
245 
246   /* Reset the SRAM controller state */
247   hsram->State = HAL_SRAM_STATE_RESET;
248 
249   /* Release Lock */
250   __HAL_UNLOCK(hsram);
251 
252   return HAL_OK;
253 }
254 
255 /**
256   * @brief  SRAM MSP Init.
257   * @param  hsram pointer to a SRAM_HandleTypeDef structure that contains
258   *                the configuration information for SRAM module.
259   * @retval None
260   */
HAL_SRAM_MspInit(SRAM_HandleTypeDef * hsram)261 __weak void HAL_SRAM_MspInit(SRAM_HandleTypeDef *hsram)
262 {
263   /* Prevent unused argument(s) compilation warning */
264   UNUSED(hsram);
265 
266   /* NOTE : This function Should not be modified, when the callback is needed,
267             the HAL_SRAM_MspInit could be implemented in the user file
268    */
269 }
270 
271 /**
272   * @brief  SRAM MSP DeInit.
273   * @param  hsram pointer to a SRAM_HandleTypeDef structure that contains
274   *                the configuration information for SRAM module.
275   * @retval None
276   */
HAL_SRAM_MspDeInit(SRAM_HandleTypeDef * hsram)277 __weak void HAL_SRAM_MspDeInit(SRAM_HandleTypeDef *hsram)
278 {
279   /* Prevent unused argument(s) compilation warning */
280   UNUSED(hsram);
281 
282   /* NOTE : This function Should not be modified, when the callback is needed,
283             the HAL_SRAM_MspDeInit could be implemented in the user file
284    */
285 }
286 
287 /**
288   * @brief  DMA transfer complete callback.
289   * @param  hdma pointer to a SRAM_HandleTypeDef structure that contains
290   *                the configuration information for SRAM module.
291   * @retval None
292   */
HAL_SRAM_DMA_XferCpltCallback(DMA_HandleTypeDef * hdma)293 __weak void HAL_SRAM_DMA_XferCpltCallback(DMA_HandleTypeDef *hdma)
294 {
295   /* Prevent unused argument(s) compilation warning */
296   UNUSED(hdma);
297 
298   /* NOTE : This function Should not be modified, when the callback is needed,
299             the HAL_SRAM_DMA_XferCpltCallback could be implemented in the user file
300    */
301 }
302 
303 /**
304   * @brief  DMA transfer complete error callback.
305   * @param  hdma pointer to a SRAM_HandleTypeDef structure that contains
306   *                the configuration information for SRAM module.
307   * @retval None
308   */
HAL_SRAM_DMA_XferErrorCallback(DMA_HandleTypeDef * hdma)309 __weak void HAL_SRAM_DMA_XferErrorCallback(DMA_HandleTypeDef *hdma)
310 {
311   /* Prevent unused argument(s) compilation warning */
312   UNUSED(hdma);
313 
314   /* NOTE : This function Should not be modified, when the callback is needed,
315             the HAL_SRAM_DMA_XferErrorCallback could be implemented in the user file
316    */
317 }
318 
319 /**
320   * @}
321   */
322 
323 /** @defgroup SRAM_Exported_Functions_Group2 Input Output and memory control functions
324   * @brief    Input Output and memory control functions
325   *
326   @verbatim
327   ==============================================================================
328                   ##### SRAM Input and Output functions #####
329   ==============================================================================
330   [..]
331     This section provides functions allowing to use and control the SRAM memory
332 
333 @endverbatim
334   * @{
335   */
336 
337 /**
338   * @brief  Reads 8-bit buffer from SRAM memory.
339   * @param  hsram pointer to a SRAM_HandleTypeDef structure that contains
340   *                the configuration information for SRAM module.
341   * @param  pAddress Pointer to read start address
342   * @param  pDstBuffer Pointer to destination buffer
343   * @param  BufferSize Size of the buffer to read from memory
344   * @retval HAL status
345   */
HAL_SRAM_Read_8b(SRAM_HandleTypeDef * hsram,uint32_t * pAddress,uint8_t * pDstBuffer,uint32_t BufferSize)346 HAL_StatusTypeDef HAL_SRAM_Read_8b(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint8_t *pDstBuffer,
347                                    uint32_t BufferSize)
348 {
349   uint32_t size;
350   __IO uint8_t *psramaddress = (uint8_t *)pAddress;
351   uint8_t *pdestbuff = pDstBuffer;
352   HAL_SRAM_StateTypeDef state = hsram->State;
353 
354   /* Check the SRAM controller state */
355   if ((state == HAL_SRAM_STATE_READY) || (state == HAL_SRAM_STATE_PROTECTED))
356   {
357     /* Process Locked */
358     __HAL_LOCK(hsram);
359 
360     /* Update the SRAM controller state */
361     hsram->State = HAL_SRAM_STATE_BUSY;
362 
363     /* Read data from memory */
364     for (size = BufferSize; size != 0U; size--)
365     {
366       *pdestbuff = *psramaddress;
367       pdestbuff++;
368       psramaddress++;
369     }
370 
371     /* Update the SRAM controller state */
372     hsram->State = state;
373 
374     /* Process unlocked */
375     __HAL_UNLOCK(hsram);
376   }
377   else
378   {
379     return HAL_ERROR;
380   }
381 
382   return HAL_OK;
383 }
384 
385 /**
386   * @brief  Writes 8-bit buffer to SRAM memory.
387   * @param  hsram pointer to a SRAM_HandleTypeDef structure that contains
388   *                the configuration information for SRAM module.
389   * @param  pAddress Pointer to write start address
390   * @param  pSrcBuffer Pointer to source buffer to write
391   * @param  BufferSize Size of the buffer to write to memory
392   * @retval HAL status
393   */
HAL_SRAM_Write_8b(SRAM_HandleTypeDef * hsram,uint32_t * pAddress,uint8_t * pSrcBuffer,uint32_t BufferSize)394 HAL_StatusTypeDef HAL_SRAM_Write_8b(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint8_t *pSrcBuffer,
395                                     uint32_t BufferSize)
396 {
397   uint32_t size;
398   __IO uint8_t *psramaddress = (uint8_t *)pAddress;
399   uint8_t *psrcbuff = pSrcBuffer;
400 
401   /* Check the SRAM controller state */
402   if (hsram->State == HAL_SRAM_STATE_READY)
403   {
404     /* Process Locked */
405     __HAL_LOCK(hsram);
406 
407     /* Update the SRAM controller state */
408     hsram->State = HAL_SRAM_STATE_BUSY;
409 
410     /* Write data to memory */
411     for (size = BufferSize; size != 0U; size--)
412     {
413       *psramaddress = *psrcbuff;
414       psrcbuff++;
415       psramaddress++;
416     }
417 
418     /* Update the SRAM controller state */
419     hsram->State = HAL_SRAM_STATE_READY;
420 
421     /* Process unlocked */
422     __HAL_UNLOCK(hsram);
423   }
424   else
425   {
426     return HAL_ERROR;
427   }
428 
429   return HAL_OK;
430 }
431 
432 /**
433   * @brief  Reads 16-bit buffer from SRAM memory.
434   * @param  hsram pointer to a SRAM_HandleTypeDef structure that contains
435   *                the configuration information for SRAM module.
436   * @param  pAddress Pointer to read start address
437   * @param  pDstBuffer Pointer to destination buffer
438   * @param  BufferSize Size of the buffer to read from memory
439   * @retval HAL status
440   */
HAL_SRAM_Read_16b(SRAM_HandleTypeDef * hsram,uint32_t * pAddress,uint16_t * pDstBuffer,uint32_t BufferSize)441 HAL_StatusTypeDef HAL_SRAM_Read_16b(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint16_t *pDstBuffer,
442                                     uint32_t BufferSize)
443 {
444   uint32_t size;
445   __IO uint32_t *psramaddress = pAddress;
446   uint16_t *pdestbuff = pDstBuffer;
447   uint8_t limit;
448   HAL_SRAM_StateTypeDef state = hsram->State;
449 
450   /* Check the SRAM controller state */
451   if ((state == HAL_SRAM_STATE_READY) || (state == HAL_SRAM_STATE_PROTECTED))
452   {
453     /* Process Locked */
454     __HAL_LOCK(hsram);
455 
456     /* Update the SRAM controller state */
457     hsram->State = HAL_SRAM_STATE_BUSY;
458 
459     /* Check if the size is a 32-bits multiple */
460     limit = (((BufferSize % 2U) != 0U) ? 1U : 0U);
461 
462     /* Read data from memory */
463     for (size = BufferSize; size != limit; size -= 2U)
464     {
465       *pdestbuff = (uint16_t)((*psramaddress) & 0x0000FFFFU);
466       pdestbuff++;
467       *pdestbuff = (uint16_t)(((*psramaddress) & 0xFFFF0000U) >> 16U);
468       pdestbuff++;
469       psramaddress++;
470     }
471 
472     /* Read last 16-bits if size is not 32-bits multiple */
473     if (limit != 0U)
474     {
475       *pdestbuff = (uint16_t)((*psramaddress) & 0x0000FFFFU);
476     }
477 
478     /* Update the SRAM controller state */
479     hsram->State = state;
480 
481     /* Process unlocked */
482     __HAL_UNLOCK(hsram);
483   }
484   else
485   {
486     return HAL_ERROR;
487   }
488 
489   return HAL_OK;
490 }
491 
492 /**
493   * @brief  Writes 16-bit buffer to SRAM memory.
494   * @param  hsram pointer to a SRAM_HandleTypeDef structure that contains
495   *                the configuration information for SRAM module.
496   * @param  pAddress Pointer to write start address
497   * @param  pSrcBuffer Pointer to source buffer to write
498   * @param  BufferSize Size of the buffer to write to memory
499   * @retval HAL status
500   */
HAL_SRAM_Write_16b(SRAM_HandleTypeDef * hsram,uint32_t * pAddress,uint16_t * pSrcBuffer,uint32_t BufferSize)501 HAL_StatusTypeDef HAL_SRAM_Write_16b(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint16_t *pSrcBuffer,
502                                      uint32_t BufferSize)
503 {
504   uint32_t size;
505   __IO uint32_t *psramaddress = pAddress;
506   uint16_t *psrcbuff = pSrcBuffer;
507   uint8_t limit;
508 
509   /* Check the SRAM controller state */
510   if (hsram->State == HAL_SRAM_STATE_READY)
511   {
512     /* Process Locked */
513     __HAL_LOCK(hsram);
514 
515     /* Update the SRAM controller state */
516     hsram->State = HAL_SRAM_STATE_BUSY;
517 
518     /* Check if the size is a 32-bits multiple */
519     limit = (((BufferSize % 2U) != 0U) ? 1U : 0U);
520 
521     /* Write data to memory */
522     for (size = BufferSize; size != limit; size -= 2U)
523     {
524       *psramaddress = (uint32_t)(*psrcbuff);
525       psrcbuff++;
526       *psramaddress |= ((uint32_t)(*psrcbuff) << 16U);
527       psrcbuff++;
528       psramaddress++;
529     }
530 
531     /* Write last 16-bits if size is not 32-bits multiple */
532     if (limit != 0U)
533     {
534       *psramaddress = ((uint32_t)(*psrcbuff) & 0x0000FFFFU) | ((*psramaddress) & 0xFFFF0000U);
535     }
536 
537     /* Update the SRAM controller state */
538     hsram->State = HAL_SRAM_STATE_READY;
539 
540     /* Process unlocked */
541     __HAL_UNLOCK(hsram);
542   }
543   else
544   {
545     return HAL_ERROR;
546   }
547 
548   return HAL_OK;
549 }
550 
551 /**
552   * @brief  Reads 32-bit buffer from SRAM memory.
553   * @param  hsram pointer to a SRAM_HandleTypeDef structure that contains
554   *                the configuration information for SRAM module.
555   * @param  pAddress Pointer to read start address
556   * @param  pDstBuffer Pointer to destination buffer
557   * @param  BufferSize Size of the buffer to read from memory
558   * @retval HAL status
559   */
HAL_SRAM_Read_32b(SRAM_HandleTypeDef * hsram,uint32_t * pAddress,uint32_t * pDstBuffer,uint32_t BufferSize)560 HAL_StatusTypeDef HAL_SRAM_Read_32b(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint32_t *pDstBuffer,
561                                     uint32_t BufferSize)
562 {
563   uint32_t size;
564   __IO uint32_t *psramaddress = pAddress;
565   uint32_t *pdestbuff = pDstBuffer;
566   HAL_SRAM_StateTypeDef state = hsram->State;
567 
568   /* Check the SRAM controller state */
569   if ((state == HAL_SRAM_STATE_READY) || (state == HAL_SRAM_STATE_PROTECTED))
570   {
571     /* Process Locked */
572     __HAL_LOCK(hsram);
573 
574     /* Update the SRAM controller state */
575     hsram->State = HAL_SRAM_STATE_BUSY;
576 
577     /* Read data from memory */
578     for (size = BufferSize; size != 0U; size--)
579     {
580       *pdestbuff = *psramaddress;
581       pdestbuff++;
582       psramaddress++;
583     }
584 
585     /* Update the SRAM controller state */
586     hsram->State = state;
587 
588     /* Process unlocked */
589     __HAL_UNLOCK(hsram);
590   }
591   else
592   {
593     return HAL_ERROR;
594   }
595 
596   return HAL_OK;
597 }
598 
599 /**
600   * @brief  Writes 32-bit buffer to SRAM memory.
601   * @param  hsram pointer to a SRAM_HandleTypeDef structure that contains
602   *                the configuration information for SRAM module.
603   * @param  pAddress Pointer to write start address
604   * @param  pSrcBuffer Pointer to source buffer to write
605   * @param  BufferSize Size of the buffer to write to memory
606   * @retval HAL status
607   */
HAL_SRAM_Write_32b(SRAM_HandleTypeDef * hsram,uint32_t * pAddress,uint32_t * pSrcBuffer,uint32_t BufferSize)608 HAL_StatusTypeDef HAL_SRAM_Write_32b(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint32_t *pSrcBuffer,
609                                      uint32_t BufferSize)
610 {
611   uint32_t size;
612   __IO uint32_t *psramaddress = pAddress;
613   uint32_t *psrcbuff = pSrcBuffer;
614 
615   /* Check the SRAM controller state */
616   if (hsram->State == HAL_SRAM_STATE_READY)
617   {
618     /* Process Locked */
619     __HAL_LOCK(hsram);
620 
621     /* Update the SRAM controller state */
622     hsram->State = HAL_SRAM_STATE_BUSY;
623 
624     /* Write data to memory */
625     for (size = BufferSize; size != 0U; size--)
626     {
627       *psramaddress = *psrcbuff;
628       psrcbuff++;
629       psramaddress++;
630     }
631 
632     /* Update the SRAM controller state */
633     hsram->State = HAL_SRAM_STATE_READY;
634 
635     /* Process unlocked */
636     __HAL_UNLOCK(hsram);
637   }
638   else
639   {
640     return HAL_ERROR;
641   }
642 
643   return HAL_OK;
644 }
645 
646 /**
647   * @brief  Reads a Words data from the SRAM memory using DMA transfer.
648   * @param  hsram pointer to a SRAM_HandleTypeDef structure that contains
649   *                the configuration information for SRAM module.
650   * @param  pAddress Pointer to read start address
651   * @param  pDstBuffer Pointer to destination buffer
652   * @param  BufferSize Size of the buffer to read from memory
653   * @retval HAL status
654   */
HAL_SRAM_Read_DMA(SRAM_HandleTypeDef * hsram,uint32_t * pAddress,uint32_t * pDstBuffer,uint32_t BufferSize)655 HAL_StatusTypeDef HAL_SRAM_Read_DMA(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint32_t *pDstBuffer,
656                                     uint32_t BufferSize)
657 {
658   HAL_StatusTypeDef status;
659   HAL_SRAM_StateTypeDef state = hsram->State;
660 
661   /* Check the SRAM controller state */
662   if ((state == HAL_SRAM_STATE_READY) || (state == HAL_SRAM_STATE_PROTECTED))
663   {
664     /* Process Locked */
665     __HAL_LOCK(hsram);
666 
667     /* Update the SRAM controller state */
668     hsram->State = HAL_SRAM_STATE_BUSY;
669 
670     /* Configure DMA user callbacks */
671     if (state == HAL_SRAM_STATE_READY)
672     {
673       hsram->hdma->XferCpltCallback = SRAM_DMACplt;
674     }
675     else
676     {
677       hsram->hdma->XferCpltCallback = SRAM_DMACpltProt;
678     }
679     hsram->hdma->XferErrorCallback = SRAM_DMAError;
680 
681     /* Enable the DMA Stream */
682     status = HAL_DMA_Start_IT(hsram->hdma, (uint32_t)pAddress, (uint32_t)pDstBuffer, (uint32_t)BufferSize);
683 
684     /* Process unlocked */
685     __HAL_UNLOCK(hsram);
686   }
687   else
688   {
689     status = HAL_ERROR;
690   }
691 
692   return status;
693 }
694 
695 /**
696   * @brief  Writes a Words data buffer to SRAM memory using DMA transfer.
697   * @param  hsram pointer to a SRAM_HandleTypeDef structure that contains
698   *                the configuration information for SRAM module.
699   * @param  pAddress Pointer to write start address
700   * @param  pSrcBuffer Pointer to source buffer to write
701   * @param  BufferSize Size of the buffer to write to memory
702   * @retval HAL status
703   */
HAL_SRAM_Write_DMA(SRAM_HandleTypeDef * hsram,uint32_t * pAddress,uint32_t * pSrcBuffer,uint32_t BufferSize)704 HAL_StatusTypeDef HAL_SRAM_Write_DMA(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint32_t *pSrcBuffer,
705                                      uint32_t BufferSize)
706 {
707   HAL_StatusTypeDef status;
708 
709   /* Check the SRAM controller state */
710   if (hsram->State == HAL_SRAM_STATE_READY)
711   {
712     /* Process Locked */
713     __HAL_LOCK(hsram);
714 
715     /* Update the SRAM controller state */
716     hsram->State = HAL_SRAM_STATE_BUSY;
717 
718     /* Configure DMA user callbacks */
719     hsram->hdma->XferCpltCallback = SRAM_DMACplt;
720     hsram->hdma->XferErrorCallback = SRAM_DMAError;
721 
722     /* Enable the DMA Stream */
723     status = HAL_DMA_Start_IT(hsram->hdma, (uint32_t)pSrcBuffer, (uint32_t)pAddress, (uint32_t)BufferSize);
724 
725     /* Process unlocked */
726     __HAL_UNLOCK(hsram);
727   }
728   else
729   {
730     status = HAL_ERROR;
731   }
732 
733   return status;
734 }
735 
736 #if (USE_HAL_SRAM_REGISTER_CALLBACKS == 1)
737 /**
738   * @brief  Register a User SRAM Callback
739   *         To be used to override the weak predefined callback
740   * @param hsram : SRAM handle
741   * @param CallbackId : ID of the callback to be registered
742   *        This parameter can be one of the following values:
743   *          @arg @ref HAL_SRAM_MSP_INIT_CB_ID       SRAM MspInit callback ID
744   *          @arg @ref HAL_SRAM_MSP_DEINIT_CB_ID     SRAM MspDeInit callback ID
745   * @param pCallback : pointer to the Callback function
746   * @retval status
747   */
HAL_SRAM_RegisterCallback(SRAM_HandleTypeDef * hsram,HAL_SRAM_CallbackIDTypeDef CallbackId,pSRAM_CallbackTypeDef pCallback)748 HAL_StatusTypeDef HAL_SRAM_RegisterCallback(SRAM_HandleTypeDef *hsram, HAL_SRAM_CallbackIDTypeDef CallbackId,
749                                             pSRAM_CallbackTypeDef pCallback)
750 {
751   HAL_StatusTypeDef status = HAL_OK;
752   HAL_SRAM_StateTypeDef state;
753 
754   if (pCallback == NULL)
755   {
756     return HAL_ERROR;
757   }
758 
759   state = hsram->State;
760   if ((state == HAL_SRAM_STATE_READY) || (state == HAL_SRAM_STATE_RESET) || (state == HAL_SRAM_STATE_PROTECTED))
761   {
762     switch (CallbackId)
763     {
764       case HAL_SRAM_MSP_INIT_CB_ID :
765         hsram->MspInitCallback = pCallback;
766         break;
767       case HAL_SRAM_MSP_DEINIT_CB_ID :
768         hsram->MspDeInitCallback = pCallback;
769         break;
770       default :
771         /* update return status */
772         status =  HAL_ERROR;
773         break;
774     }
775   }
776   else
777   {
778     /* update return status */
779     status =  HAL_ERROR;
780   }
781 
782   return status;
783 }
784 
785 /**
786   * @brief  Unregister a User SRAM Callback
787   *         SRAM Callback is redirected to the weak predefined callback
788   * @param hsram : SRAM handle
789   * @param CallbackId : ID of the callback to be unregistered
790   *        This parameter can be one of the following values:
791   *          @arg @ref HAL_SRAM_MSP_INIT_CB_ID       SRAM MspInit callback ID
792   *          @arg @ref HAL_SRAM_MSP_DEINIT_CB_ID     SRAM MspDeInit callback ID
793   *          @arg @ref HAL_SRAM_DMA_XFER_CPLT_CB_ID  SRAM DMA Xfer Complete callback ID
794   *          @arg @ref HAL_SRAM_DMA_XFER_ERR_CB_ID   SRAM DMA Xfer Error callback ID
795   * @retval status
796   */
HAL_SRAM_UnRegisterCallback(SRAM_HandleTypeDef * hsram,HAL_SRAM_CallbackIDTypeDef CallbackId)797 HAL_StatusTypeDef HAL_SRAM_UnRegisterCallback(SRAM_HandleTypeDef *hsram, HAL_SRAM_CallbackIDTypeDef CallbackId)
798 {
799   HAL_StatusTypeDef status = HAL_OK;
800   HAL_SRAM_StateTypeDef state;
801 
802   state = hsram->State;
803   if ((state == HAL_SRAM_STATE_READY) || (state == HAL_SRAM_STATE_PROTECTED))
804   {
805     switch (CallbackId)
806     {
807       case HAL_SRAM_MSP_INIT_CB_ID :
808         hsram->MspInitCallback = HAL_SRAM_MspInit;
809         break;
810       case HAL_SRAM_MSP_DEINIT_CB_ID :
811         hsram->MspDeInitCallback = HAL_SRAM_MspDeInit;
812         break;
813       case HAL_SRAM_DMA_XFER_CPLT_CB_ID :
814         hsram->DmaXferCpltCallback = HAL_SRAM_DMA_XferCpltCallback;
815         break;
816       case HAL_SRAM_DMA_XFER_ERR_CB_ID :
817         hsram->DmaXferErrorCallback = HAL_SRAM_DMA_XferErrorCallback;
818         break;
819       default :
820         /* update return status */
821         status =  HAL_ERROR;
822         break;
823     }
824   }
825   else if (state == HAL_SRAM_STATE_RESET)
826   {
827     switch (CallbackId)
828     {
829       case HAL_SRAM_MSP_INIT_CB_ID :
830         hsram->MspInitCallback = HAL_SRAM_MspInit;
831         break;
832       case HAL_SRAM_MSP_DEINIT_CB_ID :
833         hsram->MspDeInitCallback = HAL_SRAM_MspDeInit;
834         break;
835       default :
836         /* update return status */
837         status =  HAL_ERROR;
838         break;
839     }
840   }
841   else
842   {
843     /* update return status */
844     status =  HAL_ERROR;
845   }
846 
847   return status;
848 }
849 
850 /**
851   * @brief  Register a User SRAM Callback for DMA transfers
852   *         To be used to override the weak predefined callback
853   * @param hsram : SRAM handle
854   * @param CallbackId : ID of the callback to be registered
855   *        This parameter can be one of the following values:
856   *          @arg @ref HAL_SRAM_DMA_XFER_CPLT_CB_ID  SRAM DMA Xfer Complete callback ID
857   *          @arg @ref HAL_SRAM_DMA_XFER_ERR_CB_ID   SRAM DMA Xfer Error callback ID
858   * @param pCallback : pointer to the Callback function
859   * @retval status
860   */
HAL_SRAM_RegisterDmaCallback(SRAM_HandleTypeDef * hsram,HAL_SRAM_CallbackIDTypeDef CallbackId,pSRAM_DmaCallbackTypeDef pCallback)861 HAL_StatusTypeDef HAL_SRAM_RegisterDmaCallback(SRAM_HandleTypeDef *hsram, HAL_SRAM_CallbackIDTypeDef CallbackId,
862                                                pSRAM_DmaCallbackTypeDef pCallback)
863 {
864   HAL_StatusTypeDef status = HAL_OK;
865   HAL_SRAM_StateTypeDef state;
866 
867   if (pCallback == NULL)
868   {
869     return HAL_ERROR;
870   }
871 
872   /* Process locked */
873   __HAL_LOCK(hsram);
874 
875   state = hsram->State;
876   if ((state == HAL_SRAM_STATE_READY) || (state == HAL_SRAM_STATE_PROTECTED))
877   {
878     switch (CallbackId)
879     {
880       case HAL_SRAM_DMA_XFER_CPLT_CB_ID :
881         hsram->DmaXferCpltCallback = pCallback;
882         break;
883       case HAL_SRAM_DMA_XFER_ERR_CB_ID :
884         hsram->DmaXferErrorCallback = pCallback;
885         break;
886       default :
887         /* update return status */
888         status =  HAL_ERROR;
889         break;
890     }
891   }
892   else
893   {
894     /* update return status */
895     status =  HAL_ERROR;
896   }
897 
898   /* Release Lock */
899   __HAL_UNLOCK(hsram);
900   return status;
901 }
902 #endif /* USE_HAL_SRAM_REGISTER_CALLBACKS */
903 
904 /**
905   * @}
906   */
907 
908 /** @defgroup SRAM_Exported_Functions_Group3 Control functions
909   *  @brief   Control functions
910   *
911 @verbatim
912   ==============================================================================
913                         ##### SRAM Control functions #####
914   ==============================================================================
915   [..]
916     This subsection provides a set of functions allowing to control dynamically
917     the SRAM interface.
918 
919 @endverbatim
920   * @{
921   */
922 
923 /**
924   * @brief  Enables dynamically SRAM write operation.
925   * @param  hsram pointer to a SRAM_HandleTypeDef structure that contains
926   *                the configuration information for SRAM module.
927   * @retval HAL status
928   */
HAL_SRAM_WriteOperation_Enable(SRAM_HandleTypeDef * hsram)929 HAL_StatusTypeDef HAL_SRAM_WriteOperation_Enable(SRAM_HandleTypeDef *hsram)
930 {
931   /* Check the SRAM controller state */
932   if (hsram->State == HAL_SRAM_STATE_PROTECTED)
933   {
934     /* Process Locked */
935     __HAL_LOCK(hsram);
936 
937     /* Update the SRAM controller state */
938     hsram->State = HAL_SRAM_STATE_BUSY;
939 
940     /* Enable write operation */
941     (void)FSMC_NORSRAM_WriteOperation_Enable(hsram->Instance, hsram->Init.NSBank);
942 
943     /* Update the SRAM controller state */
944     hsram->State = HAL_SRAM_STATE_READY;
945 
946     /* Process unlocked */
947     __HAL_UNLOCK(hsram);
948   }
949   else
950   {
951     return HAL_ERROR;
952   }
953 
954   return HAL_OK;
955 }
956 
957 /**
958   * @brief  Disables dynamically SRAM write operation.
959   * @param  hsram pointer to a SRAM_HandleTypeDef structure that contains
960   *                the configuration information for SRAM module.
961   * @retval HAL status
962   */
HAL_SRAM_WriteOperation_Disable(SRAM_HandleTypeDef * hsram)963 HAL_StatusTypeDef HAL_SRAM_WriteOperation_Disable(SRAM_HandleTypeDef *hsram)
964 {
965   /* Check the SRAM controller state */
966   if (hsram->State == HAL_SRAM_STATE_READY)
967   {
968     /* Process Locked */
969     __HAL_LOCK(hsram);
970 
971     /* Update the SRAM controller state */
972     hsram->State = HAL_SRAM_STATE_BUSY;
973 
974     /* Disable write operation */
975     (void)FSMC_NORSRAM_WriteOperation_Disable(hsram->Instance, hsram->Init.NSBank);
976 
977     /* Update the SRAM controller state */
978     hsram->State = HAL_SRAM_STATE_PROTECTED;
979 
980     /* Process unlocked */
981     __HAL_UNLOCK(hsram);
982   }
983   else
984   {
985     return HAL_ERROR;
986   }
987 
988   return HAL_OK;
989 }
990 
991 /**
992   * @}
993   */
994 
995 /** @defgroup SRAM_Exported_Functions_Group4 Peripheral State functions
996   *  @brief   Peripheral State functions
997   *
998 @verbatim
999   ==============================================================================
1000                       ##### SRAM State functions #####
1001   ==============================================================================
1002   [..]
1003     This subsection permits to get in run-time the status of the SRAM controller
1004     and the data flow.
1005 
1006 @endverbatim
1007   * @{
1008   */
1009 
1010 /**
1011   * @brief  Returns the SRAM controller state
1012   * @param  hsram pointer to a SRAM_HandleTypeDef structure that contains
1013   *                the configuration information for SRAM module.
1014   * @retval HAL state
1015   */
HAL_SRAM_GetState(const SRAM_HandleTypeDef * hsram)1016 HAL_SRAM_StateTypeDef HAL_SRAM_GetState(const SRAM_HandleTypeDef *hsram)
1017 {
1018   return hsram->State;
1019 }
1020 
1021 /**
1022   * @}
1023   */
1024 
1025 /**
1026   * @}
1027   */
1028 
1029 /** @addtogroup SRAM_Private_Functions SRAM Private Functions
1030   * @{
1031   */
1032 
1033 /**
1034   * @brief  DMA SRAM process complete callback.
1035   * @param  hdma : DMA handle
1036   * @retval None
1037   */
SRAM_DMACplt(DMA_HandleTypeDef * hdma)1038 static void SRAM_DMACplt(DMA_HandleTypeDef *hdma)
1039 {
1040   SRAM_HandleTypeDef *hsram = (SRAM_HandleTypeDef *)(hdma->Parent);
1041 
1042   /* Disable the DMA channel */
1043   __HAL_DMA_DISABLE(hdma);
1044 
1045   /* Update the SRAM controller state */
1046   hsram->State = HAL_SRAM_STATE_READY;
1047 
1048 #if (USE_HAL_SRAM_REGISTER_CALLBACKS == 1)
1049   hsram->DmaXferCpltCallback(hdma);
1050 #else
1051   HAL_SRAM_DMA_XferCpltCallback(hdma);
1052 #endif /* USE_HAL_SRAM_REGISTER_CALLBACKS */
1053 }
1054 
1055 /**
1056   * @brief  DMA SRAM process complete callback.
1057   * @param  hdma : DMA handle
1058   * @retval None
1059   */
SRAM_DMACpltProt(DMA_HandleTypeDef * hdma)1060 static void SRAM_DMACpltProt(DMA_HandleTypeDef *hdma)
1061 {
1062   SRAM_HandleTypeDef *hsram = (SRAM_HandleTypeDef *)(hdma->Parent);
1063 
1064   /* Disable the DMA channel */
1065   __HAL_DMA_DISABLE(hdma);
1066 
1067   /* Update the SRAM controller state */
1068   hsram->State = HAL_SRAM_STATE_PROTECTED;
1069 
1070 #if (USE_HAL_SRAM_REGISTER_CALLBACKS == 1)
1071   hsram->DmaXferCpltCallback(hdma);
1072 #else
1073   HAL_SRAM_DMA_XferCpltCallback(hdma);
1074 #endif /* USE_HAL_SRAM_REGISTER_CALLBACKS */
1075 }
1076 
1077 /**
1078   * @brief  DMA SRAM error callback.
1079   * @param  hdma : DMA handle
1080   * @retval None
1081   */
SRAM_DMAError(DMA_HandleTypeDef * hdma)1082 static void SRAM_DMAError(DMA_HandleTypeDef *hdma)
1083 {
1084   SRAM_HandleTypeDef *hsram = (SRAM_HandleTypeDef *)(hdma->Parent);
1085 
1086   /* Disable the DMA channel */
1087   __HAL_DMA_DISABLE(hdma);
1088 
1089   /* Update the SRAM controller state */
1090   hsram->State = HAL_SRAM_STATE_ERROR;
1091 
1092 #if (USE_HAL_SRAM_REGISTER_CALLBACKS == 1)
1093   hsram->DmaXferErrorCallback(hdma);
1094 #else
1095   HAL_SRAM_DMA_XferErrorCallback(hdma);
1096 #endif /* USE_HAL_SRAM_REGISTER_CALLBACKS */
1097 }
1098 
1099 /**
1100   * @}
1101   */
1102 
1103 /**
1104   * @}
1105   */
1106 
1107 #endif /* HAL_SRAM_MODULE_ENABLED */
1108 
1109 /**
1110   * @}
1111   */
1112 
1113