CARME-M4 BSP  V1.5
stm32f4xx_adc.c
Go to the documentation of this file.
1 
105 /* Includes ------------------------------------------------------------------*/
106 #include "stm32f4xx_adc.h"
107 #include "stm32f4xx_rcc.h"
108 
118 /* Private typedef -----------------------------------------------------------*/
119 /* Private define ------------------------------------------------------------*/
120 
121 /* ADC DISCNUM mask */
122 #define CR1_DISCNUM_RESET ((uint32_t)0xFFFF1FFF)
123 
124 /* ADC AWDCH mask */
125 #define CR1_AWDCH_RESET ((uint32_t)0xFFFFFFE0)
126 
127 /* ADC Analog watchdog enable mode mask */
128 #define CR1_AWDMode_RESET ((uint32_t)0xFF3FFDFF)
129 
130 /* CR1 register Mask */
131 #define CR1_CLEAR_MASK ((uint32_t)0xFCFFFEFF)
132 
133 /* ADC EXTEN mask */
134 #define CR2_EXTEN_RESET ((uint32_t)0xCFFFFFFF)
135 
136 /* ADC JEXTEN mask */
137 #define CR2_JEXTEN_RESET ((uint32_t)0xFFCFFFFF)
138 
139 /* ADC JEXTSEL mask */
140 #define CR2_JEXTSEL_RESET ((uint32_t)0xFFF0FFFF)
141 
142 /* CR2 register Mask */
143 #define CR2_CLEAR_MASK ((uint32_t)0xC0FFF7FD)
144 
145 /* ADC SQx mask */
146 #define SQR3_SQ_SET ((uint32_t)0x0000001F)
147 #define SQR2_SQ_SET ((uint32_t)0x0000001F)
148 #define SQR1_SQ_SET ((uint32_t)0x0000001F)
149 
150 /* ADC L Mask */
151 #define SQR1_L_RESET ((uint32_t)0xFF0FFFFF)
152 
153 /* ADC JSQx mask */
154 #define JSQR_JSQ_SET ((uint32_t)0x0000001F)
155 
156 /* ADC JL mask */
157 #define JSQR_JL_SET ((uint32_t)0x00300000)
158 #define JSQR_JL_RESET ((uint32_t)0xFFCFFFFF)
159 
160 /* ADC SMPx mask */
161 #define SMPR1_SMP_SET ((uint32_t)0x00000007)
162 #define SMPR2_SMP_SET ((uint32_t)0x00000007)
163 
164 /* ADC JDRx registers offset */
165 #define JDR_OFFSET ((uint8_t)0x28)
166 
167 /* ADC CDR register base address */
168 #define CDR_ADDRESS ((uint32_t)0x40012308)
169 
170 /* ADC CCR register Mask */
171 #define CR_CLEAR_MASK ((uint32_t)0xFFFC30E0)
172 
173 /* Private macro -------------------------------------------------------------*/
174 /* Private variables ---------------------------------------------------------*/
175 /* Private function prototypes -----------------------------------------------*/
176 /* Private functions ---------------------------------------------------------*/
177 
213 void ADC_DeInit(void)
214 {
215  /* Enable all ADCs reset state */
216  RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC, ENABLE);
217 
218  /* Release all ADCs from reset state */
219  RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC, DISABLE);
220 }
221 
235 void ADC_Init(ADC_TypeDef* ADCx, ADC_InitTypeDef* ADC_InitStruct)
236 {
237  uint32_t tmpreg1 = 0;
238  uint8_t tmpreg2 = 0;
239  /* Check the parameters */
240  assert_param(IS_ADC_ALL_PERIPH(ADCx));
241  assert_param(IS_ADC_RESOLUTION(ADC_InitStruct->ADC_Resolution));
242  assert_param(IS_FUNCTIONAL_STATE(ADC_InitStruct->ADC_ScanConvMode));
243  assert_param(IS_FUNCTIONAL_STATE(ADC_InitStruct->ADC_ContinuousConvMode));
244  assert_param(IS_ADC_EXT_TRIG_EDGE(ADC_InitStruct->ADC_ExternalTrigConvEdge));
245  assert_param(IS_ADC_EXT_TRIG(ADC_InitStruct->ADC_ExternalTrigConv));
246  assert_param(IS_ADC_DATA_ALIGN(ADC_InitStruct->ADC_DataAlign));
247  assert_param(IS_ADC_REGULAR_LENGTH(ADC_InitStruct->ADC_NbrOfConversion));
248 
249  /*---------------------------- ADCx CR1 Configuration -----------------*/
250  /* Get the ADCx CR1 value */
251  tmpreg1 = ADCx->CR1;
252 
253  /* Clear RES and SCAN bits */
254  tmpreg1 &= CR1_CLEAR_MASK;
255 
256  /* Configure ADCx: scan conversion mode and resolution */
257  /* Set SCAN bit according to ADC_ScanConvMode value */
258  /* Set RES bit according to ADC_Resolution value */
259  tmpreg1 |= (uint32_t)(((uint32_t)ADC_InitStruct->ADC_ScanConvMode << 8) | \
260  ADC_InitStruct->ADC_Resolution);
261  /* Write to ADCx CR1 */
262  ADCx->CR1 = tmpreg1;
263  /*---------------------------- ADCx CR2 Configuration -----------------*/
264  /* Get the ADCx CR2 value */
265  tmpreg1 = ADCx->CR2;
266 
267  /* Clear CONT, ALIGN, EXTEN and EXTSEL bits */
268  tmpreg1 &= CR2_CLEAR_MASK;
269 
270  /* Configure ADCx: external trigger event and edge, data alignment and
271  continuous conversion mode */
272  /* Set ALIGN bit according to ADC_DataAlign value */
273  /* Set EXTEN bits according to ADC_ExternalTrigConvEdge value */
274  /* Set EXTSEL bits according to ADC_ExternalTrigConv value */
275  /* Set CONT bit according to ADC_ContinuousConvMode value */
276  tmpreg1 |= (uint32_t)(ADC_InitStruct->ADC_DataAlign | \
277  ADC_InitStruct->ADC_ExternalTrigConv |
278  ADC_InitStruct->ADC_ExternalTrigConvEdge | \
279  ((uint32_t)ADC_InitStruct->ADC_ContinuousConvMode << 1));
280 
281  /* Write to ADCx CR2 */
282  ADCx->CR2 = tmpreg1;
283  /*---------------------------- ADCx SQR1 Configuration -----------------*/
284  /* Get the ADCx SQR1 value */
285  tmpreg1 = ADCx->SQR1;
286 
287  /* Clear L bits */
288  tmpreg1 &= SQR1_L_RESET;
289 
290  /* Configure ADCx: regular channel sequence length */
291  /* Set L bits according to ADC_NbrOfConversion value */
292  tmpreg2 |= (uint8_t)(ADC_InitStruct->ADC_NbrOfConversion - (uint8_t)1);
293  tmpreg1 |= ((uint32_t)tmpreg2 << 20);
294 
295  /* Write to ADCx SQR1 */
296  ADCx->SQR1 = tmpreg1;
297 }
298 
310 void ADC_StructInit(ADC_InitTypeDef* ADC_InitStruct)
311 {
312  /* Initialize the ADC_Mode member */
313  ADC_InitStruct->ADC_Resolution = ADC_Resolution_12b;
314 
315  /* initialize the ADC_ScanConvMode member */
316  ADC_InitStruct->ADC_ScanConvMode = DISABLE;
317 
318  /* Initialize the ADC_ContinuousConvMode member */
319  ADC_InitStruct->ADC_ContinuousConvMode = DISABLE;
320 
321  /* Initialize the ADC_ExternalTrigConvEdge member */
322  ADC_InitStruct->ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
323 
324  /* Initialize the ADC_ExternalTrigConv member */
325  ADC_InitStruct->ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
326 
327  /* Initialize the ADC_DataAlign member */
328  ADC_InitStruct->ADC_DataAlign = ADC_DataAlign_Right;
329 
330  /* Initialize the ADC_NbrOfConversion member */
331  ADC_InitStruct->ADC_NbrOfConversion = 1;
332 }
333 
341 void ADC_CommonInit(ADC_CommonInitTypeDef* ADC_CommonInitStruct)
342 {
343  uint32_t tmpreg1 = 0;
344  /* Check the parameters */
345  assert_param(IS_ADC_MODE(ADC_CommonInitStruct->ADC_Mode));
346  assert_param(IS_ADC_PRESCALER(ADC_CommonInitStruct->ADC_Prescaler));
347  assert_param(IS_ADC_DMA_ACCESS_MODE(ADC_CommonInitStruct->ADC_DMAAccessMode));
348  assert_param(IS_ADC_SAMPLING_DELAY(ADC_CommonInitStruct->ADC_TwoSamplingDelay));
349  /*---------------------------- ADC CCR Configuration -----------------*/
350  /* Get the ADC CCR value */
351  tmpreg1 = ADC->CCR;
352 
353  /* Clear MULTI, DELAY, DMA and ADCPRE bits */
354  tmpreg1 &= CR_CLEAR_MASK;
355 
356  /* Configure ADCx: Multi mode, Delay between two sampling time, ADC prescaler,
357  and DMA access mode for multimode */
358  /* Set MULTI bits according to ADC_Mode value */
359  /* Set ADCPRE bits according to ADC_Prescaler value */
360  /* Set DMA bits according to ADC_DMAAccessMode value */
361  /* Set DELAY bits according to ADC_TwoSamplingDelay value */
362  tmpreg1 |= (uint32_t)(ADC_CommonInitStruct->ADC_Mode |
363  ADC_CommonInitStruct->ADC_Prescaler |
364  ADC_CommonInitStruct->ADC_DMAAccessMode |
365  ADC_CommonInitStruct->ADC_TwoSamplingDelay);
366 
367  /* Write to ADC CCR */
368  ADC->CCR = tmpreg1;
369 }
370 
377 void ADC_CommonStructInit(ADC_CommonInitTypeDef* ADC_CommonInitStruct)
378 {
379  /* Initialize the ADC_Mode member */
380  ADC_CommonInitStruct->ADC_Mode = ADC_Mode_Independent;
381 
382  /* initialize the ADC_Prescaler member */
383  ADC_CommonInitStruct->ADC_Prescaler = ADC_Prescaler_Div2;
384 
385  /* Initialize the ADC_DMAAccessMode member */
386  ADC_CommonInitStruct->ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
387 
388  /* Initialize the ADC_TwoSamplingDelay member */
389  ADC_CommonInitStruct->ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
390 }
391 
399 void ADC_Cmd(ADC_TypeDef* ADCx, FunctionalState NewState)
400 {
401  /* Check the parameters */
402  assert_param(IS_ADC_ALL_PERIPH(ADCx));
403  assert_param(IS_FUNCTIONAL_STATE(NewState));
404  if (NewState != DISABLE)
405  {
406  /* Set the ADON bit to wake up the ADC from power down mode */
407  ADCx->CR2 |= (uint32_t)ADC_CR2_ADON;
408  }
409  else
410  {
411  /* Disable the selected ADC peripheral */
412  ADCx->CR2 &= (uint32_t)(~ADC_CR2_ADON);
413  }
414 }
455 void ADC_AnalogWatchdogCmd(ADC_TypeDef* ADCx, uint32_t ADC_AnalogWatchdog)
456 {
457  uint32_t tmpreg = 0;
458  /* Check the parameters */
459  assert_param(IS_ADC_ALL_PERIPH(ADCx));
460  assert_param(IS_ADC_ANALOG_WATCHDOG(ADC_AnalogWatchdog));
461 
462  /* Get the old register value */
463  tmpreg = ADCx->CR1;
464 
465  /* Clear AWDEN, JAWDEN and AWDSGL bits */
466  tmpreg &= CR1_AWDMode_RESET;
467 
468  /* Set the analog watchdog enable mode */
469  tmpreg |= ADC_AnalogWatchdog;
470 
471  /* Store the new register value */
472  ADCx->CR1 = tmpreg;
473 }
474 
484 void ADC_AnalogWatchdogThresholdsConfig(ADC_TypeDef* ADCx, uint16_t HighThreshold,
485  uint16_t LowThreshold)
486 {
487  /* Check the parameters */
488  assert_param(IS_ADC_ALL_PERIPH(ADCx));
489  assert_param(IS_ADC_THRESHOLD(HighThreshold));
490  assert_param(IS_ADC_THRESHOLD(LowThreshold));
491 
492  /* Set the ADCx high threshold */
493  ADCx->HTR = HighThreshold;
494 
495  /* Set the ADCx low threshold */
496  ADCx->LTR = LowThreshold;
497 }
498 
525 void ADC_AnalogWatchdogSingleChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel)
526 {
527  uint32_t tmpreg = 0;
528  /* Check the parameters */
529  assert_param(IS_ADC_ALL_PERIPH(ADCx));
530  assert_param(IS_ADC_CHANNEL(ADC_Channel));
531 
532  /* Get the old register value */
533  tmpreg = ADCx->CR1;
534 
535  /* Clear the Analog watchdog channel select bits */
536  tmpreg &= CR1_AWDCH_RESET;
537 
538  /* Set the Analog watchdog channel */
539  tmpreg |= ADC_Channel;
540 
541  /* Store the new register value */
542  ADCx->CR1 = tmpreg;
543 }
589 void ADC_TempSensorVrefintCmd(FunctionalState NewState)
590 {
591  /* Check the parameters */
592  assert_param(IS_FUNCTIONAL_STATE(NewState));
593  if (NewState != DISABLE)
594  {
595  /* Enable the temperature sensor and Vrefint channel*/
596  ADC->CCR |= (uint32_t)ADC_CCR_TSVREFE;
597  }
598  else
599  {
600  /* Disable the temperature sensor and Vrefint channel*/
601  ADC->CCR &= (uint32_t)(~ADC_CCR_TSVREFE);
602  }
603 }
604 
615 void ADC_VBATCmd(FunctionalState NewState)
616 {
617  /* Check the parameters */
618  assert_param(IS_FUNCTIONAL_STATE(NewState));
619  if (NewState != DISABLE)
620  {
621  /* Enable the VBAT channel*/
622  ADC->CCR |= (uint32_t)ADC_CCR_VBATE;
623  }
624  else
625  {
626  /* Disable the VBAT channel*/
627  ADC->CCR &= (uint32_t)(~ADC_CCR_VBATE);
628  }
629 }
630 
715 void ADC_RegularChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel, uint8_t Rank, uint8_t ADC_SampleTime)
716 {
717  uint32_t tmpreg1 = 0, tmpreg2 = 0;
718  /* Check the parameters */
719  assert_param(IS_ADC_ALL_PERIPH(ADCx));
720  assert_param(IS_ADC_CHANNEL(ADC_Channel));
721  assert_param(IS_ADC_REGULAR_RANK(Rank));
722  assert_param(IS_ADC_SAMPLE_TIME(ADC_SampleTime));
723 
724  /* if ADC_Channel_10 ... ADC_Channel_18 is selected */
725  if (ADC_Channel > ADC_Channel_9)
726  {
727  /* Get the old register value */
728  tmpreg1 = ADCx->SMPR1;
729 
730  /* Calculate the mask to clear */
731  tmpreg2 = SMPR1_SMP_SET << (3 * (ADC_Channel - 10));
732 
733  /* Clear the old sample time */
734  tmpreg1 &= ~tmpreg2;
735 
736  /* Calculate the mask to set */
737  tmpreg2 = (uint32_t)ADC_SampleTime << (3 * (ADC_Channel - 10));
738 
739  /* Set the new sample time */
740  tmpreg1 |= tmpreg2;
741 
742  /* Store the new register value */
743  ADCx->SMPR1 = tmpreg1;
744  }
745  else /* ADC_Channel include in ADC_Channel_[0..9] */
746  {
747  /* Get the old register value */
748  tmpreg1 = ADCx->SMPR2;
749 
750  /* Calculate the mask to clear */
751  tmpreg2 = SMPR2_SMP_SET << (3 * ADC_Channel);
752 
753  /* Clear the old sample time */
754  tmpreg1 &= ~tmpreg2;
755 
756  /* Calculate the mask to set */
757  tmpreg2 = (uint32_t)ADC_SampleTime << (3 * ADC_Channel);
758 
759  /* Set the new sample time */
760  tmpreg1 |= tmpreg2;
761 
762  /* Store the new register value */
763  ADCx->SMPR2 = tmpreg1;
764  }
765  /* For Rank 1 to 6 */
766  if (Rank < 7)
767  {
768  /* Get the old register value */
769  tmpreg1 = ADCx->SQR3;
770 
771  /* Calculate the mask to clear */
772  tmpreg2 = SQR3_SQ_SET << (5 * (Rank - 1));
773 
774  /* Clear the old SQx bits for the selected rank */
775  tmpreg1 &= ~tmpreg2;
776 
777  /* Calculate the mask to set */
778  tmpreg2 = (uint32_t)ADC_Channel << (5 * (Rank - 1));
779 
780  /* Set the SQx bits for the selected rank */
781  tmpreg1 |= tmpreg2;
782 
783  /* Store the new register value */
784  ADCx->SQR3 = tmpreg1;
785  }
786  /* For Rank 7 to 12 */
787  else if (Rank < 13)
788  {
789  /* Get the old register value */
790  tmpreg1 = ADCx->SQR2;
791 
792  /* Calculate the mask to clear */
793  tmpreg2 = SQR2_SQ_SET << (5 * (Rank - 7));
794 
795  /* Clear the old SQx bits for the selected rank */
796  tmpreg1 &= ~tmpreg2;
797 
798  /* Calculate the mask to set */
799  tmpreg2 = (uint32_t)ADC_Channel << (5 * (Rank - 7));
800 
801  /* Set the SQx bits for the selected rank */
802  tmpreg1 |= tmpreg2;
803 
804  /* Store the new register value */
805  ADCx->SQR2 = tmpreg1;
806  }
807  /* For Rank 13 to 16 */
808  else
809  {
810  /* Get the old register value */
811  tmpreg1 = ADCx->SQR1;
812 
813  /* Calculate the mask to clear */
814  tmpreg2 = SQR1_SQ_SET << (5 * (Rank - 13));
815 
816  /* Clear the old SQx bits for the selected rank */
817  tmpreg1 &= ~tmpreg2;
818 
819  /* Calculate the mask to set */
820  tmpreg2 = (uint32_t)ADC_Channel << (5 * (Rank - 13));
821 
822  /* Set the SQx bits for the selected rank */
823  tmpreg1 |= tmpreg2;
824 
825  /* Store the new register value */
826  ADCx->SQR1 = tmpreg1;
827  }
828 }
829 
835 void ADC_SoftwareStartConv(ADC_TypeDef* ADCx)
836 {
837  /* Check the parameters */
838  assert_param(IS_ADC_ALL_PERIPH(ADCx));
839 
840  /* Enable the selected ADC conversion for regular group */
841  ADCx->CR2 |= (uint32_t)ADC_CR2_SWSTART;
842 }
843 
849 FlagStatus ADC_GetSoftwareStartConvStatus(ADC_TypeDef* ADCx)
850 {
851  FlagStatus bitstatus = RESET;
852  /* Check the parameters */
853  assert_param(IS_ADC_ALL_PERIPH(ADCx));
854 
855  /* Check the status of SWSTART bit */
856  if ((ADCx->CR2 & ADC_CR2_SWSTART) != (uint32_t)RESET)
857  {
858  /* SWSTART bit is set */
859  bitstatus = SET;
860  }
861  else
862  {
863  /* SWSTART bit is reset */
864  bitstatus = RESET;
865  }
866 
867  /* Return the SWSTART bit status */
868  return bitstatus;
869 }
870 
871 
879 void ADC_EOCOnEachRegularChannelCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
880 {
881  /* Check the parameters */
882  assert_param(IS_ADC_ALL_PERIPH(ADCx));
883  assert_param(IS_FUNCTIONAL_STATE(NewState));
884 
885  if (NewState != DISABLE)
886  {
887  /* Enable the selected ADC EOC rising on each regular channel conversion */
888  ADCx->CR2 |= (uint32_t)ADC_CR2_EOCS;
889  }
890  else
891  {
892  /* Disable the selected ADC EOC rising on each regular channel conversion */
893  ADCx->CR2 &= (uint32_t)(~ADC_CR2_EOCS);
894  }
895 }
896 
904 void ADC_ContinuousModeCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
905 {
906  /* Check the parameters */
907  assert_param(IS_ADC_ALL_PERIPH(ADCx));
908  assert_param(IS_FUNCTIONAL_STATE(NewState));
909 
910  if (NewState != DISABLE)
911  {
912  /* Enable the selected ADC continuous conversion mode */
913  ADCx->CR2 |= (uint32_t)ADC_CR2_CONT;
914  }
915  else
916  {
917  /* Disable the selected ADC continuous conversion mode */
918  ADCx->CR2 &= (uint32_t)(~ADC_CR2_CONT);
919  }
920 }
921 
930 void ADC_DiscModeChannelCountConfig(ADC_TypeDef* ADCx, uint8_t Number)
931 {
932  uint32_t tmpreg1 = 0;
933  uint32_t tmpreg2 = 0;
934 
935  /* Check the parameters */
936  assert_param(IS_ADC_ALL_PERIPH(ADCx));
937  assert_param(IS_ADC_REGULAR_DISC_NUMBER(Number));
938 
939  /* Get the old register value */
940  tmpreg1 = ADCx->CR1;
941 
942  /* Clear the old discontinuous mode channel count */
943  tmpreg1 &= CR1_DISCNUM_RESET;
944 
945  /* Set the discontinuous mode channel count */
946  tmpreg2 = Number - 1;
947  tmpreg1 |= tmpreg2 << 13;
948 
949  /* Store the new register value */
950  ADCx->CR1 = tmpreg1;
951 }
952 
962 void ADC_DiscModeCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
963 {
964  /* Check the parameters */
965  assert_param(IS_ADC_ALL_PERIPH(ADCx));
966  assert_param(IS_FUNCTIONAL_STATE(NewState));
967 
968  if (NewState != DISABLE)
969  {
970  /* Enable the selected ADC regular discontinuous mode */
971  ADCx->CR1 |= (uint32_t)ADC_CR1_DISCEN;
972  }
973  else
974  {
975  /* Disable the selected ADC regular discontinuous mode */
976  ADCx->CR1 &= (uint32_t)(~ADC_CR1_DISCEN);
977  }
978 }
979 
985 uint16_t ADC_GetConversionValue(ADC_TypeDef* ADCx)
986 {
987  /* Check the parameters */
988  assert_param(IS_ADC_ALL_PERIPH(ADCx));
989 
990  /* Return the selected ADC conversion value */
991  return (uint16_t) ADCx->DR;
992 }
993 
1007 {
1008  /* Return the multi mode conversion value */
1009  return (*(__IO uint32_t *) CDR_ADDRESS);
1010 }
1052 void ADC_DMACmd(ADC_TypeDef* ADCx, FunctionalState NewState)
1053 {
1054  /* Check the parameters */
1055  assert_param(IS_ADC_ALL_PERIPH(ADCx));
1056  assert_param(IS_FUNCTIONAL_STATE(NewState));
1057  if (NewState != DISABLE)
1058  {
1059  /* Enable the selected ADC DMA request */
1060  ADCx->CR2 |= (uint32_t)ADC_CR2_DMA;
1061  }
1062  else
1063  {
1064  /* Disable the selected ADC DMA request */
1065  ADCx->CR2 &= (uint32_t)(~ADC_CR2_DMA);
1066  }
1067 }
1068 
1076 void ADC_DMARequestAfterLastTransferCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
1077 {
1078  /* Check the parameters */
1079  assert_param(IS_ADC_ALL_PERIPH(ADCx));
1080  assert_param(IS_FUNCTIONAL_STATE(NewState));
1081  if (NewState != DISABLE)
1082  {
1083  /* Enable the selected ADC DMA request after last transfer */
1084  ADCx->CR2 |= (uint32_t)ADC_CR2_DDS;
1085  }
1086  else
1087  {
1088  /* Disable the selected ADC DMA request after last transfer */
1089  ADCx->CR2 &= (uint32_t)(~ADC_CR2_DDS);
1090  }
1091 }
1092 
1103 void ADC_MultiModeDMARequestAfterLastTransferCmd(FunctionalState NewState)
1104 {
1105  /* Check the parameters */
1106  assert_param(IS_FUNCTIONAL_STATE(NewState));
1107  if (NewState != DISABLE)
1108  {
1109  /* Enable the selected ADC DMA request after last transfer */
1110  ADC->CCR |= (uint32_t)ADC_CCR_DDS;
1111  }
1112  else
1113  {
1114  /* Disable the selected ADC DMA request after last transfer */
1115  ADC->CCR &= (uint32_t)(~ADC_CCR_DDS);
1116  }
1117 }
1190 void ADC_InjectedChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel, uint8_t Rank, uint8_t ADC_SampleTime)
1191 {
1192  uint32_t tmpreg1 = 0, tmpreg2 = 0, tmpreg3 = 0;
1193  /* Check the parameters */
1194  assert_param(IS_ADC_ALL_PERIPH(ADCx));
1195  assert_param(IS_ADC_CHANNEL(ADC_Channel));
1196  assert_param(IS_ADC_INJECTED_RANK(Rank));
1197  assert_param(IS_ADC_SAMPLE_TIME(ADC_SampleTime));
1198  /* if ADC_Channel_10 ... ADC_Channel_18 is selected */
1199  if (ADC_Channel > ADC_Channel_9)
1200  {
1201  /* Get the old register value */
1202  tmpreg1 = ADCx->SMPR1;
1203  /* Calculate the mask to clear */
1204  tmpreg2 = SMPR1_SMP_SET << (3*(ADC_Channel - 10));
1205  /* Clear the old sample time */
1206  tmpreg1 &= ~tmpreg2;
1207  /* Calculate the mask to set */
1208  tmpreg2 = (uint32_t)ADC_SampleTime << (3*(ADC_Channel - 10));
1209  /* Set the new sample time */
1210  tmpreg1 |= tmpreg2;
1211  /* Store the new register value */
1212  ADCx->SMPR1 = tmpreg1;
1213  }
1214  else /* ADC_Channel include in ADC_Channel_[0..9] */
1215  {
1216  /* Get the old register value */
1217  tmpreg1 = ADCx->SMPR2;
1218  /* Calculate the mask to clear */
1219  tmpreg2 = SMPR2_SMP_SET << (3 * ADC_Channel);
1220  /* Clear the old sample time */
1221  tmpreg1 &= ~tmpreg2;
1222  /* Calculate the mask to set */
1223  tmpreg2 = (uint32_t)ADC_SampleTime << (3 * ADC_Channel);
1224  /* Set the new sample time */
1225  tmpreg1 |= tmpreg2;
1226  /* Store the new register value */
1227  ADCx->SMPR2 = tmpreg1;
1228  }
1229  /* Rank configuration */
1230  /* Get the old register value */
1231  tmpreg1 = ADCx->JSQR;
1232  /* Get JL value: Number = JL+1 */
1233  tmpreg3 = (tmpreg1 & JSQR_JL_SET)>> 20;
1234  /* Calculate the mask to clear: ((Rank-1)+(4-JL-1)) */
1235  tmpreg2 = JSQR_JSQ_SET << (5 * (uint8_t)((Rank + 3) - (tmpreg3 + 1)));
1236  /* Clear the old JSQx bits for the selected rank */
1237  tmpreg1 &= ~tmpreg2;
1238  /* Calculate the mask to set: ((Rank-1)+(4-JL-1)) */
1239  tmpreg2 = (uint32_t)ADC_Channel << (5 * (uint8_t)((Rank + 3) - (tmpreg3 + 1)));
1240  /* Set the JSQx bits for the selected rank */
1241  tmpreg1 |= tmpreg2;
1242  /* Store the new register value */
1243  ADCx->JSQR = tmpreg1;
1244 }
1245 
1253 void ADC_InjectedSequencerLengthConfig(ADC_TypeDef* ADCx, uint8_t Length)
1254 {
1255  uint32_t tmpreg1 = 0;
1256  uint32_t tmpreg2 = 0;
1257  /* Check the parameters */
1258  assert_param(IS_ADC_ALL_PERIPH(ADCx));
1259  assert_param(IS_ADC_INJECTED_LENGTH(Length));
1260 
1261  /* Get the old register value */
1262  tmpreg1 = ADCx->JSQR;
1263 
1264  /* Clear the old injected sequence length JL bits */
1265  tmpreg1 &= JSQR_JL_RESET;
1266 
1267  /* Set the injected sequence length JL bits */
1268  tmpreg2 = Length - 1;
1269  tmpreg1 |= tmpreg2 << 20;
1270 
1271  /* Store the new register value */
1272  ADCx->JSQR = tmpreg1;
1273 }
1274 
1288 void ADC_SetInjectedOffset(ADC_TypeDef* ADCx, uint8_t ADC_InjectedChannel, uint16_t Offset)
1289 {
1290  __IO uint32_t tmp = 0;
1291  /* Check the parameters */
1292  assert_param(IS_ADC_ALL_PERIPH(ADCx));
1293  assert_param(IS_ADC_INJECTED_CHANNEL(ADC_InjectedChannel));
1294  assert_param(IS_ADC_OFFSET(Offset));
1295 
1296  tmp = (uint32_t)ADCx;
1297  tmp += ADC_InjectedChannel;
1298 
1299  /* Set the selected injected channel data offset */
1300  *(__IO uint32_t *) tmp = (uint32_t)Offset;
1301 }
1302 
1326 void ADC_ExternalTrigInjectedConvConfig(ADC_TypeDef* ADCx, uint32_t ADC_ExternalTrigInjecConv)
1327 {
1328  uint32_t tmpreg = 0;
1329  /* Check the parameters */
1330  assert_param(IS_ADC_ALL_PERIPH(ADCx));
1331  assert_param(IS_ADC_EXT_INJEC_TRIG(ADC_ExternalTrigInjecConv));
1332 
1333  /* Get the old register value */
1334  tmpreg = ADCx->CR2;
1335 
1336  /* Clear the old external event selection for injected group */
1337  tmpreg &= CR2_JEXTSEL_RESET;
1338 
1339  /* Set the external event selection for injected group */
1340  tmpreg |= ADC_ExternalTrigInjecConv;
1341 
1342  /* Store the new register value */
1343  ADCx->CR2 = tmpreg;
1344 }
1345 
1360 void ADC_ExternalTrigInjectedConvEdgeConfig(ADC_TypeDef* ADCx, uint32_t ADC_ExternalTrigInjecConvEdge)
1361 {
1362  uint32_t tmpreg = 0;
1363  /* Check the parameters */
1364  assert_param(IS_ADC_ALL_PERIPH(ADCx));
1365  assert_param(IS_ADC_EXT_INJEC_TRIG_EDGE(ADC_ExternalTrigInjecConvEdge));
1366  /* Get the old register value */
1367  tmpreg = ADCx->CR2;
1368  /* Clear the old external trigger edge for injected group */
1369  tmpreg &= CR2_JEXTEN_RESET;
1370  /* Set the new external trigger edge for injected group */
1371  tmpreg |= ADC_ExternalTrigInjecConvEdge;
1372  /* Store the new register value */
1373  ADCx->CR2 = tmpreg;
1374 }
1375 
1381 void ADC_SoftwareStartInjectedConv(ADC_TypeDef* ADCx)
1382 {
1383  /* Check the parameters */
1384  assert_param(IS_ADC_ALL_PERIPH(ADCx));
1385  /* Enable the selected ADC conversion for injected group */
1386  ADCx->CR2 |= (uint32_t)ADC_CR2_JSWSTART;
1387 }
1388 
1394 FlagStatus ADC_GetSoftwareStartInjectedConvCmdStatus(ADC_TypeDef* ADCx)
1395 {
1396  FlagStatus bitstatus = RESET;
1397  /* Check the parameters */
1398  assert_param(IS_ADC_ALL_PERIPH(ADCx));
1399 
1400  /* Check the status of JSWSTART bit */
1401  if ((ADCx->CR2 & ADC_CR2_JSWSTART) != (uint32_t)RESET)
1402  {
1403  /* JSWSTART bit is set */
1404  bitstatus = SET;
1405  }
1406  else
1407  {
1408  /* JSWSTART bit is reset */
1409  bitstatus = RESET;
1410  }
1411  /* Return the JSWSTART bit status */
1412  return bitstatus;
1413 }
1414 
1423 void ADC_AutoInjectedConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
1424 {
1425  /* Check the parameters */
1426  assert_param(IS_ADC_ALL_PERIPH(ADCx));
1427  assert_param(IS_FUNCTIONAL_STATE(NewState));
1428  if (NewState != DISABLE)
1429  {
1430  /* Enable the selected ADC automatic injected group conversion */
1431  ADCx->CR1 |= (uint32_t)ADC_CR1_JAUTO;
1432  }
1433  else
1434  {
1435  /* Disable the selected ADC automatic injected group conversion */
1436  ADCx->CR1 &= (uint32_t)(~ADC_CR1_JAUTO);
1437  }
1438 }
1439 
1449 void ADC_InjectedDiscModeCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
1450 {
1451  /* Check the parameters */
1452  assert_param(IS_ADC_ALL_PERIPH(ADCx));
1453  assert_param(IS_FUNCTIONAL_STATE(NewState));
1454  if (NewState != DISABLE)
1455  {
1456  /* Enable the selected ADC injected discontinuous mode */
1457  ADCx->CR1 |= (uint32_t)ADC_CR1_JDISCEN;
1458  }
1459  else
1460  {
1461  /* Disable the selected ADC injected discontinuous mode */
1462  ADCx->CR1 &= (uint32_t)(~ADC_CR1_JDISCEN);
1463  }
1464 }
1465 
1477 uint16_t ADC_GetInjectedConversionValue(ADC_TypeDef* ADCx, uint8_t ADC_InjectedChannel)
1478 {
1479  __IO uint32_t tmp = 0;
1480 
1481  /* Check the parameters */
1482  assert_param(IS_ADC_ALL_PERIPH(ADCx));
1483  assert_param(IS_ADC_INJECTED_CHANNEL(ADC_InjectedChannel));
1484 
1485  tmp = (uint32_t)ADCx;
1486  tmp += ADC_InjectedChannel + JDR_OFFSET;
1487 
1488  /* Returns the selected injected channel conversion data value */
1489  return (uint16_t) (*(__IO uint32_t*) tmp);
1490 }
1584 void ADC_ITConfig(ADC_TypeDef* ADCx, uint16_t ADC_IT, FunctionalState NewState)
1585 {
1586  uint32_t itmask = 0;
1587  /* Check the parameters */
1588  assert_param(IS_ADC_ALL_PERIPH(ADCx));
1589  assert_param(IS_FUNCTIONAL_STATE(NewState));
1590  assert_param(IS_ADC_IT(ADC_IT));
1591 
1592  /* Get the ADC IT index */
1593  itmask = (uint8_t)ADC_IT;
1594  itmask = (uint32_t)0x01 << itmask;
1595 
1596  if (NewState != DISABLE)
1597  {
1598  /* Enable the selected ADC interrupts */
1599  ADCx->CR1 |= itmask;
1600  }
1601  else
1602  {
1603  /* Disable the selected ADC interrupts */
1604  ADCx->CR1 &= (~(uint32_t)itmask);
1605  }
1606 }
1607 
1621 FlagStatus ADC_GetFlagStatus(ADC_TypeDef* ADCx, uint8_t ADC_FLAG)
1622 {
1623  FlagStatus bitstatus = RESET;
1624  /* Check the parameters */
1625  assert_param(IS_ADC_ALL_PERIPH(ADCx));
1626  assert_param(IS_ADC_GET_FLAG(ADC_FLAG));
1627 
1628  /* Check the status of the specified ADC flag */
1629  if ((ADCx->SR & ADC_FLAG) != (uint8_t)RESET)
1630  {
1631  /* ADC_FLAG is set */
1632  bitstatus = SET;
1633  }
1634  else
1635  {
1636  /* ADC_FLAG is reset */
1637  bitstatus = RESET;
1638  }
1639  /* Return the ADC_FLAG status */
1640  return bitstatus;
1641 }
1642 
1656 void ADC_ClearFlag(ADC_TypeDef* ADCx, uint8_t ADC_FLAG)
1657 {
1658  /* Check the parameters */
1659  assert_param(IS_ADC_ALL_PERIPH(ADCx));
1660  assert_param(IS_ADC_CLEAR_FLAG(ADC_FLAG));
1661 
1662  /* Clear the selected ADC flags */
1663  ADCx->SR = ~(uint32_t)ADC_FLAG;
1664 }
1665 
1677 ITStatus ADC_GetITStatus(ADC_TypeDef* ADCx, uint16_t ADC_IT)
1678 {
1679  ITStatus bitstatus = RESET;
1680  uint32_t itmask = 0, enablestatus = 0;
1681 
1682  /* Check the parameters */
1683  assert_param(IS_ADC_ALL_PERIPH(ADCx));
1684  assert_param(IS_ADC_IT(ADC_IT));
1685 
1686  /* Get the ADC IT index */
1687  itmask = ADC_IT >> 8;
1688 
1689  /* Get the ADC_IT enable bit status */
1690  enablestatus = (ADCx->CR1 & ((uint32_t)0x01 << (uint8_t)ADC_IT)) ;
1691 
1692  /* Check the status of the specified ADC interrupt */
1693  if (((ADCx->SR & itmask) != (uint32_t)RESET) && enablestatus)
1694  {
1695  /* ADC_IT is set */
1696  bitstatus = SET;
1697  }
1698  else
1699  {
1700  /* ADC_IT is reset */
1701  bitstatus = RESET;
1702  }
1703  /* Return the ADC_IT status */
1704  return bitstatus;
1705 }
1706 
1718 void ADC_ClearITPendingBit(ADC_TypeDef* ADCx, uint16_t ADC_IT)
1719 {
1720  uint8_t itmask = 0;
1721  /* Check the parameters */
1722  assert_param(IS_ADC_ALL_PERIPH(ADCx));
1723  assert_param(IS_ADC_IT(ADC_IT));
1724  /* Get the ADC IT index */
1725  itmask = (uint8_t)(ADC_IT >> 8);
1726  /* Clear the selected ADC interrupt pending bits */
1727  ADCx->SR = ~(uint32_t)itmask;
1728 }
1745 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
This file contains all the functions prototypes for the RCC firmware library.
uint32_t ADC_TwoSamplingDelay
Definition: stm32f4xx_adc.h:96
FlagStatus ADC_GetFlagStatus(ADC_TypeDef *ADCx, uint8_t ADC_FLAG)
Checks whether the specified ADC flag is set or not.
void ADC_AnalogWatchdogSingleChannelConfig(ADC_TypeDef *ADCx, uint8_t ADC_Channel)
Configures the analog watchdog guarded single channel.
void ADC_ContinuousModeCmd(ADC_TypeDef *ADCx, FunctionalState NewState)
Enables or disables the ADC continuous conversion mode.
void ADC_ExternalTrigInjectedConvConfig(ADC_TypeDef *ADCx, uint32_t ADC_ExternalTrigInjecConv)
Configures the ADCx external trigger for injected channels conversion.
void ADC_ExternalTrigInjectedConvEdgeConfig(ADC_TypeDef *ADCx, uint32_t ADC_ExternalTrigInjecConvEdge)
Configures the ADCx external trigger edge for injected channels conversion.
void ADC_TempSensorVrefintCmd(FunctionalState NewState)
Enables or disables the temperature sensor and Vrefint channels.
void ADC_InjectedChannelConfig(ADC_TypeDef *ADCx, uint8_t ADC_Channel, uint8_t Rank, uint8_t ADC_SampleTime)
Configures for the selected ADC injected channel its corresponding rank in the sequencer and its samp...
void ADC_InjectedDiscModeCmd(ADC_TypeDef *ADCx, FunctionalState NewState)
Enables or disables the discontinuous mode for injected group channel for the specified ADC...
ADC Init structure definition.
Definition: stm32f4xx_adc.h:53
void ADC_Cmd(ADC_TypeDef *ADCx, FunctionalState NewState)
Enables or disables the specified ADC peripheral.
void ADC_DiscModeCmd(ADC_TypeDef *ADCx, FunctionalState NewState)
Enables or disables the discontinuous mode on regular group channel for the specified ADC...
void ADC_CommonInit(ADC_CommonInitTypeDef *ADC_CommonInitStruct)
Initializes the ADCs peripherals according to the specified parameters in the ADC_CommonInitStruct.
ITStatus ADC_GetITStatus(ADC_TypeDef *ADCx, uint16_t ADC_IT)
Checks whether the specified ADC interrupt has occurred or not.
void ADC_VBATCmd(FunctionalState NewState)
Enables or disables the VBAT (Voltage Battery) channel.
This file contains all the functions prototypes for the ADC firmware library.
uint32_t ADC_GetMultiModeConversionValue(void)
Returns the last ADC1, ADC2 and ADC3 regular conversions results data in the selected multi mode...
void ADC_SetInjectedOffset(ADC_TypeDef *ADCx, uint8_t ADC_InjectedChannel, uint16_t Offset)
Set the injected channels conversion value offset.
void ADC_DiscModeChannelCountConfig(ADC_TypeDef *ADCx, uint8_t Number)
Configures the discontinuous mode for the selected ADC regular group channel.
#define CR1_CLEAR_MASK
uint16_t ADC_GetInjectedConversionValue(ADC_TypeDef *ADCx, uint8_t ADC_InjectedChannel)
Returns the ADC injected channel conversion result.
void ADC_DMARequestAfterLastTransferCmd(ADC_TypeDef *ADCx, FunctionalState NewState)
Enables or disables the ADC DMA request after last transfer (Single-ADC mode)
void ADC_ClearITPendingBit(ADC_TypeDef *ADCx, uint16_t ADC_IT)
Clears the ADCx's interrupt pending bits.
FunctionalState ADC_ScanConvMode
Definition: stm32f4xx_adc.h:57
uint32_t ADC_ExternalTrigConvEdge
Definition: stm32f4xx_adc.h:64
void ADC_InjectedSequencerLengthConfig(ADC_TypeDef *ADCx, uint8_t Length)
Configures the sequencer length for injected channels.
uint32_t ADC_ExternalTrigConv
Definition: stm32f4xx_adc.h:68
FunctionalState ADC_ContinuousConvMode
Definition: stm32f4xx_adc.h:61
void ADC_Init(ADC_TypeDef *ADCx, ADC_InitTypeDef *ADC_InitStruct)
Initializes the ADCx peripheral according to the specified parameters in the ADC_InitStruct.
void ADC_ITConfig(ADC_TypeDef *ADCx, uint16_t ADC_IT, FunctionalState NewState)
Enables or disables the specified ADC interrupts.
void ADC_ClearFlag(ADC_TypeDef *ADCx, uint8_t ADC_FLAG)
Clears the ADCx's pending flags.
void ADC_SoftwareStartConv(ADC_TypeDef *ADCx)
Enables the selected ADC software start conversion of the regular channels.
void ADC_EOCOnEachRegularChannelCmd(ADC_TypeDef *ADCx, FunctionalState NewState)
Enables or disables the EOC on each regular channel conversion.
void ADC_DMACmd(ADC_TypeDef *ADCx, FunctionalState NewState)
Enables or disables the specified ADC DMA request.
void ADC_SoftwareStartInjectedConv(ADC_TypeDef *ADCx)
Enables the selected ADC software start conversion of the injected channels.
FlagStatus ADC_GetSoftwareStartInjectedConvCmdStatus(ADC_TypeDef *ADCx)
Gets the selected ADC Software start injected conversion Status.
ADC Common Init structure definition.
Definition: stm32f4xx_adc.h:84
uint8_t ADC_NbrOfConversion
Definition: stm32f4xx_adc.h:75
uint32_t ADC_Resolution
Definition: stm32f4xx_adc.h:55
uint32_t ADC_DataAlign
Definition: stm32f4xx_adc.h:72
void ADC_MultiModeDMARequestAfterLastTransferCmd(FunctionalState NewState)
Enables or disables the ADC DMA request after last transfer in multi ADC mode.
void ADC_RegularChannelConfig(ADC_TypeDef *ADCx, uint8_t ADC_Channel, uint8_t Rank, uint8_t ADC_SampleTime)
Configures for the selected ADC regular channel its corresponding rank in the sequencer and its sampl...
void ADC_AutoInjectedConvCmd(ADC_TypeDef *ADCx, FunctionalState NewState)
Enables or disables the selected ADC automatic injected group conversion after regular one...
void ADC_CommonStructInit(ADC_CommonInitTypeDef *ADC_CommonInitStruct)
Fills each ADC_CommonInitStruct member with its default value.
FlagStatus ADC_GetSoftwareStartConvStatus(ADC_TypeDef *ADCx)
Gets the selected ADC Software start regular conversion Status.
void ADC_StructInit(ADC_InitTypeDef *ADC_InitStruct)
Fills each ADC_InitStruct member with its default value.
void ADC_AnalogWatchdogCmd(ADC_TypeDef *ADCx, uint32_t ADC_AnalogWatchdog)
Enables or disables the analog watchdog on single/all regular or injected channels.
void RCC_APB2PeriphResetCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)
Forces or releases High Speed APB (APB2) peripheral reset.
void ADC_DeInit(void)
Deinitializes all ADCs peripherals registers to their default reset values.
uint16_t ADC_GetConversionValue(ADC_TypeDef *ADCx)
Returns the last ADCx conversion result data for regular channel.
void ADC_AnalogWatchdogThresholdsConfig(ADC_TypeDef *ADCx, uint16_t HighThreshold, uint16_t LowThreshold)
Configures the high and low thresholds of the analog watchdog.