2 ******************************************************************************
3 * @file stm32f4xx_adc.c
4 * @author MCD Application Team
7 * @brief This file provides firmware functions to manage the following
8 * functionalities of the Analog to Digital Convertor (ADC) peripheral:
9 * - Initialization and Configuration (in addition to ADC multi mode
11 * - Analog Watchdog configuration
12 * - Temperature Sensor & Vrefint (Voltage Reference internal) & VBAT
14 * - Regular Channels Configuration
15 * - Regular Channels DMA Configuration
16 * - Injected channels Configuration
17 * - Interrupts and flags management
21 * ===================================================================
22 * How to use this driver
23 * ===================================================================
25 * 1. Enable the ADC interface clock using
26 * RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADCx, ENABLE);
28 * 2. ADC pins configuration
29 * - Enable the clock for the ADC GPIOs using the following function:
30 * RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOx, ENABLE);
31 * - Configure these ADC pins in analog mode using GPIO_Init();
33 * 3. Configure the ADC Prescaler, conversion resolution and data
34 * alignment using the ADC_Init() function.
35 * 4. Activate the ADC peripheral using ADC_Cmd() function.
37 * Regular channels group configuration
38 * ====================================
39 * - To configure the ADC regular channels group features, use
40 * ADC_Init() and ADC_RegularChannelConfig() functions.
41 * - To activate the continuous mode, use the ADC_continuousModeCmd()
43 * - To configurate and activate the Discontinuous mode, use the
44 * ADC_DiscModeChannelCountConfig() and ADC_DiscModeCmd() functions.
45 * - To read the ADC converted values, use the ADC_GetConversionValue()
48 * Multi mode ADCs Regular channels configuration
49 * ===============================================
50 * - Refer to "Regular channels group configuration" description to
51 * configure the ADC1, ADC2 and ADC3 regular channels.
52 * - Select the Multi mode ADC regular channels features (dual or
53 * triple mode) using ADC_CommonInit() function and configure
54 * the DMA mode using ADC_MultiModeDMARequestAfterLastTransferCmd()
56 * - Read the ADCs converted values using the
57 * ADC_GetMultiModeConversionValue() function.
59 * DMA for Regular channels group features configuration
60 * ======================================================
61 * - To enable the DMA mode for regular channels group, use the
62 * ADC_DMACmd() function.
63 * - To enable the generation of DMA requests continuously at the end
64 * of the last DMA transfer, use the ADC_DMARequestAfterLastTransferCmd()
67 * Injected channels group configuration
68 * =====================================
69 * - To configure the ADC Injected channels group features, use
70 * ADC_InjectedChannelConfig() and ADC_InjectedSequencerLengthConfig()
72 * - To activate the continuous mode, use the ADC_continuousModeCmd()
74 * - To activate the Injected Discontinuous mode, use the
75 * ADC_InjectedDiscModeCmd() function.
76 * - To activate the AutoInjected mode, use the ADC_AutoInjectedConvCmd()
78 * - To read the ADC converted values, use the ADC_GetInjectedConversionValue()
83 ******************************************************************************
86 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
87 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
88 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
89 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
90 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
91 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
93 * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
94 ******************************************************************************
97 /* Includes ------------------------------------------------------------------*/
98 #include "stm32f4xx_adc.h"
99 #include "stm32f4xx_rcc.h"
101 /** @addtogroup STM32F4xx_StdPeriph_Driver
106 * @brief ADC driver modules
110 /* Private typedef -----------------------------------------------------------*/
111 /* Private define ------------------------------------------------------------*/
113 /* ADC DISCNUM mask */
114 #define CR1_DISCNUM_RESET ((uint32_t)0xFFFF1FFF)
117 #define CR1_AWDCH_RESET ((uint32_t)0xFFFFFFE0)
119 /* ADC Analog watchdog enable mode mask */
120 #define CR1_AWDMode_RESET ((uint32_t)0xFF3FFDFF)
122 /* CR1 register Mask */
123 #define CR1_CLEAR_MASK ((uint32_t)0xFCFFFEFF)
126 #define CR2_EXTEN_RESET ((uint32_t)0xCFFFFFFF)
128 /* ADC JEXTEN mask */
129 #define CR2_JEXTEN_RESET ((uint32_t)0xFFCFFFFF)
131 /* ADC JEXTSEL mask */
132 #define CR2_JEXTSEL_RESET ((uint32_t)0xFFF0FFFF)
134 /* CR2 register Mask */
135 #define CR2_CLEAR_MASK ((uint32_t)0xC0FFF7FD)
138 #define SQR3_SQ_SET ((uint32_t)0x0000001F)
139 #define SQR2_SQ_SET ((uint32_t)0x0000001F)
140 #define SQR1_SQ_SET ((uint32_t)0x0000001F)
143 #define SQR1_L_RESET ((uint32_t)0xFF0FFFFF)
146 #define JSQR_JSQ_SET ((uint32_t)0x0000001F)
149 #define JSQR_JL_SET ((uint32_t)0x00300000)
150 #define JSQR_JL_RESET ((uint32_t)0xFFCFFFFF)
153 #define SMPR1_SMP_SET ((uint32_t)0x00000007)
154 #define SMPR2_SMP_SET ((uint32_t)0x00000007)
156 /* ADC JDRx registers offset */
157 #define JDR_OFFSET ((uint8_t)0x28)
159 /* ADC CDR register base address */
160 #define CDR_ADDRESS ((uint32_t)0x40012308)
162 /* ADC CCR register Mask */
163 #define CR_CLEAR_MASK ((uint32_t)0xFFFC30E0)
165 /* Private macro -------------------------------------------------------------*/
166 /* Private variables ---------------------------------------------------------*/
167 /* Private function prototypes -----------------------------------------------*/
168 /* Private functions ---------------------------------------------------------*/
170 /** @defgroup ADC_Private_Functions
174 /** @defgroup ADC_Group1 Initialization and Configuration functions
175 * @brief Initialization and Configuration functions
178 ===============================================================================
179 Initialization and Configuration functions
180 ===============================================================================
181 This section provides functions allowing to:
182 - Initialize and configure the ADC Prescaler
183 - ADC Conversion Resolution (12bit..6bit)
184 - Scan Conversion Mode (multichannels or one channel) for regular group
185 - ADC Continuous Conversion Mode (Continuous or Single conversion) for
187 - External trigger Edge and source of regular group,
188 - Converted data alignment (left or right)
189 - The number of ADC conversions that will be done using the sequencer for
190 regular channel group
191 - Multi ADC mode selection
192 - Direct memory access mode selection for multi ADC mode
193 - Delay between 2 sampling phases (used in dual or triple interleaved modes)
194 - Enable or disable the ADC peripheral
201 * @brief Deinitializes all ADCs peripherals registers to their default reset
206 void ADC_DeInit(void)
208 /* Enable all ADCs reset state */
209 RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC, ENABLE);
211 /* Release all ADCs from reset state */
212 RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC, DISABLE);
216 * @brief Initializes the ADCx peripheral according to the specified parameters
217 * in the ADC_InitStruct.
218 * @note This function is used to configure the global features of the ADC (
219 * Resolution and Data Alignment), however, the rest of the configuration
220 * parameters are specific to the regular channels group (scan mode
221 * activation, continuous mode activation, External trigger source and
222 * edge, number of conversion in the regular channels group sequencer).
223 * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
224 * @param ADC_InitStruct: pointer to an ADC_InitTypeDef structure that contains
225 * the configuration information for the specified ADC peripheral.
228 void ADC_Init(ADC_TypeDef* ADCx, ADC_InitTypeDef* ADC_InitStruct)
230 uint32_t tmpreg1 = 0;
232 /* Check the parameters */
233 assert_param(IS_ADC_ALL_PERIPH(ADCx));
234 assert_param(IS_ADC_RESOLUTION(ADC_InitStruct->ADC_Resolution));
235 assert_param(IS_FUNCTIONAL_STATE(ADC_InitStruct->ADC_ScanConvMode));
236 assert_param(IS_FUNCTIONAL_STATE(ADC_InitStruct->ADC_ContinuousConvMode));
237 assert_param(IS_ADC_EXT_TRIG_EDGE(ADC_InitStruct->ADC_ExternalTrigConvEdge));
238 assert_param(IS_ADC_EXT_TRIG(ADC_InitStruct->ADC_ExternalTrigConv));
239 assert_param(IS_ADC_DATA_ALIGN(ADC_InitStruct->ADC_DataAlign));
240 assert_param(IS_ADC_REGULAR_LENGTH(ADC_InitStruct->ADC_NbrOfConversion));
242 /*---------------------------- ADCx CR1 Configuration -----------------*/
243 /* Get the ADCx CR1 value */
246 /* Clear RES and SCAN bits */
247 tmpreg1 &= CR1_CLEAR_MASK;
249 /* Configure ADCx: scan conversion mode and resolution */
250 /* Set SCAN bit according to ADC_ScanConvMode value */
251 /* Set RES bit according to ADC_Resolution value */
252 tmpreg1 |= (uint32_t)(((uint32_t)ADC_InitStruct->ADC_ScanConvMode << 8) | \
253 ADC_InitStruct->ADC_Resolution);
254 /* Write to ADCx CR1 */
256 /*---------------------------- ADCx CR2 Configuration -----------------*/
257 /* Get the ADCx CR2 value */
260 /* Clear CONT, ALIGN, EXTEN and EXTSEL bits */
261 tmpreg1 &= CR2_CLEAR_MASK;
263 /* Configure ADCx: external trigger event and edge, data alignment and
264 continuous conversion mode */
265 /* Set ALIGN bit according to ADC_DataAlign value */
266 /* Set EXTEN bits according to ADC_ExternalTrigConvEdge value */
267 /* Set EXTSEL bits according to ADC_ExternalTrigConv value */
268 /* Set CONT bit according to ADC_ContinuousConvMode value */
269 tmpreg1 |= (uint32_t)(ADC_InitStruct->ADC_DataAlign | \
270 ADC_InitStruct->ADC_ExternalTrigConv |
271 ADC_InitStruct->ADC_ExternalTrigConvEdge | \
272 ((uint32_t)ADC_InitStruct->ADC_ContinuousConvMode << 1));
274 /* Write to ADCx CR2 */
276 /*---------------------------- ADCx SQR1 Configuration -----------------*/
277 /* Get the ADCx SQR1 value */
278 tmpreg1 = ADCx->SQR1;
281 tmpreg1 &= SQR1_L_RESET;
283 /* Configure ADCx: regular channel sequence length */
284 /* Set L bits according to ADC_NbrOfConversion value */
285 tmpreg2 |= (uint8_t)(ADC_InitStruct->ADC_NbrOfConversion - (uint8_t)1);
286 tmpreg1 |= ((uint32_t)tmpreg2 << 20);
288 /* Write to ADCx SQR1 */
289 ADCx->SQR1 = tmpreg1;
293 * @brief Fills each ADC_InitStruct member with its default value.
294 * @note This function is used to initialize the global features of the ADC (
295 * Resolution and Data Alignment), however, the rest of the configuration
296 * parameters are specific to the regular channels group (scan mode
297 * activation, continuous mode activation, External trigger source and
298 * edge, number of conversion in the regular channels group sequencer).
299 * @param ADC_InitStruct: pointer to an ADC_InitTypeDef structure which will
303 void ADC_StructInit(ADC_InitTypeDef* ADC_InitStruct)
305 /* Initialize the ADC_Mode member */
306 ADC_InitStruct->ADC_Resolution = ADC_Resolution_12b;
308 /* initialize the ADC_ScanConvMode member */
309 ADC_InitStruct->ADC_ScanConvMode = DISABLE;
311 /* Initialize the ADC_ContinuousConvMode member */
312 ADC_InitStruct->ADC_ContinuousConvMode = DISABLE;
314 /* Initialize the ADC_ExternalTrigConvEdge member */
315 ADC_InitStruct->ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
317 /* Initialize the ADC_ExternalTrigConv member */
318 ADC_InitStruct->ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
320 /* Initialize the ADC_DataAlign member */
321 ADC_InitStruct->ADC_DataAlign = ADC_DataAlign_Right;
323 /* Initialize the ADC_NbrOfConversion member */
324 ADC_InitStruct->ADC_NbrOfConversion = 1;
328 * @brief Initializes the ADCs peripherals according to the specified parameters
329 * in the ADC_CommonInitStruct.
330 * @param ADC_CommonInitStruct: pointer to an ADC_CommonInitTypeDef structure
331 * that contains the configuration information for All ADCs peripherals.
334 void ADC_CommonInit(ADC_CommonInitTypeDef* ADC_CommonInitStruct)
336 uint32_t tmpreg1 = 0;
337 /* Check the parameters */
338 assert_param(IS_ADC_MODE(ADC_CommonInitStruct->ADC_Mode));
339 assert_param(IS_ADC_PRESCALER(ADC_CommonInitStruct->ADC_Prescaler));
340 assert_param(IS_ADC_DMA_ACCESS_MODE(ADC_CommonInitStruct->ADC_DMAAccessMode));
341 assert_param(IS_ADC_SAMPLING_DELAY(ADC_CommonInitStruct->ADC_TwoSamplingDelay));
342 /*---------------------------- ADC CCR Configuration -----------------*/
343 /* Get the ADC CCR value */
346 /* Clear MULTI, DELAY, DMA and ADCPRE bits */
347 tmpreg1 &= CR_CLEAR_MASK;
349 /* Configure ADCx: Multi mode, Delay between two sampling time, ADC prescaler,
350 and DMA access mode for multimode */
351 /* Set MULTI bits according to ADC_Mode value */
352 /* Set ADCPRE bits according to ADC_Prescaler value */
353 /* Set DMA bits according to ADC_DMAAccessMode value */
354 /* Set DELAY bits according to ADC_TwoSamplingDelay value */
355 tmpreg1 |= (uint32_t)(ADC_CommonInitStruct->ADC_Mode |
356 ADC_CommonInitStruct->ADC_Prescaler |
357 ADC_CommonInitStruct->ADC_DMAAccessMode |
358 ADC_CommonInitStruct->ADC_TwoSamplingDelay);
360 /* Write to ADC CCR */
365 * @brief Fills each ADC_CommonInitStruct member with its default value.
366 * @param ADC_CommonInitStruct: pointer to an ADC_CommonInitTypeDef structure
367 * which will be initialized.
370 void ADC_CommonStructInit(ADC_CommonInitTypeDef* ADC_CommonInitStruct)
372 /* Initialize the ADC_Mode member */
373 ADC_CommonInitStruct->ADC_Mode = ADC_Mode_Independent;
375 /* initialize the ADC_Prescaler member */
376 ADC_CommonInitStruct->ADC_Prescaler = ADC_Prescaler_Div2;
378 /* Initialize the ADC_DMAAccessMode member */
379 ADC_CommonInitStruct->ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
381 /* Initialize the ADC_TwoSamplingDelay member */
382 ADC_CommonInitStruct->ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
386 * @brief Enables or disables the specified ADC peripheral.
387 * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
388 * @param NewState: new state of the ADCx peripheral.
389 * This parameter can be: ENABLE or DISABLE.
392 void ADC_Cmd(ADC_TypeDef* ADCx, FunctionalState NewState)
394 /* Check the parameters */
395 assert_param(IS_ADC_ALL_PERIPH(ADCx));
396 assert_param(IS_FUNCTIONAL_STATE(NewState));
397 if (NewState != DISABLE)
399 /* Set the ADON bit to wake up the ADC from power down mode */
400 ADCx->CR2 |= (uint32_t)ADC_CR2_ADON;
404 /* Disable the selected ADC peripheral */
405 ADCx->CR2 &= (uint32_t)(~ADC_CR2_ADON);
412 /** @defgroup ADC_Group2 Analog Watchdog configuration functions
413 * @brief Analog Watchdog configuration functions
416 ===============================================================================
417 Analog Watchdog configuration functions
418 ===============================================================================
420 This section provides functions allowing to configure the Analog Watchdog
421 (AWD) feature in the ADC.
423 A typical configuration Analog Watchdog is done following these steps :
424 1. the ADC guarded channel(s) is (are) selected using the
425 ADC_AnalogWatchdogSingleChannelConfig() function.
426 2. The Analog watchdog lower and higher threshold are configured using the
427 ADC_AnalogWatchdogThresholdsConfig() function.
428 3. The Analog watchdog is enabled and configured to enable the check, on one
429 or more channels, using the ADC_AnalogWatchdogCmd() function.
436 * @brief Enables or disables the analog watchdog on single/all regular or
438 * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
439 * @param ADC_AnalogWatchdog: the ADC analog watchdog configuration.
440 * This parameter can be one of the following values:
441 * @arg ADC_AnalogWatchdog_SingleRegEnable: Analog watchdog on a single regular channel
442 * @arg ADC_AnalogWatchdog_SingleInjecEnable: Analog watchdog on a single injected channel
443 * @arg ADC_AnalogWatchdog_SingleRegOrInjecEnable: Analog watchdog on a single regular or injected channel
444 * @arg ADC_AnalogWatchdog_AllRegEnable: Analog watchdog on all regular channel
445 * @arg ADC_AnalogWatchdog_AllInjecEnable: Analog watchdog on all injected channel
446 * @arg ADC_AnalogWatchdog_AllRegAllInjecEnable: Analog watchdog on all regular and injected channels
447 * @arg ADC_AnalogWatchdog_None: No channel guarded by the analog watchdog
450 void ADC_AnalogWatchdogCmd(ADC_TypeDef* ADCx, uint32_t ADC_AnalogWatchdog)
453 /* Check the parameters */
454 assert_param(IS_ADC_ALL_PERIPH(ADCx));
455 assert_param(IS_ADC_ANALOG_WATCHDOG(ADC_AnalogWatchdog));
457 /* Get the old register value */
460 /* Clear AWDEN, JAWDEN and AWDSGL bits */
461 tmpreg &= CR1_AWDMode_RESET;
463 /* Set the analog watchdog enable mode */
464 tmpreg |= ADC_AnalogWatchdog;
466 /* Store the new register value */
471 * @brief Configures the high and low thresholds of the analog watchdog.
472 * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
473 * @param HighThreshold: the ADC analog watchdog High threshold value.
474 * This parameter must be a 12-bit value.
475 * @param LowThreshold: the ADC analog watchdog Low threshold value.
476 * This parameter must be a 12-bit value.
479 void ADC_AnalogWatchdogThresholdsConfig(ADC_TypeDef* ADCx, uint16_t HighThreshold,
480 uint16_t LowThreshold)
482 /* Check the parameters */
483 assert_param(IS_ADC_ALL_PERIPH(ADCx));
484 assert_param(IS_ADC_THRESHOLD(HighThreshold));
485 assert_param(IS_ADC_THRESHOLD(LowThreshold));
487 /* Set the ADCx high threshold */
488 ADCx->HTR = HighThreshold;
490 /* Set the ADCx low threshold */
491 ADCx->LTR = LowThreshold;
495 * @brief Configures the analog watchdog guarded single channel
496 * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
497 * @param ADC_Channel: the ADC channel to configure for the analog watchdog.
498 * This parameter can be one of the following values:
499 * @arg ADC_Channel_0: ADC Channel0 selected
500 * @arg ADC_Channel_1: ADC Channel1 selected
501 * @arg ADC_Channel_2: ADC Channel2 selected
502 * @arg ADC_Channel_3: ADC Channel3 selected
503 * @arg ADC_Channel_4: ADC Channel4 selected
504 * @arg ADC_Channel_5: ADC Channel5 selected
505 * @arg ADC_Channel_6: ADC Channel6 selected
506 * @arg ADC_Channel_7: ADC Channel7 selected
507 * @arg ADC_Channel_8: ADC Channel8 selected
508 * @arg ADC_Channel_9: ADC Channel9 selected
509 * @arg ADC_Channel_10: ADC Channel10 selected
510 * @arg ADC_Channel_11: ADC Channel11 selected
511 * @arg ADC_Channel_12: ADC Channel12 selected
512 * @arg ADC_Channel_13: ADC Channel13 selected
513 * @arg ADC_Channel_14: ADC Channel14 selected
514 * @arg ADC_Channel_15: ADC Channel15 selected
515 * @arg ADC_Channel_16: ADC Channel16 selected
516 * @arg ADC_Channel_17: ADC Channel17 selected
517 * @arg ADC_Channel_18: ADC Channel18 selected
520 void ADC_AnalogWatchdogSingleChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel)
523 /* Check the parameters */
524 assert_param(IS_ADC_ALL_PERIPH(ADCx));
525 assert_param(IS_ADC_CHANNEL(ADC_Channel));
527 /* Get the old register value */
530 /* Clear the Analog watchdog channel select bits */
531 tmpreg &= CR1_AWDCH_RESET;
533 /* Set the Analog watchdog channel */
534 tmpreg |= ADC_Channel;
536 /* Store the new register value */
543 /** @defgroup ADC_Group3 Temperature Sensor, Vrefint (Voltage Reference internal)
544 * and VBAT (Voltage BATtery) management functions
545 * @brief Temperature Sensor, Vrefint and VBAT management functions
548 ===============================================================================
549 Temperature Sensor, Vrefint and VBAT management functions
550 ===============================================================================
552 This section provides functions allowing to enable/ disable the internal
553 connections between the ADC and the Temperature Sensor, the Vrefint and the
556 A typical configuration to get the Temperature sensor and Vrefint channels
557 voltages is done following these steps :
558 1. Enable the internal connection of Temperature sensor and Vrefint sources
559 with the ADC channels using ADC_TempSensorVrefintCmd() function.
560 2. Select the ADC_Channel_TempSensor and/or ADC_Channel_Vrefint using
561 ADC_RegularChannelConfig() or ADC_InjectedChannelConfig() functions
562 3. Get the voltage values, using ADC_GetConversionValue() or
563 ADC_GetInjectedConversionValue().
565 A typical configuration to get the VBAT channel voltage is done following
567 1. Enable the internal connection of VBAT source with the ADC channel using
568 ADC_VBATCmd() function.
569 2. Select the ADC_Channel_Vbat using ADC_RegularChannelConfig() or
570 ADC_InjectedChannelConfig() functions
571 3. Get the voltage value, using ADC_GetConversionValue() or
572 ADC_GetInjectedConversionValue().
580 * @brief Enables or disables the temperature sensor and Vrefint channels.
581 * @param NewState: new state of the temperature sensor and Vrefint channels.
582 * This parameter can be: ENABLE or DISABLE.
585 void ADC_TempSensorVrefintCmd(FunctionalState NewState)
587 /* Check the parameters */
588 assert_param(IS_FUNCTIONAL_STATE(NewState));
589 if (NewState != DISABLE)
591 /* Enable the temperature sensor and Vrefint channel*/
592 ADC->CCR |= (uint32_t)ADC_CCR_TSVREFE;
596 /* Disable the temperature sensor and Vrefint channel*/
597 ADC->CCR &= (uint32_t)(~ADC_CCR_TSVREFE);
602 * @brief Enables or disables the VBAT (Voltage Battery) channel.
603 * @param NewState: new state of the VBAT channel.
604 * This parameter can be: ENABLE or DISABLE.
607 void ADC_VBATCmd(FunctionalState NewState)
609 /* Check the parameters */
610 assert_param(IS_FUNCTIONAL_STATE(NewState));
611 if (NewState != DISABLE)
613 /* Enable the VBAT channel*/
614 ADC->CCR |= (uint32_t)ADC_CCR_VBATE;
618 /* Disable the VBAT channel*/
619 ADC->CCR &= (uint32_t)(~ADC_CCR_VBATE);
627 /** @defgroup ADC_Group4 Regular Channels Configuration functions
628 * @brief Regular Channels Configuration functions
631 ===============================================================================
632 Regular Channels Configuration functions
633 ===============================================================================
635 This section provides functions allowing to manage the ADC's regular channels,
636 it is composed of 2 sub sections :
638 1. Configuration and management functions for regular channels: This subsection
639 provides functions allowing to configure the ADC regular channels :
640 - Configure the rank in the regular group sequencer for each channel
641 - Configure the sampling time for each channel
642 - select the conversion Trigger for regular channels
643 - select the desired EOC event behavior configuration
644 - Activate the continuous Mode (*)
645 - Activate the Discontinuous Mode
646 Please Note that the following features for regular channels are configurated
647 using the ADC_Init() function :
648 - scan mode activation
649 - continuous mode activation (**)
650 - External trigger source
651 - External trigger edge
652 - number of conversion in the regular channels group sequencer.
654 @note (*) and (**) are performing the same configuration
656 2. Get the conversion data: This subsection provides an important function in
657 the ADC peripheral since it returns the converted data of the current
658 regular channel. When the Conversion value is read, the EOC Flag is
659 automatically cleared.
661 @note For multi ADC mode, the last ADC1, ADC2 and ADC3 regular conversions
662 results data (in the selected multi mode) can be returned in the same
663 time using ADC_GetMultiModeConversionValue() function.
670 * @brief Configures for the selected ADC regular channel its corresponding
671 * rank in the sequencer and its sample time.
672 * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
673 * @param ADC_Channel: the ADC channel to configure.
674 * This parameter can be one of the following values:
675 * @arg ADC_Channel_0: ADC Channel0 selected
676 * @arg ADC_Channel_1: ADC Channel1 selected
677 * @arg ADC_Channel_2: ADC Channel2 selected
678 * @arg ADC_Channel_3: ADC Channel3 selected
679 * @arg ADC_Channel_4: ADC Channel4 selected
680 * @arg ADC_Channel_5: ADC Channel5 selected
681 * @arg ADC_Channel_6: ADC Channel6 selected
682 * @arg ADC_Channel_7: ADC Channel7 selected
683 * @arg ADC_Channel_8: ADC Channel8 selected
684 * @arg ADC_Channel_9: ADC Channel9 selected
685 * @arg ADC_Channel_10: ADC Channel10 selected
686 * @arg ADC_Channel_11: ADC Channel11 selected
687 * @arg ADC_Channel_12: ADC Channel12 selected
688 * @arg ADC_Channel_13: ADC Channel13 selected
689 * @arg ADC_Channel_14: ADC Channel14 selected
690 * @arg ADC_Channel_15: ADC Channel15 selected
691 * @arg ADC_Channel_16: ADC Channel16 selected
692 * @arg ADC_Channel_17: ADC Channel17 selected
693 * @arg ADC_Channel_18: ADC Channel18 selected
694 * @param Rank: The rank in the regular group sequencer.
695 * This parameter must be between 1 to 16.
696 * @param ADC_SampleTime: The sample time value to be set for the selected channel.
697 * This parameter can be one of the following values:
698 * @arg ADC_SampleTime_3Cycles: Sample time equal to 3 cycles
699 * @arg ADC_SampleTime_15Cycles: Sample time equal to 15 cycles
700 * @arg ADC_SampleTime_28Cycles: Sample time equal to 28 cycles
701 * @arg ADC_SampleTime_56Cycles: Sample time equal to 56 cycles
702 * @arg ADC_SampleTime_84Cycles: Sample time equal to 84 cycles
703 * @arg ADC_SampleTime_112Cycles: Sample time equal to 112 cycles
704 * @arg ADC_SampleTime_144Cycles: Sample time equal to 144 cycles
705 * @arg ADC_SampleTime_480Cycles: Sample time equal to 480 cycles
708 void ADC_RegularChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel, uint8_t Rank, uint8_t ADC_SampleTime)
710 uint32_t tmpreg1 = 0, tmpreg2 = 0;
711 /* Check the parameters */
712 assert_param(IS_ADC_ALL_PERIPH(ADCx));
713 assert_param(IS_ADC_CHANNEL(ADC_Channel));
714 assert_param(IS_ADC_REGULAR_RANK(Rank));
715 assert_param(IS_ADC_SAMPLE_TIME(ADC_SampleTime));
717 /* if ADC_Channel_10 ... ADC_Channel_18 is selected */
718 if (ADC_Channel > ADC_Channel_9)
720 /* Get the old register value */
721 tmpreg1 = ADCx->SMPR1;
723 /* Calculate the mask to clear */
724 tmpreg2 = SMPR1_SMP_SET << (3 * (ADC_Channel - 10));
726 /* Clear the old sample time */
729 /* Calculate the mask to set */
730 tmpreg2 = (uint32_t)ADC_SampleTime << (3 * (ADC_Channel - 10));
732 /* Set the new sample time */
735 /* Store the new register value */
736 ADCx->SMPR1 = tmpreg1;
738 else /* ADC_Channel include in ADC_Channel_[0..9] */
740 /* Get the old register value */
741 tmpreg1 = ADCx->SMPR2;
743 /* Calculate the mask to clear */
744 tmpreg2 = SMPR2_SMP_SET << (3 * ADC_Channel);
746 /* Clear the old sample time */
749 /* Calculate the mask to set */
750 tmpreg2 = (uint32_t)ADC_SampleTime << (3 * ADC_Channel);
752 /* Set the new sample time */
755 /* Store the new register value */
756 ADCx->SMPR2 = tmpreg1;
758 /* For Rank 1 to 6 */
761 /* Get the old register value */
762 tmpreg1 = ADCx->SQR3;
764 /* Calculate the mask to clear */
765 tmpreg2 = SQR3_SQ_SET << (5 * (Rank - 1));
767 /* Clear the old SQx bits for the selected rank */
770 /* Calculate the mask to set */
771 tmpreg2 = (uint32_t)ADC_Channel << (5 * (Rank - 1));
773 /* Set the SQx bits for the selected rank */
776 /* Store the new register value */
777 ADCx->SQR3 = tmpreg1;
779 /* For Rank 7 to 12 */
782 /* Get the old register value */
783 tmpreg1 = ADCx->SQR2;
785 /* Calculate the mask to clear */
786 tmpreg2 = SQR2_SQ_SET << (5 * (Rank - 7));
788 /* Clear the old SQx bits for the selected rank */
791 /* Calculate the mask to set */
792 tmpreg2 = (uint32_t)ADC_Channel << (5 * (Rank - 7));
794 /* Set the SQx bits for the selected rank */
797 /* Store the new register value */
798 ADCx->SQR2 = tmpreg1;
800 /* For Rank 13 to 16 */
803 /* Get the old register value */
804 tmpreg1 = ADCx->SQR1;
806 /* Calculate the mask to clear */
807 tmpreg2 = SQR1_SQ_SET << (5 * (Rank - 13));
809 /* Clear the old SQx bits for the selected rank */
812 /* Calculate the mask to set */
813 tmpreg2 = (uint32_t)ADC_Channel << (5 * (Rank - 13));
815 /* Set the SQx bits for the selected rank */
818 /* Store the new register value */
819 ADCx->SQR1 = tmpreg1;
824 * @brief Enables the selected ADC software start conversion of the regular channels.
825 * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
828 void ADC_SoftwareStartConv(ADC_TypeDef* ADCx)
830 /* Check the parameters */
831 assert_param(IS_ADC_ALL_PERIPH(ADCx));
833 /* Enable the selected ADC conversion for regular group */
834 ADCx->CR2 |= (uint32_t)ADC_CR2_SWSTART;
838 * @brief Gets the selected ADC Software start regular conversion Status.
839 * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
840 * @retval The new state of ADC software start conversion (SET or RESET).
842 FlagStatus ADC_GetSoftwareStartConvStatus(ADC_TypeDef* ADCx)
844 FlagStatus bitstatus = RESET;
845 /* Check the parameters */
846 assert_param(IS_ADC_ALL_PERIPH(ADCx));
848 /* Check the status of SWSTART bit */
849 if ((ADCx->CR2 & ADC_CR2_JSWSTART) != (uint32_t)RESET)
851 /* SWSTART bit is set */
856 /* SWSTART bit is reset */
860 /* Return the SWSTART bit status */
866 * @brief Enables or disables the EOC on each regular channel conversion
867 * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
868 * @param NewState: new state of the selected ADC EOC flag rising
869 * This parameter can be: ENABLE or DISABLE.
872 void ADC_EOCOnEachRegularChannelCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
874 /* Check the parameters */
875 assert_param(IS_ADC_ALL_PERIPH(ADCx));
876 assert_param(IS_FUNCTIONAL_STATE(NewState));
878 if (NewState != DISABLE)
880 /* Enable the selected ADC EOC rising on each regular channel conversion */
881 ADCx->CR2 |= (uint32_t)ADC_CR2_EOCS;
885 /* Disable the selected ADC EOC rising on each regular channel conversion */
886 ADCx->CR2 &= (uint32_t)(~ADC_CR2_EOCS);
891 * @brief Enables or disables the ADC continuous conversion mode
892 * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
893 * @param NewState: new state of the selected ADC continuous conversion mode
894 * This parameter can be: ENABLE or DISABLE.
897 void ADC_ContinuousModeCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
899 /* Check the parameters */
900 assert_param(IS_ADC_ALL_PERIPH(ADCx));
901 assert_param(IS_FUNCTIONAL_STATE(NewState));
903 if (NewState != DISABLE)
905 /* Enable the selected ADC continuous conversion mode */
906 ADCx->CR2 |= (uint32_t)ADC_CR2_CONT;
910 /* Disable the selected ADC continuous conversion mode */
911 ADCx->CR2 &= (uint32_t)(~ADC_CR2_CONT);
916 * @brief Configures the discontinuous mode for the selected ADC regular group
918 * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
919 * @param Number: specifies the discontinuous mode regular channel count value.
920 * This number must be between 1 and 8.
923 void ADC_DiscModeChannelCountConfig(ADC_TypeDef* ADCx, uint8_t Number)
925 uint32_t tmpreg1 = 0;
926 uint32_t tmpreg2 = 0;
928 /* Check the parameters */
929 assert_param(IS_ADC_ALL_PERIPH(ADCx));
930 assert_param(IS_ADC_REGULAR_DISC_NUMBER(Number));
932 /* Get the old register value */
935 /* Clear the old discontinuous mode channel count */
936 tmpreg1 &= CR1_DISCNUM_RESET;
938 /* Set the discontinuous mode channel count */
939 tmpreg2 = Number - 1;
940 tmpreg1 |= tmpreg2 << 13;
942 /* Store the new register value */
947 * @brief Enables or disables the discontinuous mode on regular group channel
948 * for the specified ADC
949 * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
950 * @param NewState: new state of the selected ADC discontinuous mode on
951 * regular group channel.
952 * This parameter can be: ENABLE or DISABLE.
955 void ADC_DiscModeCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
957 /* Check the parameters */
958 assert_param(IS_ADC_ALL_PERIPH(ADCx));
959 assert_param(IS_FUNCTIONAL_STATE(NewState));
961 if (NewState != DISABLE)
963 /* Enable the selected ADC regular discontinuous mode */
964 ADCx->CR1 |= (uint32_t)ADC_CR1_DISCEN;
968 /* Disable the selected ADC regular discontinuous mode */
969 ADCx->CR1 &= (uint32_t)(~ADC_CR1_DISCEN);
974 * @brief Returns the last ADCx conversion result data for regular channel.
975 * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
976 * @retval The Data conversion value.
978 uint16_t ADC_GetConversionValue(ADC_TypeDef* ADCx)
980 /* Check the parameters */
981 assert_param(IS_ADC_ALL_PERIPH(ADCx));
983 /* Return the selected ADC conversion value */
984 return (uint16_t) ADCx->DR;
988 * @brief Returns the last ADC1, ADC2 and ADC3 regular conversions results
989 * data in the selected multi mode.
991 * @retval The Data conversion value.
992 * @note In dual mode, the value returned by this function is as following
993 * Data[15:0] : these bits contain the regular data of ADC1.
994 * Data[31:16]: these bits contain the regular data of ADC2.
995 * @note In triple mode, the value returned by this function is as following
996 * Data[15:0] : these bits contain alternatively the regular data of ADC1, ADC3 and ADC2.
997 * Data[31:16]: these bits contain alternatively the regular data of ADC2, ADC1 and ADC3.
999 uint32_t ADC_GetMultiModeConversionValue(void)
1001 /* Return the multi mode conversion value */
1002 return (*(__IO uint32_t *) CDR_ADDRESS);
1008 /** @defgroup ADC_Group5 Regular Channels DMA Configuration functions
1009 * @brief Regular Channels DMA Configuration functions
1012 ===============================================================================
1013 Regular Channels DMA Configuration functions
1014 ===============================================================================
1016 This section provides functions allowing to configure the DMA for ADC regular
1018 Since converted regular channel values are stored into a unique data register,
1019 it is useful to use DMA for conversion of more than one regular channel. This
1020 avoids the loss of the data already stored in the ADC Data register.
1022 When the DMA mode is enabled (using the ADC_DMACmd() function), after each
1023 conversion of a regular channel, a DMA request is generated.
1025 Depending on the "DMA disable selection for Independent ADC mode"
1026 configuration (using the ADC_DMARequestAfterLastTransferCmd() function),
1027 at the end of the last DMA transfer, two possibilities are allowed:
1028 - No new DMA request is issued to the DMA controller (feature DISABLED)
1029 - Requests can continue to be generated (feature ENABLED).
1031 Depending on the "DMA disable selection for multi ADC mode" configuration
1032 (using the void ADC_MultiModeDMARequestAfterLastTransferCmd() function),
1033 at the end of the last DMA transfer, two possibilities are allowed:
1034 - No new DMA request is issued to the DMA controller (feature DISABLED)
1035 - Requests can continue to be generated (feature ENABLED).
1042 * @brief Enables or disables the specified ADC DMA request.
1043 * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
1044 * @param NewState: new state of the selected ADC DMA transfer.
1045 * This parameter can be: ENABLE or DISABLE.
1048 void ADC_DMACmd(ADC_TypeDef* ADCx, FunctionalState NewState)
1050 /* Check the parameters */
1051 assert_param(IS_ADC_ALL_PERIPH(ADCx));
1052 assert_param(IS_FUNCTIONAL_STATE(NewState));
1053 if (NewState != DISABLE)
1055 /* Enable the selected ADC DMA request */
1056 ADCx->CR2 |= (uint32_t)ADC_CR2_DMA;
1060 /* Disable the selected ADC DMA request */
1061 ADCx->CR2 &= (uint32_t)(~ADC_CR2_DMA);
1066 * @brief Enables or disables the ADC DMA request after last transfer (Single-ADC mode)
1067 * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
1068 * @param NewState: new state of the selected ADC DMA request after last transfer.
1069 * This parameter can be: ENABLE or DISABLE.
1072 void ADC_DMARequestAfterLastTransferCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
1074 /* Check the parameters */
1075 assert_param(IS_ADC_ALL_PERIPH(ADCx));
1076 assert_param(IS_FUNCTIONAL_STATE(NewState));
1077 if (NewState != DISABLE)
1079 /* Enable the selected ADC DMA request after last transfer */
1080 ADCx->CR2 |= (uint32_t)ADC_CR2_DDS;
1084 /* Disable the selected ADC DMA request after last transfer */
1085 ADCx->CR2 &= (uint32_t)(~ADC_CR2_DDS);
1090 * @brief Enables or disables the ADC DMA request after last transfer in multi ADC mode
1091 * @param NewState: new state of the selected ADC DMA request after last transfer.
1092 * This parameter can be: ENABLE or DISABLE.
1093 * @note if Enabled, DMA requests are issued as long as data are converted and
1094 * DMA mode for multi ADC mode (selected using ADC_CommonInit() function
1095 * by ADC_CommonInitStruct.ADC_DMAAccessMode structure member) is
1096 * ADC_DMAAccessMode_1, ADC_DMAAccessMode_2 or ADC_DMAAccessMode_3.
1099 void ADC_MultiModeDMARequestAfterLastTransferCmd(FunctionalState NewState)
1101 /* Check the parameters */
1102 assert_param(IS_FUNCTIONAL_STATE(NewState));
1103 if (NewState != DISABLE)
1105 /* Enable the selected ADC DMA request after last transfer */
1106 ADC->CCR |= (uint32_t)ADC_CCR_DDS;
1110 /* Disable the selected ADC DMA request after last transfer */
1111 ADC->CCR &= (uint32_t)(~ADC_CCR_DDS);
1118 /** @defgroup ADC_Group6 Injected channels Configuration functions
1119 * @brief Injected channels Configuration functions
1122 ===============================================================================
1123 Injected channels Configuration functions
1124 ===============================================================================
1126 This section provide functions allowing to configure the ADC Injected channels,
1127 it is composed of 2 sub sections :
1129 1. Configuration functions for Injected channels: This subsection provides
1130 functions allowing to configure the ADC injected channels :
1131 - Configure the rank in the injected group sequencer for each channel
1132 - Configure the sampling time for each channel
1133 - Activate the Auto injected Mode
1134 - Activate the Discontinuous Mode
1135 - scan mode activation
1136 - External/software trigger source
1137 - External trigger edge
1138 - injected channels sequencer.
1140 2. Get the Specified Injected channel conversion data: This subsection
1141 provides an important function in the ADC peripheral since it returns the
1142 converted data of the specific injected channel.
1148 * @brief Configures for the selected ADC injected channel its corresponding
1149 * rank in the sequencer and its sample time.
1150 * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
1151 * @param ADC_Channel: the ADC channel to configure.
1152 * This parameter can be one of the following values:
1153 * @arg ADC_Channel_0: ADC Channel0 selected
1154 * @arg ADC_Channel_1: ADC Channel1 selected
1155 * @arg ADC_Channel_2: ADC Channel2 selected
1156 * @arg ADC_Channel_3: ADC Channel3 selected
1157 * @arg ADC_Channel_4: ADC Channel4 selected
1158 * @arg ADC_Channel_5: ADC Channel5 selected
1159 * @arg ADC_Channel_6: ADC Channel6 selected
1160 * @arg ADC_Channel_7: ADC Channel7 selected
1161 * @arg ADC_Channel_8: ADC Channel8 selected
1162 * @arg ADC_Channel_9: ADC Channel9 selected
1163 * @arg ADC_Channel_10: ADC Channel10 selected
1164 * @arg ADC_Channel_11: ADC Channel11 selected
1165 * @arg ADC_Channel_12: ADC Channel12 selected
1166 * @arg ADC_Channel_13: ADC Channel13 selected
1167 * @arg ADC_Channel_14: ADC Channel14 selected
1168 * @arg ADC_Channel_15: ADC Channel15 selected
1169 * @arg ADC_Channel_16: ADC Channel16 selected
1170 * @arg ADC_Channel_17: ADC Channel17 selected
1171 * @arg ADC_Channel_18: ADC Channel18 selected
1172 * @param Rank: The rank in the injected group sequencer.
1173 * This parameter must be between 1 to 4.
1174 * @param ADC_SampleTime: The sample time value to be set for the selected channel.
1175 * This parameter can be one of the following values:
1176 * @arg ADC_SampleTime_3Cycles: Sample time equal to 3 cycles
1177 * @arg ADC_SampleTime_15Cycles: Sample time equal to 15 cycles
1178 * @arg ADC_SampleTime_28Cycles: Sample time equal to 28 cycles
1179 * @arg ADC_SampleTime_56Cycles: Sample time equal to 56 cycles
1180 * @arg ADC_SampleTime_84Cycles: Sample time equal to 84 cycles
1181 * @arg ADC_SampleTime_112Cycles: Sample time equal to 112 cycles
1182 * @arg ADC_SampleTime_144Cycles: Sample time equal to 144 cycles
1183 * @arg ADC_SampleTime_480Cycles: Sample time equal to 480 cycles
1186 void ADC_InjectedChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel, uint8_t Rank, uint8_t ADC_SampleTime)
1188 uint32_t tmpreg1 = 0, tmpreg2 = 0, tmpreg3 = 0;
1189 /* Check the parameters */
1190 assert_param(IS_ADC_ALL_PERIPH(ADCx));
1191 assert_param(IS_ADC_CHANNEL(ADC_Channel));
1192 assert_param(IS_ADC_INJECTED_RANK(Rank));
1193 assert_param(IS_ADC_SAMPLE_TIME(ADC_SampleTime));
1194 /* if ADC_Channel_10 ... ADC_Channel_18 is selected */
1195 if (ADC_Channel > ADC_Channel_9)
1197 /* Get the old register value */
1198 tmpreg1 = ADCx->SMPR1;
1199 /* Calculate the mask to clear */
1200 tmpreg2 = SMPR1_SMP_SET << (3*(ADC_Channel - 10));
1201 /* Clear the old sample time */
1202 tmpreg1 &= ~tmpreg2;
1203 /* Calculate the mask to set */
1204 tmpreg2 = (uint32_t)ADC_SampleTime << (3*(ADC_Channel - 10));
1205 /* Set the new sample time */
1207 /* Store the new register value */
1208 ADCx->SMPR1 = tmpreg1;
1210 else /* ADC_Channel include in ADC_Channel_[0..9] */
1212 /* Get the old register value */
1213 tmpreg1 = ADCx->SMPR2;
1214 /* Calculate the mask to clear */
1215 tmpreg2 = SMPR2_SMP_SET << (3 * ADC_Channel);
1216 /* Clear the old sample time */
1217 tmpreg1 &= ~tmpreg2;
1218 /* Calculate the mask to set */
1219 tmpreg2 = (uint32_t)ADC_SampleTime << (3 * ADC_Channel);
1220 /* Set the new sample time */
1222 /* Store the new register value */
1223 ADCx->SMPR2 = tmpreg1;
1225 /* Rank configuration */
1226 /* Get the old register value */
1227 tmpreg1 = ADCx->JSQR;
1228 /* Get JL value: Number = JL+1 */
1229 tmpreg3 = (tmpreg1 & JSQR_JL_SET)>> 20;
1230 /* Calculate the mask to clear: ((Rank-1)+(4-JL-1)) */
1231 tmpreg2 = JSQR_JSQ_SET << (5 * (uint8_t)((Rank + 3) - (tmpreg3 + 1)));
1232 /* Clear the old JSQx bits for the selected rank */
1233 tmpreg1 &= ~tmpreg2;
1234 /* Calculate the mask to set: ((Rank-1)+(4-JL-1)) */
1235 tmpreg2 = (uint32_t)ADC_Channel << (5 * (uint8_t)((Rank + 3) - (tmpreg3 + 1)));
1236 /* Set the JSQx bits for the selected rank */
1238 /* Store the new register value */
1239 ADCx->JSQR = tmpreg1;
1243 * @brief Configures the sequencer length for injected channels
1244 * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
1245 * @param Length: The sequencer length.
1246 * This parameter must be a number between 1 to 4.
1249 void ADC_InjectedSequencerLengthConfig(ADC_TypeDef* ADCx, uint8_t Length)
1251 uint32_t tmpreg1 = 0;
1252 uint32_t tmpreg2 = 0;
1253 /* Check the parameters */
1254 assert_param(IS_ADC_ALL_PERIPH(ADCx));
1255 assert_param(IS_ADC_INJECTED_LENGTH(Length));
1257 /* Get the old register value */
1258 tmpreg1 = ADCx->JSQR;
1260 /* Clear the old injected sequence length JL bits */
1261 tmpreg1 &= JSQR_JL_RESET;
1263 /* Set the injected sequence length JL bits */
1264 tmpreg2 = Length - 1;
1265 tmpreg1 |= tmpreg2 << 20;
1267 /* Store the new register value */
1268 ADCx->JSQR = tmpreg1;
1272 * @brief Set the injected channels conversion value offset
1273 * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
1274 * @param ADC_InjectedChannel: the ADC injected channel to set its offset.
1275 * This parameter can be one of the following values:
1276 * @arg ADC_InjectedChannel_1: Injected Channel1 selected
1277 * @arg ADC_InjectedChannel_2: Injected Channel2 selected
1278 * @arg ADC_InjectedChannel_3: Injected Channel3 selected
1279 * @arg ADC_InjectedChannel_4: Injected Channel4 selected
1280 * @param Offset: the offset value for the selected ADC injected channel
1281 * This parameter must be a 12bit value.
1284 void ADC_SetInjectedOffset(ADC_TypeDef* ADCx, uint8_t ADC_InjectedChannel, uint16_t Offset)
1286 __IO uint32_t tmp = 0;
1287 /* Check the parameters */
1288 assert_param(IS_ADC_ALL_PERIPH(ADCx));
1289 assert_param(IS_ADC_INJECTED_CHANNEL(ADC_InjectedChannel));
1290 assert_param(IS_ADC_OFFSET(Offset));
1292 tmp = (uint32_t)ADCx;
1293 tmp += ADC_InjectedChannel;
1295 /* Set the selected injected channel data offset */
1296 *(__IO uint32_t *) tmp = (uint32_t)Offset;
1300 * @brief Configures the ADCx external trigger for injected channels conversion.
1301 * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
1302 * @param ADC_ExternalTrigInjecConv: specifies the ADC trigger to start injected conversion.
1303 * This parameter can be one of the following values:
1304 * @arg ADC_ExternalTrigInjecConv_T1_CC4: Timer1 capture compare4 selected
1305 * @arg ADC_ExternalTrigInjecConv_T1_TRGO: Timer1 TRGO event selected
1306 * @arg ADC_ExternalTrigInjecConv_T2_CC1: Timer2 capture compare1 selected
1307 * @arg ADC_ExternalTrigInjecConv_T2_TRGO: Timer2 TRGO event selected
1308 * @arg ADC_ExternalTrigInjecConv_T3_CC2: Timer3 capture compare2 selected
1309 * @arg ADC_ExternalTrigInjecConv_T3_CC4: Timer3 capture compare4 selected
1310 * @arg ADC_ExternalTrigInjecConv_T4_CC1: Timer4 capture compare1 selected
1311 * @arg ADC_ExternalTrigInjecConv_T4_CC2: Timer4 capture compare2 selected
1312 * @arg ADC_ExternalTrigInjecConv_T4_CC3: Timer4 capture compare3 selected
1313 * @arg ADC_ExternalTrigInjecConv_T4_TRGO: Timer4 TRGO event selected
1314 * @arg ADC_ExternalTrigInjecConv_T5_CC4: Timer5 capture compare4 selected
1315 * @arg ADC_ExternalTrigInjecConv_T5_TRGO: Timer5 TRGO event selected
1316 * @arg ADC_ExternalTrigInjecConv_T8_CC2: Timer8 capture compare2 selected
1317 * @arg ADC_ExternalTrigInjecConv_T8_CC3: Timer8 capture compare3 selected
1318 * @arg ADC_ExternalTrigInjecConv_T8_CC4: Timer8 capture compare4 selected
1319 * @arg ADC_ExternalTrigInjecConv_Ext_IT15: External interrupt line 15 event selected
1322 void ADC_ExternalTrigInjectedConvConfig(ADC_TypeDef* ADCx, uint32_t ADC_ExternalTrigInjecConv)
1324 uint32_t tmpreg = 0;
1325 /* Check the parameters */
1326 assert_param(IS_ADC_ALL_PERIPH(ADCx));
1327 assert_param(IS_ADC_EXT_INJEC_TRIG(ADC_ExternalTrigInjecConv));
1329 /* Get the old register value */
1332 /* Clear the old external event selection for injected group */
1333 tmpreg &= CR2_JEXTSEL_RESET;
1335 /* Set the external event selection for injected group */
1336 tmpreg |= ADC_ExternalTrigInjecConv;
1338 /* Store the new register value */
1343 * @brief Configures the ADCx external trigger edge for injected channels conversion.
1344 * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
1345 * @param ADC_ExternalTrigInjecConvEdge: specifies the ADC external trigger edge
1346 * to start injected conversion.
1347 * This parameter can be one of the following values:
1348 * @arg ADC_ExternalTrigInjecConvEdge_None: external trigger disabled for
1349 * injected conversion
1350 * @arg ADC_ExternalTrigInjecConvEdge_Rising: detection on rising edge
1351 * @arg ADC_ExternalTrigInjecConvEdge_Falling: detection on falling edge
1352 * @arg ADC_ExternalTrigInjecConvEdge_RisingFalling: detection on both rising
1356 void ADC_ExternalTrigInjectedConvEdgeConfig(ADC_TypeDef* ADCx, uint32_t ADC_ExternalTrigInjecConvEdge)
1358 uint32_t tmpreg = 0;
1359 /* Check the parameters */
1360 assert_param(IS_ADC_ALL_PERIPH(ADCx));
1361 assert_param(IS_ADC_EXT_INJEC_TRIG_EDGE(ADC_ExternalTrigInjecConvEdge));
1362 /* Get the old register value */
1364 /* Clear the old external trigger edge for injected group */
1365 tmpreg &= CR2_JEXTEN_RESET;
1366 /* Set the new external trigger edge for injected group */
1367 tmpreg |= ADC_ExternalTrigInjecConvEdge;
1368 /* Store the new register value */
1373 * @brief Enables the selected ADC software start conversion of the injected channels.
1374 * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
1377 void ADC_SoftwareStartInjectedConv(ADC_TypeDef* ADCx)
1379 /* Check the parameters */
1380 assert_param(IS_ADC_ALL_PERIPH(ADCx));
1381 /* Enable the selected ADC conversion for injected group */
1382 ADCx->CR2 |= (uint32_t)ADC_CR2_JSWSTART;
1386 * @brief Gets the selected ADC Software start injected conversion Status.
1387 * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
1388 * @retval The new state of ADC software start injected conversion (SET or RESET).
1390 FlagStatus ADC_GetSoftwareStartInjectedConvCmdStatus(ADC_TypeDef* ADCx)
1392 FlagStatus bitstatus = RESET;
1393 /* Check the parameters */
1394 assert_param(IS_ADC_ALL_PERIPH(ADCx));
1396 /* Check the status of JSWSTART bit */
1397 if ((ADCx->CR2 & ADC_CR2_JSWSTART) != (uint32_t)RESET)
1399 /* JSWSTART bit is set */
1404 /* JSWSTART bit is reset */
1407 /* Return the JSWSTART bit status */
1412 * @brief Enables or disables the selected ADC automatic injected group
1413 * conversion after regular one.
1414 * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
1415 * @param NewState: new state of the selected ADC auto injected conversion
1416 * This parameter can be: ENABLE or DISABLE.
1419 void ADC_AutoInjectedConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
1421 /* Check the parameters */
1422 assert_param(IS_ADC_ALL_PERIPH(ADCx));
1423 assert_param(IS_FUNCTIONAL_STATE(NewState));
1424 if (NewState != DISABLE)
1426 /* Enable the selected ADC automatic injected group conversion */
1427 ADCx->CR1 |= (uint32_t)ADC_CR1_JAUTO;
1431 /* Disable the selected ADC automatic injected group conversion */
1432 ADCx->CR1 &= (uint32_t)(~ADC_CR1_JAUTO);
1437 * @brief Enables or disables the discontinuous mode for injected group
1438 * channel for the specified ADC
1439 * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
1440 * @param NewState: new state of the selected ADC discontinuous mode on injected
1442 * This parameter can be: ENABLE or DISABLE.
1445 void ADC_InjectedDiscModeCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
1447 /* Check the parameters */
1448 assert_param(IS_ADC_ALL_PERIPH(ADCx));
1449 assert_param(IS_FUNCTIONAL_STATE(NewState));
1450 if (NewState != DISABLE)
1452 /* Enable the selected ADC injected discontinuous mode */
1453 ADCx->CR1 |= (uint32_t)ADC_CR1_JDISCEN;
1457 /* Disable the selected ADC injected discontinuous mode */
1458 ADCx->CR1 &= (uint32_t)(~ADC_CR1_JDISCEN);
1463 * @brief Returns the ADC injected channel conversion result
1464 * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
1465 * @param ADC_InjectedChannel: the converted ADC injected channel.
1466 * This parameter can be one of the following values:
1467 * @arg ADC_InjectedChannel_1: Injected Channel1 selected
1468 * @arg ADC_InjectedChannel_2: Injected Channel2 selected
1469 * @arg ADC_InjectedChannel_3: Injected Channel3 selected
1470 * @arg ADC_InjectedChannel_4: Injected Channel4 selected
1471 * @retval The Data conversion value.
1473 uint16_t ADC_GetInjectedConversionValue(ADC_TypeDef* ADCx, uint8_t ADC_InjectedChannel)
1475 __IO uint32_t tmp = 0;
1477 /* Check the parameters */
1478 assert_param(IS_ADC_ALL_PERIPH(ADCx));
1479 assert_param(IS_ADC_INJECTED_CHANNEL(ADC_InjectedChannel));
1481 tmp = (uint32_t)ADCx;
1482 tmp += ADC_InjectedChannel + JDR_OFFSET;
1484 /* Returns the selected injected channel conversion data value */
1485 return (uint16_t) (*(__IO uint32_t*) tmp);
1491 /** @defgroup ADC_Group7 Interrupts and flags management functions
1492 * @brief Interrupts and flags management functions
1495 ===============================================================================
1496 Interrupts and flags management functions
1497 ===============================================================================
1499 This section provides functions allowing to configure the ADC Interrupts and
1500 to get the status and clear flags and Interrupts pending bits.
1502 Each ADC provides 4 Interrupts sources and 6 Flags which can be divided into
1505 I. Flags and Interrupts for ADC regular channels
1506 =================================================
1509 1. ADC_FLAG_OVR : Overrun detection when regular converted data are lost
1511 2. ADC_FLAG_EOC : Regular channel end of conversion ==> to indicate (depending
1512 on EOCS bit, managed by ADC_EOCOnEachRegularChannelCmd() ) the end of:
1513 ==> a regular CHANNEL conversion
1514 ==> sequence of regular GROUP conversions .
1516 3. ADC_FLAG_STRT: Regular channel start ==> to indicate when regular CHANNEL
1521 1. ADC_IT_OVR : specifies the interrupt source for Overrun detection event.
1522 2. ADC_IT_EOC : specifies the interrupt source for Regular channel end of
1526 II. Flags and Interrupts for ADC Injected channels
1527 =================================================
1530 1. ADC_FLAG_JEOC : Injected channel end of conversion ==> to indicate at
1531 the end of injected GROUP conversion
1533 2. ADC_FLAG_JSTRT: Injected channel start ==> to indicate hardware when
1534 injected GROUP conversion starts.
1538 1. ADC_IT_JEOC : specifies the interrupt source for Injected channel end of
1541 III. General Flags and Interrupts for the ADC
1542 =================================================
1545 1. ADC_FLAG_AWD: Analog watchdog ==> to indicate if the converted voltage
1546 crosses the programmed thresholds values.
1550 1. ADC_IT_AWD : specifies the interrupt source for Analog watchdog event.
1553 The user should identify which mode will be used in his application to manage
1554 the ADC controller events: Polling mode or Interrupt mode.
1556 In the Polling Mode it is advised to use the following functions:
1557 - ADC_GetFlagStatus() : to check if flags events occur.
1558 - ADC_ClearFlag() : to clear the flags events.
1560 In the Interrupt Mode it is advised to use the following functions:
1561 - ADC_ITConfig() : to enable or disable the interrupt source.
1562 - ADC_GetITStatus() : to check if Interrupt occurs.
1563 - ADC_ClearITPendingBit() : to clear the Interrupt pending Bit
1564 (corresponding Flag).
1569 * @brief Enables or disables the specified ADC interrupts.
1570 * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
1571 * @param ADC_IT: specifies the ADC interrupt sources to be enabled or disabled.
1572 * This parameter can be one of the following values:
1573 * @arg ADC_IT_EOC: End of conversion interrupt mask
1574 * @arg ADC_IT_AWD: Analog watchdog interrupt mask
1575 * @arg ADC_IT_JEOC: End of injected conversion interrupt mask
1576 * @arg ADC_IT_OVR: Overrun interrupt enable
1577 * @param NewState: new state of the specified ADC interrupts.
1578 * This parameter can be: ENABLE or DISABLE.
1581 void ADC_ITConfig(ADC_TypeDef* ADCx, uint16_t ADC_IT, FunctionalState NewState)
1583 uint32_t itmask = 0;
1584 /* Check the parameters */
1585 assert_param(IS_ADC_ALL_PERIPH(ADCx));
1586 assert_param(IS_FUNCTIONAL_STATE(NewState));
1587 assert_param(IS_ADC_IT(ADC_IT));
1589 /* Get the ADC IT index */
1590 itmask = (uint8_t)ADC_IT;
1591 itmask = (uint32_t)0x01 << itmask;
1593 if (NewState != DISABLE)
1595 /* Enable the selected ADC interrupts */
1596 ADCx->CR1 |= itmask;
1600 /* Disable the selected ADC interrupts */
1601 ADCx->CR1 &= (~(uint32_t)itmask);
1606 * @brief Checks whether the specified ADC flag is set or not.
1607 * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
1608 * @param ADC_FLAG: specifies the flag to check.
1609 * This parameter can be one of the following values:
1610 * @arg ADC_FLAG_AWD: Analog watchdog flag
1611 * @arg ADC_FLAG_EOC: End of conversion flag
1612 * @arg ADC_FLAG_JEOC: End of injected group conversion flag
1613 * @arg ADC_FLAG_JSTRT: Start of injected group conversion flag
1614 * @arg ADC_FLAG_STRT: Start of regular group conversion flag
1615 * @arg ADC_FLAG_OVR: Overrun flag
1616 * @retval The new state of ADC_FLAG (SET or RESET).
1618 FlagStatus ADC_GetFlagStatus(ADC_TypeDef* ADCx, uint8_t ADC_FLAG)
1620 FlagStatus bitstatus = RESET;
1621 /* Check the parameters */
1622 assert_param(IS_ADC_ALL_PERIPH(ADCx));
1623 assert_param(IS_ADC_GET_FLAG(ADC_FLAG));
1625 /* Check the status of the specified ADC flag */
1626 if ((ADCx->SR & ADC_FLAG) != (uint8_t)RESET)
1628 /* ADC_FLAG is set */
1633 /* ADC_FLAG is reset */
1636 /* Return the ADC_FLAG status */
1641 * @brief Clears the ADCx's pending flags.
1642 * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
1643 * @param ADC_FLAG: specifies the flag to clear.
1644 * This parameter can be any combination of the following values:
1645 * @arg ADC_FLAG_AWD: Analog watchdog flag
1646 * @arg ADC_FLAG_EOC: End of conversion flag
1647 * @arg ADC_FLAG_JEOC: End of injected group conversion flag
1648 * @arg ADC_FLAG_JSTRT: Start of injected group conversion flag
1649 * @arg ADC_FLAG_STRT: Start of regular group conversion flag
1650 * @arg ADC_FLAG_OVR: Overrun flag
1653 void ADC_ClearFlag(ADC_TypeDef* ADCx, uint8_t ADC_FLAG)
1655 /* Check the parameters */
1656 assert_param(IS_ADC_ALL_PERIPH(ADCx));
1657 assert_param(IS_ADC_CLEAR_FLAG(ADC_FLAG));
1659 /* Clear the selected ADC flags */
1660 ADCx->SR = ~(uint32_t)ADC_FLAG;
1664 * @brief Checks whether the specified ADC interrupt has occurred or not.
1665 * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
1666 * @param ADC_IT: specifies the ADC interrupt source to check.
1667 * This parameter can be one of the following values:
1668 * @arg ADC_IT_EOC: End of conversion interrupt mask
1669 * @arg ADC_IT_AWD: Analog watchdog interrupt mask
1670 * @arg ADC_IT_JEOC: End of injected conversion interrupt mask
1671 * @arg ADC_IT_OVR: Overrun interrupt mask
1672 * @retval The new state of ADC_IT (SET or RESET).
1674 ITStatus ADC_GetITStatus(ADC_TypeDef* ADCx, uint16_t ADC_IT)
1676 ITStatus bitstatus = RESET;
1677 uint32_t itmask = 0, enablestatus = 0;
1679 /* Check the parameters */
1680 assert_param(IS_ADC_ALL_PERIPH(ADCx));
1681 assert_param(IS_ADC_IT(ADC_IT));
1683 /* Get the ADC IT index */
1684 itmask = ADC_IT >> 8;
1686 /* Get the ADC_IT enable bit status */
1687 enablestatus = (ADCx->CR1 & ((uint32_t)0x01 << (uint8_t)ADC_IT)) ;
1689 /* Check the status of the specified ADC interrupt */
1690 if (((ADCx->SR & itmask) != (uint32_t)RESET) && enablestatus)
1697 /* ADC_IT is reset */
1700 /* Return the ADC_IT status */
1705 * @brief Clears the ADCx's interrupt pending bits.
1706 * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
1707 * @param ADC_IT: specifies the ADC interrupt pending bit to clear.
1708 * This parameter can be one of the following values:
1709 * @arg ADC_IT_EOC: End of conversion interrupt mask
1710 * @arg ADC_IT_AWD: Analog watchdog interrupt mask
1711 * @arg ADC_IT_JEOC: End of injected conversion interrupt mask
1712 * @arg ADC_IT_OVR: Overrun interrupt mask
1715 void ADC_ClearITPendingBit(ADC_TypeDef* ADCx, uint16_t ADC_IT)
1718 /* Check the parameters */
1719 assert_param(IS_ADC_ALL_PERIPH(ADCx));
1720 assert_param(IS_ADC_IT(ADC_IT));
1721 /* Get the ADC IT index */
1722 itmask = (uint8_t)(ADC_IT >> 8);
1723 /* Clear the selected ADC interrupt pending bits */
1724 ADCx->SR = ~(uint32_t)itmask;
1742 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/