Create release.yml
[betaflight.git] / lib / main / STM32H7 / Drivers / STM32H7xx_HAL_Driver / Src / stm32h7xx_hal_sram.c
blob6da1757d89f07465652ec3d7b74a51bc1c6d9ea4
1 /**
2 ******************************************************************************
3 * @file stm32h7xx_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 "stm32h7xx_hal.h"
119 /** @addtogroup STM32H7xx_HAL_Driver
120 * @{
123 #ifdef HAL_SRAM_MODULE_ENABLED
125 /** @defgroup SRAM SRAM
126 * @brief SRAM driver modules
127 * @{
131 @cond 0
133 /* Private typedef -----------------------------------------------------------*/
134 /* Private define ------------------------------------------------------------*/
135 /* Private macro -------------------------------------------------------------*/
136 /* Private variables ---------------------------------------------------------*/
137 /* Private function prototypes -----------------------------------------------*/
138 static void SRAM_DMACplt (MDMA_HandleTypeDef *hmdma);
139 static void SRAM_DMACpltProt(MDMA_HandleTypeDef *hmdma);
140 static void SRAM_DMAError (MDMA_HandleTypeDef *hmdma);
142 @endcond
145 /* Exported functions --------------------------------------------------------*/
147 /** @defgroup SRAM_Exported_Functions SRAM Exported Functions
148 * @{
151 /** @defgroup SRAM_Exported_Functions_Group1 Initialization and de-initialization functions
152 * @brief Initialization and Configuration functions.
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
161 @endverbatim
162 * @{
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
173 HAL_StatusTypeDef HAL_SRAM_Init(SRAM_HandleTypeDef *hsram, FMC_NORSRAM_TimingTypeDef *Timing, FMC_NORSRAM_TimingTypeDef *ExtTiming)
175 /* Check the SRAM handle parameter */
176 if (hsram == NULL)
178 return HAL_ERROR;
181 if (hsram->State == HAL_SRAM_STATE_RESET)
183 /* Allocate lock resource and initialize it */
184 hsram->Lock = HAL_UNLOCKED;
186 #if (USE_HAL_SRAM_REGISTER_CALLBACKS == 1)
187 if(hsram->MspInitCallback == NULL)
189 hsram->MspInitCallback = HAL_SRAM_MspInit;
191 hsram->DmaXferCpltCallback = HAL_SRAM_DMA_XferCpltCallback;
192 hsram->DmaXferErrorCallback = HAL_SRAM_DMA_XferErrorCallback;
194 /* Init the low level hardware */
195 hsram->MspInitCallback(hsram);
196 #else
197 /* Initialize the low level hardware (MSP) */
198 HAL_SRAM_MspInit(hsram);
199 #endif
202 /* Initialize SRAM control Interface */
203 (void)FMC_NORSRAM_Init(hsram->Instance, &(hsram->Init));
205 /* Initialize SRAM timing Interface */
206 (void)FMC_NORSRAM_Timing_Init(hsram->Instance, Timing, hsram->Init.NSBank);
208 /* Initialize SRAM extended mode timing Interface */
209 (void)FMC_NORSRAM_Extended_Timing_Init(hsram->Extended, ExtTiming, hsram->Init.NSBank, hsram->Init.ExtendedMode);
211 /* Enable the NORSRAM device */
212 __FMC_NORSRAM_ENABLE(hsram->Instance, hsram->Init.NSBank);
214 /* Enable FMC Peripheral */
215 __FMC_ENABLE();
217 /* Initialize the SRAM controller state */
218 hsram->State = HAL_SRAM_STATE_READY;
220 return HAL_OK;
224 * @brief Performs the SRAM device De-initialization sequence.
225 * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
226 * the configuration information for SRAM module.
227 * @retval HAL status
229 HAL_StatusTypeDef HAL_SRAM_DeInit(SRAM_HandleTypeDef *hsram)
231 #if (USE_HAL_SRAM_REGISTER_CALLBACKS == 1)
232 if(hsram->MspDeInitCallback == NULL)
234 hsram->MspDeInitCallback = HAL_SRAM_MspDeInit;
237 /* DeInit the low level hardware */
238 hsram->MspDeInitCallback(hsram);
239 #else
240 /* De-Initialize the low level hardware (MSP) */
241 HAL_SRAM_MspDeInit(hsram);
242 #endif
244 /* Configure the SRAM registers with their reset values */
245 (void)FMC_NORSRAM_DeInit(hsram->Instance, hsram->Extended, hsram->Init.NSBank);
247 /* Reset the SRAM controller state */
248 hsram->State = HAL_SRAM_STATE_RESET;
250 /* Release Lock */
251 __HAL_UNLOCK(hsram);
253 return HAL_OK;
257 * @brief SRAM MSP Init.
258 * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
259 * the configuration information for SRAM module.
260 * @retval None
262 __weak void HAL_SRAM_MspInit(SRAM_HandleTypeDef *hsram)
264 /* Prevent unused argument(s) compilation warning */
265 UNUSED(hsram);
267 /* NOTE : This function Should not be modified, when the callback is needed,
268 the HAL_SRAM_MspInit could be implemented in the user file
273 * @brief SRAM MSP DeInit.
274 * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
275 * the configuration information for SRAM module.
276 * @retval None
278 __weak void HAL_SRAM_MspDeInit(SRAM_HandleTypeDef *hsram)
280 /* Prevent unused argument(s) compilation warning */
281 UNUSED(hsram);
283 /* NOTE : This function Should not be modified, when the callback is needed,
284 the HAL_SRAM_MspDeInit could be implemented in the user file
289 * @brief DMA transfer complete callback.
290 * @param hmdma pointer to a SRAM_HandleTypeDef structure that contains
291 * the configuration information for SRAM module.
292 * @retval None
294 __weak void HAL_SRAM_DMA_XferCpltCallback(MDMA_HandleTypeDef *hmdma)
296 /* Prevent unused argument(s) compilation warning */
297 UNUSED(hmdma);
299 /* NOTE : This function Should not be modified, when the callback is needed,
300 the HAL_SRAM_DMA_XferCpltCallback could be implemented in the user file
305 * @brief DMA transfer complete error callback.
306 * @param hmdma pointer to a SRAM_HandleTypeDef structure that contains
307 * the configuration information for SRAM module.
308 * @retval None
310 __weak void HAL_SRAM_DMA_XferErrorCallback(MDMA_HandleTypeDef *hmdma)
312 /* Prevent unused argument(s) compilation warning */
313 UNUSED(hmdma);
315 /* NOTE : This function Should not be modified, when the callback is needed,
316 the HAL_SRAM_DMA_XferErrorCallback could be implemented in the user file
321 * @}
324 /** @defgroup SRAM_Exported_Functions_Group2 Input Output and memory control functions
325 * @brief Input Output and memory control functions
327 @verbatim
328 ==============================================================================
329 ##### SRAM Input and Output functions #####
330 ==============================================================================
331 [..]
332 This section provides functions allowing to use and control the SRAM memory
334 @endverbatim
335 * @{
339 * @brief Reads 8-bit buffer from SRAM memory.
340 * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
341 * the configuration information for SRAM module.
342 * @param pAddress Pointer to read start address
343 * @param pDstBuffer Pointer to destination buffer
344 * @param BufferSize Size of the buffer to read from memory
345 * @retval HAL status
347 HAL_StatusTypeDef HAL_SRAM_Read_8b(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint8_t *pDstBuffer, uint32_t BufferSize)
349 uint32_t size;
350 __IO uint8_t *psramaddress = (uint8_t *)pAddress;
351 uint8_t * pdestbuff = pDstBuffer;
352 HAL_SRAM_StateTypeDef state = hsram->State;
354 /* Check the SRAM controller state */
355 if ((state == HAL_SRAM_STATE_READY) || (state == HAL_SRAM_STATE_PROTECTED))
357 /* Process Locked */
358 __HAL_LOCK(hsram);
360 /* Update the SRAM controller state */
361 hsram->State = HAL_SRAM_STATE_BUSY;
363 /* Read data from memory */
364 for (size = BufferSize; size != 0U; size--)
366 *pdestbuff = *psramaddress;
367 pdestbuff++;
368 psramaddress++;
371 /* Update the SRAM controller state */
372 hsram->State = state;
374 /* Process unlocked */
375 __HAL_UNLOCK(hsram);
377 else
379 return HAL_ERROR;
382 return HAL_OK;
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
394 HAL_StatusTypeDef HAL_SRAM_Write_8b(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint8_t *pSrcBuffer, uint32_t BufferSize)
396 uint32_t size;
397 __IO uint8_t *psramaddress = (uint8_t *)pAddress;
398 uint8_t * psrcbuff = pSrcBuffer;
400 /* Check the SRAM controller state */
401 if (hsram->State == HAL_SRAM_STATE_READY)
403 /* Process Locked */
404 __HAL_LOCK(hsram);
406 /* Update the SRAM controller state */
407 hsram->State = HAL_SRAM_STATE_BUSY;
409 /* Write data to memory */
410 for (size = BufferSize; size != 0U; size--)
412 *psramaddress = *psrcbuff;
413 psrcbuff++;
414 psramaddress++;
417 /* Update the SRAM controller state */
418 hsram->State = HAL_SRAM_STATE_READY;
420 /* Process unlocked */
421 __HAL_UNLOCK(hsram);
423 else
425 return HAL_ERROR;
428 return HAL_OK;
432 * @brief Reads 16-bit buffer from SRAM memory.
433 * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
434 * the configuration information for SRAM module.
435 * @param pAddress Pointer to read start address
436 * @param pDstBuffer Pointer to destination buffer
437 * @param BufferSize Size of the buffer to read from memory
438 * @retval HAL status
440 HAL_StatusTypeDef HAL_SRAM_Read_16b(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint16_t *pDstBuffer, uint32_t BufferSize)
442 uint32_t size;
443 __IO uint32_t *psramaddress = pAddress;
444 uint16_t *pdestbuff = pDstBuffer;
445 uint8_t limit;
446 HAL_SRAM_StateTypeDef state = hsram->State;
448 /* Check the SRAM controller state */
449 if ((state == HAL_SRAM_STATE_READY) || (state == HAL_SRAM_STATE_PROTECTED))
451 /* Process Locked */
452 __HAL_LOCK(hsram);
454 /* Update the SRAM controller state */
455 hsram->State = HAL_SRAM_STATE_BUSY;
457 /* Check if the size is a 32-bits mulitple */
458 limit = (((BufferSize % 2U) != 0U) ? 1U : 0U);
460 /* Read data from memory */
461 for (size = BufferSize; size != limit; size-=2U)
463 *pdestbuff = (uint16_t)((*psramaddress) & 0x0000FFFFU);
464 pdestbuff++;
465 *pdestbuff = (uint16_t)(((*psramaddress) & 0xFFFF0000U) >> 16U);
466 pdestbuff++;
467 psramaddress++;
470 /* Read last 16-bits if size is not 32-bits multiple */
471 if (limit != 0U)
473 *pdestbuff = (uint16_t)((*psramaddress) & 0x0000FFFFU);
476 /* Update the SRAM controller state */
477 hsram->State = state;
479 /* Process unlocked */
480 __HAL_UNLOCK(hsram);
482 else
484 return HAL_ERROR;
487 return HAL_OK;
491 * @brief Writes 16-bit buffer to SRAM memory.
492 * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
493 * the configuration information for SRAM module.
494 * @param pAddress Pointer to write start address
495 * @param pSrcBuffer Pointer to source buffer to write
496 * @param BufferSize Size of the buffer to write to memory
497 * @retval HAL status
499 HAL_StatusTypeDef HAL_SRAM_Write_16b(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint16_t *pSrcBuffer, uint32_t BufferSize)
501 uint32_t size;
502 __IO uint32_t *psramaddress = pAddress;
503 uint16_t * psrcbuff = pSrcBuffer;
504 uint8_t limit;
506 /* Check the SRAM controller state */
507 if (hsram->State == HAL_SRAM_STATE_READY)
509 /* Process Locked */
510 __HAL_LOCK(hsram);
512 /* Update the SRAM controller state */
513 hsram->State = HAL_SRAM_STATE_BUSY;
515 /* Check if the size is a 32-bits mulitple */
516 limit = (((BufferSize % 2U) != 0U) ? 1U : 0U);
518 /* Write data to memory */
519 for (size = BufferSize; size != limit; size-=2U)
521 *psramaddress = (uint32_t)(*psrcbuff);
522 psrcbuff++;
523 *psramaddress |= ((uint32_t)(*psrcbuff) << 16U);
524 psrcbuff++;
525 psramaddress++;
528 /* Write last 16-bits if size is not 32-bits multiple */
529 if (limit != 0U)
531 *psramaddress = ((uint32_t)(*psrcbuff) & 0x0000FFFFU) | ((*psramaddress) & 0xFFFF0000U);
534 /* Update the SRAM controller state */
535 hsram->State = HAL_SRAM_STATE_READY;
537 /* Process unlocked */
538 __HAL_UNLOCK(hsram);
540 else
542 return HAL_ERROR;
545 return HAL_OK;
549 * @brief Reads 32-bit buffer from SRAM memory.
550 * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
551 * the configuration information for SRAM module.
552 * @param pAddress Pointer to read start address
553 * @param pDstBuffer Pointer to destination buffer
554 * @param BufferSize Size of the buffer to read from memory
555 * @retval HAL status
557 HAL_StatusTypeDef HAL_SRAM_Read_32b(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint32_t *pDstBuffer, uint32_t BufferSize)
559 uint32_t size;
560 __IO uint32_t * psramaddress = pAddress;
561 uint32_t * pdestbuff = pDstBuffer;
562 HAL_SRAM_StateTypeDef state = hsram->State;
564 /* Check the SRAM controller state */
565 if ((state == HAL_SRAM_STATE_READY) || (state == HAL_SRAM_STATE_PROTECTED))
567 /* Process Locked */
568 __HAL_LOCK(hsram);
570 /* Update the SRAM controller state */
571 hsram->State = HAL_SRAM_STATE_BUSY;
573 /* Read data from memory */
574 for (size = BufferSize; size != 0U; size--)
576 *pdestbuff = *psramaddress;
577 pdestbuff++;
578 psramaddress++;
581 /* Update the SRAM controller state */
582 hsram->State = state;
584 /* Process unlocked */
585 __HAL_UNLOCK(hsram);
587 else
589 return HAL_ERROR;
592 return HAL_OK;
596 * @brief Writes 32-bit buffer to SRAM memory.
597 * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
598 * the configuration information for SRAM module.
599 * @param pAddress Pointer to write start address
600 * @param pSrcBuffer Pointer to source buffer to write
601 * @param BufferSize Size of the buffer to write to memory
602 * @retval HAL status
604 HAL_StatusTypeDef HAL_SRAM_Write_32b(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint32_t *pSrcBuffer, uint32_t BufferSize)
606 uint32_t size;
607 __IO uint32_t * psramaddress = pAddress;
608 uint32_t * psrcbuff = pSrcBuffer;
610 /* Check the SRAM controller state */
611 if (hsram->State == HAL_SRAM_STATE_READY)
613 /* Process Locked */
614 __HAL_LOCK(hsram);
616 /* Update the SRAM controller state */
617 hsram->State = HAL_SRAM_STATE_BUSY;
619 /* Write data to memory */
620 for (size = BufferSize; size != 0U; size--)
622 *psramaddress = *psrcbuff;
623 psrcbuff++;
624 psramaddress++;
627 /* Update the SRAM controller state */
628 hsram->State = HAL_SRAM_STATE_READY;
630 /* Process unlocked */
631 __HAL_UNLOCK(hsram);
633 else
635 return HAL_ERROR;
638 return HAL_OK;
642 * @brief Reads a Words data from the SRAM memory using DMA transfer.
643 * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
644 * the configuration information for SRAM module.
645 * @param pAddress Pointer to read start address
646 * @param pDstBuffer Pointer to destination buffer
647 * @param BufferSize Size of the buffer to read from memory
648 * @retval HAL status
650 HAL_StatusTypeDef HAL_SRAM_Read_DMA(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint32_t *pDstBuffer, uint32_t BufferSize)
652 HAL_StatusTypeDef status;
653 HAL_SRAM_StateTypeDef state = hsram->State;
655 /* Check the SRAM controller state */
656 if ((state == HAL_SRAM_STATE_READY) || (state == HAL_SRAM_STATE_PROTECTED))
658 /* Process Locked */
659 __HAL_LOCK(hsram);
661 /* Update the SRAM controller state */
662 hsram->State = HAL_SRAM_STATE_BUSY;
664 /* Configure DMA user callbacks */
665 if (state == HAL_SRAM_STATE_READY)
667 hsram->hmdma->XferCpltCallback = SRAM_DMACplt;
669 else
671 hsram->hmdma->XferCpltCallback = SRAM_DMACpltProt;
673 hsram->hmdma->XferErrorCallback = SRAM_DMAError;
675 /* Enable the DMA Stream */
676 status = HAL_MDMA_Start_IT(hsram->hmdma, (uint32_t)pAddress, (uint32_t)pDstBuffer, (uint32_t)(BufferSize * 4U), 1);
678 /* Process unlocked */
679 __HAL_UNLOCK(hsram);
681 else
683 return HAL_ERROR;
686 return status;
690 * @brief Writes a Words data buffer to SRAM memory using DMA transfer.
691 * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
692 * the configuration information for SRAM module.
693 * @param pAddress Pointer to write start address
694 * @param pSrcBuffer Pointer to source buffer to write
695 * @param BufferSize Size of the buffer to write to memory
696 * @retval HAL status
698 HAL_StatusTypeDef HAL_SRAM_Write_DMA(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint32_t *pSrcBuffer, uint32_t BufferSize)
700 HAL_StatusTypeDef status;
702 /* Check the SRAM controller state */
703 if (hsram->State == HAL_SRAM_STATE_READY)
705 /* Process Locked */
706 __HAL_LOCK(hsram);
708 /* Update the SRAM controller state */
709 hsram->State = HAL_SRAM_STATE_BUSY;
711 /* Configure DMA user callbacks */
712 hsram->hmdma->XferCpltCallback = SRAM_DMACplt;
713 hsram->hmdma->XferErrorCallback = SRAM_DMAError;
715 /* Enable the DMA Stream */
716 status = HAL_MDMA_Start_IT(hsram->hmdma, (uint32_t)pSrcBuffer, (uint32_t)pAddress, (uint32_t)(BufferSize * 4U), 1);
718 /* Process unlocked */
719 __HAL_UNLOCK(hsram);
721 else
723 return HAL_ERROR;
726 return status;
729 #if (USE_HAL_SRAM_REGISTER_CALLBACKS == 1)
731 * @brief Register a User SRAM Callback
732 * To be used instead of the weak (surcharged) predefined callback
733 * @param hsram : SRAM handle
734 * @param CallbackId : ID of the callback to be registered
735 * This parameter can be one of the following values:
736 * @arg @ref HAL_SRAM_MSP_INIT_CB_ID SRAM MspInit callback ID
737 * @arg @ref HAL_SRAM_MSP_DEINIT_CB_ID SRAM MspDeInit callback ID
738 * @param pCallback : pointer to the Callback function
739 * @retval status
741 HAL_StatusTypeDef HAL_SRAM_RegisterCallback (SRAM_HandleTypeDef *hsram, HAL_SRAM_CallbackIDTypeDef CallbackId, pSRAM_CallbackTypeDef pCallback)
743 HAL_StatusTypeDef status = HAL_OK;
744 HAL_SRAM_StateTypeDef state;
746 if(pCallback == NULL)
748 return HAL_ERROR;
751 /* Process locked */
752 __HAL_LOCK(hsram);
754 state = hsram->State;
755 if((state == HAL_SRAM_STATE_READY) || (state == HAL_SRAM_STATE_RESET) || (state == HAL_SRAM_STATE_PROTECTED))
757 switch (CallbackId)
759 case HAL_SRAM_MSP_INIT_CB_ID :
760 hsram->MspInitCallback = pCallback;
761 break;
762 case HAL_SRAM_MSP_DEINIT_CB_ID :
763 hsram->MspDeInitCallback = pCallback;
764 break;
765 default :
766 /* update return status */
767 status = HAL_ERROR;
768 break;
771 else
773 /* update return status */
774 status = HAL_ERROR;
777 /* Release Lock */
778 __HAL_UNLOCK(hsram);
779 return status;
783 * @brief Unregister a User SRAM Callback
784 * SRAM Callback is redirected to the weak (surcharged) predefined callback
785 * @param hsram : SRAM handle
786 * @param CallbackId : ID of the callback to be unregistered
787 * This parameter can be one of the following values:
788 * @arg @ref HAL_SRAM_MSP_INIT_CB_ID SRAM MspInit callback ID
789 * @arg @ref HAL_SRAM_MSP_DEINIT_CB_ID SRAM MspDeInit callback ID
790 * @arg @ref HAL_SRAM_DMA_XFER_CPLT_CB_ID SRAM DMA Xfer Complete callback ID
791 * @arg @ref HAL_SRAM_DMA_XFER_ERR_CB_ID SRAM DMA Xfer Error callback ID
792 * @retval status
794 HAL_StatusTypeDef HAL_SRAM_UnRegisterCallback (SRAM_HandleTypeDef *hsram, HAL_SRAM_CallbackIDTypeDef CallbackId)
796 HAL_StatusTypeDef status = HAL_OK;
797 HAL_SRAM_StateTypeDef state;
799 /* Process locked */
800 __HAL_LOCK(hsram);
802 state = hsram->State;
803 if((state == HAL_SRAM_STATE_READY) || (state == HAL_SRAM_STATE_PROTECTED))
805 switch (CallbackId)
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;
825 else if(state == HAL_SRAM_STATE_RESET)
827 switch (CallbackId)
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;
841 else
843 /* update return status */
844 status = HAL_ERROR;
847 /* Release Lock */
848 __HAL_UNLOCK(hsram);
849 return status;
853 * @brief Register a User SRAM Callback for DMA transfers
854 * To be used instead of the weak (surcharged) predefined callback
855 * @param hsram : SRAM handle
856 * @param CallbackId : ID of the callback to be registered
857 * This parameter can be one of the following values:
858 * @arg @ref HAL_SRAM_DMA_XFER_CPLT_CB_ID SRAM DMA Xfer Complete callback ID
859 * @arg @ref HAL_SRAM_DMA_XFER_ERR_CB_ID SRAM DMA Xfer Error callback ID
860 * @param pCallback : pointer to the Callback function
861 * @retval status
863 HAL_StatusTypeDef HAL_SRAM_RegisterDmaCallback(SRAM_HandleTypeDef *hsram, HAL_SRAM_CallbackIDTypeDef CallbackId, pSRAM_DmaCallbackTypeDef pCallback)
865 HAL_StatusTypeDef status = HAL_OK;
866 HAL_SRAM_StateTypeDef state;
868 if(pCallback == NULL)
870 return HAL_ERROR;
873 /* Process locked */
874 __HAL_LOCK(hsram);
876 state = hsram->State;
877 if((state == HAL_SRAM_STATE_READY) || (state == HAL_SRAM_STATE_PROTECTED))
879 switch (CallbackId)
881 case HAL_SRAM_DMA_XFER_CPLT_CB_ID :
882 hsram->DmaXferCpltCallback = pCallback;
883 break;
884 case HAL_SRAM_DMA_XFER_ERR_CB_ID :
885 hsram->DmaXferErrorCallback = pCallback;
886 break;
887 default :
888 /* update return status */
889 status = HAL_ERROR;
890 break;
893 else
895 /* update return status */
896 status = HAL_ERROR;
899 /* Release Lock */
900 __HAL_UNLOCK(hsram);
901 return status;
903 #endif
906 * @}
909 /** @defgroup SRAM_Exported_Functions_Group3 Control functions
910 * @brief Control functions
912 @verbatim
913 ==============================================================================
914 ##### SRAM Control functions #####
915 ==============================================================================
916 [..]
917 This subsection provides a set of functions allowing to control dynamically
918 the SRAM interface.
920 @endverbatim
921 * @{
925 * @brief Enables dynamically SRAM write operation.
926 * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
927 * the configuration information for SRAM module.
928 * @retval HAL status
930 HAL_StatusTypeDef HAL_SRAM_WriteOperation_Enable(SRAM_HandleTypeDef *hsram)
932 /* Check the SRAM controller state */
933 if(hsram->State == HAL_SRAM_STATE_PROTECTED)
935 /* Process Locked */
936 __HAL_LOCK(hsram);
938 /* Update the SRAM controller state */
939 hsram->State = HAL_SRAM_STATE_BUSY;
941 /* Enable write operation */
942 (void)FMC_NORSRAM_WriteOperation_Enable(hsram->Instance, hsram->Init.NSBank);
944 /* Update the SRAM controller state */
945 hsram->State = HAL_SRAM_STATE_READY;
947 /* Process unlocked */
948 __HAL_UNLOCK(hsram);
950 else
952 return HAL_ERROR;
955 return HAL_OK;
959 * @brief Disables dynamically SRAM write operation.
960 * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
961 * the configuration information for SRAM module.
962 * @retval HAL status
964 HAL_StatusTypeDef HAL_SRAM_WriteOperation_Disable(SRAM_HandleTypeDef *hsram)
966 /* Check the SRAM controller state */
967 if(hsram->State == HAL_SRAM_STATE_READY)
969 /* Process Locked */
970 __HAL_LOCK(hsram);
972 /* Update the SRAM controller state */
973 hsram->State = HAL_SRAM_STATE_BUSY;
975 /* Disable write operation */
976 (void)FMC_NORSRAM_WriteOperation_Disable(hsram->Instance, hsram->Init.NSBank);
978 /* Update the SRAM controller state */
979 hsram->State = HAL_SRAM_STATE_PROTECTED;
981 /* Process unlocked */
982 __HAL_UNLOCK(hsram);
984 else
986 return HAL_ERROR;
989 return HAL_OK;
993 * @}
996 /** @defgroup SRAM_Exported_Functions_Group4 Peripheral State functions
997 * @brief Peripheral State functions
999 @verbatim
1000 ==============================================================================
1001 ##### SRAM State functions #####
1002 ==============================================================================
1003 [..]
1004 This subsection permits to get in run-time the status of the SRAM controller
1005 and the data flow.
1007 @endverbatim
1008 * @{
1012 * @brief Returns the SRAM controller state
1013 * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
1014 * the configuration information for SRAM module.
1015 * @retval HAL state
1017 HAL_SRAM_StateTypeDef HAL_SRAM_GetState(SRAM_HandleTypeDef *hsram)
1019 return hsram->State;
1023 * @}
1027 * @}
1031 @cond 0
1034 * @brief MDMA SRAM process complete callback.
1035 * @param hmdma : MDMA handle
1036 * @retval None
1038 static void SRAM_DMACplt(MDMA_HandleTypeDef *hmdma)
1040 SRAM_HandleTypeDef* hsram = ( SRAM_HandleTypeDef* )(hmdma->Parent);
1042 /* Disable the MDMA channel */
1043 __HAL_MDMA_DISABLE(hmdma);
1045 /* Update the SRAM controller state */
1046 hsram->State = HAL_SRAM_STATE_READY;
1048 #if (USE_HAL_SRAM_REGISTER_CALLBACKS == 1)
1049 hsram->DmaXferCpltCallback(hmdma);
1050 #else
1051 HAL_SRAM_DMA_XferCpltCallback(hmdma);
1052 #endif
1056 * @brief MDMA SRAM process complete callback.
1057 * @param hmdma : MDMA handle
1058 * @retval None
1060 static void SRAM_DMACpltProt(MDMA_HandleTypeDef *hmdma)
1062 SRAM_HandleTypeDef* hsram = ( SRAM_HandleTypeDef* )(hmdma->Parent);
1064 /* Disable the MDMA channel */
1065 __HAL_MDMA_DISABLE(hmdma);
1067 /* Update the SRAM controller state */
1068 hsram->State = HAL_SRAM_STATE_PROTECTED;
1070 #if (USE_HAL_SRAM_REGISTER_CALLBACKS == 1)
1071 hsram->DmaXferCpltCallback(hmdma);
1072 #else
1073 HAL_SRAM_DMA_XferCpltCallback(hmdma);
1074 #endif
1078 * @brief MDMA SRAM error callback.
1079 * @param hmdma : MDMA handle
1080 * @retval None
1082 static void SRAM_DMAError(MDMA_HandleTypeDef *hmdma)
1084 SRAM_HandleTypeDef* hsram = ( SRAM_HandleTypeDef* )(hmdma->Parent);
1086 /* Disable the MDMA channel */
1087 __HAL_MDMA_DISABLE(hmdma);
1089 /* Update the SRAM controller state */
1090 hsram->State = HAL_SRAM_STATE_ERROR;
1092 #if (USE_HAL_SRAM_REGISTER_CALLBACKS == 1)
1093 hsram->DmaXferErrorCallback(hmdma);
1094 #else
1095 HAL_SRAM_DMA_XferErrorCallback(hmdma);
1096 #endif
1099 @endcond
1103 * @}
1106 #endif /* HAL_SRAM_MODULE_ENABLED */
1109 * @}
1113 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/