2 ******************************************************************************
3 * @file stm32f0xx_hal_dma.c
4 * @author MCD Application Team
6 * @date 11-December-2014
7 * @brief DMA HAL module driver.
9 * This file provides firmware functions to manage the following
10 * functionalities of the Direct Memory Access (DMA) peripheral:
11 * + Initialization and de-initialization functions
12 * + IO operation functions
13 * + Peripheral State and errors functions
15 ==============================================================================
16 ##### How to use this driver #####
17 ==============================================================================
19 (#) Enable and configure the peripheral to be connected to the DMA Channel
20 (except for internal SRAM / FLASH memories: no initialization is
21 necessary) please refer to Reference manual for connection between peripherals
24 (#) For a given Channel, program the required configuration through the following parameters:
25 Transfer Direction, Source and Destination data formats,
26 Circular or Normal mode, Channel Priority level, Source and Destination Increment mode,
27 using HAL_DMA_Init() function.
29 (#) Use HAL_DMA_GetState() function to return the DMA state and HAL_DMA_GetError() in case of error
32 (#) Use HAL_DMA_Abort() function to abort the current transfer
34 -@- In Memory-to-Memory transfer mode, Circular mode is not allowed.
35 *** Polling mode IO operation ***
36 =================================
38 (+) Use HAL_DMA_Start() to start DMA transfer after the configuration of Source
39 address and destination address and the Length of data to be transferred
40 (+) Use HAL_DMA_PollForTransfer() to poll for the end of current transfer, in this
41 case a fixed Timeout can be configured by User depending from his application.
43 *** Interrupt mode IO operation ***
44 ===================================
46 (+) Configure the DMA interrupt priority using HAL_NVIC_SetPriority()
47 (+) Enable the DMA IRQ handler using HAL_NVIC_EnableIRQ()
48 (+) Use HAL_DMA_Start_IT() to start DMA transfer after the configuration of
49 Source address and destination address and the Length of data to be transferred.
50 In this case the DMA interrupt is configured
51 (+) Use HAL_DMAy_Channelx_IRQHandler() called under DMA_IRQHandler() Interrupt subroutine
52 (+) At the end of data transfer HAL_DMA_IRQHandler() function is executed and user can
53 add his own function by customization of function pointer XferCpltCallback and
54 XferErrorCallback (i.e a member of DMA handle structure).
56 *** DMA HAL driver macros list ***
57 =============================================
59 Below the list of most used macros in DMA HAL driver.
61 (+) __HAL_DMA_ENABLE: Enable the specified DMA Channel.
62 (+) __HAL_DMA_DISABLE: Disable the specified DMA Channel.
63 (+) __HAL_DMA_GET_FLAG: Get the DMA Channel pending flags.
64 (+) __HAL_DMA_CLEAR_FLAG: Clear the DMA Channel pending flags.
65 (+) __HAL_DMA_ENABLE_IT: Enable the specified DMA Channel interrupts.
66 (+) __HAL_DMA_DISABLE_IT: Disable the specified DMA Channel interrupts.
67 (+) __HAL_DMA_GET_IT_SOURCE: Check whether the specified DMA Channel interrupt has occurred or not.
70 (@) You can refer to the DMA HAL driver header file for more useful macros
73 ******************************************************************************
76 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
78 * Redistribution and use in source and binary forms, with or without modification,
79 * are permitted provided that the following conditions are met:
80 * 1. Redistributions of source code must retain the above copyright notice,
81 * this list of conditions and the following disclaimer.
82 * 2. Redistributions in binary form must reproduce the above copyright notice,
83 * this list of conditions and the following disclaimer in the documentation
84 * and/or other materials provided with the distribution.
85 * 3. Neither the name of STMicroelectronics nor the names of its contributors
86 * may be used to endorse or promote products derived from this software
87 * without specific prior written permission.
89 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
90 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
91 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
92 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
93 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
94 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
95 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
96 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
97 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
98 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
100 ******************************************************************************
103 /* Includes ------------------------------------------------------------------*/
104 #include "stm32f0xx_hal.h"
106 /** @addtogroup STM32F0xx_HAL_Driver
110 /** @defgroup DMA DMA
111 * @brief DMA HAL module driver
115 #ifdef HAL_DMA_MODULE_ENABLED
117 /* Private typedef -----------------------------------------------------------*/
118 /* Private define ------------------------------------------------------------*/
119 /** @defgroup DMA_Private_Constants DMA Private Constants
122 #define HAL_TIMEOUT_DMA_ABORT ((uint32_t)1000) /* 1s */
127 /* Private macro -------------------------------------------------------------*/
128 /* Private variables ---------------------------------------------------------*/
129 /* Private function prototypes -----------------------------------------------*/
130 /** @defgroup DMA_Private_Functions DMA Private Functions
133 static void DMA_SetConfig(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength);
138 /* Exported functions ---------------------------------------------------------*/
140 /** @defgroup DMA_Exported_Functions DMA Exported Functions
144 /** @defgroup DMA_Exported_Functions_Group1 Initialization and de-initialization functions
145 * @brief Initialization and de-initialization functions
148 ===============================================================================
149 ##### Initialization and de-initialization functions #####
150 ===============================================================================
152 This section provides functions allowing to initialize the DMA Channel source
153 and destination addresses, incrementation and data sizes, transfer direction,
154 circular/normal mode selection, memory-to-memory mode selection and Channel priority value.
156 The HAL_DMA_Init() function follows the DMA configuration procedures as described in
164 * @brief Initializes the DMA according to the specified
165 * parameters in the DMA_InitTypeDef and create the associated handle.
166 * @param hdma: Pointer to a DMA_HandleTypeDef structure that contains
167 * the configuration information for the specified DMA Channel.
170 HAL_StatusTypeDef HAL_DMA_Init(DMA_HandleTypeDef *hdma)
174 /* Check the DMA handle allocation */
180 /* Check the parameters */
181 assert_param(IS_DMA_ALL_INSTANCE(hdma->Instance));
182 assert_param(IS_DMA_DIRECTION(hdma->Init.Direction));
183 assert_param(IS_DMA_PERIPHERAL_INC_STATE(hdma->Init.PeriphInc));
184 assert_param(IS_DMA_MEMORY_INC_STATE(hdma->Init.MemInc));
185 assert_param(IS_DMA_PERIPHERAL_DATA_SIZE(hdma->Init.PeriphDataAlignment));
186 assert_param(IS_DMA_MEMORY_DATA_SIZE(hdma->Init.MemDataAlignment));
187 assert_param(IS_DMA_MODE(hdma->Init.Mode));
188 assert_param(IS_DMA_PRIORITY(hdma->Init.Priority));
190 /* Change DMA peripheral state */
191 hdma->State = HAL_DMA_STATE_BUSY;
193 /* Get the CR register value */
194 tmp = hdma->Instance->CCR;
196 /* Clear PL, MSIZE, PSIZE, MINC, PINC, CIRC, DIR bits */
197 tmp &= ((uint32_t)~(DMA_CCR_PL | DMA_CCR_MSIZE | DMA_CCR_PSIZE | \
198 DMA_CCR_MINC | DMA_CCR_PINC | DMA_CCR_CIRC | \
201 /* Prepare the DMA Channel configuration */
202 tmp |= hdma->Init.Direction |
203 hdma->Init.PeriphInc | hdma->Init.MemInc |
204 hdma->Init.PeriphDataAlignment | hdma->Init.MemDataAlignment |
205 hdma->Init.Mode | hdma->Init.Priority;
207 /* Write to DMA Channel CR register */
208 hdma->Instance->CCR = tmp;
210 /* Initialise the error code */
211 hdma->ErrorCode = HAL_DMA_ERROR_NONE;
213 /* Initialize the DMA state*/
214 hdma->State = HAL_DMA_STATE_READY;
220 * @brief DeInitializes the DMA peripheral
221 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
222 * the configuration information for the specified DMA Channel.
225 HAL_StatusTypeDef HAL_DMA_DeInit(DMA_HandleTypeDef *hdma)
227 /* Check the DMA handle allocation */
233 /* Check the parameters */
234 assert_param(IS_DMA_ALL_INSTANCE(hdma->Instance));
236 /* Check the DMA peripheral state */
237 if(hdma->State == HAL_DMA_STATE_BUSY)
242 /* Disable the selected DMA Channelx */
243 __HAL_DMA_DISABLE(hdma);
245 /* Reset DMA Channel control register */
246 hdma->Instance->CCR = 0;
248 /* Reset DMA Channel Number of Data to Transfer register */
249 hdma->Instance->CNDTR = 0;
251 /* Reset DMA Channel peripheral address register */
252 hdma->Instance->CPAR = 0;
254 /* Reset DMA Channel memory address register */
255 hdma->Instance->CMAR = 0;
257 /* Clear all flags */
258 __HAL_DMA_CLEAR_FLAG(hdma, __HAL_DMA_GET_TC_FLAG_INDEX(hdma));
259 __HAL_DMA_CLEAR_FLAG(hdma, __HAL_DMA_GET_TE_FLAG_INDEX(hdma));
260 __HAL_DMA_CLEAR_FLAG(hdma, __HAL_DMA_GET_HT_FLAG_INDEX(hdma));
262 /* Initialise the error code */
263 hdma->ErrorCode = HAL_DMA_ERROR_NONE;
265 /* Initialize the DMA state */
266 hdma->State = HAL_DMA_STATE_RESET;
278 /** @defgroup DMA_Exported_Functions_Group2 Input and Output operation functions
279 * @brief I/O operation functions
282 ===============================================================================
283 ##### IO operation functions #####
284 ===============================================================================
285 [..] This section provides functions allowing to:
286 (+) Configure the source, destination address and data length and Start DMA transfer
287 (+) Configure the source, destination address and data length and
288 Start DMA transfer with interrupt
289 (+) Abort DMA transfer
290 (+) Poll for transfer complete
291 (+) Handle DMA interrupt request
298 * @brief Starts the DMA Transfer.
299 * @param hdma : pointer to a DMA_HandleTypeDef structure that contains
300 * the configuration information for the specified DMA Channel.
301 * @param SrcAddress: The source memory Buffer address
302 * @param DstAddress: The destination memory Buffer address
303 * @param DataLength: The length of data to be transferred from source to destination
306 HAL_StatusTypeDef HAL_DMA_Start(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
311 /* Change DMA peripheral state */
312 hdma->State = HAL_DMA_STATE_BUSY;
314 /* Check the parameters */
315 assert_param(IS_DMA_BUFFER_SIZE(DataLength));
317 /* Disable the peripheral */
318 __HAL_DMA_DISABLE(hdma);
320 /* Configure the source, destination address and the data length */
321 DMA_SetConfig(hdma, SrcAddress, DstAddress, DataLength);
323 /* Enable the Peripheral */
324 __HAL_DMA_ENABLE(hdma);
330 * @brief Start the DMA Transfer with interrupt enabled.
331 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
332 * the configuration information for the specified DMA Channel.
333 * @param SrcAddress: The source memory Buffer address
334 * @param DstAddress: The destination memory Buffer address
335 * @param DataLength: The length of data to be transferred from source to destination
338 HAL_StatusTypeDef HAL_DMA_Start_IT(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
343 /* Change DMA peripheral state */
344 hdma->State = HAL_DMA_STATE_BUSY;
346 /* Check the parameters */
347 assert_param(IS_DMA_BUFFER_SIZE(DataLength));
349 /* Disable the peripheral */
350 __HAL_DMA_DISABLE(hdma);
352 /* Configure the source, destination address and the data length */
353 DMA_SetConfig(hdma, SrcAddress, DstAddress, DataLength);
355 /* Enable the transfer complete interrupt */
356 __HAL_DMA_ENABLE_IT(hdma, DMA_IT_TC);
358 /* Enable the Half transfer complete interrupt */
359 __HAL_DMA_ENABLE_IT(hdma, DMA_IT_HT);
361 /* Enable the transfer Error interrupt */
362 __HAL_DMA_ENABLE_IT(hdma, DMA_IT_TE);
364 /* Enable the Peripheral */
365 __HAL_DMA_ENABLE(hdma);
371 * @brief Aborts the DMA Transfer.
372 * @param hdma : pointer to a DMA_HandleTypeDef structure that contains
373 * the configuration information for the specified DMA Channel.
375 * @note After disabling a DMA Channel, a check for wait until the DMA Channel is
376 * effectively disabled is added. If a Channel is disabled
377 * while a data transfer is ongoing, the current data will be transferred
378 * and the Channel will be effectively disabled only after the transfer of
379 * this single data is finished.
382 HAL_StatusTypeDef HAL_DMA_Abort(DMA_HandleTypeDef *hdma)
384 uint32_t tickstart = 0x00;
386 /* Disable the channel */
387 __HAL_DMA_DISABLE(hdma);
390 tickstart = HAL_GetTick();
392 /* Check if the DMA Channel is effectively disabled */
393 while((hdma->Instance->CCR & DMA_CCR_EN) != 0)
395 /* Check for the Timeout */
396 if((HAL_GetTick() - tickstart) > HAL_TIMEOUT_DMA_ABORT)
398 /* Update error code */
399 SET_BIT(hdma->ErrorCode, HAL_DMA_ERROR_TIMEOUT);
401 /* Change the DMA state */
402 hdma->State = HAL_DMA_STATE_TIMEOUT;
404 /* Process Unlocked */
410 /* Change the DMA state*/
411 hdma->State = HAL_DMA_STATE_READY;
413 /* Process Unlocked */
420 * @brief Polling for transfer complete.
421 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
422 * the configuration information for the specified DMA Channel.
423 * @param CompleteLevel: Specifies the DMA level complete.
424 * @param Timeout: Timeout duration.
427 HAL_StatusTypeDef HAL_DMA_PollForTransfer(DMA_HandleTypeDef *hdma, uint32_t CompleteLevel, uint32_t Timeout)
430 uint32_t tickstart = 0x00;
432 /* Get the level transfer complete flag */
433 if(CompleteLevel == HAL_DMA_FULL_TRANSFER)
435 /* Transfer Complete flag */
436 temp = __HAL_DMA_GET_TC_FLAG_INDEX(hdma);
440 /* Half Transfer Complete flag */
441 temp = __HAL_DMA_GET_HT_FLAG_INDEX(hdma);
445 tickstart = HAL_GetTick();
447 while(__HAL_DMA_GET_FLAG(hdma, temp) == RESET)
449 if((__HAL_DMA_GET_FLAG(hdma, __HAL_DMA_GET_TE_FLAG_INDEX(hdma)) != RESET))
451 /* Clear the transfer error flags */
452 __HAL_DMA_CLEAR_FLAG(hdma, __HAL_DMA_GET_TE_FLAG_INDEX(hdma));
454 /* Update error code */
455 SET_BIT(hdma->ErrorCode, HAL_DMA_ERROR_TE);
457 /* Change the DMA state */
458 hdma->State= HAL_DMA_STATE_ERROR;
460 /* Process Unlocked */
465 /* Check for the Timeout */
466 if(Timeout != HAL_MAX_DELAY)
468 if((Timeout == 0) || ((HAL_GetTick() - tickstart) > Timeout))
470 /* Update error code */
471 SET_BIT(hdma->ErrorCode, HAL_DMA_ERROR_TIMEOUT);
473 /* Change the DMA state */
474 hdma->State = HAL_DMA_STATE_TIMEOUT;
476 /* Process Unlocked */
484 if(CompleteLevel == HAL_DMA_FULL_TRANSFER)
486 /* Clear the transfer complete flag */
487 __HAL_DMA_CLEAR_FLAG(hdma, __HAL_DMA_GET_TC_FLAG_INDEX(hdma));
489 /* The selected Channelx EN bit is cleared (DMA is disabled and
490 all transfers are complete) */
491 hdma->State = HAL_DMA_STATE_READY;
496 /* Clear the half transfer complete flag */
497 __HAL_DMA_CLEAR_FLAG(hdma, __HAL_DMA_GET_HT_FLAG_INDEX(hdma));
499 /* The selected Channelx EN bit is cleared (DMA is disabled and
500 all transfers of half buffer are complete) */
501 hdma->State = HAL_DMA_STATE_READY_HALF;
504 /* Process unlocked */
511 * @brief Handles DMA interrupt request.
512 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
513 * the configuration information for the specified DMA Channel.
516 void HAL_DMA_IRQHandler(DMA_HandleTypeDef *hdma)
518 /* Transfer Error Interrupt management ***************************************/
519 if(__HAL_DMA_GET_FLAG(hdma, __HAL_DMA_GET_TE_FLAG_INDEX(hdma)) != RESET)
521 if(__HAL_DMA_GET_IT_SOURCE(hdma, DMA_IT_TE) != RESET)
523 /* Disable the transfer error interrupt */
524 __HAL_DMA_DISABLE_IT(hdma, DMA_IT_TE);
526 /* Clear the transfer error flag */
527 __HAL_DMA_CLEAR_FLAG(hdma, __HAL_DMA_GET_TE_FLAG_INDEX(hdma));
529 /* Update error code */
530 SET_BIT(hdma->ErrorCode, HAL_DMA_ERROR_TE);
532 /* Change the DMA state */
533 hdma->State = HAL_DMA_STATE_ERROR;
535 /* Process Unlocked */
538 if (hdma->XferErrorCallback != (void (*)(DMA_HandleTypeDef *))NULL)
540 /* Transfer error callback */
541 hdma->XferErrorCallback(hdma);
546 /* Half Transfer Complete Interrupt management ******************************/
547 if(__HAL_DMA_GET_FLAG(hdma, __HAL_DMA_GET_HT_FLAG_INDEX(hdma)) != RESET)
549 if(__HAL_DMA_GET_IT_SOURCE(hdma, DMA_IT_HT) != RESET)
551 /* Disable the half transfer interrupt if the DMA mode is not CIRCULAR */
552 if((hdma->Instance->CCR & DMA_CCR_CIRC) == 0)
554 /* Disable the half transfer interrupt */
555 __HAL_DMA_DISABLE_IT(hdma, DMA_IT_HT);
557 /* Clear the half transfer complete flag */
558 __HAL_DMA_CLEAR_FLAG(hdma, __HAL_DMA_GET_HT_FLAG_INDEX(hdma));
560 /* Change DMA peripheral state */
561 hdma->State = HAL_DMA_STATE_READY_HALF;
563 if(hdma->XferHalfCpltCallback != (void (*)(DMA_HandleTypeDef *))NULL)
565 /* Half transfer callback */
566 hdma->XferHalfCpltCallback(hdma);
571 /* Transfer Complete Interrupt management ***********************************/
572 if(__HAL_DMA_GET_FLAG(hdma, __HAL_DMA_GET_TC_FLAG_INDEX(hdma)) != RESET)
574 if(__HAL_DMA_GET_IT_SOURCE(hdma, DMA_IT_TC) != RESET)
576 if((hdma->Instance->CCR & DMA_CCR_CIRC) == 0)
578 /* Disable the transfer complete interrupt */
579 __HAL_DMA_DISABLE_IT(hdma, DMA_IT_TC);
581 /* Clear the transfer complete flag */
582 __HAL_DMA_CLEAR_FLAG(hdma, __HAL_DMA_GET_TC_FLAG_INDEX(hdma));
584 /* Update error code */
585 SET_BIT(hdma->ErrorCode, HAL_DMA_ERROR_NONE);
587 /* Change the DMA state */
588 hdma->State = HAL_DMA_STATE_READY;
590 /* Process Unlocked */
593 if(hdma->XferCpltCallback != (void (*)(DMA_HandleTypeDef *))NULL)
595 /* Transfer complete callback */
596 hdma->XferCpltCallback(hdma);
606 /** @defgroup DMA_Exported_Functions_Group3 Peripheral State functions
607 * @brief Peripheral State functions
610 ===============================================================================
611 ##### State and Errors functions #####
612 ===============================================================================
614 This subsection provides functions allowing to
615 (+) Check the DMA state
623 * @brief Returns the DMA state.
624 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
625 * the configuration information for the specified DMA Channel.
628 HAL_DMA_StateTypeDef HAL_DMA_GetState(DMA_HandleTypeDef *hdma)
634 * @brief Return the DMA error code
635 * @param hdma : pointer to a DMA_HandleTypeDef structure that contains
636 * the configuration information for the specified DMA Channel.
637 * @retval DMA Error Code
639 uint32_t HAL_DMA_GetError(DMA_HandleTypeDef *hdma)
641 return hdma->ErrorCode;
652 /** @addtogroup DMA_Private_Functions DMA Private Functions
657 * @brief Sets the DMA Transfer parameter.
658 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
659 * the configuration information for the specified DMA Channel.
660 * @param SrcAddress: The source memory Buffer address
661 * @param DstAddress: The destination memory Buffer address
662 * @param DataLength: The length of data to be transferred from source to destination
665 static void DMA_SetConfig(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
667 /* Configure DMA Channel data length */
668 hdma->Instance->CNDTR = DataLength;
670 /* Peripheral to Memory */
671 if((hdma->Init.Direction) == DMA_MEMORY_TO_PERIPH)
673 /* Configure DMA Channel destination address */
674 hdma->Instance->CPAR = DstAddress;
676 /* Configure DMA Channel source address */
677 hdma->Instance->CMAR = SrcAddress;
679 /* Memory to Peripheral */
682 /* Configure DMA Channel source address */
683 hdma->Instance->CPAR = SrcAddress;
685 /* Configure DMA Channel destination address */
686 hdma->Instance->CMAR = DstAddress;
694 #endif /* HAL_DMA_MODULE_ENABLED */
703 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/