STM32F10x Standard Peripherals Library  3.5.0
/opt/STM32F10x_StdPeriph_Lib_V3.5.0/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_adc.c
Go to the documentation of this file.
00001 
00022 /* Includes ------------------------------------------------------------------*/
00023 #include "stm32f10x_adc.h"
00024 #include "stm32f10x_rcc.h"
00025 
00047 /* ADC DISCNUM mask */
00048 #define CR1_DISCNUM_Reset           ((uint32_t)0xFFFF1FFF)
00049 
00050 /* ADC DISCEN mask */
00051 #define CR1_DISCEN_Set              ((uint32_t)0x00000800)
00052 #define CR1_DISCEN_Reset            ((uint32_t)0xFFFFF7FF)
00053 
00054 /* ADC JAUTO mask */
00055 #define CR1_JAUTO_Set               ((uint32_t)0x00000400)
00056 #define CR1_JAUTO_Reset             ((uint32_t)0xFFFFFBFF)
00057 
00058 /* ADC JDISCEN mask */
00059 #define CR1_JDISCEN_Set             ((uint32_t)0x00001000)
00060 #define CR1_JDISCEN_Reset           ((uint32_t)0xFFFFEFFF)
00061 
00062 /* ADC AWDCH mask */
00063 #define CR1_AWDCH_Reset             ((uint32_t)0xFFFFFFE0)
00064 
00065 /* ADC Analog watchdog enable mode mask */
00066 #define CR1_AWDMode_Reset           ((uint32_t)0xFF3FFDFF)
00067 
00068 /* CR1 register Mask */
00069 #define CR1_CLEAR_Mask              ((uint32_t)0xFFF0FEFF)
00070 
00071 /* ADC ADON mask */
00072 #define CR2_ADON_Set                ((uint32_t)0x00000001)
00073 #define CR2_ADON_Reset              ((uint32_t)0xFFFFFFFE)
00074 
00075 /* ADC DMA mask */
00076 #define CR2_DMA_Set                 ((uint32_t)0x00000100)
00077 #define CR2_DMA_Reset               ((uint32_t)0xFFFFFEFF)
00078 
00079 /* ADC RSTCAL mask */
00080 #define CR2_RSTCAL_Set              ((uint32_t)0x00000008)
00081 
00082 /* ADC CAL mask */
00083 #define CR2_CAL_Set                 ((uint32_t)0x00000004)
00084 
00085 /* ADC SWSTART mask */
00086 #define CR2_SWSTART_Set             ((uint32_t)0x00400000)
00087 
00088 /* ADC EXTTRIG mask */
00089 #define CR2_EXTTRIG_Set             ((uint32_t)0x00100000)
00090 #define CR2_EXTTRIG_Reset           ((uint32_t)0xFFEFFFFF)
00091 
00092 /* ADC Software start mask */
00093 #define CR2_EXTTRIG_SWSTART_Set     ((uint32_t)0x00500000)
00094 #define CR2_EXTTRIG_SWSTART_Reset   ((uint32_t)0xFFAFFFFF)
00095 
00096 /* ADC JEXTSEL mask */
00097 #define CR2_JEXTSEL_Reset           ((uint32_t)0xFFFF8FFF)
00098 
00099 /* ADC JEXTTRIG mask */
00100 #define CR2_JEXTTRIG_Set            ((uint32_t)0x00008000)
00101 #define CR2_JEXTTRIG_Reset          ((uint32_t)0xFFFF7FFF)
00102 
00103 /* ADC JSWSTART mask */
00104 #define CR2_JSWSTART_Set            ((uint32_t)0x00200000)
00105 
00106 /* ADC injected software start mask */
00107 #define CR2_JEXTTRIG_JSWSTART_Set   ((uint32_t)0x00208000)
00108 #define CR2_JEXTTRIG_JSWSTART_Reset ((uint32_t)0xFFDF7FFF)
00109 
00110 /* ADC TSPD mask */
00111 #define CR2_TSVREFE_Set             ((uint32_t)0x00800000)
00112 #define CR2_TSVREFE_Reset           ((uint32_t)0xFF7FFFFF)
00113 
00114 /* CR2 register Mask */
00115 #define CR2_CLEAR_Mask              ((uint32_t)0xFFF1F7FD)
00116 
00117 /* ADC SQx mask */
00118 #define SQR3_SQ_Set                 ((uint32_t)0x0000001F)
00119 #define SQR2_SQ_Set                 ((uint32_t)0x0000001F)
00120 #define SQR1_SQ_Set                 ((uint32_t)0x0000001F)
00121 
00122 /* SQR1 register Mask */
00123 #define SQR1_CLEAR_Mask             ((uint32_t)0xFF0FFFFF)
00124 
00125 /* ADC JSQx mask */
00126 #define JSQR_JSQ_Set                ((uint32_t)0x0000001F)
00127 
00128 /* ADC JL mask */
00129 #define JSQR_JL_Set                 ((uint32_t)0x00300000)
00130 #define JSQR_JL_Reset               ((uint32_t)0xFFCFFFFF)
00131 
00132 /* ADC SMPx mask */
00133 #define SMPR1_SMP_Set               ((uint32_t)0x00000007)
00134 #define SMPR2_SMP_Set               ((uint32_t)0x00000007)
00135 
00136 /* ADC JDRx registers offset */
00137 #define JDR_Offset                  ((uint8_t)0x28)
00138 
00139 /* ADC1 DR register base address */
00140 #define DR_ADDRESS                  ((uint32_t)0x4001244C)
00141 
00179 void ADC_DeInit(ADC_TypeDef* ADCx)
00180 {
00181   /* Check the parameters */
00182   assert_param(IS_ADC_ALL_PERIPH(ADCx));
00183   
00184   if (ADCx == ADC1)
00185   {
00186     /* Enable ADC1 reset state */
00187     RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1, ENABLE);
00188     /* Release ADC1 from reset state */
00189     RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1, DISABLE);
00190   }
00191   else if (ADCx == ADC2)
00192   {
00193     /* Enable ADC2 reset state */
00194     RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC2, ENABLE);
00195     /* Release ADC2 from reset state */
00196     RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC2, DISABLE);
00197   }
00198   else
00199   {
00200     if (ADCx == ADC3)
00201     {
00202       /* Enable ADC3 reset state */
00203       RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC3, ENABLE);
00204       /* Release ADC3 from reset state */
00205       RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC3, DISABLE);
00206     }
00207   }
00208 }
00209 
00218 void ADC_Init(ADC_TypeDef* ADCx, ADC_InitTypeDef* ADC_InitStruct)
00219 {
00220   uint32_t tmpreg1 = 0;
00221   uint8_t tmpreg2 = 0;
00222   /* Check the parameters */
00223   assert_param(IS_ADC_ALL_PERIPH(ADCx));
00224   assert_param(IS_ADC_MODE(ADC_InitStruct->ADC_Mode));
00225   assert_param(IS_FUNCTIONAL_STATE(ADC_InitStruct->ADC_ScanConvMode));
00226   assert_param(IS_FUNCTIONAL_STATE(ADC_InitStruct->ADC_ContinuousConvMode));
00227   assert_param(IS_ADC_EXT_TRIG(ADC_InitStruct->ADC_ExternalTrigConv));   
00228   assert_param(IS_ADC_DATA_ALIGN(ADC_InitStruct->ADC_DataAlign)); 
00229   assert_param(IS_ADC_REGULAR_LENGTH(ADC_InitStruct->ADC_NbrOfChannel));
00230 
00231   /*---------------------------- ADCx CR1 Configuration -----------------*/
00232   /* Get the ADCx CR1 value */
00233   tmpreg1 = ADCx->CR1;
00234   /* Clear DUALMOD and SCAN bits */
00235   tmpreg1 &= CR1_CLEAR_Mask;
00236   /* Configure ADCx: Dual mode and scan conversion mode */
00237   /* Set DUALMOD bits according to ADC_Mode value */
00238   /* Set SCAN bit according to ADC_ScanConvMode value */
00239   tmpreg1 |= (uint32_t)(ADC_InitStruct->ADC_Mode | ((uint32_t)ADC_InitStruct->ADC_ScanConvMode << 8));
00240   /* Write to ADCx CR1 */
00241   ADCx->CR1 = tmpreg1;
00242 
00243   /*---------------------------- ADCx CR2 Configuration -----------------*/
00244   /* Get the ADCx CR2 value */
00245   tmpreg1 = ADCx->CR2;
00246   /* Clear CONT, ALIGN and EXTSEL bits */
00247   tmpreg1 &= CR2_CLEAR_Mask;
00248   /* Configure ADCx: external trigger event and continuous conversion mode */
00249   /* Set ALIGN bit according to ADC_DataAlign value */
00250   /* Set EXTSEL bits according to ADC_ExternalTrigConv value */
00251   /* Set CONT bit according to ADC_ContinuousConvMode value */
00252   tmpreg1 |= (uint32_t)(ADC_InitStruct->ADC_DataAlign | ADC_InitStruct->ADC_ExternalTrigConv |
00253             ((uint32_t)ADC_InitStruct->ADC_ContinuousConvMode << 1));
00254   /* Write to ADCx CR2 */
00255   ADCx->CR2 = tmpreg1;
00256 
00257   /*---------------------------- ADCx SQR1 Configuration -----------------*/
00258   /* Get the ADCx SQR1 value */
00259   tmpreg1 = ADCx->SQR1;
00260   /* Clear L bits */
00261   tmpreg1 &= SQR1_CLEAR_Mask;
00262   /* Configure ADCx: regular channel sequence length */
00263   /* Set L bits according to ADC_NbrOfChannel value */
00264   tmpreg2 |= (uint8_t) (ADC_InitStruct->ADC_NbrOfChannel - (uint8_t)1);
00265   tmpreg1 |= (uint32_t)tmpreg2 << 20;
00266   /* Write to ADCx SQR1 */
00267   ADCx->SQR1 = tmpreg1;
00268 }
00269 
00275 void ADC_StructInit(ADC_InitTypeDef* ADC_InitStruct)
00276 {
00277   /* Reset ADC init structure parameters values */
00278   /* Initialize the ADC_Mode member */
00279   ADC_InitStruct->ADC_Mode = ADC_Mode_Independent;
00280   /* initialize the ADC_ScanConvMode member */
00281   ADC_InitStruct->ADC_ScanConvMode = DISABLE;
00282   /* Initialize the ADC_ContinuousConvMode member */
00283   ADC_InitStruct->ADC_ContinuousConvMode = DISABLE;
00284   /* Initialize the ADC_ExternalTrigConv member */
00285   ADC_InitStruct->ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
00286   /* Initialize the ADC_DataAlign member */
00287   ADC_InitStruct->ADC_DataAlign = ADC_DataAlign_Right;
00288   /* Initialize the ADC_NbrOfChannel member */
00289   ADC_InitStruct->ADC_NbrOfChannel = 1;
00290 }
00291 
00299 void ADC_Cmd(ADC_TypeDef* ADCx, FunctionalState NewState)
00300 {
00301   /* Check the parameters */
00302   assert_param(IS_ADC_ALL_PERIPH(ADCx));
00303   assert_param(IS_FUNCTIONAL_STATE(NewState));
00304   if (NewState != DISABLE)
00305   {
00306     /* Set the ADON bit to wake up the ADC from power down mode */
00307     ADCx->CR2 |= CR2_ADON_Set;
00308   }
00309   else
00310   {
00311     /* Disable the selected ADC peripheral */
00312     ADCx->CR2 &= CR2_ADON_Reset;
00313   }
00314 }
00315 
00324 void ADC_DMACmd(ADC_TypeDef* ADCx, FunctionalState NewState)
00325 {
00326   /* Check the parameters */
00327   assert_param(IS_ADC_DMA_PERIPH(ADCx));
00328   assert_param(IS_FUNCTIONAL_STATE(NewState));
00329   if (NewState != DISABLE)
00330   {
00331     /* Enable the selected ADC DMA request */
00332     ADCx->CR2 |= CR2_DMA_Set;
00333   }
00334   else
00335   {
00336     /* Disable the selected ADC DMA request */
00337     ADCx->CR2 &= CR2_DMA_Reset;
00338   }
00339 }
00340 
00353 void ADC_ITConfig(ADC_TypeDef* ADCx, uint16_t ADC_IT, FunctionalState NewState)
00354 {
00355   uint8_t itmask = 0;
00356   /* Check the parameters */
00357   assert_param(IS_ADC_ALL_PERIPH(ADCx));
00358   assert_param(IS_FUNCTIONAL_STATE(NewState));
00359   assert_param(IS_ADC_IT(ADC_IT));
00360   /* Get the ADC IT index */
00361   itmask = (uint8_t)ADC_IT;
00362   if (NewState != DISABLE)
00363   {
00364     /* Enable the selected ADC interrupts */
00365     ADCx->CR1 |= itmask;
00366   }
00367   else
00368   {
00369     /* Disable the selected ADC interrupts */
00370     ADCx->CR1 &= (~(uint32_t)itmask);
00371   }
00372 }
00373 
00379 void ADC_ResetCalibration(ADC_TypeDef* ADCx)
00380 {
00381   /* Check the parameters */
00382   assert_param(IS_ADC_ALL_PERIPH(ADCx));
00383   /* Resets the selected ADC calibration registers */  
00384   ADCx->CR2 |= CR2_RSTCAL_Set;
00385 }
00386 
00392 FlagStatus ADC_GetResetCalibrationStatus(ADC_TypeDef* ADCx)
00393 {
00394   FlagStatus bitstatus = RESET;
00395   /* Check the parameters */
00396   assert_param(IS_ADC_ALL_PERIPH(ADCx));
00397   /* Check the status of RSTCAL bit */
00398   if ((ADCx->CR2 & CR2_RSTCAL_Set) != (uint32_t)RESET)
00399   {
00400     /* RSTCAL bit is set */
00401     bitstatus = SET;
00402   }
00403   else
00404   {
00405     /* RSTCAL bit is reset */
00406     bitstatus = RESET;
00407   }
00408   /* Return the RSTCAL bit status */
00409   return  bitstatus;
00410 }
00411 
00417 void ADC_StartCalibration(ADC_TypeDef* ADCx)
00418 {
00419   /* Check the parameters */
00420   assert_param(IS_ADC_ALL_PERIPH(ADCx));
00421   /* Enable the selected ADC calibration process */  
00422   ADCx->CR2 |= CR2_CAL_Set;
00423 }
00424 
00430 FlagStatus ADC_GetCalibrationStatus(ADC_TypeDef* ADCx)
00431 {
00432   FlagStatus bitstatus = RESET;
00433   /* Check the parameters */
00434   assert_param(IS_ADC_ALL_PERIPH(ADCx));
00435   /* Check the status of CAL bit */
00436   if ((ADCx->CR2 & CR2_CAL_Set) != (uint32_t)RESET)
00437   {
00438     /* CAL bit is set: calibration on going */
00439     bitstatus = SET;
00440   }
00441   else
00442   {
00443     /* CAL bit is reset: end of calibration */
00444     bitstatus = RESET;
00445   }
00446   /* Return the CAL bit status */
00447   return  bitstatus;
00448 }
00449 
00457 void ADC_SoftwareStartConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
00458 {
00459   /* Check the parameters */
00460   assert_param(IS_ADC_ALL_PERIPH(ADCx));
00461   assert_param(IS_FUNCTIONAL_STATE(NewState));
00462   if (NewState != DISABLE)
00463   {
00464     /* Enable the selected ADC conversion on external event and start the selected
00465        ADC conversion */
00466     ADCx->CR2 |= CR2_EXTTRIG_SWSTART_Set;
00467   }
00468   else
00469   {
00470     /* Disable the selected ADC conversion on external event and stop the selected
00471        ADC conversion */
00472     ADCx->CR2 &= CR2_EXTTRIG_SWSTART_Reset;
00473   }
00474 }
00475 
00481 FlagStatus ADC_GetSoftwareStartConvStatus(ADC_TypeDef* ADCx)
00482 {
00483   FlagStatus bitstatus = RESET;
00484   /* Check the parameters */
00485   assert_param(IS_ADC_ALL_PERIPH(ADCx));
00486   /* Check the status of SWSTART bit */
00487   if ((ADCx->CR2 & CR2_SWSTART_Set) != (uint32_t)RESET)
00488   {
00489     /* SWSTART bit is set */
00490     bitstatus = SET;
00491   }
00492   else
00493   {
00494     /* SWSTART bit is reset */
00495     bitstatus = RESET;
00496   }
00497   /* Return the SWSTART bit status */
00498   return  bitstatus;
00499 }
00500 
00509 void ADC_DiscModeChannelCountConfig(ADC_TypeDef* ADCx, uint8_t Number)
00510 {
00511   uint32_t tmpreg1 = 0;
00512   uint32_t tmpreg2 = 0;
00513   /* Check the parameters */
00514   assert_param(IS_ADC_ALL_PERIPH(ADCx));
00515   assert_param(IS_ADC_REGULAR_DISC_NUMBER(Number));
00516   /* Get the old register value */
00517   tmpreg1 = ADCx->CR1;
00518   /* Clear the old discontinuous mode channel count */
00519   tmpreg1 &= CR1_DISCNUM_Reset;
00520   /* Set the discontinuous mode channel count */
00521   tmpreg2 = Number - 1;
00522   tmpreg1 |= tmpreg2 << 13;
00523   /* Store the new register value */
00524   ADCx->CR1 = tmpreg1;
00525 }
00526 
00536 void ADC_DiscModeCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
00537 {
00538   /* Check the parameters */
00539   assert_param(IS_ADC_ALL_PERIPH(ADCx));
00540   assert_param(IS_FUNCTIONAL_STATE(NewState));
00541   if (NewState != DISABLE)
00542   {
00543     /* Enable the selected ADC regular discontinuous mode */
00544     ADCx->CR1 |= CR1_DISCEN_Set;
00545   }
00546   else
00547   {
00548     /* Disable the selected ADC regular discontinuous mode */
00549     ADCx->CR1 &= CR1_DISCEN_Reset;
00550   }
00551 }
00552 
00590 void ADC_RegularChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel, uint8_t Rank, uint8_t ADC_SampleTime)
00591 {
00592   uint32_t tmpreg1 = 0, tmpreg2 = 0;
00593   /* Check the parameters */
00594   assert_param(IS_ADC_ALL_PERIPH(ADCx));
00595   assert_param(IS_ADC_CHANNEL(ADC_Channel));
00596   assert_param(IS_ADC_REGULAR_RANK(Rank));
00597   assert_param(IS_ADC_SAMPLE_TIME(ADC_SampleTime));
00598   /* if ADC_Channel_10 ... ADC_Channel_17 is selected */
00599   if (ADC_Channel > ADC_Channel_9)
00600   {
00601     /* Get the old register value */
00602     tmpreg1 = ADCx->SMPR1;
00603     /* Calculate the mask to clear */
00604     tmpreg2 = SMPR1_SMP_Set << (3 * (ADC_Channel - 10));
00605     /* Clear the old channel sample time */
00606     tmpreg1 &= ~tmpreg2;
00607     /* Calculate the mask to set */
00608     tmpreg2 = (uint32_t)ADC_SampleTime << (3 * (ADC_Channel - 10));
00609     /* Set the new channel sample time */
00610     tmpreg1 |= tmpreg2;
00611     /* Store the new register value */
00612     ADCx->SMPR1 = tmpreg1;
00613   }
00614   else /* ADC_Channel include in ADC_Channel_[0..9] */
00615   {
00616     /* Get the old register value */
00617     tmpreg1 = ADCx->SMPR2;
00618     /* Calculate the mask to clear */
00619     tmpreg2 = SMPR2_SMP_Set << (3 * ADC_Channel);
00620     /* Clear the old channel sample time */
00621     tmpreg1 &= ~tmpreg2;
00622     /* Calculate the mask to set */
00623     tmpreg2 = (uint32_t)ADC_SampleTime << (3 * ADC_Channel);
00624     /* Set the new channel sample time */
00625     tmpreg1 |= tmpreg2;
00626     /* Store the new register value */
00627     ADCx->SMPR2 = tmpreg1;
00628   }
00629   /* For Rank 1 to 6 */
00630   if (Rank < 7)
00631   {
00632     /* Get the old register value */
00633     tmpreg1 = ADCx->SQR3;
00634     /* Calculate the mask to clear */
00635     tmpreg2 = SQR3_SQ_Set << (5 * (Rank - 1));
00636     /* Clear the old SQx bits for the selected rank */
00637     tmpreg1 &= ~tmpreg2;
00638     /* Calculate the mask to set */
00639     tmpreg2 = (uint32_t)ADC_Channel << (5 * (Rank - 1));
00640     /* Set the SQx bits for the selected rank */
00641     tmpreg1 |= tmpreg2;
00642     /* Store the new register value */
00643     ADCx->SQR3 = tmpreg1;
00644   }
00645   /* For Rank 7 to 12 */
00646   else if (Rank < 13)
00647   {
00648     /* Get the old register value */
00649     tmpreg1 = ADCx->SQR2;
00650     /* Calculate the mask to clear */
00651     tmpreg2 = SQR2_SQ_Set << (5 * (Rank - 7));
00652     /* Clear the old SQx bits for the selected rank */
00653     tmpreg1 &= ~tmpreg2;
00654     /* Calculate the mask to set */
00655     tmpreg2 = (uint32_t)ADC_Channel << (5 * (Rank - 7));
00656     /* Set the SQx bits for the selected rank */
00657     tmpreg1 |= tmpreg2;
00658     /* Store the new register value */
00659     ADCx->SQR2 = tmpreg1;
00660   }
00661   /* For Rank 13 to 16 */
00662   else
00663   {
00664     /* Get the old register value */
00665     tmpreg1 = ADCx->SQR1;
00666     /* Calculate the mask to clear */
00667     tmpreg2 = SQR1_SQ_Set << (5 * (Rank - 13));
00668     /* Clear the old SQx bits for the selected rank */
00669     tmpreg1 &= ~tmpreg2;
00670     /* Calculate the mask to set */
00671     tmpreg2 = (uint32_t)ADC_Channel << (5 * (Rank - 13));
00672     /* Set the SQx bits for the selected rank */
00673     tmpreg1 |= tmpreg2;
00674     /* Store the new register value */
00675     ADCx->SQR1 = tmpreg1;
00676   }
00677 }
00678 
00686 void ADC_ExternalTrigConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
00687 {
00688   /* Check the parameters */
00689   assert_param(IS_ADC_ALL_PERIPH(ADCx));
00690   assert_param(IS_FUNCTIONAL_STATE(NewState));
00691   if (NewState != DISABLE)
00692   {
00693     /* Enable the selected ADC conversion on external event */
00694     ADCx->CR2 |= CR2_EXTTRIG_Set;
00695   }
00696   else
00697   {
00698     /* Disable the selected ADC conversion on external event */
00699     ADCx->CR2 &= CR2_EXTTRIG_Reset;
00700   }
00701 }
00702 
00708 uint16_t ADC_GetConversionValue(ADC_TypeDef* ADCx)
00709 {
00710   /* Check the parameters */
00711   assert_param(IS_ADC_ALL_PERIPH(ADCx));
00712   /* Return the selected ADC conversion value */
00713   return (uint16_t) ADCx->DR;
00714 }
00715 
00720 uint32_t ADC_GetDualModeConversionValue(void)
00721 {
00722   /* Return the dual mode conversion value */
00723   return (*(__IO uint32_t *) DR_ADDRESS);
00724 }
00725 
00734 void ADC_AutoInjectedConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
00735 {
00736   /* Check the parameters */
00737   assert_param(IS_ADC_ALL_PERIPH(ADCx));
00738   assert_param(IS_FUNCTIONAL_STATE(NewState));
00739   if (NewState != DISABLE)
00740   {
00741     /* Enable the selected ADC automatic injected group conversion */
00742     ADCx->CR1 |= CR1_JAUTO_Set;
00743   }
00744   else
00745   {
00746     /* Disable the selected ADC automatic injected group conversion */
00747     ADCx->CR1 &= CR1_JAUTO_Reset;
00748   }
00749 }
00750 
00760 void ADC_InjectedDiscModeCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
00761 {
00762   /* Check the parameters */
00763   assert_param(IS_ADC_ALL_PERIPH(ADCx));
00764   assert_param(IS_FUNCTIONAL_STATE(NewState));
00765   if (NewState != DISABLE)
00766   {
00767     /* Enable the selected ADC injected discontinuous mode */
00768     ADCx->CR1 |= CR1_JDISCEN_Set;
00769   }
00770   else
00771   {
00772     /* Disable the selected ADC injected discontinuous mode */
00773     ADCx->CR1 &= CR1_JDISCEN_Reset;
00774   }
00775 }
00776 
00799 void ADC_ExternalTrigInjectedConvConfig(ADC_TypeDef* ADCx, uint32_t ADC_ExternalTrigInjecConv)
00800 {
00801   uint32_t tmpreg = 0;
00802   /* Check the parameters */
00803   assert_param(IS_ADC_ALL_PERIPH(ADCx));
00804   assert_param(IS_ADC_EXT_INJEC_TRIG(ADC_ExternalTrigInjecConv));
00805   /* Get the old register value */
00806   tmpreg = ADCx->CR2;
00807   /* Clear the old external event selection for injected group */
00808   tmpreg &= CR2_JEXTSEL_Reset;
00809   /* Set the external event selection for injected group */
00810   tmpreg |= ADC_ExternalTrigInjecConv;
00811   /* Store the new register value */
00812   ADCx->CR2 = tmpreg;
00813 }
00814 
00824 void ADC_ExternalTrigInjectedConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
00825 {
00826   /* Check the parameters */
00827   assert_param(IS_ADC_ALL_PERIPH(ADCx));
00828   assert_param(IS_FUNCTIONAL_STATE(NewState));
00829   if (NewState != DISABLE)
00830   {
00831     /* Enable the selected ADC external event selection for injected group */
00832     ADCx->CR2 |= CR2_JEXTTRIG_Set;
00833   }
00834   else
00835   {
00836     /* Disable the selected ADC external event selection for injected group */
00837     ADCx->CR2 &= CR2_JEXTTRIG_Reset;
00838   }
00839 }
00840 
00849 void ADC_SoftwareStartInjectedConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
00850 {
00851   /* Check the parameters */
00852   assert_param(IS_ADC_ALL_PERIPH(ADCx));
00853   assert_param(IS_FUNCTIONAL_STATE(NewState));
00854   if (NewState != DISABLE)
00855   {
00856     /* Enable the selected ADC conversion for injected group on external event and start the selected
00857        ADC injected conversion */
00858     ADCx->CR2 |= CR2_JEXTTRIG_JSWSTART_Set;
00859   }
00860   else
00861   {
00862     /* Disable the selected ADC conversion on external event for injected group and stop the selected
00863        ADC injected conversion */
00864     ADCx->CR2 &= CR2_JEXTTRIG_JSWSTART_Reset;
00865   }
00866 }
00867 
00873 FlagStatus ADC_GetSoftwareStartInjectedConvCmdStatus(ADC_TypeDef* ADCx)
00874 {
00875   FlagStatus bitstatus = RESET;
00876   /* Check the parameters */
00877   assert_param(IS_ADC_ALL_PERIPH(ADCx));
00878   /* Check the status of JSWSTART bit */
00879   if ((ADCx->CR2 & CR2_JSWSTART_Set) != (uint32_t)RESET)
00880   {
00881     /* JSWSTART bit is set */
00882     bitstatus = SET;
00883   }
00884   else
00885   {
00886     /* JSWSTART bit is reset */
00887     bitstatus = RESET;
00888   }
00889   /* Return the JSWSTART bit status */
00890   return  bitstatus;
00891 }
00892 
00930 void ADC_InjectedChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel, uint8_t Rank, uint8_t ADC_SampleTime)
00931 {
00932   uint32_t tmpreg1 = 0, tmpreg2 = 0, tmpreg3 = 0;
00933   /* Check the parameters */
00934   assert_param(IS_ADC_ALL_PERIPH(ADCx));
00935   assert_param(IS_ADC_CHANNEL(ADC_Channel));
00936   assert_param(IS_ADC_INJECTED_RANK(Rank));
00937   assert_param(IS_ADC_SAMPLE_TIME(ADC_SampleTime));
00938   /* if ADC_Channel_10 ... ADC_Channel_17 is selected */
00939   if (ADC_Channel > ADC_Channel_9)
00940   {
00941     /* Get the old register value */
00942     tmpreg1 = ADCx->SMPR1;
00943     /* Calculate the mask to clear */
00944     tmpreg2 = SMPR1_SMP_Set << (3*(ADC_Channel - 10));
00945     /* Clear the old channel sample time */
00946     tmpreg1 &= ~tmpreg2;
00947     /* Calculate the mask to set */
00948     tmpreg2 = (uint32_t)ADC_SampleTime << (3*(ADC_Channel - 10));
00949     /* Set the new channel sample time */
00950     tmpreg1 |= tmpreg2;
00951     /* Store the new register value */
00952     ADCx->SMPR1 = tmpreg1;
00953   }
00954   else /* ADC_Channel include in ADC_Channel_[0..9] */
00955   {
00956     /* Get the old register value */
00957     tmpreg1 = ADCx->SMPR2;
00958     /* Calculate the mask to clear */
00959     tmpreg2 = SMPR2_SMP_Set << (3 * ADC_Channel);
00960     /* Clear the old channel sample time */
00961     tmpreg1 &= ~tmpreg2;
00962     /* Calculate the mask to set */
00963     tmpreg2 = (uint32_t)ADC_SampleTime << (3 * ADC_Channel);
00964     /* Set the new channel sample time */
00965     tmpreg1 |= tmpreg2;
00966     /* Store the new register value */
00967     ADCx->SMPR2 = tmpreg1;
00968   }
00969   /* Rank configuration */
00970   /* Get the old register value */
00971   tmpreg1 = ADCx->JSQR;
00972   /* Get JL value: Number = JL+1 */
00973   tmpreg3 =  (tmpreg1 & JSQR_JL_Set)>> 20;
00974   /* Calculate the mask to clear: ((Rank-1)+(4-JL-1)) */
00975   tmpreg2 = JSQR_JSQ_Set << (5 * (uint8_t)((Rank + 3) - (tmpreg3 + 1)));
00976   /* Clear the old JSQx bits for the selected rank */
00977   tmpreg1 &= ~tmpreg2;
00978   /* Calculate the mask to set: ((Rank-1)+(4-JL-1)) */
00979   tmpreg2 = (uint32_t)ADC_Channel << (5 * (uint8_t)((Rank + 3) - (tmpreg3 + 1)));
00980   /* Set the JSQx bits for the selected rank */
00981   tmpreg1 |= tmpreg2;
00982   /* Store the new register value */
00983   ADCx->JSQR = tmpreg1;
00984 }
00985 
00993 void ADC_InjectedSequencerLengthConfig(ADC_TypeDef* ADCx, uint8_t Length)
00994 {
00995   uint32_t tmpreg1 = 0;
00996   uint32_t tmpreg2 = 0;
00997   /* Check the parameters */
00998   assert_param(IS_ADC_ALL_PERIPH(ADCx));
00999   assert_param(IS_ADC_INJECTED_LENGTH(Length));
01000   
01001   /* Get the old register value */
01002   tmpreg1 = ADCx->JSQR;
01003   /* Clear the old injected sequnence lenght JL bits */
01004   tmpreg1 &= JSQR_JL_Reset;
01005   /* Set the injected sequnence lenght JL bits */
01006   tmpreg2 = Length - 1; 
01007   tmpreg1 |= tmpreg2 << 20;
01008   /* Store the new register value */
01009   ADCx->JSQR = tmpreg1;
01010 }
01011 
01025 void ADC_SetInjectedOffset(ADC_TypeDef* ADCx, uint8_t ADC_InjectedChannel, uint16_t Offset)
01026 {
01027   __IO uint32_t tmp = 0;
01028   
01029   /* Check the parameters */
01030   assert_param(IS_ADC_ALL_PERIPH(ADCx));
01031   assert_param(IS_ADC_INJECTED_CHANNEL(ADC_InjectedChannel));
01032   assert_param(IS_ADC_OFFSET(Offset));  
01033   
01034   tmp = (uint32_t)ADCx;
01035   tmp += ADC_InjectedChannel;
01036   
01037   /* Set the selected injected channel data offset */
01038   *(__IO uint32_t *) tmp = (uint32_t)Offset;
01039 }
01040 
01052 uint16_t ADC_GetInjectedConversionValue(ADC_TypeDef* ADCx, uint8_t ADC_InjectedChannel)
01053 {
01054   __IO uint32_t tmp = 0;
01055   
01056   /* Check the parameters */
01057   assert_param(IS_ADC_ALL_PERIPH(ADCx));
01058   assert_param(IS_ADC_INJECTED_CHANNEL(ADC_InjectedChannel));
01059 
01060   tmp = (uint32_t)ADCx;
01061   tmp += ADC_InjectedChannel + JDR_Offset;
01062   
01063   /* Returns the selected injected channel conversion data value */
01064   return (uint16_t) (*(__IO uint32_t*)  tmp);   
01065 }
01066 
01082 void ADC_AnalogWatchdogCmd(ADC_TypeDef* ADCx, uint32_t ADC_AnalogWatchdog)
01083 {
01084   uint32_t tmpreg = 0;
01085   /* Check the parameters */
01086   assert_param(IS_ADC_ALL_PERIPH(ADCx));
01087   assert_param(IS_ADC_ANALOG_WATCHDOG(ADC_AnalogWatchdog));
01088   /* Get the old register value */
01089   tmpreg = ADCx->CR1;
01090   /* Clear AWDEN, AWDENJ and AWDSGL bits */
01091   tmpreg &= CR1_AWDMode_Reset;
01092   /* Set the analog watchdog enable mode */
01093   tmpreg |= ADC_AnalogWatchdog;
01094   /* Store the new register value */
01095   ADCx->CR1 = tmpreg;
01096 }
01097 
01107 void ADC_AnalogWatchdogThresholdsConfig(ADC_TypeDef* ADCx, uint16_t HighThreshold,
01108                                         uint16_t LowThreshold)
01109 {
01110   /* Check the parameters */
01111   assert_param(IS_ADC_ALL_PERIPH(ADCx));
01112   assert_param(IS_ADC_THRESHOLD(HighThreshold));
01113   assert_param(IS_ADC_THRESHOLD(LowThreshold));
01114   /* Set the ADCx high threshold */
01115   ADCx->HTR = HighThreshold;
01116   /* Set the ADCx low threshold */
01117   ADCx->LTR = LowThreshold;
01118 }
01119 
01145 void ADC_AnalogWatchdogSingleChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel)
01146 {
01147   uint32_t tmpreg = 0;
01148   /* Check the parameters */
01149   assert_param(IS_ADC_ALL_PERIPH(ADCx));
01150   assert_param(IS_ADC_CHANNEL(ADC_Channel));
01151   /* Get the old register value */
01152   tmpreg = ADCx->CR1;
01153   /* Clear the Analog watchdog channel select bits */
01154   tmpreg &= CR1_AWDCH_Reset;
01155   /* Set the Analog watchdog channel */
01156   tmpreg |= ADC_Channel;
01157   /* Store the new register value */
01158   ADCx->CR1 = tmpreg;
01159 }
01160 
01167 void ADC_TempSensorVrefintCmd(FunctionalState NewState)
01168 {
01169   /* Check the parameters */
01170   assert_param(IS_FUNCTIONAL_STATE(NewState));
01171   if (NewState != DISABLE)
01172   {
01173     /* Enable the temperature sensor and Vrefint channel*/
01174     ADC1->CR2 |= CR2_TSVREFE_Set;
01175   }
01176   else
01177   {
01178     /* Disable the temperature sensor and Vrefint channel*/
01179     ADC1->CR2 &= CR2_TSVREFE_Reset;
01180   }
01181 }
01182 
01195 FlagStatus ADC_GetFlagStatus(ADC_TypeDef* ADCx, uint8_t ADC_FLAG)
01196 {
01197   FlagStatus bitstatus = RESET;
01198   /* Check the parameters */
01199   assert_param(IS_ADC_ALL_PERIPH(ADCx));
01200   assert_param(IS_ADC_GET_FLAG(ADC_FLAG));
01201   /* Check the status of the specified ADC flag */
01202   if ((ADCx->SR & ADC_FLAG) != (uint8_t)RESET)
01203   {
01204     /* ADC_FLAG is set */
01205     bitstatus = SET;
01206   }
01207   else
01208   {
01209     /* ADC_FLAG is reset */
01210     bitstatus = RESET;
01211   }
01212   /* Return the ADC_FLAG status */
01213   return  bitstatus;
01214 }
01215 
01228 void ADC_ClearFlag(ADC_TypeDef* ADCx, uint8_t ADC_FLAG)
01229 {
01230   /* Check the parameters */
01231   assert_param(IS_ADC_ALL_PERIPH(ADCx));
01232   assert_param(IS_ADC_CLEAR_FLAG(ADC_FLAG));
01233   /* Clear the selected ADC flags */
01234   ADCx->SR = ~(uint32_t)ADC_FLAG;
01235 }
01236 
01247 ITStatus ADC_GetITStatus(ADC_TypeDef* ADCx, uint16_t ADC_IT)
01248 {
01249   ITStatus bitstatus = RESET;
01250   uint32_t itmask = 0, enablestatus = 0;
01251   /* Check the parameters */
01252   assert_param(IS_ADC_ALL_PERIPH(ADCx));
01253   assert_param(IS_ADC_GET_IT(ADC_IT));
01254   /* Get the ADC IT index */
01255   itmask = ADC_IT >> 8;
01256   /* Get the ADC_IT enable bit status */
01257   enablestatus = (ADCx->CR1 & (uint8_t)ADC_IT) ;
01258   /* Check the status of the specified ADC interrupt */
01259   if (((ADCx->SR & itmask) != (uint32_t)RESET) && enablestatus)
01260   {
01261     /* ADC_IT is set */
01262     bitstatus = SET;
01263   }
01264   else
01265   {
01266     /* ADC_IT is reset */
01267     bitstatus = RESET;
01268   }
01269   /* Return the ADC_IT status */
01270   return  bitstatus;
01271 }
01272 
01283 void ADC_ClearITPendingBit(ADC_TypeDef* ADCx, uint16_t ADC_IT)
01284 {
01285   uint8_t itmask = 0;
01286   /* Check the parameters */
01287   assert_param(IS_ADC_ALL_PERIPH(ADCx));
01288   assert_param(IS_ADC_IT(ADC_IT));
01289   /* Get the ADC IT index */
01290   itmask = (uint8_t)(ADC_IT >> 8);
01291   /* Clear the selected ADC interrupt pending bits */
01292   ADCx->SR = ~(uint32_t)itmask;
01293 }
01294 
01307 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/