2 ******************************************************************************
3 * @file stm32g4xx_hal_dma.c
4 * @author MCD Application Team
5 * @brief DMA HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the Direct Memory Access (DMA) peripheral:
8 * + Initialization and de-initialization functions
9 * + IO operation functions
10 * + Peripheral State and errors functions
12 ==============================================================================
13 ##### How to use this driver #####
14 ==============================================================================
16 (#) Enable and configure the peripheral to be connected to the DMA Channel
17 (except for internal SRAM / FLASH memories: no initialization is
18 necessary). Please refer to the Reference manual for connection between peripherals
21 (#) For a given Channel, program the required configuration through the following parameters:
22 Channel request, Transfer Direction, Source and Destination data formats,
23 Circular or Normal mode, Channel Priority level, Source and Destination Increment mode
24 using HAL_DMA_Init() function.
26 Prior to HAL_DMA_Init the peripheral clock shall be enabled for both DMA & DMAMUX
28 (##) DMA1 or DMA2: __HAL_RCC_DMA1_CLK_ENABLE() or __HAL_RCC_DMA2_CLK_ENABLE() ;
29 (##) DMAMUX1: __HAL_RCC_DMAMUX1_CLK_ENABLE();
31 (#) Use HAL_DMA_GetState() function to return the DMA state and HAL_DMA_GetError() in case of error
34 (#) Use HAL_DMA_Abort() function to abort the current transfer
36 -@- In Memory-to-Memory transfer mode, Circular mode is not allowed.
38 *** Polling mode IO operation ***
39 =================================
41 (+) Use HAL_DMA_Start() to start DMA transfer after the configuration of Source
42 address and destination address and the Length of data to be transferred
43 (+) Use HAL_DMA_PollForTransfer() to poll for the end of current transfer, in this
44 case a fixed Timeout can be configured by User depending from his application.
46 *** Interrupt mode IO operation ***
47 ===================================
49 (+) Configure the DMA interrupt priority using HAL_NVIC_SetPriority()
50 (+) Enable the DMA IRQ handler using HAL_NVIC_EnableIRQ()
51 (+) Use HAL_DMA_Start_IT() to start DMA transfer after the configuration of
52 Source address and destination address and the Length of data to be transferred.
53 In this case the DMA interrupt is configured
54 (+) Use HAL_DMA_IRQHandler() called under DMA_IRQHandler() Interrupt subroutine
55 (+) At the end of data transfer HAL_DMA_IRQHandler() function is executed and user can
56 add his own function to register callbacks with HAL_DMA_RegisterCallback().
58 *** DMA HAL driver macros list ***
59 =============================================
61 Below the list of macros in DMA HAL driver.
63 (+) __HAL_DMA_ENABLE: Enable the specified DMA Channel.
64 (+) __HAL_DMA_DISABLE: Disable the specified DMA Channel.
65 (+) __HAL_DMA_GET_FLAG: Get the DMA Channel pending flags.
66 (+) __HAL_DMA_CLEAR_FLAG: Clear the DMA Channel pending flags.
67 (+) __HAL_DMA_ENABLE_IT: Enable the specified DMA Channel interrupts.
68 (+) __HAL_DMA_DISABLE_IT: Disable the specified DMA Channel interrupts.
69 (+) __HAL_DMA_GET_IT_SOURCE: Check whether the specified DMA Channel interrupt has occurred or not.
72 (@) You can refer to the DMA HAL driver header file for more useful macros
75 ******************************************************************************
78 * <h2><center>© Copyright (c) 2017 STMicroelectronics.
79 * All rights reserved.</center></h2>
81 * This software component is licensed by ST under BSD 3-Clause license,
82 * the "License"; You may not use this file except in compliance with the
83 * License. You may obtain a copy of the License at:
84 * opensource.org/licenses/BSD-3-Clause
86 ******************************************************************************
89 /* Includes ------------------------------------------------------------------*/
90 #include "stm32g4xx_hal.h"
92 /** @addtogroup STM32G4xx_HAL_Driver
97 * @brief DMA HAL module driver
101 #ifdef HAL_DMA_MODULE_ENABLED
103 /* Private typedef -----------------------------------------------------------*/
104 /* Private define ------------------------------------------------------------*/
105 /* Private macro -------------------------------------------------------------*/
106 /* Private variables ---------------------------------------------------------*/
107 /* Private function prototypes -----------------------------------------------*/
108 /** @defgroup DMA_Private_Functions DMA Private Functions
111 static void DMA_SetConfig(DMA_HandleTypeDef
*hdma
, uint32_t SrcAddress
, uint32_t DstAddress
, uint32_t DataLength
);
113 static void DMA_CalcDMAMUXChannelBaseAndMask(DMA_HandleTypeDef
*hdma
);
114 static void DMA_CalcDMAMUXRequestGenBaseAndMask(DMA_HandleTypeDef
*hdma
);
120 /* Exported functions ---------------------------------------------------------*/
122 /** @defgroup DMA_Exported_Functions DMA Exported Functions
126 /** @defgroup DMA_Exported_Functions_Group1 Initialization and de-initialization functions
127 * @brief Initialization and de-initialization functions
130 ===============================================================================
131 ##### Initialization and de-initialization functions #####
132 ===============================================================================
134 This section provides functions allowing to initialize the DMA Channel source
135 and destination addresses, incrementation and data sizes, transfer direction,
136 circular/normal mode selection, memory-to-memory mode selection and Channel priority value.
138 The HAL_DMA_Init() function follows the DMA configuration procedures as described in
146 * @brief Initialize the DMA according to the specified
147 * parameters in the DMA_InitTypeDef and initialize the associated handle.
148 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
149 * the configuration information for the specified DMA Channel.
152 HAL_StatusTypeDef
HAL_DMA_Init(DMA_HandleTypeDef
*hdma
)
156 /* Check the DMA handle allocation */
162 /* Check the parameters */
163 assert_param(IS_DMA_ALL_INSTANCE(hdma
->Instance
));
164 assert_param(IS_DMA_DIRECTION(hdma
->Init
.Direction
));
165 assert_param(IS_DMA_PERIPHERAL_INC_STATE(hdma
->Init
.PeriphInc
));
166 assert_param(IS_DMA_MEMORY_INC_STATE(hdma
->Init
.MemInc
));
167 assert_param(IS_DMA_PERIPHERAL_DATA_SIZE(hdma
->Init
.PeriphDataAlignment
));
168 assert_param(IS_DMA_MEMORY_DATA_SIZE(hdma
->Init
.MemDataAlignment
));
169 assert_param(IS_DMA_MODE(hdma
->Init
.Mode
));
170 assert_param(IS_DMA_PRIORITY(hdma
->Init
.Priority
));
172 assert_param(IS_DMA_ALL_REQUEST(hdma
->Init
.Request
));
174 /* Compute the channel index */
175 if ((uint32_t)(hdma
->Instance
) < (uint32_t)(DMA2_Channel1
))
178 hdma
->ChannelIndex
= (((uint32_t)hdma
->Instance
- (uint32_t)DMA1_Channel1
) / ((uint32_t)DMA1_Channel2
- (uint32_t)DMA1_Channel1
)) << 2;
179 hdma
->DmaBaseAddress
= DMA1
;
184 hdma
->ChannelIndex
= (((uint32_t)hdma
->Instance
- (uint32_t)DMA2_Channel1
) / ((uint32_t)DMA2_Channel2
- (uint32_t)DMA2_Channel1
)) << 2;
185 hdma
->DmaBaseAddress
= DMA2
;
188 /* Change DMA peripheral state */
189 hdma
->State
= HAL_DMA_STATE_BUSY
;
191 /* Get the CR register value */
192 tmp
= hdma
->Instance
->CCR
;
194 /* Clear PL, MSIZE, PSIZE, MINC, PINC, CIRC, DIR and MEM2MEM bits */
195 tmp
&= ((uint32_t)~(DMA_CCR_PL
| DMA_CCR_MSIZE
| DMA_CCR_PSIZE
|
196 DMA_CCR_MINC
| DMA_CCR_PINC
| DMA_CCR_CIRC
|
197 DMA_CCR_DIR
| DMA_CCR_MEM2MEM
));
199 /* Prepare the DMA Channel configuration */
200 tmp
|= hdma
->Init
.Direction
|
201 hdma
->Init
.PeriphInc
| hdma
->Init
.MemInc
|
202 hdma
->Init
.PeriphDataAlignment
| hdma
->Init
.MemDataAlignment
|
203 hdma
->Init
.Mode
| hdma
->Init
.Priority
;
205 /* Write to DMA Channel CR register */
206 hdma
->Instance
->CCR
= tmp
;
208 /* Initialize parameters for DMAMUX channel :
209 DMAmuxChannel, DMAmuxChannelStatus and DMAmuxChannelStatusMask
211 DMA_CalcDMAMUXChannelBaseAndMask(hdma
);
213 if (hdma
->Init
.Direction
== DMA_MEMORY_TO_MEMORY
)
215 /* if memory to memory force the request to 0*/
216 hdma
->Init
.Request
= DMA_REQUEST_MEM2MEM
;
219 /* Set peripheral request to DMAMUX channel */
220 hdma
->DMAmuxChannel
->CCR
= (hdma
->Init
.Request
& DMAMUX_CxCR_DMAREQ_ID
);
222 /* Clear the DMAMUX synchro overrun flag */
223 hdma
->DMAmuxChannelStatus
->CFR
= hdma
->DMAmuxChannelStatusMask
;
225 if (((hdma
->Init
.Request
> 0U) && (hdma
->Init
.Request
<= DMA_REQUEST_GENERATOR3
)))
227 /* Initialize parameters for DMAMUX request generator :
228 DMAmuxRequestGen, DMAmuxRequestGenStatus and DMAmuxRequestGenStatusMask
230 DMA_CalcDMAMUXRequestGenBaseAndMask(hdma
);
232 /* Reset the DMAMUX request generator register*/
233 hdma
->DMAmuxRequestGen
->RGCR
= 0U;
235 /* Clear the DMAMUX request generator overrun flag */
236 hdma
->DMAmuxRequestGenStatus
->RGCFR
= hdma
->DMAmuxRequestGenStatusMask
;
240 hdma
->DMAmuxRequestGen
= 0U;
241 hdma
->DMAmuxRequestGenStatus
= 0U;
242 hdma
->DMAmuxRequestGenStatusMask
= 0U;
245 /* Initialize the error code */
246 hdma
->ErrorCode
= HAL_DMA_ERROR_NONE
;
248 /* Initialize the DMA state*/
249 hdma
->State
= HAL_DMA_STATE_READY
;
251 /* Allocate lock resource and initialize it */
252 hdma
->Lock
= HAL_UNLOCKED
;
258 * @brief DeInitialize the DMA peripheral.
259 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
260 * the configuration information for the specified DMA Channel.
263 HAL_StatusTypeDef
HAL_DMA_DeInit(DMA_HandleTypeDef
*hdma
)
266 /* Check the DMA handle allocation */
272 /* Check the parameters */
273 assert_param(IS_DMA_ALL_INSTANCE(hdma
->Instance
));
275 /* Disable the selected DMA Channelx */
276 __HAL_DMA_DISABLE(hdma
);
278 /* Compute the channel index */
279 if ((uint32_t)(hdma
->Instance
) < (uint32_t)(DMA2_Channel1
))
282 hdma
->ChannelIndex
= (((uint32_t)hdma
->Instance
- (uint32_t)DMA1_Channel1
) / ((uint32_t)DMA1_Channel2
- (uint32_t)DMA1_Channel1
)) << 2;
283 hdma
->DmaBaseAddress
= DMA1
;
288 hdma
->ChannelIndex
= (((uint32_t)hdma
->Instance
- (uint32_t)DMA2_Channel1
) / ((uint32_t)DMA2_Channel2
- (uint32_t)DMA2_Channel1
)) << 2;
289 hdma
->DmaBaseAddress
= DMA2
;
292 /* Reset DMA Channel control register */
293 hdma
->Instance
->CCR
= 0;
295 /* Clear all flags */
296 hdma
->DmaBaseAddress
->IFCR
= (DMA_ISR_GIF1
<< (hdma
->ChannelIndex
& 0x1FU
));
298 /* Initialize parameters for DMAMUX channel :
299 DMAmuxChannel, DMAmuxChannelStatus and DMAmuxChannelStatusMask */
301 DMA_CalcDMAMUXChannelBaseAndMask(hdma
);
303 /* Reset the DMAMUX channel that corresponds to the DMA channel */
304 hdma
->DMAmuxChannel
->CCR
= 0;
306 /* Clear the DMAMUX synchro overrun flag */
307 hdma
->DMAmuxChannelStatus
->CFR
= hdma
->DMAmuxChannelStatusMask
;
309 /* Reset Request generator parameters if any */
310 if (((hdma
->Init
.Request
> 0U) && (hdma
->Init
.Request
<= DMA_REQUEST_GENERATOR3
)))
312 /* Initialize parameters for DMAMUX request generator :
313 DMAmuxRequestGen, DMAmuxRequestGenStatus and DMAmuxRequestGenStatusMask
315 DMA_CalcDMAMUXRequestGenBaseAndMask(hdma
);
317 /* Reset the DMAMUX request generator register*/
318 hdma
->DMAmuxRequestGen
->RGCR
= 0U;
320 /* Clear the DMAMUX request generator overrun flag */
321 hdma
->DMAmuxRequestGenStatus
->RGCFR
= hdma
->DMAmuxRequestGenStatusMask
;
324 hdma
->DMAmuxRequestGen
= 0U;
325 hdma
->DMAmuxRequestGenStatus
= 0U;
326 hdma
->DMAmuxRequestGenStatusMask
= 0U;
328 /* Clean callbacks */
329 hdma
->XferCpltCallback
= NULL
;
330 hdma
->XferHalfCpltCallback
= NULL
;
331 hdma
->XferErrorCallback
= NULL
;
332 hdma
->XferAbortCallback
= NULL
;
334 /* Initialize the error code */
335 hdma
->ErrorCode
= HAL_DMA_ERROR_NONE
;
337 /* Initialize the DMA state */
338 hdma
->State
= HAL_DMA_STATE_RESET
;
350 /** @defgroup DMA_Exported_Functions_Group2 Input and Output operation functions
351 * @brief Input and Output operation functions
354 ===============================================================================
355 ##### IO operation functions #####
356 ===============================================================================
357 [..] This section provides functions allowing to:
358 (+) Configure the source, destination address and data length and Start DMA transfer
359 (+) Configure the source, destination address and data length and
360 Start DMA transfer with interrupt
361 (+) Abort DMA transfer
362 (+) Poll for transfer complete
363 (+) Handle DMA interrupt request
370 * @brief Start the DMA Transfer.
371 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
372 * the configuration information for the specified DMA Channel.
373 * @param SrcAddress The source memory Buffer address
374 * @param DstAddress The destination memory Buffer address
375 * @param DataLength The length of data to be transferred from source to destination (up to 256Kbytes-1)
378 HAL_StatusTypeDef
HAL_DMA_Start(DMA_HandleTypeDef
*hdma
, uint32_t SrcAddress
, uint32_t DstAddress
, uint32_t DataLength
)
380 HAL_StatusTypeDef status
= HAL_OK
;
382 /* Check the parameters */
383 assert_param(IS_DMA_BUFFER_SIZE(DataLength
));
388 if (HAL_DMA_STATE_READY
== hdma
->State
)
390 /* Change DMA peripheral state */
391 hdma
->State
= HAL_DMA_STATE_BUSY
;
392 hdma
->ErrorCode
= HAL_DMA_ERROR_NONE
;
394 /* Disable the peripheral */
395 __HAL_DMA_DISABLE(hdma
);
397 /* Configure the source, destination address and the data length & clear flags*/
398 DMA_SetConfig(hdma
, SrcAddress
, DstAddress
, DataLength
);
400 /* Enable the Peripheral */
401 __HAL_DMA_ENABLE(hdma
);
405 /* Process Unlocked */
413 * @brief Start the DMA Transfer with interrupt enabled.
414 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
415 * the configuration information for the specified DMA Channel.
416 * @param SrcAddress The source memory Buffer address
417 * @param DstAddress The destination memory Buffer address
418 * @param DataLength The length of data to be transferred from source to destination (up to 256Kbytes-1)
421 HAL_StatusTypeDef
HAL_DMA_Start_IT(DMA_HandleTypeDef
*hdma
, uint32_t SrcAddress
, uint32_t DstAddress
,
424 HAL_StatusTypeDef status
= HAL_OK
;
426 /* Check the parameters */
427 assert_param(IS_DMA_BUFFER_SIZE(DataLength
));
432 if (HAL_DMA_STATE_READY
== hdma
->State
)
434 /* Change DMA peripheral state */
435 hdma
->State
= HAL_DMA_STATE_BUSY
;
436 hdma
->ErrorCode
= HAL_DMA_ERROR_NONE
;
438 /* Disable the peripheral */
439 __HAL_DMA_DISABLE(hdma
);
441 /* Configure the source, destination address and the data length & clear flags*/
442 DMA_SetConfig(hdma
, SrcAddress
, DstAddress
, DataLength
);
444 /* Enable the transfer complete interrupt */
445 /* Enable the transfer Error interrupt */
446 if (NULL
!= hdma
->XferHalfCpltCallback
)
448 /* Enable the Half transfer complete interrupt as well */
449 __HAL_DMA_ENABLE_IT(hdma
, (DMA_IT_TC
| DMA_IT_HT
| DMA_IT_TE
));
453 __HAL_DMA_DISABLE_IT(hdma
, DMA_IT_HT
);
454 __HAL_DMA_ENABLE_IT(hdma
, (DMA_IT_TC
| DMA_IT_TE
));
457 /* Check if DMAMUX Synchronization is enabled*/
458 if ((hdma
->DMAmuxChannel
->CCR
& DMAMUX_CxCR_SE
) != 0U)
460 /* Enable DMAMUX sync overrun IT*/
461 hdma
->DMAmuxChannel
->CCR
|= DMAMUX_CxCR_SOIE
;
464 if (hdma
->DMAmuxRequestGen
!= 0U)
466 /* if using DMAMUX request generator, enable the DMAMUX request generator overrun IT*/
467 /* enable the request gen overrun IT*/
468 hdma
->DMAmuxRequestGen
->RGCR
|= DMAMUX_RGxCR_OIE
;
471 /* Enable the Peripheral */
472 __HAL_DMA_ENABLE(hdma
);
476 /* Process Unlocked */
486 * @brief Abort the DMA Transfer.
487 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
488 * the configuration information for the specified DMA Channel.
491 HAL_StatusTypeDef
HAL_DMA_Abort(DMA_HandleTypeDef
*hdma
)
493 HAL_StatusTypeDef status
= HAL_OK
;
495 if(hdma
->State
!= HAL_DMA_STATE_BUSY
)
497 /* no transfer ongoing */
498 hdma
->ErrorCode
= HAL_DMA_ERROR_NO_XFER
;
505 __HAL_DMA_DISABLE_IT(hdma
, (DMA_IT_TC
| DMA_IT_HT
| DMA_IT_TE
));
507 /* disable the DMAMUX sync overrun IT*/
508 hdma
->DMAmuxChannel
->CCR
&= ~DMAMUX_CxCR_SOIE
;
510 /* Disable the channel */
511 __HAL_DMA_DISABLE(hdma
);
513 /* Clear all flags */
514 hdma
->DmaBaseAddress
->IFCR
= (DMA_ISR_GIF1
<< (hdma
->ChannelIndex
& 0x1FU
));
516 /* Clear the DMAMUX synchro overrun flag */
517 hdma
->DMAmuxChannelStatus
->CFR
= hdma
->DMAmuxChannelStatusMask
;
519 if (hdma
->DMAmuxRequestGen
!= 0U)
521 /* if using DMAMUX request generator, disable the DMAMUX request generator overrun IT*/
522 /* disable the request gen overrun IT*/
523 hdma
->DMAmuxRequestGen
->RGCR
&= ~DMAMUX_RGxCR_OIE
;
525 /* Clear the DMAMUX request generator overrun flag */
526 hdma
->DMAmuxRequestGenStatus
->RGCFR
= hdma
->DMAmuxRequestGenStatusMask
;
529 /* Change the DMA state */
530 hdma
->State
= HAL_DMA_STATE_READY
;
532 /* Process Unlocked */
539 * @brief Aborts the DMA Transfer in Interrupt mode.
540 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
541 * the configuration information for the specified DMA Channel.
544 HAL_StatusTypeDef
HAL_DMA_Abort_IT(DMA_HandleTypeDef
*hdma
)
546 HAL_StatusTypeDef status
= HAL_OK
;
548 if (HAL_DMA_STATE_BUSY
!= hdma
->State
)
550 /* no transfer ongoing */
551 hdma
->ErrorCode
= HAL_DMA_ERROR_NO_XFER
;
553 /* Change the DMA state */
554 hdma
->State
= HAL_DMA_STATE_READY
;
556 /* Process Unlocked */
564 __HAL_DMA_DISABLE_IT(hdma
, (DMA_IT_TC
| DMA_IT_HT
| DMA_IT_TE
));
566 /* Disable the channel */
567 __HAL_DMA_DISABLE(hdma
);
569 /* disable the DMAMUX sync overrun IT*/
570 hdma
->DMAmuxChannel
->CCR
&= ~DMAMUX_CxCR_SOIE
;
572 /* Clear all flags */
573 hdma
->DmaBaseAddress
->IFCR
= (DMA_ISR_GIF1
<< (hdma
->ChannelIndex
& 0x1FU
));
575 /* Clear the DMAMUX synchro overrun flag */
576 hdma
->DMAmuxChannelStatus
->CFR
= hdma
->DMAmuxChannelStatusMask
;
578 if (hdma
->DMAmuxRequestGen
!= 0U)
580 /* if using DMAMUX request generator, disable the DMAMUX request generator overrun IT*/
581 /* disable the request gen overrun IT*/
582 hdma
->DMAmuxRequestGen
->RGCR
&= ~DMAMUX_RGxCR_OIE
;
584 /* Clear the DMAMUX request generator overrun flag */
585 hdma
->DMAmuxRequestGenStatus
->RGCFR
= hdma
->DMAmuxRequestGenStatusMask
;
588 /* Change the DMA state */
589 hdma
->State
= HAL_DMA_STATE_READY
;
591 /* Process Unlocked */
594 /* Call User Abort callback */
595 if (hdma
->XferAbortCallback
!= NULL
)
597 hdma
->XferAbortCallback(hdma
);
604 * @brief Polling for transfer complete.
605 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
606 * the configuration information for the specified DMA Channel.
607 * @param CompleteLevel Specifies the DMA level complete.
608 * @param Timeout Timeout duration.
611 HAL_StatusTypeDef
HAL_DMA_PollForTransfer(DMA_HandleTypeDef
*hdma
, HAL_DMA_LevelCompleteTypeDef CompleteLevel
,
617 if (HAL_DMA_STATE_BUSY
!= hdma
->State
)
619 /* no transfer ongoing */
620 hdma
->ErrorCode
= HAL_DMA_ERROR_NO_XFER
;
625 /* Polling mode not supported in circular mode */
626 if (0U != (hdma
->Instance
->CCR
& DMA_CCR_CIRC
))
628 hdma
->ErrorCode
= HAL_DMA_ERROR_NOT_SUPPORTED
;
632 /* Get the level transfer complete flag */
633 if (HAL_DMA_FULL_TRANSFER
== CompleteLevel
)
635 /* Transfer Complete flag */
637 temp
= (uint32_t)DMA_FLAG_TC1
<< (hdma
->ChannelIndex
& 0x1FU
);
641 /* Half Transfer Complete flag */
642 temp
= (uint32_t)DMA_FLAG_HT1
<< (hdma
->ChannelIndex
& 0x1FU
);
646 tickstart
= HAL_GetTick();
648 while (0U == (hdma
->DmaBaseAddress
->ISR
& temp
))
650 if ((0U != (hdma
->DmaBaseAddress
->ISR
& ((uint32_t)DMA_FLAG_TE1
<< (hdma
->ChannelIndex
& 0x1FU
)))))
652 /* When a DMA transfer error occurs */
653 /* A hardware clear of its EN bits is performed */
654 /* Clear all flags */
655 hdma
->DmaBaseAddress
->IFCR
= ((uint32_t)DMA_ISR_GIF1
<< (hdma
->ChannelIndex
& 0x1FU
));
657 /* Update error code */
658 hdma
->ErrorCode
= HAL_DMA_ERROR_TE
;
660 /* Change the DMA state */
661 hdma
->State
= HAL_DMA_STATE_READY
;
663 /* Process Unlocked */
668 /* Check for the Timeout */
669 if (Timeout
!= HAL_MAX_DELAY
)
671 if (((HAL_GetTick() - tickstart
) > Timeout
) || (Timeout
== 0U))
673 /* Update error code */
674 hdma
->ErrorCode
= HAL_DMA_ERROR_TIMEOUT
;
676 /* Change the DMA state */
677 hdma
->State
= HAL_DMA_STATE_READY
;
679 /* Process Unlocked */
687 /*Check for DMAMUX Request generator (if used) overrun status */
688 if (hdma
->DMAmuxRequestGen
!= 0U)
690 /* if using DMAMUX request generator Check for DMAMUX request generator overrun */
691 if ((hdma
->DMAmuxRequestGenStatus
->RGSR
& hdma
->DMAmuxRequestGenStatusMask
) != 0U)
693 /* Disable the request gen overrun interrupt */
694 hdma
->DMAmuxRequestGen
->RGCR
|= DMAMUX_RGxCR_OIE
;
696 /* Clear the DMAMUX request generator overrun flag */
697 hdma
->DMAmuxRequestGenStatus
->RGCFR
= hdma
->DMAmuxRequestGenStatusMask
;
699 /* Update error code */
700 hdma
->ErrorCode
|= HAL_DMA_ERROR_REQGEN
;
704 /* Check for DMAMUX Synchronization overrun */
705 if ((hdma
->DMAmuxChannelStatus
->CSR
& hdma
->DMAmuxChannelStatusMask
) != 0U)
707 /* Clear the DMAMUX synchro overrun flag */
708 hdma
->DMAmuxChannelStatus
->CFR
= hdma
->DMAmuxChannelStatusMask
;
710 /* Update error code */
711 hdma
->ErrorCode
|= HAL_DMA_ERROR_SYNC
;
714 if (HAL_DMA_FULL_TRANSFER
== CompleteLevel
)
716 /* Clear the transfer complete flag */
717 hdma
->DmaBaseAddress
->IFCR
= ((uint32_t)DMA_FLAG_TC1
<< (hdma
->ChannelIndex
& 0x1FU
));
719 /* The selected Channelx EN bit is cleared (DMA is disabled and
720 all transfers are complete) */
721 hdma
->State
= HAL_DMA_STATE_READY
;
725 /* Clear the half transfer complete flag */
726 hdma
->DmaBaseAddress
->IFCR
= ((uint32_t)DMA_FLAG_HT1
<< (hdma
->ChannelIndex
& 0x1FU
));
729 /* Process unlocked */
736 * @brief Handle DMA interrupt request.
737 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
738 * the configuration information for the specified DMA Channel.
741 void HAL_DMA_IRQHandler(DMA_HandleTypeDef
*hdma
)
743 uint32_t flag_it
= hdma
->DmaBaseAddress
->ISR
;
744 uint32_t source_it
= hdma
->Instance
->CCR
;
746 /* Half Transfer Complete Interrupt management ******************************/
747 if ((0U != (flag_it
& ((uint32_t)DMA_FLAG_HT1
<< (hdma
->ChannelIndex
& 0x1FU
)))) && (0U != (source_it
& DMA_IT_HT
)))
749 /* Disable the half transfer interrupt if the DMA mode is not CIRCULAR */
750 if ((hdma
->Instance
->CCR
& DMA_CCR_CIRC
) == 0U)
752 /* Disable the half transfer interrupt */
753 __HAL_DMA_DISABLE_IT(hdma
, DMA_IT_HT
);
755 /* Clear the half transfer complete flag */
756 hdma
->DmaBaseAddress
->IFCR
= ((uint32_t)DMA_ISR_HTIF1
<< (hdma
->ChannelIndex
& 0x1FU
));
758 /* DMA peripheral state is not updated in Half Transfer */
759 /* but in Transfer Complete case */
761 if (hdma
->XferHalfCpltCallback
!= NULL
)
763 /* Half transfer callback */
764 hdma
->XferHalfCpltCallback(hdma
);
767 /* Transfer Complete Interrupt management ***********************************/
768 else if ((0U != (flag_it
& ((uint32_t)DMA_FLAG_TC1
<< (hdma
->ChannelIndex
& 0x1FU
))))
769 && (0U != (source_it
& DMA_IT_TC
)))
771 if ((hdma
->Instance
->CCR
& DMA_CCR_CIRC
) == 0U)
773 /* Disable the transfer complete and error interrupt */
774 __HAL_DMA_DISABLE_IT(hdma
, DMA_IT_TE
| DMA_IT_TC
);
776 /* Change the DMA state */
777 hdma
->State
= HAL_DMA_STATE_READY
;
779 /* Clear the transfer complete flag */
780 hdma
->DmaBaseAddress
->IFCR
= ((uint32_t)DMA_ISR_TCIF1
<< (hdma
->ChannelIndex
& 0x1FU
));
782 /* Process Unlocked */
785 if (hdma
->XferCpltCallback
!= NULL
)
787 /* Transfer complete callback */
788 hdma
->XferCpltCallback(hdma
);
791 /* Transfer Error Interrupt management **************************************/
792 else if ((0U != (flag_it
& ((uint32_t)DMA_FLAG_TE1
<< (hdma
->ChannelIndex
& 0x1FU
))))
793 && (0U != (source_it
& DMA_IT_TE
)))
795 /* When a DMA transfer error occurs */
796 /* A hardware clear of its EN bits is performed */
797 /* Disable ALL DMA IT */
798 __HAL_DMA_DISABLE_IT(hdma
, (DMA_IT_TC
| DMA_IT_HT
| DMA_IT_TE
));
800 /* Clear all flags */
801 hdma
->DmaBaseAddress
->IFCR
= ((uint32_t)DMA_ISR_GIF1
<< (hdma
->ChannelIndex
& 0x1FU
));
803 /* Update error code */
804 hdma
->ErrorCode
= HAL_DMA_ERROR_TE
;
806 /* Change the DMA state */
807 hdma
->State
= HAL_DMA_STATE_READY
;
809 /* Process Unlocked */
812 if (hdma
->XferErrorCallback
!= NULL
)
814 /* Transfer error callback */
815 hdma
->XferErrorCallback(hdma
);
826 * @brief Register callbacks
827 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
828 * the configuration information for the specified DMA Channel.
829 * @param CallbackID User Callback identifer
830 * a HAL_DMA_CallbackIDTypeDef ENUM as parameter.
831 * @param pCallback pointer to private callbacsk function which has pointer to
832 * a DMA_HandleTypeDef structure as parameter.
835 HAL_StatusTypeDef
HAL_DMA_RegisterCallback(DMA_HandleTypeDef
*hdma
, HAL_DMA_CallbackIDTypeDef CallbackID
, void (* pCallback
)(DMA_HandleTypeDef
*_hdma
))
837 HAL_StatusTypeDef status
= HAL_OK
;
842 if (HAL_DMA_STATE_READY
== hdma
->State
)
846 case HAL_DMA_XFER_CPLT_CB_ID
:
847 hdma
->XferCpltCallback
= pCallback
;
850 case HAL_DMA_XFER_HALFCPLT_CB_ID
:
851 hdma
->XferHalfCpltCallback
= pCallback
;
854 case HAL_DMA_XFER_ERROR_CB_ID
:
855 hdma
->XferErrorCallback
= pCallback
;
858 case HAL_DMA_XFER_ABORT_CB_ID
:
859 hdma
->XferAbortCallback
= pCallback
;
879 * @brief UnRegister callbacks
880 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
881 * the configuration information for the specified DMA Channel.
882 * @param CallbackID User Callback identifer
883 * a HAL_DMA_CallbackIDTypeDef ENUM as parameter.
886 HAL_StatusTypeDef
HAL_DMA_UnRegisterCallback(DMA_HandleTypeDef
*hdma
, HAL_DMA_CallbackIDTypeDef CallbackID
)
888 HAL_StatusTypeDef status
= HAL_OK
;
893 if (HAL_DMA_STATE_READY
== hdma
->State
)
897 case HAL_DMA_XFER_CPLT_CB_ID
:
898 hdma
->XferCpltCallback
= NULL
;
901 case HAL_DMA_XFER_HALFCPLT_CB_ID
:
902 hdma
->XferHalfCpltCallback
= NULL
;
905 case HAL_DMA_XFER_ERROR_CB_ID
:
906 hdma
->XferErrorCallback
= NULL
;
909 case HAL_DMA_XFER_ABORT_CB_ID
:
910 hdma
->XferAbortCallback
= NULL
;
913 case HAL_DMA_XFER_ALL_CB_ID
:
914 hdma
->XferCpltCallback
= NULL
;
915 hdma
->XferHalfCpltCallback
= NULL
;
916 hdma
->XferErrorCallback
= NULL
;
917 hdma
->XferAbortCallback
= NULL
;
942 /** @defgroup DMA_Exported_Functions_Group3 Peripheral State and Errors functions
943 * @brief Peripheral State and Errors functions
946 ===============================================================================
947 ##### Peripheral State and Errors functions #####
948 ===============================================================================
950 This subsection provides functions allowing to
951 (+) Check the DMA state
959 * @brief Return the DMA hande state.
960 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
961 * the configuration information for the specified DMA Channel.
964 HAL_DMA_StateTypeDef
HAL_DMA_GetState(DMA_HandleTypeDef
*hdma
)
966 /* Return DMA handle state */
971 * @brief Return the DMA error code.
972 * @param hdma : pointer to a DMA_HandleTypeDef structure that contains
973 * the configuration information for the specified DMA Channel.
974 * @retval DMA Error Code
976 uint32_t HAL_DMA_GetError(DMA_HandleTypeDef
*hdma
)
978 return hdma
->ErrorCode
;
989 /** @addtogroup DMA_Private_Functions
994 * @brief Sets the DMA Transfer parameter.
995 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
996 * the configuration information for the specified DMA Channel.
997 * @param SrcAddress The source memory Buffer address
998 * @param DstAddress The destination memory Buffer address
999 * @param DataLength The length of data to be transferred from source to destination
1000 * @retval HAL status
1002 static void DMA_SetConfig(DMA_HandleTypeDef
*hdma
, uint32_t SrcAddress
, uint32_t DstAddress
, uint32_t DataLength
)
1004 /* Clear the DMAMUX synchro overrun flag */
1005 hdma
->DMAmuxChannelStatus
->CFR
= hdma
->DMAmuxChannelStatusMask
;
1007 if (hdma
->DMAmuxRequestGen
!= 0U)
1009 /* Clear the DMAMUX request generator overrun flag */
1010 hdma
->DMAmuxRequestGenStatus
->RGCFR
= hdma
->DMAmuxRequestGenStatusMask
;
1013 /* Clear all flags */
1014 hdma
->DmaBaseAddress
->IFCR
= (DMA_ISR_GIF1
<< (hdma
->ChannelIndex
& 0x1FU
));
1016 /* Configure DMA Channel data length */
1017 hdma
->Instance
->CNDTR
= DataLength
;
1019 /* Memory to Peripheral */
1020 if ((hdma
->Init
.Direction
) == DMA_MEMORY_TO_PERIPH
)
1022 /* Configure DMA Channel destination address */
1023 hdma
->Instance
->CPAR
= DstAddress
;
1025 /* Configure DMA Channel source address */
1026 hdma
->Instance
->CMAR
= SrcAddress
;
1028 /* Peripheral to Memory */
1031 /* Configure DMA Channel source address */
1032 hdma
->Instance
->CPAR
= SrcAddress
;
1034 /* Configure DMA Channel destination address */
1035 hdma
->Instance
->CMAR
= DstAddress
;
1040 * @brief Updates the DMA handle with the DMAMUX channel and status mask depending on stream number
1041 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
1042 * the configuration information for the specified DMA Stream.
1045 static void DMA_CalcDMAMUXChannelBaseAndMask(DMA_HandleTypeDef
*hdma
)
1047 uint32_t dmamux_base_addr
;
1048 uint32_t channel_number
;
1049 DMAMUX_Channel_TypeDef
*DMAMUX1_ChannelBase
;
1051 /* check if instance is not outside the DMA channel range */
1052 if ((uint32_t)hdma
->Instance
< (uint32_t)DMA2_Channel1
)
1055 DMAMUX1_ChannelBase
= DMAMUX1_Channel0
;
1060 #if defined (STM32G474xx) || defined (STM32G473xx) || defined (STM32G471xx) || defined (STM32G484xx)
1061 DMAMUX1_ChannelBase
= DMAMUX1_Channel8
;
1062 #elif defined (STM32G431xx) || defined (STM32G441xx) || defined (STM32GBK1CB)
1063 DMAMUX1_ChannelBase
= DMAMUX1_Channel6
;
1065 DMAMUX1_ChannelBase
= DMAMUX1_Channel7
;
1066 #endif /* STM32G4x1xx) */
1068 dmamux_base_addr
= (uint32_t)DMAMUX1_ChannelBase
;
1069 channel_number
= (((uint32_t)hdma
->Instance
& 0xFFU
) - 8U) / 20U;
1070 hdma
->DMAmuxChannel
= (DMAMUX_Channel_TypeDef
*)(uint32_t)(dmamux_base_addr
+ ((hdma
->ChannelIndex
>> 2U) * ((uint32_t)DMAMUX1_Channel1
- (uint32_t)DMAMUX1_Channel0
)));
1071 hdma
->DMAmuxChannelStatus
= DMAMUX1_ChannelStatus
;
1072 hdma
->DMAmuxChannelStatusMask
= 1UL << (channel_number
& 0x1FU
);
1076 * @brief Updates the DMA handle with the DMAMUX request generator params
1077 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
1078 * the configuration information for the specified DMA Channel.
1082 static void DMA_CalcDMAMUXRequestGenBaseAndMask(DMA_HandleTypeDef
*hdma
)
1084 uint32_t request
= hdma
->Init
.Request
& DMAMUX_CxCR_DMAREQ_ID
;
1086 /* DMA Channels are connected to DMAMUX1 request generator blocks*/
1087 hdma
->DMAmuxRequestGen
= (DMAMUX_RequestGen_TypeDef
*)((uint32_t)(((uint32_t)DMAMUX1_RequestGenerator0
) + ((request
- 1U) * 4U)));
1089 hdma
->DMAmuxRequestGenStatus
= DMAMUX1_RequestGenStatus
;
1091 hdma
->DMAmuxRequestGenStatusMask
= 1UL << ((request
- 1U) & 0x1FU
);
1102 #endif /* HAL_DMA_MODULE_ENABLED */
1111 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/