]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32L0/stm32l0xx_hal_dma.c
Merge commit '1fe4406f374291ab2e86e95a97341fd9c475fcb8'
[max/tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32L0 / stm32l0xx_hal_dma.c
1 /**
2   ******************************************************************************
3   * @file    stm32l0xx_hal_dma.c
4   * @author  MCD Application Team
5   * @version V1.2.0
6   * @date    06-February-2015
7   * @brief   DMA HAL module driver.
8   *    
9   *         This file provides firmware functions to manage the following 
10   *         functionalities of the Direct Memory Access (DMA) peripheral:
11   *           + Initialization/de-initialization functions
12   *           + I/O operation functions
13   *           + Peripheral State functions
14   *  
15   *           
16   @verbatim     
17   ==============================================================================      
18                         ##### How to use this driver #####
19   ============================================================================== 
20   [..]
21    (#) Enable and configure the peripheral to be connected to the DMA Channel
22        (except for internal SRAM / FLASH memories: no initialization is 
23        necessary). 
24           
25    (#) For a given Channel, program the required configuration through the following parameters:   
26        Channel request, Transfer Direction, Source and Destination data formats, 
27        Circular, Normal or peripheral flow control mode, Channel Priority level, 
28        Source and Destination Increment mode using HAL_DMA_Init() function.
29    
30      *** Polling mode IO operation ***
31      =================================   
32     [..] 
33           (+) Use HAL_DMA_Start() to start DMA transfer after the configuration of Source 
34               address and destination address and the Length of data to be transferred
35           (+) Use HAL_DMA_PollForTransfer() to poll for the end of current transfer, in this  
36               case a fixed Timeout can be configured by User depending from his application.
37                
38      *** Interrupt mode IO operation ***    
39      =================================== 
40     [..]     
41           (+) Configure the DMA interrupt priority using HAL_NVIC_SetPriority()
42           (+) Enable the DMA IRQ handler using HAL_NVIC_EnableIRQ() 
43           (+) Use HAL_DMA_Start_IT() to start DMA transfer after the configuration of  
44               Source address and destination address and the Length of data to be transferred. In this 
45               case the DMA interrupt is configured 
46           (+) Use HAL_DMA_IRQHandler() called under DMA_IRQHandler() Interrupt subroutine
47           (+) At the end of data transfer HAL_DMA_IRQHandler() function is executed and user can 
48               add his own function by customization of function pointer XferCpltCallback and 
49               XferErrorCallback (i.e a member of DMA handle structure). 
50                 
51    (#) Use HAL_DMA_GetState() function to return the DMA state and HAL_DMA_GetError() in case of error 
52        detection.
53          
54    (#) Use HAL_DMA_Abort() function to abort the current transfer              
55                    
56    -@-   In Memory-to-Memory transfer mode, Circular mode is not allowed.          
57   
58   @endverbatim
59   ******************************************************************************
60   * @attention
61   *
62   * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
63   *
64   * Redistribution and use in source and binary forms, with or without modification,
65   * are permitted provided that the following conditions are met:
66   *   1. Redistributions of source code must retain the above copyright notice,
67   *      this list of conditions and the following disclaimer.
68   *   2. Redistributions in binary form must reproduce the above copyright notice,
69   *      this list of conditions and the following disclaimer in the documentation
70   *      and/or other materials provided with the distribution.
71   *   3. Neither the name of STMicroelectronics nor the names of its contributors
72   *      may be used to endorse or promote products derived from this software
73   *      without specific prior written permission.
74   *
75   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
76   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
77   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
78   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
79   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
80   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
81   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
82   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
83   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
84   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
85   *
86   ******************************************************************************
87   */ 
88
89 /* Includes ------------------------------------------------------------------*/
90 #include "stm32l0xx_hal.h"
91
92 /** @addtogroup STM32L0xx_HAL_Driver
93   * @{
94   */
95
96 /** @addtogroup DMA DMA
97   * @brief DMA HAL module driver
98   * @{
99   */
100
101 #ifdef HAL_DMA_MODULE_ENABLED
102
103 /* Private typedef -----------------------------------------------------------*/  
104 /** @defgroup DMA_Private_Types DMA Private Types
105   *
106   * @{
107   */
108 #define HAL_TIMEOUT_DMA_ABORT    ((uint32_t)1000)  /* 1s  */
109
110 /**
111   * @}
112   */
113
114 /* Private function prototypes -----------------------------------------------*/  
115 /** @defgroup DMA_Private_Functions DMA Private Functions
116   * @{
117   */
118 static void DMA_SetConfig(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength);
119 /**
120   * @}
121   */
122
123 /** @addtogroup DMA_Exported_Functions DMA Exported Functions
124   * @{
125   */
126
127 /** @addtogroup DMA_Exported_Functions_Group1 
128  *  @brief   Initialization/de-initialization functions 
129  *
130 @verbatim   
131  ===============================================================================
132              ##### Initialization and de-initialization functions  #####
133  ===============================================================================  
134     [..]  This section provides functions allowing to:
135       (+) Initialize and configure the DMA
136       (+) De-Initialize the DMA 
137
138 @endverbatim
139   * @{
140   */
141   
142 /**
143   * @brief  Initializes the DMA according to the specified
144   *         parameters in the DMA_InitTypeDef and create the associated handle.
145   * @param  hdma: Pointer to a DMA_HandleTypeDef structure that contains
146   *               the configuration information for the specified DMA Channel.  
147   * @retval HAL status
148   */
149 HAL_StatusTypeDef HAL_DMA_Init(DMA_HandleTypeDef *hdma)
150
151   uint32_t tmp = 0;
152   
153   /* Check the DMA peripheral state */
154   if(hdma == NULL)
155   {
156     return HAL_ERROR;
157   }
158
159   /* Check the parameters */
160   assert_param(IS_DMA_ALL_PERIPH(hdma->Instance));
161   assert_param(IS_DMA_ALL_REQUEST(hdma->Init.Request));
162   assert_param(IS_DMA_DIRECTION(hdma->Init.Direction));
163   assert_param(IS_DMA_PERIPHERAL_INC_STATE(hdma->Init.PeriphInc));
164   assert_param(IS_DMA_MEMORY_INC_STATE(hdma->Init.MemInc));
165   assert_param(IS_DMA_PERIPHERAL_DATA_SIZE(hdma->Init.PeriphDataAlignment));
166   assert_param(IS_DMA_MEMORY_DATA_SIZE(hdma->Init.MemDataAlignment));
167   assert_param(IS_DMA_MODE(hdma->Init.Mode));
168   assert_param(IS_DMA_PRIORITY(hdma->Init.Priority));
169   
170   /* Change DMA peripheral state */
171   hdma->State = HAL_DMA_STATE_BUSY;
172
173   /* Get the CR register value */
174   tmp = hdma->Instance->CCR;
175   
176   /* Clear PL, MSIZE, PSIZE, MINC, PINC, CIRC, DIR bits */
177   tmp &= ((uint32_t)~(DMA_CCR_PL    | DMA_CCR_MSIZE  | DMA_CCR_PSIZE  | \
178                       DMA_CCR_MINC  | DMA_CCR_PINC   | DMA_CCR_CIRC   | \
179                       DMA_CCR_DIR));
180   
181   /* Prepare the DMA Channel configuration */
182   tmp |=  hdma->Init.Direction        |
183           hdma->Init.PeriphInc           | hdma->Init.MemInc           |
184           hdma->Init.PeriphDataAlignment | hdma->Init.MemDataAlignment |
185           hdma->Init.Mode                | hdma->Init.Priority;
186
187   /* Write to DMA Channel CR register */
188   hdma->Instance->CCR = tmp;  
189   
190   /* Write to DMA channel selection register */
191   if (hdma->Instance == DMA1_Channel1)
192   {
193     /*Reset request selection for DMA1 Channel1*/
194     DMA1_CSELR->CSELR &= ~DMA_CSELR_C1S;
195     
196     /* Configure request selection for DMA1 Channel1 */
197     DMA1_CSELR->CSELR |= hdma->Init.Request;
198   }
199   else if (hdma->Instance == DMA1_Channel2)
200   {
201     /*Reset request selection for DMA1 Channel2*/
202     DMA1_CSELR->CSELR &= ~DMA_CSELR_C2S;
203     
204     /* Configure request selection for DMA1 Channel2 */
205     DMA1_CSELR->CSELR |= (uint32_t)(hdma->Init.Request << 4);
206   }
207   else if (hdma->Instance == DMA1_Channel3)
208   {
209     /*Reset request selection for DMA1 Channel3*/
210     DMA1_CSELR->CSELR &= ~DMA_CSELR_C3S;
211     
212     /* Configure request selection for DMA1 Channel3 */
213     DMA1_CSELR->CSELR |= (uint32_t) (hdma->Init.Request << 8);
214   }
215   else if (hdma->Instance == DMA1_Channel4)
216   {
217     /*Reset request selection for DMA1 Channel4*/
218     DMA1_CSELR->CSELR &= ~DMA_CSELR_C4S;
219     
220     /* Configure request selection for DMA1 Channel4 */
221     DMA1_CSELR->CSELR |= (uint32_t) (hdma->Init.Request << 12);
222   }
223   else if (hdma->Instance == DMA1_Channel5)
224   {
225     /*Reset request selection for DMA1 Channel5*/
226     DMA1_CSELR->CSELR &= ~DMA_CSELR_C5S;
227     
228     /* Configure request selection for DMA1 Channel5 */
229     DMA1_CSELR->CSELR |= (uint32_t) (hdma->Init.Request << 16);
230   }
231   else if (hdma->Instance == DMA1_Channel6)
232   {
233     /*Reset request selection for DMA1 Channel6*/
234     DMA1_CSELR->CSELR &= ~DMA_CSELR_C6S;
235     
236     /* Configure request selection for DMA1 Channel6 */
237     DMA1_CSELR->CSELR |= (uint32_t) (hdma->Init.Request << 20);
238   }
239   else if (hdma->Instance == DMA1_Channel7)
240   {
241     /*Reset request selection for DMA1 Channel7*/
242     DMA1_CSELR->CSELR &= ~DMA_CSELR_C7S;
243     
244     /* Configure request selection for DMA1 Channel7 */
245     DMA1_CSELR->CSELR |= (uint32_t) (hdma->Init.Request << 24);
246   }
247    
248   /* Initialize the DMA state*/
249   hdma->State  = HAL_DMA_STATE_READY;
250   
251   return HAL_OK;
252 }  
253   
254 /**
255   * @brief  DeInitializes the DMA peripheral 
256   * @param  hdma: pointer to a DMA_HandleTypeDef structure that contains
257   *               the configuration information for the specified DMA Channel.  
258   * @retval HAL status
259   */
260 HAL_StatusTypeDef HAL_DMA_DeInit(DMA_HandleTypeDef *hdma)
261 {
262   /* Check the DMA peripheral state */
263   if(hdma == NULL)
264   {
265     return HAL_ERROR;
266   }
267   
268   /* Check the DMA peripheral state */
269   if(hdma->State == HAL_DMA_STATE_BUSY)
270   {
271     return HAL_ERROR;
272   }
273
274   /* Disable the selected DMA Channelx */
275   __HAL_DMA_DISABLE(hdma);
276   
277   /* Reset DMA Channel control register */
278   hdma->Instance->CCR  = 0;
279   
280   /* Reset DMA Channel Number of Data to Transfer register */
281   hdma->Instance->CNDTR = 0;
282   
283   /* Reset DMA Channel peripheral address register */
284   hdma->Instance->CPAR  = 0;
285   
286   /* Reset DMA Channel memory address register */
287   hdma->Instance->CMAR = 0;
288
289   /* Clear all flags */
290   __HAL_DMA_CLEAR_FLAG(hdma, __HAL_DMA_GET_GI_FLAG_INDEX(hdma));
291   __HAL_DMA_CLEAR_FLAG(hdma, __HAL_DMA_GET_TC_FLAG_INDEX(hdma));
292   __HAL_DMA_CLEAR_FLAG(hdma, __HAL_DMA_GET_TE_FLAG_INDEX(hdma));
293   __HAL_DMA_CLEAR_FLAG(hdma, __HAL_DMA_GET_HT_FLAG_INDEX(hdma));
294   
295   /* Reset DMA channel selection register */
296   if (hdma->Instance == DMA1_Channel1)
297   {
298     /*Reset DMA request*/
299     DMA1_CSELR->CSELR &= ~DMA_CSELR_C1S;
300   }
301   else if (hdma->Instance == DMA1_Channel2)
302   {
303     /*Reset DMA request*/
304     DMA1_CSELR->CSELR &= ~DMA_CSELR_C2S;
305   }
306   else if (hdma->Instance == DMA1_Channel3)
307   {
308     /*Reset DMA request*/
309     DMA1_CSELR->CSELR &= ~DMA_CSELR_C3S;
310   }
311   else if (hdma->Instance == DMA1_Channel4)
312   {
313     /*Reset DMA request*/
314     DMA1_CSELR->CSELR &= ~DMA_CSELR_C4S;
315   }
316   else if (hdma->Instance == DMA1_Channel5)
317   {
318     /*Reset DMA request*/
319     DMA1_CSELR->CSELR &= ~DMA_CSELR_C5S;
320   }
321   else if (hdma->Instance == DMA1_Channel6)
322   {
323     /*Reset DMA request*/
324     DMA1_CSELR->CSELR &= ~DMA_CSELR_C6S;
325   }
326   else if (hdma->Instance == DMA1_Channel7)
327   {
328     /*Reset DMA request*/
329     DMA1_CSELR->CSELR &= ~DMA_CSELR_C7S;
330   }
331   
332   /* Initialise the error code */
333   hdma->ErrorCode = HAL_DMA_ERROR_NONE;
334
335   /* Initialize the DMA state */
336   hdma->State = HAL_DMA_STATE_RESET;
337   
338   /* Release Lock */
339   __HAL_UNLOCK(hdma);
340
341   return HAL_OK;
342 }
343
344 /**
345   * @}
346   */
347
348 /** @addtogroup DMA_Exported_Functions_Group2  
349  *  @brief   I/O operation functions  
350  *
351 @verbatim   
352  ===============================================================================
353                       #####  IO operation functions  #####
354  ===============================================================================  
355     [..]  This section provides functions allowing to:
356       (+) Configure the source, destination address and data length and Start DMA transfer
357       (+) Configure the source, destination address and data length and 
358           Start DMA transfer with interrupt
359       (+) Abort DMA transfer
360       (+) Poll for transfer complete
361       (+) Handle DMA interrupt request  
362
363 @endverbatim
364   * @{
365   */
366
367 /**
368   * @brief  Starts the DMA Transfer.
369   * @param  hdma      : pointer to a DMA_HandleTypeDef structure that contains
370   *                     the configuration information for the specified DMA Channel.  
371   * @param  SrcAddress: The source memory Buffer address
372   * @param  DstAddress: The destination memory Buffer address
373   * @param  DataLength: The length of data to be transferred from source to destination
374   * @retval HAL status
375   */
376 HAL_StatusTypeDef HAL_DMA_Start(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
377
378   /* Process locked */
379   __HAL_LOCK(hdma);  
380
381   /* Change DMA peripheral state */  
382   hdma->State = HAL_DMA_STATE_BUSY;  
383
384    /* Check the parameters */
385   assert_param(IS_DMA_BUFFER_SIZE(DataLength));
386   
387   /* Disable the peripheral */
388   __HAL_DMA_DISABLE(hdma);  
389   
390   /* Configure the source, destination address and the data length */
391   DMA_SetConfig(hdma, SrcAddress, DstAddress, DataLength);
392
393   /* Enable the Peripheral */
394   __HAL_DMA_ENABLE(hdma);  
395
396   return HAL_OK; 
397
398
399 /**
400   * @brief  Start the DMA Transfer with interrupt enabled.
401   * @param  hdma:       pointer to a DMA_HandleTypeDef structure that contains
402   *                     the configuration information for the specified DMA Channel.  
403   * @param  SrcAddress: The source memory Buffer address
404   * @param  DstAddress: The destination memory Buffer address
405   * @param  DataLength: The length of data to be transferred from source to destination
406   * @retval HAL status
407   */
408 HAL_StatusTypeDef HAL_DMA_Start_IT(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
409 {
410   /* Process locked */
411   __HAL_LOCK(hdma);
412
413   /* Change DMA peripheral state */  
414   hdma->State = HAL_DMA_STATE_BUSY;  
415
416    /* Check the parameters */
417   assert_param(IS_DMA_BUFFER_SIZE(DataLength));
418   
419   /* Disable the peripheral */
420   __HAL_DMA_DISABLE(hdma);
421   
422   /* Configure the source, destination address and the data length */  
423   DMA_SetConfig(hdma, SrcAddress, DstAddress, DataLength);
424   
425   /* Enable the transfer complete interrupt */
426   __HAL_DMA_ENABLE_IT(hdma, DMA_IT_TC);
427
428   /* Enable the Half transfer complete interrupt */
429   __HAL_DMA_ENABLE_IT(hdma, DMA_IT_HT);  
430
431   /* Enable the transfer Error interrupt */
432   __HAL_DMA_ENABLE_IT(hdma, DMA_IT_TE);
433   
434    /* Enable the Peripheral */
435   __HAL_DMA_ENABLE(hdma);   
436   
437   return HAL_OK;    
438
439
440 /**
441   * @brief  Aborts the DMA Transfer.
442   * @param  hdma  : pointer to a DMA_HandleTypeDef structure that contains
443   *                 the configuration information for the specified DMA Channel.
444   * @param  Timeout: Timeout duration
445   * @retval HAL status
446   */
447 HAL_StatusTypeDef HAL_DMA_Abort(DMA_HandleTypeDef *hdma)
448 {
449   uint32_t tickstart = 0;
450   
451   /* Disable the channel */
452   __HAL_DMA_DISABLE(hdma);
453   
454   /* Get timeout */
455   tickstart = HAL_GetTick();
456   
457   /* Check if the DMA Channel is effectively disabled */
458   while((hdma->Instance->CCR & DMA_CCR_EN) != 0) 
459   {
460     /* Check for the Timeout */
461     if( (HAL_GetTick()  - tickstart ) > HAL_TIMEOUT_DMA_ABORT)
462     {
463       /* Update error code */
464       hdma->ErrorCode |= HAL_DMA_ERROR_TIMEOUT;
465       
466       /* Process Unlocked */
467       __HAL_UNLOCK(hdma);
468       
469       /* Change the DMA state */
470       hdma->State = HAL_DMA_STATE_TIMEOUT;
471       
472       return HAL_TIMEOUT;
473     }
474   }
475   /* Process Unlocked */
476   __HAL_UNLOCK(hdma);
477   
478   /* Change the DMA state*/
479   hdma->State = HAL_DMA_STATE_READY; 
480   
481   return HAL_OK; 
482 }
483
484 /**
485   * @brief  Polling for transfer complete.
486   * @param  hdma:    pointer to a DMA_HandleTypeDef structure that contains
487   *                  the configuration information for the specified DMA Channel.
488   * @param  CompleteLevel: Specifies the DMA level complete.  
489   * @param  Timeout:       Timeout duration.
490   * @retval HAL status
491   */
492 HAL_StatusTypeDef HAL_DMA_PollForTransfer(DMA_HandleTypeDef *hdma, uint32_t CompleteLevel, uint32_t Timeout)
493 {
494   uint32_t temp;
495   uint32_t tickstart = 0;
496   
497   /* Get the level transfer complete flag */
498   if(CompleteLevel == HAL_DMA_FULL_TRANSFER)
499   {
500     /* Transfer Complete flag */
501     temp = __HAL_DMA_GET_TC_FLAG_INDEX(hdma);
502   }
503   else
504   {
505     /* Half Transfer Complete flag */
506     temp = __HAL_DMA_GET_HT_FLAG_INDEX(hdma);
507   }
508
509   /* Get timeout */
510   tickstart = HAL_GetTick();
511
512   while(__HAL_DMA_GET_FLAG(hdma, temp) == RESET)
513   {
514     if((__HAL_DMA_GET_FLAG(hdma, __HAL_DMA_GET_TE_FLAG_INDEX(hdma)) != RESET))
515     {      
516       /* Clear the transfer error flags */
517       __HAL_DMA_CLEAR_FLAG(hdma, __HAL_DMA_GET_TE_FLAG_INDEX(hdma));
518       
519       /* Update error code */
520       SET_BIT(hdma->ErrorCode, HAL_DMA_ERROR_TE);
521       
522       /* Change the DMA state */
523       hdma->State= HAL_DMA_STATE_ERROR;       
524       
525       /* Process Unlocked */
526       __HAL_UNLOCK(hdma);
527       
528       return HAL_ERROR;      
529     }      
530     /* Check for the Timeout */
531     if(Timeout != HAL_MAX_DELAY)
532     {
533       if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
534       {
535         /* Update error code */
536         SET_BIT(hdma->ErrorCode, HAL_DMA_ERROR_TIMEOUT);
537         
538         /* Change the DMA state */
539         hdma->State= HAL_DMA_STATE_TIMEOUT;
540         
541         /* Process Unlocked */
542         __HAL_UNLOCK(hdma);
543       
544         return HAL_TIMEOUT;
545       }
546     }
547   }
548
549   if(CompleteLevel == HAL_DMA_FULL_TRANSFER)
550   {
551     /* Clear the transfer complete flag */
552     __HAL_DMA_CLEAR_FLAG(hdma, __HAL_DMA_GET_TC_FLAG_INDEX(hdma));
553
554     /* The selected Channelx EN bit is cleared (DMA is disabled and 
555     all transfers are complete) */
556     hdma->State = HAL_DMA_STATE_READY;
557
558     /* Process unlocked */
559     __HAL_UNLOCK(hdma);  
560   }
561   else
562   { 
563     /* Clear the half transfer complete flag */
564     __HAL_DMA_CLEAR_FLAG(hdma, __HAL_DMA_GET_HT_FLAG_INDEX(hdma));
565
566     /* The selected Channelx EN bit is cleared (DMA is disabled and 
567     all transfers are complete) */
568     hdma->State = HAL_DMA_STATE_READY_HALF;
569     
570     /* Process unlocked */
571     __HAL_UNLOCK(hdma); 
572   }
573   
574   return HAL_OK;
575 }
576 /**
577   * @brief  Handles DMA interrupt request.
578   * @param  hdma: pointer to a DMA_HandleTypeDef structure that contains
579   *               the configuration information for the specified DMA Channel.  
580   * @retval None
581   */
582 void HAL_DMA_IRQHandler(DMA_HandleTypeDef *hdma)
583 {        
584   /* Transfer Error Interrupt management ***************************************/
585   if(__HAL_DMA_GET_FLAG(hdma, __HAL_DMA_GET_TE_FLAG_INDEX(hdma)) != RESET)
586   {
587     if(__HAL_DMA_GET_IT_SOURCE(hdma, DMA_IT_TE) != RESET)
588     {
589       /* Disable the transfer error interrupt */
590       __HAL_DMA_DISABLE_IT(hdma, DMA_IT_TE);
591     
592       /* Clear the transfer error flag */
593       __HAL_DMA_CLEAR_FLAG(hdma, __HAL_DMA_GET_TE_FLAG_INDEX(hdma));
594     
595       /* Update error code */
596       hdma->ErrorCode |= HAL_DMA_ERROR_TE;
597
598       /* Change the DMA state */
599       hdma->State = HAL_DMA_STATE_ERROR;    
600     
601       /* Process Unlocked */
602       __HAL_UNLOCK(hdma); 
603     
604       if (hdma->XferErrorCallback != NULL)
605       {
606         /* Transfer error callback */
607         hdma->XferErrorCallback(hdma);
608       }
609     }
610   }
611
612   /* Half Transfer Complete Interrupt management ******************************/
613   if(__HAL_DMA_GET_FLAG(hdma, __HAL_DMA_GET_HT_FLAG_INDEX(hdma)) != RESET)
614   {
615     if(__HAL_DMA_GET_IT_SOURCE(hdma, DMA_IT_HT) != RESET)
616     { 
617       /* Disable the half transfer interrupt if the DMA mode is not CIRCULAR */
618       if((hdma->Instance->CCR & DMA_CCR_CIRC) == 0)
619       {
620         /* Disable the half transfer interrupt */
621         __HAL_DMA_DISABLE_IT(hdma, DMA_IT_HT);
622       }
623       /* Clear the half transfer complete flag */
624       __HAL_DMA_CLEAR_FLAG(hdma, __HAL_DMA_GET_HT_FLAG_INDEX(hdma));
625
626       /* Change DMA peripheral state */
627       hdma->State = HAL_DMA_STATE_READY_HALF;
628
629       if(hdma->XferHalfCpltCallback != NULL)
630       {
631         /* Half transfer callback */
632         hdma->XferHalfCpltCallback(hdma);
633       }
634     }
635   }
636   
637   /* Transfer Complete Interrupt management ***********************************/
638   if(__HAL_DMA_GET_FLAG(hdma, __HAL_DMA_GET_TC_FLAG_INDEX(hdma)) != RESET)
639   {
640     if(__HAL_DMA_GET_IT_SOURCE(hdma, DMA_IT_TC) != RESET)
641     {
642       if((hdma->Instance->CCR & DMA_CCR_CIRC) == 0)
643       {
644         /* Disable the transfer complete interrupt */
645         __HAL_DMA_DISABLE_IT(hdma, DMA_IT_TC);
646       }
647       /* Clear the transfer complete flag */
648       __HAL_DMA_CLEAR_FLAG(hdma, __HAL_DMA_GET_TC_FLAG_INDEX(hdma));
649     
650       /* Update error code */
651       hdma->ErrorCode |= HAL_DMA_ERROR_NONE;
652
653       /* Change the DMA state */
654       hdma->State = HAL_DMA_STATE_READY;    
655     
656       /* Process Unlocked */
657       __HAL_UNLOCK(hdma);
658     
659       if(hdma->XferCpltCallback != NULL)
660       {       
661         /* Transfer complete callback */
662         hdma->XferCpltCallback(hdma);
663       }
664     }
665   }
666 }  
667
668 /**
669   * @}
670   */
671
672 /** @addtogroup DMA_Exported_Functions_Group3 
673  *  @brief    Peripheral State functions 
674  *
675 @verbatim   
676  ===============================================================================
677                         ##### Peripheral State functions #####
678  ===============================================================================  
679     [..]
680     This subsection provides functions allowing to
681       (+) Check the DMA state
682       (+) Get error code
683
684 @endverbatim
685   * @{
686   */  
687
688 /**
689   * @brief  Returns the DMA state.
690   * @param  hdma: pointer to a DMA_HandleTypeDef structure that contains
691   *               the configuration information for the specified DMA Channel.  
692   * @retval HAL state
693   */
694 HAL_DMA_StateTypeDef HAL_DMA_GetState(DMA_HandleTypeDef *hdma)
695 {
696   return hdma->State;
697 }
698
699 /**
700   * @brief  Return the DMA error code
701   * @param  hdma : pointer to a DMA_HandleTypeDef structure that contains
702   *              the configuration information for the specified DMA Channel.
703   * @retval DMA Error Code
704   */
705 uint32_t HAL_DMA_GetError(DMA_HandleTypeDef *hdma)
706 {
707   return hdma->ErrorCode;
708 }
709
710 /**
711   * @}
712   */
713
714 /**
715   * @}
716   */
717 /* Private macro -------------------------------------------------------------*/
718 /* Private variables ---------------------------------------------------------*/
719
720
721 /* Private function prototypes -----------------------------------------------*/  
722 /** @addtogroup DMA_Private_Functions DMA Private Functions
723   * @{
724   */
725   
726   /*
727   * @brief  Sets the DMA Transfer parameter.
728   * @param  hdma:       pointer to a DMA_HandleTypeDef structure that contains
729   *                     the configuration information for the specified DMA Channel.  
730   * @param  SrcAddress: The source memory Buffer address
731   * @param  DstAddress: The destination memory Buffer address
732   * @param  DataLength: The length of data to be transferred from source to destination
733   * @retval HAL status
734   */
735 static void DMA_SetConfig(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
736 {  
737   /* Configure DMA Channel data length */
738   hdma->Instance->CNDTR = DataLength;
739   
740   /* Peripheral to Memory */
741   if((hdma->Init.Direction) == DMA_MEMORY_TO_PERIPH)
742   {   
743     /* Configure DMA Channel destination address */
744     hdma->Instance->CPAR = DstAddress;
745     
746     /* Configure DMA Channel source address */
747     hdma->Instance->CMAR = SrcAddress;
748   }
749   /* Memory to Peripheral */
750   else
751   {
752     /* Configure DMA Channel source address */
753     hdma->Instance->CPAR = SrcAddress;
754     
755     /* Configure DMA Channel destination address */
756     hdma->Instance->CMAR = DstAddress;
757   }
758 }
759 /**
760   * @}
761   */
762
763 #endif /* HAL_DMA_MODULE_ENABLED */
764 /**
765   * @}
766   */
767
768 /**
769   * @}
770   */
771
772 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
773