Merge pull request #11189 from klutvott123/move-telemetry-displayport-init
[betaflight.git] / lib / main / STM32G4 / Drivers / STM32G4xx_HAL_Driver / Src / stm32g4xx_hal_sram.c
blobac18daee26e8b15b4185a2456d399ddd34c6d1bc
1 /**
2 ******************************************************************************
3 * @file stm32g4xx_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.
9 @verbatim
10 ==============================================================================
11 ##### How to use this driver #####
12 ==============================================================================
13 [..]
14 This driver is a generic layered driver which contains a set of APIs used to
15 control SRAM memories. It uses the FMC layer functions to interface
16 with SRAM devices.
17 The following sequence should be followed to configure the FMC to interface
18 with SRAM/PSRAM memories:
20 (#) Declare a SRAM_HandleTypeDef handle structure, for example:
21 SRAM_HandleTypeDef hsram; and:
23 (++) Fill the SRAM_HandleTypeDef handle "Init" field with the allowed
24 values of the structure member.
26 (++) Fill the SRAM_HandleTypeDef handle "Instance" field with a predefined
27 base register instance for NOR or SRAM device
29 (++) Fill the SRAM_HandleTypeDef handle "Extended" field with a predefined
30 base register instance for NOR or SRAM extended mode
32 (#) Declare two FMC_NORSRAM_TimingTypeDef structures, for both normal and extended
33 mode timings; for example:
34 FMC_NORSRAM_TimingTypeDef Timing and FMC_NORSRAM_TimingTypeDef ExTiming;
35 and fill its fields with the allowed values of the structure member.
37 (#) Initialize the SRAM Controller by calling the function HAL_SRAM_Init(). This function
38 performs the following sequence:
40 (##) MSP hardware layer configuration using the function HAL_SRAM_MspInit()
41 (##) Control register configuration using the FMC NORSRAM interface function
42 FMC_NORSRAM_Init()
43 (##) Timing register configuration using the FMC NORSRAM interface function
44 FMC_NORSRAM_Timing_Init()
45 (##) Extended mode Timing register configuration using the FMC NORSRAM interface function
46 FMC_NORSRAM_Extended_Timing_Init()
47 (##) Enable the SRAM device using the macro __FMC_NORSRAM_ENABLE()
49 (#) At this stage you can perform read/write accesses from/to the memory connected
50 to the NOR/SRAM Bank. You can perform either polling or DMA transfer using the
51 following APIs:
52 (++) HAL_SRAM_Read()/HAL_SRAM_Write() for polling read/write access
53 (++) HAL_SRAM_Read_DMA()/HAL_SRAM_Write_DMA() for DMA read/write transfer
55 (#) You can also control the SRAM device by calling the control APIs HAL_SRAM_WriteOperation_Enable()/
56 HAL_SRAM_WriteOperation_Disable() to respectively enable/disable the SRAM write operation
58 (#) You can continuously monitor the SRAM device HAL state by calling the function
59 HAL_SRAM_GetState()
61 *** Callback registration ***
62 =============================================
63 [..]
64 The compilation define USE_HAL_SRAM_REGISTER_CALLBACKS when set to 1
65 allows the user to configure dynamically the driver callbacks.
67 Use Functions @ref HAL_SRAM_RegisterCallback() to register a user callback,
68 it allows to register following callbacks:
69 (+) MspInitCallback : SRAM MspInit.
70 (+) MspDeInitCallback : SRAM MspDeInit.
71 This function takes as parameters the HAL peripheral handle, the Callback ID
72 and a pointer to the user callback function.
74 Use function @ref HAL_SRAM_UnRegisterCallback() to reset a callback to the default
75 weak (surcharged) function. It allows to reset following callbacks:
76 (+) MspInitCallback : SRAM MspInit.
77 (+) MspDeInitCallback : SRAM MspDeInit.
78 This function) takes as parameters the HAL peripheral handle and the Callback ID.
80 By default, after the @ref HAL_SRAM_Init and if the state is HAL_SRAM_STATE_RESET
81 all callbacks are reset to the corresponding legacy weak (surcharged) functions.
82 Exception done for MspInit and MspDeInit callbacks that are respectively
83 reset to the legacy weak (surcharged) functions in the @ref HAL_SRAM_Init
84 and @ref HAL_SRAM_DeInit only when these callbacks are null (not registered beforehand).
85 If not, MspInit or MspDeInit are not null, the @ref HAL_SRAM_Init and @ref HAL_SRAM_DeInit
86 keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
88 Callbacks can be registered/unregistered in READY state only.
89 Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
90 in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
91 during the Init/DeInit.
92 In that case first register the MspInit/MspDeInit user callbacks
93 using @ref HAL_SRAM_RegisterCallback before calling @ref HAL_SRAM_DeInit
94 or @ref HAL_SRAM_Init function.
96 When The compilation define USE_HAL_SRAM_REGISTER_CALLBACKS is set to 0 or
97 not defined, the callback registering feature is not available
98 and weak (surcharged) callbacks are used.
100 @endverbatim
101 ******************************************************************************
102 * @attention
104 * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
105 * All rights reserved.</center></h2>
107 * This software component is licensed by ST under BSD 3-Clause license,
108 * the "License"; You may not use this file except in compliance with the
109 * License. You may obtain a copy of the License at:
110 * opensource.org/licenses/BSD-3-Clause
112 ******************************************************************************
115 /* Includes ------------------------------------------------------------------*/
116 #include "stm32g4xx_hal.h"
118 #if defined(FMC_BANK1)
120 /** @addtogroup STM32G4xx_HAL_Driver
121 * @{
124 #ifdef HAL_SRAM_MODULE_ENABLED
126 /** @defgroup SRAM SRAM
127 * @brief SRAM driver modules
128 * @{
132 @cond 0
134 /* Private typedef -----------------------------------------------------------*/
135 /* Private define ------------------------------------------------------------*/
136 /* Private macro -------------------------------------------------------------*/
137 /* Private variables ---------------------------------------------------------*/
138 /* Private function prototypes -----------------------------------------------*/
139 static void SRAM_DMACplt (DMA_HandleTypeDef *hdma);
140 static void SRAM_DMACpltProt(DMA_HandleTypeDef *hdma);
141 static void SRAM_DMAError (DMA_HandleTypeDef *hdma);
143 @endcond
146 /* Exported functions --------------------------------------------------------*/
148 /** @defgroup SRAM_Exported_Functions SRAM Exported Functions
149 * @{
152 /** @defgroup SRAM_Exported_Functions_Group1 Initialization and de-initialization functions
153 * @brief Initialization and Configuration functions.
155 @verbatim
156 ==============================================================================
157 ##### SRAM Initialization and de_initialization functions #####
158 ==============================================================================
159 [..] This section provides functions allowing to initialize/de-initialize
160 the SRAM memory
162 @endverbatim
163 * @{
167 * @brief Performs the SRAM device initialization sequence
168 * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
169 * the configuration information for SRAM module.
170 * @param Timing Pointer to SRAM control timing structure
171 * @param ExtTiming Pointer to SRAM extended mode timing structure
172 * @retval HAL status
174 HAL_StatusTypeDef HAL_SRAM_Init(SRAM_HandleTypeDef *hsram, FMC_NORSRAM_TimingTypeDef *Timing, FMC_NORSRAM_TimingTypeDef *ExtTiming)
176 /* Check the SRAM handle parameter */
177 if (hsram == NULL)
179 return HAL_ERROR;
182 if (hsram->State == HAL_SRAM_STATE_RESET)
184 /* Allocate lock resource and initialize it */
185 hsram->Lock = HAL_UNLOCKED;
187 #if (USE_HAL_SRAM_REGISTER_CALLBACKS == 1)
188 if(hsram->MspInitCallback == NULL)
190 hsram->MspInitCallback = HAL_SRAM_MspInit;
192 hsram->DmaXferCpltCallback = HAL_SRAM_DMA_XferCpltCallback;
193 hsram->DmaXferErrorCallback = HAL_SRAM_DMA_XferErrorCallback;
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
203 /* Initialize SRAM control Interface */
204 (void)FMC_NORSRAM_Init(hsram->Instance, &(hsram->Init));
206 /* Initialize SRAM timing Interface */
207 (void)FMC_NORSRAM_Timing_Init(hsram->Instance, Timing, hsram->Init.NSBank);
209 /* Initialize SRAM extended mode timing Interface */
210 (void)FMC_NORSRAM_Extended_Timing_Init(hsram->Extended, ExtTiming, hsram->Init.NSBank, hsram->Init.ExtendedMode);
212 /* Enable the NORSRAM device */
213 __FMC_NORSRAM_ENABLE(hsram->Instance, hsram->Init.NSBank);
215 /* Initialize the SRAM controller state */
216 hsram->State = HAL_SRAM_STATE_READY;
218 return HAL_OK;
222 * @brief Performs the SRAM device De-initialization sequence.
223 * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
224 * the configuration information for SRAM module.
225 * @retval HAL status
227 HAL_StatusTypeDef HAL_SRAM_DeInit(SRAM_HandleTypeDef *hsram)
229 #if (USE_HAL_SRAM_REGISTER_CALLBACKS == 1)
230 if(hsram->MspDeInitCallback == NULL)
232 hsram->MspDeInitCallback = HAL_SRAM_MspDeInit;
235 /* DeInit the low level hardware */
236 hsram->MspDeInitCallback(hsram);
237 #else
238 /* De-Initialize the low level hardware (MSP) */
239 HAL_SRAM_MspDeInit(hsram);
240 #endif
242 /* Configure the SRAM registers with their reset values */
243 (void)FMC_NORSRAM_DeInit(hsram->Instance, hsram->Extended, hsram->Init.NSBank);
245 /* Reset the SRAM controller state */
246 hsram->State = HAL_SRAM_STATE_RESET;
248 /* Release Lock */
249 __HAL_UNLOCK(hsram);
251 return HAL_OK;
255 * @brief SRAM MSP Init.
256 * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
257 * the configuration information for SRAM module.
258 * @retval None
260 __weak void HAL_SRAM_MspInit(SRAM_HandleTypeDef *hsram)
262 /* Prevent unused argument(s) compilation warning */
263 UNUSED(hsram);
265 /* NOTE : This function Should not be modified, when the callback is needed,
266 the HAL_SRAM_MspInit could be implemented in the user file
271 * @brief SRAM MSP DeInit.
272 * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
273 * the configuration information for SRAM module.
274 * @retval None
276 __weak void HAL_SRAM_MspDeInit(SRAM_HandleTypeDef *hsram)
278 /* Prevent unused argument(s) compilation warning */
279 UNUSED(hsram);
281 /* NOTE : This function Should not be modified, when the callback is needed,
282 the HAL_SRAM_MspDeInit could be implemented in the user file
287 * @brief DMA transfer complete callback.
288 * @param hdma pointer to a SRAM_HandleTypeDef structure that contains
289 * the configuration information for SRAM module.
290 * @retval None
292 __weak void HAL_SRAM_DMA_XferCpltCallback(DMA_HandleTypeDef *hdma)
294 /* Prevent unused argument(s) compilation warning */
295 UNUSED(hdma);
297 /* NOTE : This function Should not be modified, when the callback is needed,
298 the HAL_SRAM_DMA_XferCpltCallback could be implemented in the user file
303 * @brief DMA transfer complete error callback.
304 * @param hdma pointer to a SRAM_HandleTypeDef structure that contains
305 * the configuration information for SRAM module.
306 * @retval None
308 __weak void HAL_SRAM_DMA_XferErrorCallback(DMA_HandleTypeDef *hdma)
310 /* Prevent unused argument(s) compilation warning */
311 UNUSED(hdma);
313 /* NOTE : This function Should not be modified, when the callback is needed,
314 the HAL_SRAM_DMA_XferErrorCallback could be implemented in the user file
319 * @}
322 /** @defgroup SRAM_Exported_Functions_Group2 Input Output and memory control functions
323 * @brief Input Output and memory control functions
325 @verbatim
326 ==============================================================================
327 ##### SRAM Input and Output functions #####
328 ==============================================================================
329 [..]
330 This section provides functions allowing to use and control the SRAM memory
332 @endverbatim
333 * @{
337 * @brief Reads 8-bit buffer from SRAM memory.
338 * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
339 * the configuration information for SRAM module.
340 * @param pAddress Pointer to read start address
341 * @param pDstBuffer Pointer to destination buffer
342 * @param BufferSize Size of the buffer to read from memory
343 * @retval HAL status
345 HAL_StatusTypeDef HAL_SRAM_Read_8b(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint8_t *pDstBuffer, uint32_t BufferSize)
347 uint32_t size;
348 __IO uint8_t *psramaddress = (uint8_t *)pAddress;
349 uint8_t * pdestbuff = pDstBuffer;
350 HAL_SRAM_StateTypeDef state = hsram->State;
352 /* Check the SRAM controller state */
353 if ((state == HAL_SRAM_STATE_READY) || (state == HAL_SRAM_STATE_PROTECTED))
355 /* Process Locked */
356 __HAL_LOCK(hsram);
358 /* Update the SRAM controller state */
359 hsram->State = HAL_SRAM_STATE_BUSY;
361 /* Read data from memory */
362 for (size = BufferSize; size != 0U; size--)
364 *pdestbuff = *psramaddress;
365 pdestbuff++;
366 psramaddress++;
369 /* Update the SRAM controller state */
370 hsram->State = state;
372 /* Process unlocked */
373 __HAL_UNLOCK(hsram);
375 else
377 return HAL_ERROR;
380 return HAL_OK;
384 * @brief Writes 8-bit buffer to SRAM memory.
385 * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
386 * the configuration information for SRAM module.
387 * @param pAddress Pointer to write start address
388 * @param pSrcBuffer Pointer to source buffer to write
389 * @param BufferSize Size of the buffer to write to memory
390 * @retval HAL status
392 HAL_StatusTypeDef HAL_SRAM_Write_8b(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint8_t *pSrcBuffer, uint32_t BufferSize)
394 uint32_t size;
395 __IO uint8_t *psramaddress = (uint8_t *)pAddress;
396 uint8_t * psrcbuff = pSrcBuffer;
398 /* Check the SRAM controller state */
399 if (hsram->State == HAL_SRAM_STATE_READY)
401 /* Process Locked */
402 __HAL_LOCK(hsram);
404 /* Update the SRAM controller state */
405 hsram->State = HAL_SRAM_STATE_BUSY;
407 /* Write data to memory */
408 for (size = BufferSize; size != 0U; size--)
410 *psramaddress = *psrcbuff;
411 psrcbuff++;
412 psramaddress++;
415 /* Update the SRAM controller state */
416 hsram->State = HAL_SRAM_STATE_READY;
418 /* Process unlocked */
419 __HAL_UNLOCK(hsram);
421 else
423 return HAL_ERROR;
426 return HAL_OK;
430 * @brief Reads 16-bit buffer from SRAM memory.
431 * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
432 * the configuration information for SRAM module.
433 * @param pAddress Pointer to read start address
434 * @param pDstBuffer Pointer to destination buffer
435 * @param BufferSize Size of the buffer to read from memory
436 * @retval HAL status
438 HAL_StatusTypeDef HAL_SRAM_Read_16b(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint16_t *pDstBuffer, uint32_t BufferSize)
440 uint32_t size;
441 __IO uint32_t *psramaddress = pAddress;
442 uint16_t *pdestbuff = pDstBuffer;
443 uint8_t limit;
444 HAL_SRAM_StateTypeDef state = hsram->State;
446 /* Check the SRAM controller state */
447 if ((state == HAL_SRAM_STATE_READY) || (state == HAL_SRAM_STATE_PROTECTED))
449 /* Process Locked */
450 __HAL_LOCK(hsram);
452 /* Update the SRAM controller state */
453 hsram->State = HAL_SRAM_STATE_BUSY;
455 /* Check if the size is a 32-bits mulitple */
456 limit = (((BufferSize % 2U) != 0U) ? 1U : 0U);
458 /* Read data from memory */
459 for (size = BufferSize; size != limit; size-=2U)
461 *pdestbuff = (uint16_t)((*psramaddress) & 0x0000FFFFU);
462 pdestbuff++;
463 *pdestbuff = (uint16_t)(((*psramaddress) & 0xFFFF0000U) >> 16U);
464 pdestbuff++;
465 psramaddress++;
468 /* Read last 16-bits if size is not 32-bits multiple */
469 if (limit != 0U)
471 *pdestbuff = (uint16_t)((*psramaddress) & 0x0000FFFFU);
474 /* Update the SRAM controller state */
475 hsram->State = state;
477 /* Process unlocked */
478 __HAL_UNLOCK(hsram);
480 else
482 return HAL_ERROR;
485 return HAL_OK;
489 * @brief Writes 16-bit buffer to SRAM memory.
490 * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
491 * the configuration information for SRAM module.
492 * @param pAddress Pointer to write start address
493 * @param pSrcBuffer Pointer to source buffer to write
494 * @param BufferSize Size of the buffer to write to memory
495 * @retval HAL status
497 HAL_StatusTypeDef HAL_SRAM_Write_16b(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint16_t *pSrcBuffer, uint32_t BufferSize)
499 uint32_t size;
500 __IO uint32_t *psramaddress = pAddress;
501 uint16_t * psrcbuff = pSrcBuffer;
502 uint8_t limit;
504 /* Check the SRAM controller state */
505 if (hsram->State == HAL_SRAM_STATE_READY)
507 /* Process Locked */
508 __HAL_LOCK(hsram);
510 /* Update the SRAM controller state */
511 hsram->State = HAL_SRAM_STATE_BUSY;
513 /* Check if the size is a 32-bits mulitple */
514 limit = (((BufferSize % 2U) != 0U) ? 1U : 0U);
516 /* Write data to memory */
517 for (size = BufferSize; size != limit; size-=2U)
519 *psramaddress = (uint32_t)(*psrcbuff);
520 psrcbuff++;
521 *psramaddress |= ((uint32_t)(*psrcbuff) << 16U);
522 psrcbuff++;
523 psramaddress++;
526 /* Write last 16-bits if size is not 32-bits multiple */
527 if (limit != 0U)
529 *psramaddress = ((uint32_t)(*psrcbuff) & 0x0000FFFFU) | ((*psramaddress) & 0xFFFF0000U);
532 /* Update the SRAM controller state */
533 hsram->State = HAL_SRAM_STATE_READY;
535 /* Process unlocked */
536 __HAL_UNLOCK(hsram);
538 else
540 return HAL_ERROR;
543 return HAL_OK;
547 * @brief Reads 32-bit buffer from SRAM memory.
548 * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
549 * the configuration information for SRAM module.
550 * @param pAddress Pointer to read start address
551 * @param pDstBuffer Pointer to destination buffer
552 * @param BufferSize Size of the buffer to read from memory
553 * @retval HAL status
555 HAL_StatusTypeDef HAL_SRAM_Read_32b(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint32_t *pDstBuffer, uint32_t BufferSize)
557 uint32_t size;
558 __IO uint32_t * psramaddress = pAddress;
559 uint32_t * pdestbuff = pDstBuffer;
560 HAL_SRAM_StateTypeDef state = hsram->State;
562 /* Check the SRAM controller state */
563 if ((state == HAL_SRAM_STATE_READY) || (state == HAL_SRAM_STATE_PROTECTED))
565 /* Process Locked */
566 __HAL_LOCK(hsram);
568 /* Update the SRAM controller state */
569 hsram->State = HAL_SRAM_STATE_BUSY;
571 /* Read data from memory */
572 for (size = BufferSize; size != 0U; size--)
574 *pdestbuff = *psramaddress;
575 pdestbuff++;
576 psramaddress++;
579 /* Update the SRAM controller state */
580 hsram->State = state;
582 /* Process unlocked */
583 __HAL_UNLOCK(hsram);
585 else
587 return HAL_ERROR;
590 return HAL_OK;
594 * @brief Writes 32-bit buffer to SRAM memory.
595 * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
596 * the configuration information for SRAM module.
597 * @param pAddress Pointer to write start address
598 * @param pSrcBuffer Pointer to source buffer to write
599 * @param BufferSize Size of the buffer to write to memory
600 * @retval HAL status
602 HAL_StatusTypeDef HAL_SRAM_Write_32b(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint32_t *pSrcBuffer, uint32_t BufferSize)
604 uint32_t size;
605 __IO uint32_t * psramaddress = pAddress;
606 uint32_t * psrcbuff = pSrcBuffer;
608 /* Check the SRAM controller state */
609 if (hsram->State == HAL_SRAM_STATE_READY)
611 /* Process Locked */
612 __HAL_LOCK(hsram);
614 /* Update the SRAM controller state */
615 hsram->State = HAL_SRAM_STATE_BUSY;
617 /* Write data to memory */
618 for (size = BufferSize; size != 0U; size--)
620 *psramaddress = *psrcbuff;
621 psrcbuff++;
622 psramaddress++;
625 /* Update the SRAM controller state */
626 hsram->State = HAL_SRAM_STATE_READY;
628 /* Process unlocked */
629 __HAL_UNLOCK(hsram);
631 else
633 return HAL_ERROR;
636 return HAL_OK;
640 * @brief Reads a Words data from the SRAM memory using DMA transfer.
641 * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
642 * the configuration information for SRAM module.
643 * @param pAddress Pointer to read start address
644 * @param pDstBuffer Pointer to destination buffer
645 * @param BufferSize Size of the buffer to read from memory
646 * @retval HAL status
648 HAL_StatusTypeDef HAL_SRAM_Read_DMA(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint32_t *pDstBuffer, uint32_t BufferSize)
650 HAL_StatusTypeDef status;
651 HAL_SRAM_StateTypeDef state = hsram->State;
653 /* Check the SRAM controller state */
654 if ((state == HAL_SRAM_STATE_READY) || (state == HAL_SRAM_STATE_PROTECTED))
656 /* Process Locked */
657 __HAL_LOCK(hsram);
659 /* Update the SRAM controller state */
660 hsram->State = HAL_SRAM_STATE_BUSY;
662 /* Configure DMA user callbacks */
663 if (state == HAL_SRAM_STATE_READY)
665 hsram->hdma->XferCpltCallback = SRAM_DMACplt;
667 else
669 hsram->hdma->XferCpltCallback = SRAM_DMACpltProt;
671 hsram->hdma->XferErrorCallback = SRAM_DMAError;
673 /* Enable the DMA Stream */
674 status = HAL_DMA_Start_IT(hsram->hdma, (uint32_t)pAddress, (uint32_t)pDstBuffer, (uint32_t)BufferSize);
676 /* Process unlocked */
677 __HAL_UNLOCK(hsram);
679 else
681 return HAL_ERROR;
684 return status;
688 * @brief Writes a Words data buffer to SRAM memory using DMA transfer.
689 * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
690 * the configuration information for SRAM module.
691 * @param pAddress Pointer to write start address
692 * @param pSrcBuffer Pointer to source buffer to write
693 * @param BufferSize Size of the buffer to write to memory
694 * @retval HAL status
696 HAL_StatusTypeDef HAL_SRAM_Write_DMA(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint32_t *pSrcBuffer, uint32_t BufferSize)
698 HAL_StatusTypeDef status;
700 /* Check the SRAM controller state */
701 if (hsram->State == HAL_SRAM_STATE_READY)
703 /* Process Locked */
704 __HAL_LOCK(hsram);
706 /* Update the SRAM controller state */
707 hsram->State = HAL_SRAM_STATE_BUSY;
709 /* Configure DMA user callbacks */
710 hsram->hdma->XferCpltCallback = SRAM_DMACplt;
711 hsram->hdma->XferErrorCallback = SRAM_DMAError;
713 /* Enable the DMA Stream */
714 status = HAL_DMA_Start_IT(hsram->hdma, (uint32_t)pSrcBuffer, (uint32_t)pAddress, (uint32_t)BufferSize);
716 /* Process unlocked */
717 __HAL_UNLOCK(hsram);
719 else
721 return HAL_ERROR;
724 return status;
727 #if (USE_HAL_SRAM_REGISTER_CALLBACKS == 1)
729 * @brief Register a User SRAM Callback
730 * To be used instead of the weak (surcharged) predefined callback
731 * @param hsram : SRAM handle
732 * @param CallbackId : ID of the callback to be registered
733 * This parameter can be one of the following values:
734 * @arg @ref HAL_SRAM_MSP_INIT_CB_ID SRAM MspInit callback ID
735 * @arg @ref HAL_SRAM_MSP_DEINIT_CB_ID SRAM MspDeInit callback ID
736 * @param pCallback : pointer to the Callback function
737 * @retval status
739 HAL_StatusTypeDef HAL_SRAM_RegisterCallback (SRAM_HandleTypeDef *hsram, HAL_SRAM_CallbackIDTypeDef CallbackId, pSRAM_CallbackTypeDef pCallback)
741 HAL_StatusTypeDef status = HAL_OK;
742 HAL_SRAM_StateTypeDef state;
744 if(pCallback == NULL)
746 return HAL_ERROR;
749 /* Process locked */
750 __HAL_LOCK(hsram);
752 state = hsram->State;
753 if((state == HAL_SRAM_STATE_READY) || (state == HAL_SRAM_STATE_RESET) || (state == HAL_SRAM_STATE_PROTECTED))
755 switch (CallbackId)
757 case HAL_SRAM_MSP_INIT_CB_ID :
758 hsram->MspInitCallback = pCallback;
759 break;
760 case HAL_SRAM_MSP_DEINIT_CB_ID :
761 hsram->MspDeInitCallback = pCallback;
762 break;
763 default :
764 /* update return status */
765 status = HAL_ERROR;
766 break;
769 else
771 /* update return status */
772 status = HAL_ERROR;
775 /* Release Lock */
776 __HAL_UNLOCK(hsram);
777 return status;
781 * @brief Unregister a User SRAM Callback
782 * SRAM Callback is redirected to the weak (surcharged) predefined callback
783 * @param hsram : SRAM handle
784 * @param CallbackId : ID of the callback to be unregistered
785 * This parameter can be one of the following values:
786 * @arg @ref HAL_SRAM_MSP_INIT_CB_ID SRAM MspInit callback ID
787 * @arg @ref HAL_SRAM_MSP_DEINIT_CB_ID SRAM MspDeInit callback ID
788 * @arg @ref HAL_SRAM_DMA_XFER_CPLT_CB_ID SRAM DMA Xfer Complete callback ID
789 * @arg @ref HAL_SRAM_DMA_XFER_ERR_CB_ID SRAM DMA Xfer Error callback ID
790 * @retval status
792 HAL_StatusTypeDef HAL_SRAM_UnRegisterCallback (SRAM_HandleTypeDef *hsram, HAL_SRAM_CallbackIDTypeDef CallbackId)
794 HAL_StatusTypeDef status = HAL_OK;
795 HAL_SRAM_StateTypeDef state;
797 /* Process locked */
798 __HAL_LOCK(hsram);
800 state = hsram->State;
801 if((state == HAL_SRAM_STATE_READY) || (state == HAL_SRAM_STATE_PROTECTED))
803 switch (CallbackId)
805 case HAL_SRAM_MSP_INIT_CB_ID :
806 hsram->MspInitCallback = HAL_SRAM_MspInit;
807 break;
808 case HAL_SRAM_MSP_DEINIT_CB_ID :
809 hsram->MspDeInitCallback = HAL_SRAM_MspDeInit;
810 break;
811 case HAL_SRAM_DMA_XFER_CPLT_CB_ID :
812 hsram->DmaXferCpltCallback = HAL_SRAM_DMA_XferCpltCallback;
813 break;
814 case HAL_SRAM_DMA_XFER_ERR_CB_ID :
815 hsram->DmaXferErrorCallback = HAL_SRAM_DMA_XferErrorCallback;
816 break;
817 default :
818 /* update return status */
819 status = HAL_ERROR;
820 break;
823 else if(state == HAL_SRAM_STATE_RESET)
825 switch (CallbackId)
827 case HAL_SRAM_MSP_INIT_CB_ID :
828 hsram->MspInitCallback = HAL_SRAM_MspInit;
829 break;
830 case HAL_SRAM_MSP_DEINIT_CB_ID :
831 hsram->MspDeInitCallback = HAL_SRAM_MspDeInit;
832 break;
833 default :
834 /* update return status */
835 status = HAL_ERROR;
836 break;
839 else
841 /* update return status */
842 status = HAL_ERROR;
845 /* Release Lock */
846 __HAL_UNLOCK(hsram);
847 return status;
851 * @brief Register a User SRAM Callback for DMA transfers
852 * To be used instead of the weak (surcharged) 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
861 HAL_StatusTypeDef HAL_SRAM_RegisterDmaCallback(SRAM_HandleTypeDef *hsram, HAL_SRAM_CallbackIDTypeDef CallbackId, pSRAM_DmaCallbackTypeDef pCallback)
863 HAL_StatusTypeDef status = HAL_OK;
864 HAL_SRAM_StateTypeDef state;
866 if(pCallback == NULL)
868 return HAL_ERROR;
871 /* Process locked */
872 __HAL_LOCK(hsram);
874 state = hsram->State;
875 if((state == HAL_SRAM_STATE_READY) || (state == HAL_SRAM_STATE_PROTECTED))
877 switch (CallbackId)
879 case HAL_SRAM_DMA_XFER_CPLT_CB_ID :
880 hsram->DmaXferCpltCallback = pCallback;
881 break;
882 case HAL_SRAM_DMA_XFER_ERR_CB_ID :
883 hsram->DmaXferErrorCallback = pCallback;
884 break;
885 default :
886 /* update return status */
887 status = HAL_ERROR;
888 break;
891 else
893 /* update return status */
894 status = HAL_ERROR;
897 /* Release Lock */
898 __HAL_UNLOCK(hsram);
899 return status;
901 #endif
904 * @}
907 /** @defgroup SRAM_Exported_Functions_Group3 Control functions
908 * @brief Control functions
910 @verbatim
911 ==============================================================================
912 ##### SRAM Control functions #####
913 ==============================================================================
914 [..]
915 This subsection provides a set of functions allowing to control dynamically
916 the SRAM interface.
918 @endverbatim
919 * @{
923 * @brief Enables dynamically SRAM write operation.
924 * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
925 * the configuration information for SRAM module.
926 * @retval HAL status
928 HAL_StatusTypeDef HAL_SRAM_WriteOperation_Enable(SRAM_HandleTypeDef *hsram)
930 /* Check the SRAM controller state */
931 if(hsram->State == HAL_SRAM_STATE_PROTECTED)
933 /* Process Locked */
934 __HAL_LOCK(hsram);
936 /* Update the SRAM controller state */
937 hsram->State = HAL_SRAM_STATE_BUSY;
939 /* Enable write operation */
940 (void)FMC_NORSRAM_WriteOperation_Enable(hsram->Instance, hsram->Init.NSBank);
942 /* Update the SRAM controller state */
943 hsram->State = HAL_SRAM_STATE_READY;
945 /* Process unlocked */
946 __HAL_UNLOCK(hsram);
948 else
950 return HAL_ERROR;
953 return HAL_OK;
957 * @brief Disables dynamically SRAM write operation.
958 * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
959 * the configuration information for SRAM module.
960 * @retval HAL status
962 HAL_StatusTypeDef HAL_SRAM_WriteOperation_Disable(SRAM_HandleTypeDef *hsram)
964 /* Check the SRAM controller state */
965 if(hsram->State == HAL_SRAM_STATE_READY)
967 /* Process Locked */
968 __HAL_LOCK(hsram);
970 /* Update the SRAM controller state */
971 hsram->State = HAL_SRAM_STATE_BUSY;
973 /* Disable write operation */
974 (void)FMC_NORSRAM_WriteOperation_Disable(hsram->Instance, hsram->Init.NSBank);
976 /* Update the SRAM controller state */
977 hsram->State = HAL_SRAM_STATE_PROTECTED;
979 /* Process unlocked */
980 __HAL_UNLOCK(hsram);
982 else
984 return HAL_ERROR;
987 return HAL_OK;
991 * @}
994 /** @defgroup SRAM_Exported_Functions_Group4 Peripheral State functions
995 * @brief Peripheral State functions
997 @verbatim
998 ==============================================================================
999 ##### SRAM State functions #####
1000 ==============================================================================
1001 [..]
1002 This subsection permits to get in run-time the status of the SRAM controller
1003 and the data flow.
1005 @endverbatim
1006 * @{
1010 * @brief Returns the SRAM controller state
1011 * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
1012 * the configuration information for SRAM module.
1013 * @retval HAL state
1015 HAL_SRAM_StateTypeDef HAL_SRAM_GetState(SRAM_HandleTypeDef *hsram)
1017 return hsram->State;
1021 * @}
1025 * @}
1029 @cond 0
1032 * @brief DMA SRAM process complete callback.
1033 * @param hdma : DMA handle
1034 * @retval None
1036 static void SRAM_DMACplt(DMA_HandleTypeDef *hdma)
1038 SRAM_HandleTypeDef* hsram = ( SRAM_HandleTypeDef* )(hdma->Parent);
1040 /* Disable the DMA channel */
1041 __HAL_DMA_DISABLE(hdma);
1043 /* Update the SRAM controller state */
1044 hsram->State = HAL_SRAM_STATE_READY;
1046 #if (USE_HAL_SRAM_REGISTER_CALLBACKS == 1)
1047 hsram->DmaXferCpltCallback(hdma);
1048 #else
1049 HAL_SRAM_DMA_XferCpltCallback(hdma);
1050 #endif
1054 * @brief DMA SRAM process complete callback.
1055 * @param hdma : DMA handle
1056 * @retval None
1058 static void SRAM_DMACpltProt(DMA_HandleTypeDef *hdma)
1060 SRAM_HandleTypeDef* hsram = ( SRAM_HandleTypeDef* )(hdma->Parent);
1062 /* Disable the DMA channel */
1063 __HAL_DMA_DISABLE(hdma);
1065 /* Update the SRAM controller state */
1066 hsram->State = HAL_SRAM_STATE_PROTECTED;
1068 #if (USE_HAL_SRAM_REGISTER_CALLBACKS == 1)
1069 hsram->DmaXferCpltCallback(hdma);
1070 #else
1071 HAL_SRAM_DMA_XferCpltCallback(hdma);
1072 #endif
1076 * @brief DMA SRAM error callback.
1077 * @param hdma : DMA handle
1078 * @retval None
1080 static void SRAM_DMAError(DMA_HandleTypeDef *hdma)
1082 SRAM_HandleTypeDef* hsram = ( SRAM_HandleTypeDef* )(hdma->Parent);
1084 /* Disable the DMA channel */
1085 __HAL_DMA_DISABLE(hdma);
1087 /* Update the SRAM controller state */
1088 hsram->State = HAL_SRAM_STATE_ERROR;
1090 #if (USE_HAL_SRAM_REGISTER_CALLBACKS == 1)
1091 hsram->DmaXferErrorCallback(hdma);
1092 #else
1093 HAL_SRAM_DMA_XferErrorCallback(hdma);
1094 #endif
1097 @endcond
1101 * @}
1104 #endif /* HAL_SRAM_MODULE_ENABLED */
1107 * @}
1110 #endif /* FMC_BANK1 */
1112 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/