STM32F10x Standard Peripherals Library  3.5.0
/opt/STM32F10x_StdPeriph_Lib_V3.5.0/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_dma.c
Go to the documentation of this file.
00001 
00022 /* Includes ------------------------------------------------------------------*/
00023 #include "stm32f10x_dma.h"
00024 #include "stm32f10x_rcc.h"
00025 
00047 /* DMA1 Channelx interrupt pending bit masks */
00048 #define DMA1_Channel1_IT_Mask    ((uint32_t)(DMA_ISR_GIF1 | DMA_ISR_TCIF1 | DMA_ISR_HTIF1 | DMA_ISR_TEIF1))
00049 #define DMA1_Channel2_IT_Mask    ((uint32_t)(DMA_ISR_GIF2 | DMA_ISR_TCIF2 | DMA_ISR_HTIF2 | DMA_ISR_TEIF2))
00050 #define DMA1_Channel3_IT_Mask    ((uint32_t)(DMA_ISR_GIF3 | DMA_ISR_TCIF3 | DMA_ISR_HTIF3 | DMA_ISR_TEIF3))
00051 #define DMA1_Channel4_IT_Mask    ((uint32_t)(DMA_ISR_GIF4 | DMA_ISR_TCIF4 | DMA_ISR_HTIF4 | DMA_ISR_TEIF4))
00052 #define DMA1_Channel5_IT_Mask    ((uint32_t)(DMA_ISR_GIF5 | DMA_ISR_TCIF5 | DMA_ISR_HTIF5 | DMA_ISR_TEIF5))
00053 #define DMA1_Channel6_IT_Mask    ((uint32_t)(DMA_ISR_GIF6 | DMA_ISR_TCIF6 | DMA_ISR_HTIF6 | DMA_ISR_TEIF6))
00054 #define DMA1_Channel7_IT_Mask    ((uint32_t)(DMA_ISR_GIF7 | DMA_ISR_TCIF7 | DMA_ISR_HTIF7 | DMA_ISR_TEIF7))
00055 
00056 /* DMA2 Channelx interrupt pending bit masks */
00057 #define DMA2_Channel1_IT_Mask    ((uint32_t)(DMA_ISR_GIF1 | DMA_ISR_TCIF1 | DMA_ISR_HTIF1 | DMA_ISR_TEIF1))
00058 #define DMA2_Channel2_IT_Mask    ((uint32_t)(DMA_ISR_GIF2 | DMA_ISR_TCIF2 | DMA_ISR_HTIF2 | DMA_ISR_TEIF2))
00059 #define DMA2_Channel3_IT_Mask    ((uint32_t)(DMA_ISR_GIF3 | DMA_ISR_TCIF3 | DMA_ISR_HTIF3 | DMA_ISR_TEIF3))
00060 #define DMA2_Channel4_IT_Mask    ((uint32_t)(DMA_ISR_GIF4 | DMA_ISR_TCIF4 | DMA_ISR_HTIF4 | DMA_ISR_TEIF4))
00061 #define DMA2_Channel5_IT_Mask    ((uint32_t)(DMA_ISR_GIF5 | DMA_ISR_TCIF5 | DMA_ISR_HTIF5 | DMA_ISR_TEIF5))
00062 
00063 /* DMA2 FLAG mask */
00064 #define FLAG_Mask                ((uint32_t)0x10000000)
00065 
00066 /* DMA registers Masks */
00067 #define CCR_CLEAR_Mask           ((uint32_t)0xFFFF800F)
00068 
00108 void DMA_DeInit(DMA_Channel_TypeDef* DMAy_Channelx)
00109 {
00110   /* Check the parameters */
00111   assert_param(IS_DMA_ALL_PERIPH(DMAy_Channelx));
00112   
00113   /* Disable the selected DMAy Channelx */
00114   DMAy_Channelx->CCR &= (uint16_t)(~DMA_CCR1_EN);
00115   
00116   /* Reset DMAy Channelx control register */
00117   DMAy_Channelx->CCR  = 0;
00118   
00119   /* Reset DMAy Channelx remaining bytes register */
00120   DMAy_Channelx->CNDTR = 0;
00121   
00122   /* Reset DMAy Channelx peripheral address register */
00123   DMAy_Channelx->CPAR  = 0;
00124   
00125   /* Reset DMAy Channelx memory address register */
00126   DMAy_Channelx->CMAR = 0;
00127   
00128   if (DMAy_Channelx == DMA1_Channel1)
00129   {
00130     /* Reset interrupt pending bits for DMA1 Channel1 */
00131     DMA1->IFCR |= DMA1_Channel1_IT_Mask;
00132   }
00133   else if (DMAy_Channelx == DMA1_Channel2)
00134   {
00135     /* Reset interrupt pending bits for DMA1 Channel2 */
00136     DMA1->IFCR |= DMA1_Channel2_IT_Mask;
00137   }
00138   else if (DMAy_Channelx == DMA1_Channel3)
00139   {
00140     /* Reset interrupt pending bits for DMA1 Channel3 */
00141     DMA1->IFCR |= DMA1_Channel3_IT_Mask;
00142   }
00143   else if (DMAy_Channelx == DMA1_Channel4)
00144   {
00145     /* Reset interrupt pending bits for DMA1 Channel4 */
00146     DMA1->IFCR |= DMA1_Channel4_IT_Mask;
00147   }
00148   else if (DMAy_Channelx == DMA1_Channel5)
00149   {
00150     /* Reset interrupt pending bits for DMA1 Channel5 */
00151     DMA1->IFCR |= DMA1_Channel5_IT_Mask;
00152   }
00153   else if (DMAy_Channelx == DMA1_Channel6)
00154   {
00155     /* Reset interrupt pending bits for DMA1 Channel6 */
00156     DMA1->IFCR |= DMA1_Channel6_IT_Mask;
00157   }
00158   else if (DMAy_Channelx == DMA1_Channel7)
00159   {
00160     /* Reset interrupt pending bits for DMA1 Channel7 */
00161     DMA1->IFCR |= DMA1_Channel7_IT_Mask;
00162   }
00163   else if (DMAy_Channelx == DMA2_Channel1)
00164   {
00165     /* Reset interrupt pending bits for DMA2 Channel1 */
00166     DMA2->IFCR |= DMA2_Channel1_IT_Mask;
00167   }
00168   else if (DMAy_Channelx == DMA2_Channel2)
00169   {
00170     /* Reset interrupt pending bits for DMA2 Channel2 */
00171     DMA2->IFCR |= DMA2_Channel2_IT_Mask;
00172   }
00173   else if (DMAy_Channelx == DMA2_Channel3)
00174   {
00175     /* Reset interrupt pending bits for DMA2 Channel3 */
00176     DMA2->IFCR |= DMA2_Channel3_IT_Mask;
00177   }
00178   else if (DMAy_Channelx == DMA2_Channel4)
00179   {
00180     /* Reset interrupt pending bits for DMA2 Channel4 */
00181     DMA2->IFCR |= DMA2_Channel4_IT_Mask;
00182   }
00183   else
00184   { 
00185     if (DMAy_Channelx == DMA2_Channel5)
00186     {
00187       /* Reset interrupt pending bits for DMA2 Channel5 */
00188       DMA2->IFCR |= DMA2_Channel5_IT_Mask;
00189     }
00190   }
00191 }
00192 
00202 void DMA_Init(DMA_Channel_TypeDef* DMAy_Channelx, DMA_InitTypeDef* DMA_InitStruct)
00203 {
00204   uint32_t tmpreg = 0;
00205 
00206   /* Check the parameters */
00207   assert_param(IS_DMA_ALL_PERIPH(DMAy_Channelx));
00208   assert_param(IS_DMA_DIR(DMA_InitStruct->DMA_DIR));
00209   assert_param(IS_DMA_BUFFER_SIZE(DMA_InitStruct->DMA_BufferSize));
00210   assert_param(IS_DMA_PERIPHERAL_INC_STATE(DMA_InitStruct->DMA_PeripheralInc));
00211   assert_param(IS_DMA_MEMORY_INC_STATE(DMA_InitStruct->DMA_MemoryInc));   
00212   assert_param(IS_DMA_PERIPHERAL_DATA_SIZE(DMA_InitStruct->DMA_PeripheralDataSize));
00213   assert_param(IS_DMA_MEMORY_DATA_SIZE(DMA_InitStruct->DMA_MemoryDataSize));
00214   assert_param(IS_DMA_MODE(DMA_InitStruct->DMA_Mode));
00215   assert_param(IS_DMA_PRIORITY(DMA_InitStruct->DMA_Priority));
00216   assert_param(IS_DMA_M2M_STATE(DMA_InitStruct->DMA_M2M));
00217 
00218 /*--------------------------- DMAy Channelx CCR Configuration -----------------*/
00219   /* Get the DMAy_Channelx CCR value */
00220   tmpreg = DMAy_Channelx->CCR;
00221   /* Clear MEM2MEM, PL, MSIZE, PSIZE, MINC, PINC, CIRC and DIR bits */
00222   tmpreg &= CCR_CLEAR_Mask;
00223   /* Configure DMAy Channelx: data transfer, data size, priority level and mode */
00224   /* Set DIR bit according to DMA_DIR value */
00225   /* Set CIRC bit according to DMA_Mode value */
00226   /* Set PINC bit according to DMA_PeripheralInc value */
00227   /* Set MINC bit according to DMA_MemoryInc value */
00228   /* Set PSIZE bits according to DMA_PeripheralDataSize value */
00229   /* Set MSIZE bits according to DMA_MemoryDataSize value */
00230   /* Set PL bits according to DMA_Priority value */
00231   /* Set the MEM2MEM bit according to DMA_M2M value */
00232   tmpreg |= DMA_InitStruct->DMA_DIR | DMA_InitStruct->DMA_Mode |
00233             DMA_InitStruct->DMA_PeripheralInc | DMA_InitStruct->DMA_MemoryInc |
00234             DMA_InitStruct->DMA_PeripheralDataSize | DMA_InitStruct->DMA_MemoryDataSize |
00235             DMA_InitStruct->DMA_Priority | DMA_InitStruct->DMA_M2M;
00236 
00237   /* Write to DMAy Channelx CCR */
00238   DMAy_Channelx->CCR = tmpreg;
00239 
00240 /*--------------------------- DMAy Channelx CNDTR Configuration ---------------*/
00241   /* Write to DMAy Channelx CNDTR */
00242   DMAy_Channelx->CNDTR = DMA_InitStruct->DMA_BufferSize;
00243 
00244 /*--------------------------- DMAy Channelx CPAR Configuration ----------------*/
00245   /* Write to DMAy Channelx CPAR */
00246   DMAy_Channelx->CPAR = DMA_InitStruct->DMA_PeripheralBaseAddr;
00247 
00248 /*--------------------------- DMAy Channelx CMAR Configuration ----------------*/
00249   /* Write to DMAy Channelx CMAR */
00250   DMAy_Channelx->CMAR = DMA_InitStruct->DMA_MemoryBaseAddr;
00251 }
00252 
00259 void DMA_StructInit(DMA_InitTypeDef* DMA_InitStruct)
00260 {
00261 /*-------------- Reset DMA init structure parameters values ------------------*/
00262   /* Initialize the DMA_PeripheralBaseAddr member */
00263   DMA_InitStruct->DMA_PeripheralBaseAddr = 0;
00264   /* Initialize the DMA_MemoryBaseAddr member */
00265   DMA_InitStruct->DMA_MemoryBaseAddr = 0;
00266   /* Initialize the DMA_DIR member */
00267   DMA_InitStruct->DMA_DIR = DMA_DIR_PeripheralSRC;
00268   /* Initialize the DMA_BufferSize member */
00269   DMA_InitStruct->DMA_BufferSize = 0;
00270   /* Initialize the DMA_PeripheralInc member */
00271   DMA_InitStruct->DMA_PeripheralInc = DMA_PeripheralInc_Disable;
00272   /* Initialize the DMA_MemoryInc member */
00273   DMA_InitStruct->DMA_MemoryInc = DMA_MemoryInc_Disable;
00274   /* Initialize the DMA_PeripheralDataSize member */
00275   DMA_InitStruct->DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
00276   /* Initialize the DMA_MemoryDataSize member */
00277   DMA_InitStruct->DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
00278   /* Initialize the DMA_Mode member */
00279   DMA_InitStruct->DMA_Mode = DMA_Mode_Normal;
00280   /* Initialize the DMA_Priority member */
00281   DMA_InitStruct->DMA_Priority = DMA_Priority_Low;
00282   /* Initialize the DMA_M2M member */
00283   DMA_InitStruct->DMA_M2M = DMA_M2M_Disable;
00284 }
00285 
00294 void DMA_Cmd(DMA_Channel_TypeDef* DMAy_Channelx, FunctionalState NewState)
00295 {
00296   /* Check the parameters */
00297   assert_param(IS_DMA_ALL_PERIPH(DMAy_Channelx));
00298   assert_param(IS_FUNCTIONAL_STATE(NewState));
00299 
00300   if (NewState != DISABLE)
00301   {
00302     /* Enable the selected DMAy Channelx */
00303     DMAy_Channelx->CCR |= DMA_CCR1_EN;
00304   }
00305   else
00306   {
00307     /* Disable the selected DMAy Channelx */
00308     DMAy_Channelx->CCR &= (uint16_t)(~DMA_CCR1_EN);
00309   }
00310 }
00311 
00326 void DMA_ITConfig(DMA_Channel_TypeDef* DMAy_Channelx, uint32_t DMA_IT, FunctionalState NewState)
00327 {
00328   /* Check the parameters */
00329   assert_param(IS_DMA_ALL_PERIPH(DMAy_Channelx));
00330   assert_param(IS_DMA_CONFIG_IT(DMA_IT));
00331   assert_param(IS_FUNCTIONAL_STATE(NewState));
00332   if (NewState != DISABLE)
00333   {
00334     /* Enable the selected DMA interrupts */
00335     DMAy_Channelx->CCR |= DMA_IT;
00336   }
00337   else
00338   {
00339     /* Disable the selected DMA interrupts */
00340     DMAy_Channelx->CCR &= ~DMA_IT;
00341   }
00342 }
00343 
00353 void DMA_SetCurrDataCounter(DMA_Channel_TypeDef* DMAy_Channelx, uint16_t DataNumber)
00354 {
00355   /* Check the parameters */
00356   assert_param(IS_DMA_ALL_PERIPH(DMAy_Channelx));
00357   
00358 /*--------------------------- DMAy Channelx CNDTR Configuration ---------------*/
00359   /* Write to DMAy Channelx CNDTR */
00360   DMAy_Channelx->CNDTR = DataNumber;  
00361 }
00362 
00371 uint16_t DMA_GetCurrDataCounter(DMA_Channel_TypeDef* DMAy_Channelx)
00372 {
00373   /* Check the parameters */
00374   assert_param(IS_DMA_ALL_PERIPH(DMAy_Channelx));
00375   /* Return the number of remaining data units for DMAy Channelx */
00376   return ((uint16_t)(DMAy_Channelx->CNDTR));
00377 }
00378 
00433 FlagStatus DMA_GetFlagStatus(uint32_t DMAy_FLAG)
00434 {
00435   FlagStatus bitstatus = RESET;
00436   uint32_t tmpreg = 0;
00437   
00438   /* Check the parameters */
00439   assert_param(IS_DMA_GET_FLAG(DMAy_FLAG));
00440 
00441   /* Calculate the used DMAy */
00442   if ((DMAy_FLAG & FLAG_Mask) != (uint32_t)RESET)
00443   {
00444     /* Get DMA2 ISR register value */
00445     tmpreg = DMA2->ISR ;
00446   }
00447   else
00448   {
00449     /* Get DMA1 ISR register value */
00450     tmpreg = DMA1->ISR ;
00451   }
00452 
00453   /* Check the status of the specified DMAy flag */
00454   if ((tmpreg & DMAy_FLAG) != (uint32_t)RESET)
00455   {
00456     /* DMAy_FLAG is set */
00457     bitstatus = SET;
00458   }
00459   else
00460   {
00461     /* DMAy_FLAG is reset */
00462     bitstatus = RESET;
00463   }
00464   
00465   /* Return the DMAy_FLAG status */
00466   return  bitstatus;
00467 }
00468 
00523 void DMA_ClearFlag(uint32_t DMAy_FLAG)
00524 {
00525   /* Check the parameters */
00526   assert_param(IS_DMA_CLEAR_FLAG(DMAy_FLAG));
00527 
00528   /* Calculate the used DMAy */
00529   if ((DMAy_FLAG & FLAG_Mask) != (uint32_t)RESET)
00530   {
00531     /* Clear the selected DMAy flags */
00532     DMA2->IFCR = DMAy_FLAG;
00533   }
00534   else
00535   {
00536     /* Clear the selected DMAy flags */
00537     DMA1->IFCR = DMAy_FLAG;
00538   }
00539 }
00540 
00595 ITStatus DMA_GetITStatus(uint32_t DMAy_IT)
00596 {
00597   ITStatus bitstatus = RESET;
00598   uint32_t tmpreg = 0;
00599 
00600   /* Check the parameters */
00601   assert_param(IS_DMA_GET_IT(DMAy_IT));
00602 
00603   /* Calculate the used DMA */
00604   if ((DMAy_IT & FLAG_Mask) != (uint32_t)RESET)
00605   {
00606     /* Get DMA2 ISR register value */
00607     tmpreg = DMA2->ISR;
00608   }
00609   else
00610   {
00611     /* Get DMA1 ISR register value */
00612     tmpreg = DMA1->ISR;
00613   }
00614 
00615   /* Check the status of the specified DMAy interrupt */
00616   if ((tmpreg & DMAy_IT) != (uint32_t)RESET)
00617   {
00618     /* DMAy_IT is set */
00619     bitstatus = SET;
00620   }
00621   else
00622   {
00623     /* DMAy_IT is reset */
00624     bitstatus = RESET;
00625   }
00626   /* Return the DMA_IT status */
00627   return  bitstatus;
00628 }
00629 
00684 void DMA_ClearITPendingBit(uint32_t DMAy_IT)
00685 {
00686   /* Check the parameters */
00687   assert_param(IS_DMA_CLEAR_IT(DMAy_IT));
00688 
00689   /* Calculate the used DMAy */
00690   if ((DMAy_IT & FLAG_Mask) != (uint32_t)RESET)
00691   {
00692     /* Clear the selected DMAy interrupt pending bits */
00693     DMA2->IFCR = DMAy_IT;
00694   }
00695   else
00696   {
00697     /* Clear the selected DMAy interrupt pending bits */
00698     DMA1->IFCR = DMAy_IT;
00699   }
00700 }
00701 
00714 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/