STM32F10x Standard Peripherals Library  3.5.0
/opt/STM32F10x_StdPeriph_Lib_V3.5.0/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_dac.c
Go to the documentation of this file.
00001 
00022 /* Includes ------------------------------------------------------------------*/
00023 #include "stm32f10x_dac.h"
00024 #include "stm32f10x_rcc.h"
00025 
00047 /* CR register Mask */
00048 #define CR_CLEAR_MASK              ((uint32_t)0x00000FFE)
00049 
00050 /* DAC Dual Channels SWTRIG masks */
00051 #define DUAL_SWTRIG_SET            ((uint32_t)0x00000003)
00052 #define DUAL_SWTRIG_RESET          ((uint32_t)0xFFFFFFFC)
00053 
00054 /* DHR registers offsets */
00055 #define DHR12R1_OFFSET             ((uint32_t)0x00000008)
00056 #define DHR12R2_OFFSET             ((uint32_t)0x00000014)
00057 #define DHR12RD_OFFSET             ((uint32_t)0x00000020)
00058 
00059 /* DOR register offset */
00060 #define DOR_OFFSET                 ((uint32_t)0x0000002C)
00061 
00098 void DAC_DeInit(void)
00099 {
00100   /* Enable DAC reset state */
00101   RCC_APB1PeriphResetCmd(RCC_APB1Periph_DAC, ENABLE);
00102   /* Release DAC from reset state */
00103   RCC_APB1PeriphResetCmd(RCC_APB1Periph_DAC, DISABLE);
00104 }
00105 
00117 void DAC_Init(uint32_t DAC_Channel, DAC_InitTypeDef* DAC_InitStruct)
00118 {
00119   uint32_t tmpreg1 = 0, tmpreg2 = 0;
00120   /* Check the DAC parameters */
00121   assert_param(IS_DAC_TRIGGER(DAC_InitStruct->DAC_Trigger));
00122   assert_param(IS_DAC_GENERATE_WAVE(DAC_InitStruct->DAC_WaveGeneration));
00123   assert_param(IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(DAC_InitStruct->DAC_LFSRUnmask_TriangleAmplitude));
00124   assert_param(IS_DAC_OUTPUT_BUFFER_STATE(DAC_InitStruct->DAC_OutputBuffer));
00125 /*---------------------------- DAC CR Configuration --------------------------*/
00126   /* Get the DAC CR value */
00127   tmpreg1 = DAC->CR;
00128   /* Clear BOFFx, TENx, TSELx, WAVEx and MAMPx bits */
00129   tmpreg1 &= ~(CR_CLEAR_MASK << DAC_Channel);
00130   /* Configure for the selected DAC channel: buffer output, trigger, wave generation,
00131      mask/amplitude for wave generation */
00132   /* Set TSELx and TENx bits according to DAC_Trigger value */
00133   /* Set WAVEx bits according to DAC_WaveGeneration value */
00134   /* Set MAMPx bits according to DAC_LFSRUnmask_TriangleAmplitude value */ 
00135   /* Set BOFFx bit according to DAC_OutputBuffer value */   
00136   tmpreg2 = (DAC_InitStruct->DAC_Trigger | DAC_InitStruct->DAC_WaveGeneration |
00137              DAC_InitStruct->DAC_LFSRUnmask_TriangleAmplitude | DAC_InitStruct->DAC_OutputBuffer);
00138   /* Calculate CR register value depending on DAC_Channel */
00139   tmpreg1 |= tmpreg2 << DAC_Channel;
00140   /* Write to DAC CR */
00141   DAC->CR = tmpreg1;
00142 }
00143 
00150 void DAC_StructInit(DAC_InitTypeDef* DAC_InitStruct)
00151 {
00152 /*--------------- Reset DAC init structure parameters values -----------------*/
00153   /* Initialize the DAC_Trigger member */
00154   DAC_InitStruct->DAC_Trigger = DAC_Trigger_None;
00155   /* Initialize the DAC_WaveGeneration member */
00156   DAC_InitStruct->DAC_WaveGeneration = DAC_WaveGeneration_None;
00157   /* Initialize the DAC_LFSRUnmask_TriangleAmplitude member */
00158   DAC_InitStruct->DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bit0;
00159   /* Initialize the DAC_OutputBuffer member */
00160   DAC_InitStruct->DAC_OutputBuffer = DAC_OutputBuffer_Enable;
00161 }
00162 
00173 void DAC_Cmd(uint32_t DAC_Channel, FunctionalState NewState)
00174 {
00175   /* Check the parameters */
00176   assert_param(IS_DAC_CHANNEL(DAC_Channel));
00177   assert_param(IS_FUNCTIONAL_STATE(NewState));
00178   if (NewState != DISABLE)
00179   {
00180     /* Enable the selected DAC channel */
00181     DAC->CR |= (DAC_CR_EN1 << DAC_Channel);
00182   }
00183   else
00184   {
00185     /* Disable the selected DAC channel */
00186     DAC->CR &= ~(DAC_CR_EN1 << DAC_Channel);
00187   }
00188 }
00189 #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)
00190 
00203 void DAC_ITConfig(uint32_t DAC_Channel, uint32_t DAC_IT, FunctionalState NewState)  
00204 {
00205   /* Check the parameters */
00206   assert_param(IS_DAC_CHANNEL(DAC_Channel));
00207   assert_param(IS_FUNCTIONAL_STATE(NewState));
00208   assert_param(IS_DAC_IT(DAC_IT)); 
00209 
00210   if (NewState != DISABLE)
00211   {
00212     /* Enable the selected DAC interrupts */
00213     DAC->CR |=  (DAC_IT << DAC_Channel);
00214   }
00215   else
00216   {
00217     /* Disable the selected DAC interrupts */
00218     DAC->CR &= (~(uint32_t)(DAC_IT << DAC_Channel));
00219   }
00220 }
00221 #endif
00222 
00233 void DAC_DMACmd(uint32_t DAC_Channel, FunctionalState NewState)
00234 {
00235   /* Check the parameters */
00236   assert_param(IS_DAC_CHANNEL(DAC_Channel));
00237   assert_param(IS_FUNCTIONAL_STATE(NewState));
00238   if (NewState != DISABLE)
00239   {
00240     /* Enable the selected DAC channel DMA request */
00241     DAC->CR |= (DAC_CR_DMAEN1 << DAC_Channel);
00242   }
00243   else
00244   {
00245     /* Disable the selected DAC channel DMA request */
00246     DAC->CR &= ~(DAC_CR_DMAEN1 << DAC_Channel);
00247   }
00248 }
00249 
00260 void DAC_SoftwareTriggerCmd(uint32_t DAC_Channel, FunctionalState NewState)
00261 {
00262   /* Check the parameters */
00263   assert_param(IS_DAC_CHANNEL(DAC_Channel));
00264   assert_param(IS_FUNCTIONAL_STATE(NewState));
00265   if (NewState != DISABLE)
00266   {
00267     /* Enable software trigger for the selected DAC channel */
00268     DAC->SWTRIGR |= (uint32_t)DAC_SWTRIGR_SWTRIG1 << (DAC_Channel >> 4);
00269   }
00270   else
00271   {
00272     /* Disable software trigger for the selected DAC channel */
00273     DAC->SWTRIGR &= ~((uint32_t)DAC_SWTRIGR_SWTRIG1 << (DAC_Channel >> 4));
00274   }
00275 }
00276 
00284 void DAC_DualSoftwareTriggerCmd(FunctionalState NewState)
00285 {
00286   /* Check the parameters */
00287   assert_param(IS_FUNCTIONAL_STATE(NewState));
00288   if (NewState != DISABLE)
00289   {
00290     /* Enable software trigger for both DAC channels */
00291     DAC->SWTRIGR |= DUAL_SWTRIG_SET ;
00292   }
00293   else
00294   {
00295     /* Disable software trigger for both DAC channels */
00296     DAC->SWTRIGR &= DUAL_SWTRIG_RESET;
00297   }
00298 }
00299 
00314 void DAC_WaveGenerationCmd(uint32_t DAC_Channel, uint32_t DAC_Wave, FunctionalState NewState)
00315 {
00316   /* Check the parameters */
00317   assert_param(IS_DAC_CHANNEL(DAC_Channel));
00318   assert_param(IS_DAC_WAVE(DAC_Wave)); 
00319   assert_param(IS_FUNCTIONAL_STATE(NewState));
00320   if (NewState != DISABLE)
00321   {
00322     /* Enable the selected wave generation for the selected DAC channel */
00323     DAC->CR |= DAC_Wave << DAC_Channel;
00324   }
00325   else
00326   {
00327     /* Disable the selected wave generation for the selected DAC channel */
00328     DAC->CR &= ~(DAC_Wave << DAC_Channel);
00329   }
00330 }
00331 
00342 void DAC_SetChannel1Data(uint32_t DAC_Align, uint16_t Data)
00343 {  
00344   __IO uint32_t tmp = 0;
00345   
00346   /* Check the parameters */
00347   assert_param(IS_DAC_ALIGN(DAC_Align));
00348   assert_param(IS_DAC_DATA(Data));
00349   
00350   tmp = (uint32_t)DAC_BASE; 
00351   tmp += DHR12R1_OFFSET + DAC_Align;
00352 
00353   /* Set the DAC channel1 selected data holding register */
00354   *(__IO uint32_t *) tmp = Data;
00355 }
00356 
00367 void DAC_SetChannel2Data(uint32_t DAC_Align, uint16_t Data)
00368 {
00369   __IO uint32_t tmp = 0;
00370 
00371   /* Check the parameters */
00372   assert_param(IS_DAC_ALIGN(DAC_Align));
00373   assert_param(IS_DAC_DATA(Data));
00374   
00375   tmp = (uint32_t)DAC_BASE;
00376   tmp += DHR12R2_OFFSET + DAC_Align;
00377 
00378   /* Set the DAC channel2 selected data holding register */
00379   *(__IO uint32_t *)tmp = Data;
00380 }
00381 
00396 void DAC_SetDualChannelData(uint32_t DAC_Align, uint16_t Data2, uint16_t Data1)
00397 {
00398   uint32_t data = 0, tmp = 0;
00399   
00400   /* Check the parameters */
00401   assert_param(IS_DAC_ALIGN(DAC_Align));
00402   assert_param(IS_DAC_DATA(Data1));
00403   assert_param(IS_DAC_DATA(Data2));
00404   
00405   /* Calculate and set dual DAC data holding register value */
00406   if (DAC_Align == DAC_Align_8b_R)
00407   {
00408     data = ((uint32_t)Data2 << 8) | Data1; 
00409   }
00410   else
00411   {
00412     data = ((uint32_t)Data2 << 16) | Data1;
00413   }
00414   
00415   tmp = (uint32_t)DAC_BASE;
00416   tmp += DHR12RD_OFFSET + DAC_Align;
00417 
00418   /* Set the dual DAC selected data holding register */
00419   *(__IO uint32_t *)tmp = data;
00420 }
00421 
00430 uint16_t DAC_GetDataOutputValue(uint32_t DAC_Channel)
00431 {
00432   __IO uint32_t tmp = 0;
00433   
00434   /* Check the parameters */
00435   assert_param(IS_DAC_CHANNEL(DAC_Channel));
00436   
00437   tmp = (uint32_t) DAC_BASE ;
00438   tmp += DOR_OFFSET + ((uint32_t)DAC_Channel >> 2);
00439   
00440   /* Returns the DAC channel data output register value */
00441   return (uint16_t) (*(__IO uint32_t*) tmp);
00442 }
00443 
00444 #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)
00445 
00456 FlagStatus DAC_GetFlagStatus(uint32_t DAC_Channel, uint32_t DAC_FLAG)
00457 {
00458   FlagStatus bitstatus = RESET;
00459   /* Check the parameters */
00460   assert_param(IS_DAC_CHANNEL(DAC_Channel));
00461   assert_param(IS_DAC_FLAG(DAC_FLAG));
00462 
00463   /* Check the status of the specified DAC flag */
00464   if ((DAC->SR & (DAC_FLAG << DAC_Channel)) != (uint8_t)RESET)
00465   {
00466     /* DAC_FLAG is set */
00467     bitstatus = SET;
00468   }
00469   else
00470   {
00471     /* DAC_FLAG is reset */
00472     bitstatus = RESET;
00473   }
00474   /* Return the DAC_FLAG status */
00475   return  bitstatus;
00476 }
00477 
00489 void DAC_ClearFlag(uint32_t DAC_Channel, uint32_t DAC_FLAG)
00490 {
00491   /* Check the parameters */
00492   assert_param(IS_DAC_CHANNEL(DAC_Channel));
00493   assert_param(IS_DAC_FLAG(DAC_FLAG));
00494 
00495   /* Clear the selected DAC flags */
00496   DAC->SR = (DAC_FLAG << DAC_Channel);
00497 }
00498 
00510 ITStatus DAC_GetITStatus(uint32_t DAC_Channel, uint32_t DAC_IT)
00511 {
00512   ITStatus bitstatus = RESET;
00513   uint32_t enablestatus = 0;
00514   
00515   /* Check the parameters */
00516   assert_param(IS_DAC_CHANNEL(DAC_Channel));
00517   assert_param(IS_DAC_IT(DAC_IT));
00518 
00519   /* Get the DAC_IT enable bit status */
00520   enablestatus = (DAC->CR & (DAC_IT << DAC_Channel)) ;
00521   
00522   /* Check the status of the specified DAC interrupt */
00523   if (((DAC->SR & (DAC_IT << DAC_Channel)) != (uint32_t)RESET) && enablestatus)
00524   {
00525     /* DAC_IT is set */
00526     bitstatus = SET;
00527   }
00528   else
00529   {
00530     /* DAC_IT is reset */
00531     bitstatus = RESET;
00532   }
00533   /* Return the DAC_IT status */
00534   return  bitstatus;
00535 }
00536 
00548 void DAC_ClearITPendingBit(uint32_t DAC_Channel, uint32_t DAC_IT)
00549 {
00550   /* Check the parameters */
00551   assert_param(IS_DAC_CHANNEL(DAC_Channel));
00552   assert_param(IS_DAC_IT(DAC_IT)); 
00553 
00554   /* Clear the selected DAC interrupt pending bits */
00555   DAC->SR = (DAC_IT << DAC_Channel);
00556 }
00557 #endif
00558 
00571 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/