2 ******************************************************************************
3 * @file stm32l0xx_hal_adc.c
4 * @author MCD Application Team
6 * @date 06-February-2015
7 * @brief This file provides firmware functions to manage the following
8 * functionalities of the Analog to Digital Convertor (ADC)
10 * + Initialization and de-initialization functions
11 * ++ Initialization and Configuration of ADC
12 * + Operation functions
13 * ++ Start, stop, get result of conversions of regular
14 * groups, using 3 possible modes : polling, interruption or DMA.
15 * ++ Calibration feature
17 * ++ Analog Watchdog configuration
18 * ++ Regular Channels Configuration
20 * ++ ADC state machine management
21 * ++ Interrupts and flags management
24 ==============================================================================
25 ##### ADC specific features #####
26 ==============================================================================
28 (#) 12-bit, 10-bit, 8-bit or 6-bit configurable resolution.
30 (#) A built-in hardware oversampler allows to improve analog performances
31 while off-loading the related computational burden from the CPU.
33 (#) Interrupt generation at the end of conversion and in case of analog
34 watchdog or overrun events.
36 (#) Single and continuous conversion modes.
38 (#) Scan or discontinuous mode conversion of channel 0 to channel 18.
40 (#) Configurable scan direction (Upward from channel 0 to 18 or Backward from
41 channel 18 to channel 0)
43 (#) Data alignment with in-built data coherency.
45 (#) Channel-wise programmable sampling time.
47 (#) External trigger option with configurable polarity.
49 (#) DMA request generation during regular channel conversion.
51 (#) ADC supply requirements: 2.4 V to 3.6 V at full speed and down to 1.8 V at
54 (#) ADC input range: VREF- =VIN =VREF+.
56 (#) ADC self-calibration.
58 (#) ADC is automatically powered off (AutoOff mode) except during the active
59 conversion phase. This dramatically reduces the power consumption of the
62 (#) Wait mode to prevent ADC overrun in applications with low frequency.
65 ##### How to use this driver #####
66 ==============================================================================
69 (#) Enable the ADC interface
70 As prerequisite, into HAL_ADC_MspInit(), ADC clock must be configured
73 Depending on both possible clock sources: PCLK clock or ADC asynchronous
75 __HAL_RCC_ADC1_CLK_ENABLE();
78 (#) ADC pins configuration
79 (++) Enable the clock for the ADC GPIOs using the following function:
80 __HAL_RCC_GPIOx_CLK_ENABLE();
81 (++) Configure these ADC pins in analog mode using HAL_GPIO_Init();
83 (#) Configure the ADC parameters (conversion resolution, oversampler,
84 data alignment, continuous mode,...) using the HAL_ADC_Init() function.
86 (#) Activate the ADC peripheral using one of the start functions:
87 HAL_ADC_Start(), HAL_ADC_Start_IT() or HAL_ADC_Start_DMA()
89 *** Channels configuration ***
90 ===============================
92 (+) To configure the ADC channels group, use HAL_ADC_ConfigChannel() function.
93 (+) To read the ADC converted values, use the HAL_ADC_GetValue() function.
95 *** DMA feature configuration ***
96 =================================
98 (+) To enable the DMA mode, use the HAL_ADC_Start_DMA() function.
99 (+) To enable the generation of DMA requests continuously at the end of
100 the last DMA transfer, set .Init.DMAContinuousRequests to ENABLE and
101 call HAL_ADC_Init() function.
105 ******************************************************************************
108 * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
110 * Redistribution and use in source and binary forms, with or without modification,
111 * are permitted provided that the following conditions are met:
112 * 1. Redistributions of source code must retain the above copyright notice,
113 * this list of conditions and the following disclaimer.
114 * 2. Redistributions in binary form must reproduce the above copyright notice,
115 * this list of conditions and the following disclaimer in the documentation
116 * and/or other materials provided with the distribution.
117 * 3. Neither the name of STMicroelectronics nor the names of its contributors
118 * may be used to endorse or promote products derived from this software
119 * without specific prior written permission.
121 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
122 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
123 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
124 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
125 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
126 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
127 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
128 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
129 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
130 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
132 ******************************************************************************
135 /* Includes ------------------------------------------------------------------*/
136 #include "stm32l0xx_hal.h"
138 /** @addtogroup STM32L0xx_HAL_Driver
143 * @brief ADC driver modules
147 #ifdef HAL_ADC_MODULE_ENABLED
149 /* Private typedef -----------------------------------------------------------*/
150 /* Private define ------------------------------------------------------------*/
152 /* Delay for ADC stabilization time. */
153 /* Maximum delay is 1us (refer to device datasheet, parameter tSTART). */
155 #define ADC_STAB_DELAY_US ((uint32_t) 1)
157 /* Delay for temperature sensor stabilization time. */
158 /* Maximum delay is 10us (refer to device datasheet, parameter tSTART). */
160 #define ADC_TEMPSENSOR_DELAY_US ((uint32_t) 10)
162 /* Private macro -------------------------------------------------------------*/
163 /* Private variables ---------------------------------------------------------*/
164 /* Private function prototypes -----------------------------------------------*/
165 static HAL_StatusTypeDef ADC_Enable(ADC_HandleTypeDef* hadc);
166 static void ADC_DMAConvCplt(DMA_HandleTypeDef *hdma);
167 static void ADC_DMAHalfConvCplt(DMA_HandleTypeDef *hdma);
168 static void ADC_DMAError(DMA_HandleTypeDef *hdma);
169 static HAL_StatusTypeDef ADC_ConversionStop(ADC_HandleTypeDef* hadc, uint32_t ConversionGroup);
170 static HAL_StatusTypeDef ADC_Disable(ADC_HandleTypeDef* hadc);
171 static void ADC_DelayMicroSecond(uint32_t microSecond);
173 /* Private functions ---------------------------------------------------------*/
175 /** @defgroup ADC_Private_Functions
179 /** @defgroup ADC_Group1 Initialization/de-initialization functions
180 * @brief Initialization and Configuration functions
183 ===============================================================================
184 ##### Initialization and de-initialization functions #####
185 ===============================================================================
186 [..] This section provides functions allowing to:
187 (+) Initialize and configure the ADC.
188 (+) De-initialize the ADC.
196 * @brief Initializes the ADCx peripheral according to the specified parameters
197 * in the ADC_InitStruct.
198 * @note This function is used to configure the global features of the ADC
199 * (ClockPrescaler, Resolution, Data Alignment and number of conversion), however,
200 * the rest of the configuration parameters are specific to the regular
201 * channels group (scan mode activation, continuous mode activation,
202 * External trigger source and edge, DMA continuous request after the
203 * last transfer and End of conversion selection).
205 * As prerequisite, into HAL_ADC_MspInit(), ADC clock must be
206 * configured at RCC top level.
207 * See commented example code below that can be copied
208 * and uncommented into HAL_ADC_MspInit().
210 * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
211 * the configuration information for the specified ADC.
214 HAL_StatusTypeDef HAL_ADC_Init(ADC_HandleTypeDef* hadc)
216 uint32_t tickstart = 0x00;
218 /* Check ADC handle */
224 /* Check the parameters */
225 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
226 assert_param(IS_ADC_CLOCKPRESCALER(hadc->Init.ClockPrescaler));
227 assert_param(IS_ADC_RESOLUTION(hadc->Init.Resolution));
228 assert_param(IS_ADC_SAMPLE_TIME(hadc->Init.SamplingTime));
229 assert_param(IS_ADC_SCAN_MODE(hadc->Init.ScanConvMode));
230 assert_param(IS_ADC_DATA_ALIGN(hadc->Init.DataAlign));
231 assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
232 assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DiscontinuousConvMode));
233 assert_param(IS_ADC_EXTTRIG_EDGE(hadc->Init.ExternalTrigConvEdge));
234 assert_param(IS_ADC_EXTERNAL_TRIG_CONV(hadc->Init.ExternalTrigConv));
235 assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DMAContinuousRequests));
236 assert_param(IS_ADC_EOC_SELECTION(hadc->Init.EOCSelection));
237 assert_param(IS_ADC_OVERRUN(hadc->Init.Overrun));
238 assert_param(IS_FUNCTIONAL_STATE(hadc->Init.LowPowerAutoWait));
239 assert_param(IS_FUNCTIONAL_STATE(hadc->Init.LowPowerFrequencyMode));
240 assert_param(IS_FUNCTIONAL_STATE(hadc->Init.LowPowerAutoPowerOff));
241 assert_param(IS_FUNCTIONAL_STATE(hadc->Init.OversamplingMode));
243 if(hadc->State == HAL_ADC_STATE_RESET)
245 /* Init the low level hardware */
246 HAL_ADC_MspInit(hadc);
249 /* Configuration of ADC parameters if previous preliminary actions are */
250 /* correctly completed. */
251 /* and if there is no conversion on going (ADC can be enabled anyway, */
252 /* in case of call of this function to update a parameter */
254 if ((hadc->State == HAL_ADC_STATE_ERROR) ||
255 (ADC_IS_CONVERSION_ONGOING(hadc) != RESET) )
257 /* Update ADC state machine to error */
258 hadc->State = HAL_ADC_STATE_ERROR;
259 /* Process unlocked */
264 /* Initialize the ADC state */
265 hadc->State = HAL_ADC_STATE_BUSY;
267 /* Configuration of ADC clock: clock source PCLK or asynchronous with
268 selectable prescaler */
269 __HAL_ADC_CLOCK_PRESCALER(hadc);
271 /* Set the Low Frequency mode */
272 ADC->CCR &= (uint32_t)~ADC_CCR_LFMEN;
273 ADC->CCR |=__HAL_ADC_CCR_LOWFREQUENCY(hadc->Init.LowPowerFrequencyMode);
275 /* Enable voltage regulator (if disabled at this step) */
276 if (HAL_IS_BIT_CLR(hadc->Instance->CR, ADC_CR_ADVREGEN))
278 /* Disable the ADC (if not already disabled) */
279 if (ADC_IS_ENABLE(hadc) != RESET )
281 /* Check if conditions to disable the ADC are fulfilled */
282 if (ADC_DISABLING_CONDITIONS(hadc) != RESET)
284 __HAL_ADC_DISABLE(hadc);
288 hadc->State= HAL_ADC_STATE_ERROR;
290 /* Process unlocked */
297 tickstart = HAL_GetTick();
299 /* Wait for disabling completion */
300 while(HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADEN))
302 /* Check for the Timeout */
303 if(ADC_ENABLE_TIMEOUT != HAL_MAX_DELAY)
305 if((HAL_GetTick() - tickstart ) > ADC_DISABLE_TIMEOUT)
307 hadc->State= HAL_ADC_STATE_TIMEOUT;
309 /* Process unlocked */
319 /* Set ADVREGEN bit */
320 hadc->Instance->CR |= ADC_CR_ADVREGEN;
323 /* Configuration of ADC: */
325 /* - Data alignment */
326 /* - Scan direction */
327 /* - External trigger to start conversion */
328 /* - External trigger polarity */
329 /* - Continuous conversion mode */
330 /* - DMA continuous request */
332 /* - AutoDelay feature */
333 /* - Discontinuous mode */
334 hadc->Instance->CFGR1 &= ~( ADC_CFGR1_RES |
346 hadc->Instance->CFGR1 |= ( hadc->Init.Resolution |
347 hadc->Init.DataAlign |
348 ADC_SCANDIR(hadc->Init.ScanConvMode) |
349 hadc->Init.ExternalTrigConvEdge |
350 ADC_CONTINUOUS(hadc->Init.ContinuousConvMode) |
351 ADC_DMACONTREQ(hadc->Init.DMAContinuousRequests) |
353 __HAL_ADC_CFGR1_AutoDelay(hadc->Init.LowPowerAutoWait) |
354 __HAL_ADC_CFGR1_AUTOFF(hadc->Init.LowPowerAutoPowerOff));
356 /* Configure the external trigger only if Conversion edge is not "NONE" */
357 if (hadc->Init.ExternalTrigConvEdge != ADC_EXTERNALTRIGCONVEDGE_NONE)
359 hadc->Instance->CFGR1 |= hadc->Init.ExternalTrigConv;
362 /* Enable discontinuous mode only if continuous mode is disabled */
363 if ((hadc->Init.DiscontinuousConvMode == ENABLE) && (hadc->Init.ContinuousConvMode == DISABLE))
365 /* Enable the selected ADC discontinuous mode */
366 hadc->Instance->CFGR1 |= ( ADC_CFGR1_DISCEN);
369 if (hadc->Init.OversamplingMode == ENABLE)
371 assert_param(IS_ADC_OVERSAMPLING_RATIO(hadc->Init.Oversample.Ratio));
372 assert_param(IS_ADC_RIGHT_BIT_SHIFT(hadc->Init.Oversample.RightBitShift));
373 assert_param(IS_ADC_TRIGGERED_OVERSAMPLING_MODE(hadc->Init.Oversample.TriggeredMode));
375 /* Configuration of Oversampler: */
376 /* - Oversampling Ratio */
377 /* - Right bit shift */
378 /* - Triggered mode */
380 hadc->Instance->CFGR2 &= ~( ADC_CFGR2_OVSR |
384 hadc->Instance->CFGR2 |= ( hadc->Init.Oversample.Ratio |
385 hadc->Init.Oversample.RightBitShift |
386 hadc->Init.Oversample.TriggeredMode );
388 /* Enable OverSampling mode */
389 hadc->Instance->CFGR2 |= ADC_CFGR2_OVSE;
393 /* Disable OverSampling mode */
394 hadc->Instance->CFGR2 &= ~ADC_CFGR2_OVSE;
397 /* Clear the old sampling time */
398 hadc->Instance->SMPR &= (uint32_t)(~ADC_SMPR_SMPR);
400 /* Set the new sample time */
401 hadc->Instance->SMPR |= hadc->Init.SamplingTime;
404 /* Set ADC error code to none */
405 hadc->ErrorCode = HAL_ADC_ERROR_NONE;
407 /* Initialize the ADC state */
408 hadc->State = HAL_ADC_STATE_READY;
410 /* Return function status */
415 * @brief Deinitialize the ADC peripheral registers to its default reset values.
416 * @note To not impact other ADCs, reset of common ADC registers have been
417 * left commented below.
418 * If needed, the example code can be copied and uncommented into
419 * function HAL_ADC_MspDeInit().
420 * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
421 * the configuration information for the specified ADC.
424 HAL_StatusTypeDef HAL_ADC_DeInit(ADC_HandleTypeDef* hadc)
426 uint32_t tickstart = 0;
428 /* Check ADC handle */
434 /* Check the parameters */
435 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
437 /* Change ADC state */
438 hadc->State = HAL_ADC_STATE_BUSY;
440 /* Stop potential conversion ongoing */
441 if (HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADSTART) && HAL_IS_BIT_CLR(hadc->Instance->CR, ADC_CR_ADDIS))
443 /* Stop regular conversion */
444 hadc->Instance->CR |= ADC_CR_ADSTP;
447 /* Disable ADC: Solution to recover from an unknown ADC state (for example, */
448 /* in case of forbidden action on register bits) */
449 /* Procedure to disable the ADC peripheral: wait for conversions */
450 /* effectively stopped, then disable ADC */
451 /* 1. Wait until ADSTART = 0 */
454 tickstart = HAL_GetTick();
456 while(HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADSTART))
458 /* Check for the Timeout */
459 if(ADC_STOP_CONVERSION_TIMEOUT != HAL_MAX_DELAY)
461 if((HAL_GetTick() - tickstart ) > ADC_STOP_CONVERSION_TIMEOUT)
463 hadc->State= HAL_ADC_STATE_TIMEOUT;
465 /* Process unlocked */
473 /* 2. Disable the ADC peripheral */
474 __HAL_ADC_DISABLE(hadc);
477 /* Reset ADC registers****************/
478 /* Reset register IER */
479 __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_AWD | ADC_IT_OVR | ADC_IT_EOCAL | ADC_IT_EOS | \
480 ADC_IT_EOC | ADC_IT_RDY | ADC_IT_EOSMP ));
482 /* Reset register ISR */
483 __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_AWD | ADC_FLAG_EOCAL | ADC_FLAG_OVR | ADC_FLAG_EOS | \
484 ADC_FLAG_EOC | ADC_FLAG_EOSMP | ADC_FLAG_RDY));
486 /* Reset register CR */
487 /* Disable voltage regulator */
488 /* Note: Regulator disable useful for power saving */
489 /* Reset ADVREGEN bit */
490 hadc->Instance->CR &= ~ADC_CR_ADVREGEN;
492 /* Bits ADC_CR_ADSTP, ADC_CR_ADSTART are in access mode "read-set": no direct reset applicable */
495 /* Reset register CFGR1 */
496 hadc->Instance->CFGR1 &= ~(ADC_CFGR1_AWDCH | ADC_CFGR1_AWDEN | ADC_CFGR1_AWDSGL | \
497 ADC_CFGR1_DISCEN | ADC_CFGR1_AUTOFF | ADC_CFGR1_AUTDLY | \
498 ADC_CFGR1_CONT | ADC_CFGR1_OVRMOD | ADC_CFGR1_EXTEN | \
499 ADC_CFGR1_EXTSEL | ADC_CFGR1_ALIGN | ADC_CFGR1_RES | \
500 ADC_CFGR1_SCANDIR| ADC_CFGR1_DMACFG | ADC_CFGR1_DMAEN);
502 /* Reset register CFGR2 */
503 hadc->Instance->CFGR2 &= ~(ADC_CFGR2_TOVS | ADC_CFGR2_OVSS | ADC_CFGR2_OVSR | \
504 ADC_CFGR2_OVSE | ADC_CFGR2_CKMODE );
506 /* Reset register SMPR */
507 hadc->Instance->SMPR &= ~(ADC_SMPR_SMPR);
509 /* Reset register TR */
510 hadc->Instance->TR &= ~(ADC_TR_LT | ADC_TR_HT);
512 /* Reset register CALFACT */
513 hadc->Instance->CALFACT &= ~(ADC_CALFACT_CALFACT);
515 /* Reset register DR */
516 /* bits in access mode read only, no direct reset applicable*/
518 /* Reset register CALFACT */
519 hadc->Instance->CALFACT &= ~(ADC_CALFACT_CALFACT);
522 /* DeInit the low level hardware */
523 HAL_ADC_MspDeInit(hadc);
525 /* Set ADC error code to none */
526 hadc->ErrorCode = HAL_ADC_ERROR_NONE;
528 /* Change ADC state */
529 hadc->State = HAL_ADC_STATE_RESET;
531 /* Return function status */
536 * @brief Initializes the ADC MSP.
537 * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
538 * the configuration information for the specified ADC.
541 __weak void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
543 /* NOTE : This function Should not be modified, when the callback is needed,
544 the HAL_ADC_MspInit could be implemented in the user file
549 * @brief DeInitializes the ADC MSP.
550 * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
551 * the configuration information for the specified ADC.
554 __weak void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc)
556 /* NOTE : This function Should not be modified, when the callback is needed,
557 the HAL_ADC_MspDeInit could be implemented in the user file
565 /** @defgroup ADC_Group2 I/O operation functions
566 * @brief I/O operation functions
569 ===============================================================================
570 ##### IO operation functions #####
571 ===============================================================================
572 [..] This section provides functions allowing to:
573 (+) Start conversion.
575 (+) poll for conversion complete.
576 (+) poll for conversion event.
577 (+) Start conversion and enable interrupt.
578 (+) Stop conversion and disable interrupt.
579 (+) handle ADC interrupt request.
580 (+) Start conversion of regular channel and enable DMA transfer.
581 (+) Stop conversion of regular channel and disable DMA transfer.
582 (+) Get result of regular channel conversion.
583 (+) Handle ADC interrupt request.
591 * @brief Enables ADC and starts conversion of the regular channels.
592 * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
593 * the configuration information for the specified ADC.
596 HAL_StatusTypeDef HAL_ADC_Start(ADC_HandleTypeDef* hadc)
598 HAL_StatusTypeDef tmpHALStatus = HAL_OK;
600 /* Check the parameters */
601 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
603 /* Perform ADC enable and conversion start if no conversion is on going */
604 if (ADC_IS_CONVERSION_ONGOING(hadc) == RESET)
609 /* Change ADC state */
610 hadc->State = HAL_ADC_STATE_BUSY_REG;
612 /* Set ADC error code to none */
613 hadc->ErrorCode = HAL_ADC_ERROR_NONE;
615 /* Enable the ADC peripheral */
616 /* If low power mode AutoPowerOff is enabled, power-on/off phases are */
617 /* performed automatically by hardware. */
618 if (hadc->Init.LowPowerAutoPowerOff != ENABLE)
620 tmpHALStatus = ADC_Enable(hadc);
623 /* Start conversion if ADC is effectively enabled */
624 if (tmpHALStatus != HAL_ERROR)
626 /* ADC start conversion command */
627 hadc->Instance->CR |= ADC_CR_ADSTART;
630 /* Process unlocked */
635 tmpHALStatus = HAL_BUSY;
638 /* Return function status */
643 * @brief Stop ADC conversion of regular channels, disable ADC peripheral.
644 * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
645 * the configuration information for the specified ADC.
648 HAL_StatusTypeDef HAL_ADC_Stop(ADC_HandleTypeDef* hadc)
650 HAL_StatusTypeDef tmpHALStatus = HAL_OK;
655 /* 1. Stop potential conversion ongoing (regular conversion) */
656 tmpHALStatus = ADC_ConversionStop(hadc, ADC_REGULAR_GROUP);
658 /* 2. Disable ADC peripheral if conversions are effectively stopped */
659 if (tmpHALStatus != HAL_ERROR)
661 /* Disable the ADC peripheral */
662 tmpHALStatus = ADC_Disable(hadc);
664 /* Check if ADC is effectively disabled */
665 if ((hadc->State != HAL_ADC_STATE_ERROR) && (tmpHALStatus != HAL_ERROR))
667 /* Change ADC state */
668 hadc->State = HAL_ADC_STATE_READY;
680 /* Process unlocked */
683 /* Return function status */
688 * @brief Wait for regular group conversion to be completed.
689 * @note ADC conversion flags EOS (end of sequence) and EOC (end of
690 * conversion) are cleared by this function, with an exception:
691 * if low power feature "LowPowerAutoWait" is enabled, flags are
692 * not cleared to not interfere with this feature until data register
693 * is read using function HAL_ADC_GetValue().
694 * @note This function cannot be used in a particular setup: ADC configured
695 * in DMA mode and polling for end of each conversion (ADC init
696 * parameter "EOCSelection" set to ADC_EOC_SINGLE_CONV).
697 * In this case, DMA resets the flag EOC and polling cannot be
698 * performed on each conversion. Nevertheless, polling can still
699 * be performed on the complete sequence.
700 * @param hadc: ADC handle
701 * @param Timeout: Timeout value in millisecond.
704 HAL_StatusTypeDef HAL_ADC_PollForConversion(ADC_HandleTypeDef* hadc, uint32_t Timeout)
706 uint32_t tickstart = 0;
707 uint32_t tmp_Flag_EOC;
709 /* Check the parameters */
710 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
712 /* If end of conversion selected to end of sequence */
713 if (hadc->Init.EOCSelection == ADC_EOC_SEQ_CONV)
715 tmp_Flag_EOC = ADC_FLAG_EOS;
717 /* If end of conversion selected to end of each conversion */
718 else /* ADC_EOC_SINGLE_CONV */
720 /* Verification that ADC configuration is compliant with polling for */
721 /* each conversion: */
722 /* Particular case is ADC configured in DMA mode and ADC sequencer with */
723 /* several ranks and polling for end of each conversion. */
724 /* For code simplicity sake, this particular case is generalized to */
725 /* ADC configured in DMA mode and and polling for end of each conversion. */
726 if (HAL_IS_BIT_SET(hadc->Instance->CFGR1, ADC_CFGR1_DMAEN))
728 /* Update ADC state machine to error */
729 hadc->State = HAL_ADC_STATE_ERROR;
731 /* Process unlocked */
738 tmp_Flag_EOC = (ADC_FLAG_EOC | ADC_FLAG_EOS);
743 tickstart = HAL_GetTick();
745 /* Wait until End of Conversion flag is raised */
746 while(HAL_IS_BIT_CLR(hadc->Instance->ISR, tmp_Flag_EOC))
748 /* Check if timeout is disabled (set to infinite wait) */
749 if(Timeout != HAL_MAX_DELAY)
751 if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
753 /* Update ADC state machine to timeout */
754 hadc->State = HAL_ADC_STATE_TIMEOUT;
756 /* Process unlocked */
764 /* Clear end of conversion flag of regular group if low power feature */
765 /* "LowPowerAutoWait " is disabled, to not interfere with this feature */
766 /* until data register is read using function HAL_ADC_GetValue(). */
767 if (hadc->Init.LowPowerAutoWait == DISABLE)
769 /* Clear regular group conversion flag */
770 __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS));
773 /* Update state machine on conversion status if not in error state */
774 if(hadc->State != HAL_ADC_STATE_ERROR)
776 /* Change ADC state */
777 hadc->State = HAL_ADC_STATE_EOC;
780 /* Return ADC state */
785 * @brief Poll for conversion event.
786 * @param hadc: ADC handle.
787 * @param EventType: the ADC event type.
788 * This parameter can be one of the following values:
789 * @arg ADC_AWD_EVENT: ADC Analog watchdog event.
790 * @arg ADC_OVR_EVENT: ADC Overrun event.
791 * @param Timeout: Timeout value in millisecond.
794 HAL_StatusTypeDef HAL_ADC_PollForEvent(ADC_HandleTypeDef* hadc, uint32_t EventType, uint32_t Timeout)
796 uint32_t tickstart = 0;
798 /* Check the parameters */
799 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
800 assert_param(IS_ADC_EVENT_TYPE(EventType));
803 tickstart = HAL_GetTick();
805 /* Check selected event flag */
806 while(!(__HAL_ADC_GET_FLAG(hadc,EventType)))
808 /* Check if timeout is disabled (set to infinite wait) */
809 if(Timeout != HAL_MAX_DELAY)
811 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
813 /* Update ADC state machine to timeout */
814 hadc->State = HAL_ADC_STATE_TIMEOUT;
816 /* Process unlocked */
826 /* Check analog watchdog flag */
828 /* Change ADC state */
829 hadc->State = HAL_ADC_STATE_AWD;
831 /* Clear ADC analog watchdog flag */
832 __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD);
835 /* Case ADC_OVR_EVENT */
837 /* Change ADC state */
838 hadc->State = HAL_ADC_STATE_ERROR;
840 /* Clear ADC Overrun flag */
841 __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
845 /* Return ADC state */
850 * @brief Enables the interrupt and starts ADC conversion of regular channels.
851 * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
852 * the configuration information for the specified ADC.
853 * @retval HAL status.
855 HAL_StatusTypeDef HAL_ADC_Start_IT(ADC_HandleTypeDef* hadc)
857 HAL_StatusTypeDef tmpHALStatus = HAL_OK;
859 /* Check the parameters */
860 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
862 /* Perform ADC enable and conversion start if no conversion is on going */
863 if (ADC_IS_CONVERSION_ONGOING(hadc) == RESET)
868 /* State machine update: Change ADC state */
869 hadc->State = HAL_ADC_STATE_BUSY_REG;
871 /* Set ADC error code to none */
872 hadc->ErrorCode = HAL_ADC_ERROR_NONE;
874 /* Enable the ADC peripheral */
875 /* If low power mode AutoPowerOff is enabled, power-on/off phases are */
876 /* performed automatically by hardware. */
877 if (hadc->Init.LowPowerAutoPowerOff != ENABLE)
879 tmpHALStatus = ADC_Enable(hadc);
882 /* Start conversion if ADC is effectively enabled */
883 if (tmpHALStatus != HAL_ERROR)
885 /* Enable ADC overrun interrupt */
886 __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);
888 /* Enable ADC end of conversion interrupt */
889 switch(hadc->Init.EOCSelection)
891 case ADC_EOC_SEQ_CONV:
892 __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC);
893 __HAL_ADC_ENABLE_IT(hadc, ADC_IT_EOS);
895 /* case ADC_EOC_SINGLE_CONV */
897 __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOS);
898 __HAL_ADC_ENABLE_IT(hadc, ADC_IT_EOC);
902 /* ADC start conversion command */
903 hadc->Instance->CR |= ADC_CR_ADSTART;
908 tmpHALStatus = HAL_ERROR;
911 /* Process unlocked */
916 tmpHALStatus = HAL_BUSY;
919 /* Return function status */
924 * @brief Stop ADC conversion of regular channels, disable interruptions
925 * EOC/EOS/OVR, disable ADC peripheral.
926 * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
927 * the configuration information for the specified ADC.
930 HAL_StatusTypeDef HAL_ADC_Stop_IT(ADC_HandleTypeDef* hadc)
932 HAL_StatusTypeDef tmpHALStatus = HAL_OK;
937 /* 1. Stop potential conversion ongoing (regular conversion) */
938 tmpHALStatus = ADC_ConversionStop(hadc, ADC_REGULAR_GROUP);
940 /* 2. Disable ADC peripheral if conversions are effectively stopped */
941 if (tmpHALStatus != HAL_ERROR)
943 /* Disable ADC interrupts */
944 __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_EOS | ADC_IT_OVR));
946 /* Disable the ADC peripheral */
947 tmpHALStatus = ADC_Disable(hadc);
949 /* Check if ADC is effectively disabled */
950 if ((hadc->State != HAL_ADC_STATE_ERROR) && (tmpHALStatus != HAL_ERROR))
952 /* Change ADC state */
953 hadc->State = HAL_ADC_STATE_READY;
966 /* Process unlocked */
969 /* Return function status */
974 * @brief Handles ADC interrupt request
975 * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
976 * the configuration information for the specified ADC.
979 void HAL_ADC_IRQHandler(ADC_HandleTypeDef* hadc)
981 /* Check the parameters */
982 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
983 assert_param(IS_ADC_EOC_SELECTION(hadc->Init.EOCSelection));
986 /* Check End of Conversion flag for regular channels */
987 if( (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOC) && __HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_EOC)) || \
988 (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOS) && __HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_EOS)) )
990 /* Change ADC state */
991 hadc->State = HAL_ADC_STATE_EOC;
994 /* Disable interruption if no further conversion upcoming by continuous mode or external trigger */
995 if((hadc->Init.ContinuousConvMode == DISABLE) && \
996 (hadc->Init.ExternalTrigConvEdge == ADC_EXTERNALTRIGCONVEDGE_NONE)
999 /* Allowed to modify bits ADC_IT_EOC/ADC_IT_EOS only if bit ADSTART==0 (no conversion on going) */
1000 if (HAL_IS_BIT_CLR(hadc->Instance->CR, ADC_CR_ADSTART))
1002 /* Cases of interruption after each conversion or after each sequence */
1003 /* If interruption after each sequence */
1004 if (hadc->Init.EOCSelection == ADC_EOC_SEQ_CONV)
1006 /* Allowed to modify bits ADC_IT_EOC/ADC_IT_EOS/ADC_IT_OVR only if bit*/
1007 /* ADSTART==0 (no conversion on going) */
1008 if (ADC_IS_CONVERSION_ONGOING(hadc) == RESET)
1010 /* If End of Sequence is reached, disable interrupts */
1011 if( __HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOS) )
1013 /* DISABLE ADC end of sequence conversion interrupt */
1014 /* DISABLE ADC overrun interrupt */
1015 __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC | ADC_IT_EOS | ADC_IT_OVR);
1020 /* Change ADC state to error state */
1021 hadc->State = HAL_ADC_STATE_ERROR;
1022 /* Set ADC error code to ADC IP internal error */
1023 hadc->ErrorCode |= HAL_ADC_ERROR_INTERNAL;
1026 /* If interruption after each conversion */
1029 /* DISABLE ADC end of single conversion interrupt */
1030 /* DISABLE ADC overrun interrupt */
1031 __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC | ADC_IT_OVR);
1036 /* Change ADC state to error state */
1037 hadc->State = HAL_ADC_STATE_ERROR;
1041 /* Conversion complete callback */
1042 /* Note: into callback, to determine if callback has been triggered from EOC or EOS, */
1043 /* it is possible to use: if( __HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOS)) */
1044 HAL_ADC_ConvCpltCallback(hadc);
1046 /* Clear regular channels conversion flag */
1047 if (hadc->Init.LowPowerAutoWait != ENABLE)
1049 __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS) );
1054 /* Check Analog watchdog flags */
1055 if( (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_AWD) && __HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_AWD)))
1057 /* Change ADC state */
1058 hadc->State = HAL_ADC_STATE_AWD;
1060 /* Level out of window callback */
1061 HAL_ADC_LevelOutOfWindowCallback(hadc);
1063 /* Clear ADC Analog watchdog flag */
1064 __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD);
1067 /* Check Overrun flag */
1068 if(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_OVR) && __HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_OVR))
1070 /* Change ADC state to overrun state */
1071 hadc->State = HAL_ADC_STATE_ERROR;
1073 /* Set ADC error code to overrun */
1074 hadc->ErrorCode |= HAL_ADC_ERROR_OVR;
1076 /* Clear the Overrun flag */
1077 __HAL_ADC_CLEAR_FLAG(hadc,ADC_FLAG_OVR);
1079 /* Error callback */
1080 HAL_ADC_ErrorCallback(hadc);
1085 * @brief Enables ADC DMA request after last transfer (Single-ADC mode) and enables ADC peripheral
1086 * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
1087 * the configuration information for the specified ADC.
1088 * @param pData: The destination Buffer address.
1089 * @param Length: The length of data to be transferred from ADC peripheral to memory.
1092 HAL_StatusTypeDef HAL_ADC_Start_DMA(ADC_HandleTypeDef* hadc, uint32_t* pData, uint32_t Length)
1094 HAL_StatusTypeDef tmpHALStatus = HAL_OK;
1096 /* Check the parameters */
1097 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
1099 /* Perform ADC enable and conversion start if no conversion is on going */
1100 if (ADC_IS_CONVERSION_ONGOING(hadc) == RESET)
1102 /* Process locked */
1105 /* Change ADC state */
1106 hadc->State = HAL_ADC_STATE_BUSY_REG;
1108 /* Set ADC error code to none */
1109 hadc->ErrorCode = HAL_ADC_ERROR_NONE;
1111 /* Enable the ADC peripheral */
1112 /* If low power mode AutoPowerOff is enabled, power-on/off phases are */
1113 /* performed automatically by hardware. */
1114 if (hadc->Init.LowPowerAutoPowerOff != ENABLE)
1116 tmpHALStatus = ADC_Enable(hadc);
1119 /* Start conversion if ADC is effectively enabled */
1120 if (tmpHALStatus != HAL_ERROR)
1122 /* Enable ADC DMA mode */
1123 hadc->Instance->CFGR1 |= ADC_CFGR1_DMAEN;
1125 /* Set the DMA transfer complete callback */
1126 hadc->DMA_Handle->XferCpltCallback = ADC_DMAConvCplt;
1128 /* Set the DMA half transfer complete callback */
1129 hadc->DMA_Handle->XferHalfCpltCallback = ADC_DMAHalfConvCplt;
1131 /* Set the DMA error callback */
1132 hadc->DMA_Handle->XferErrorCallback = ADC_DMAError;
1134 /* Manage ADC and DMA start: ADC overrun interruption, DMA start,
1135 ADC start (in case of SW start) */
1137 /* Enable ADC overrun interrupt */
1138 __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);
1140 /* Enable the DMA Stream */
1141 HAL_DMA_Start_IT(hadc->DMA_Handle, (uint32_t)&hadc->Instance->DR, (uint32_t)pData, Length);
1143 /* ADC start conversion command */
1144 hadc->Instance->CR |= ADC_CR_ADSTART;
1147 /* Process unlocked */
1152 tmpHALStatus = HAL_BUSY;
1155 /* Return function status */
1156 return tmpHALStatus;
1160 * @brief Disable ADC DMA (Single-ADC mode), disable ADC peripheral
1161 * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
1162 * the configuration information for the specified ADC.
1165 HAL_StatusTypeDef HAL_ADC_Stop_DMA(ADC_HandleTypeDef* hadc)
1167 HAL_StatusTypeDef tmpHALStatus = HAL_OK;
1169 /* Process locked */
1172 /* 1. Stop potential conversion ongoing (regular conversion) */
1173 tmpHALStatus = ADC_ConversionStop(hadc, ADC_REGULAR_GROUP);
1175 /* 2. Disable ADC peripheral if conversions are effectively stopped */
1176 if (tmpHALStatus != HAL_ERROR)
1178 /* Disable ADC DMA (ADC DMA configuration ADC_CFGR_DMACFG is kept) */
1179 hadc->Instance->CFGR1 &= ~ADC_CFGR1_DMAEN;
1181 /* Disable the DMA Stream */
1182 if (HAL_DMA_Abort(hadc->DMA_Handle) != HAL_OK)
1184 /* Update ADC state machine to error */
1185 hadc->State = HAL_ADC_STATE_ERROR;
1187 /* Process unlocked */
1193 /* Disable ADC overrun interrupt */
1194 __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);
1196 /* Disable the ADC peripheral */
1197 tmpHALStatus = ADC_Disable(hadc);
1199 /* Check if ADC is effectively disabled */
1200 if ((hadc->State != HAL_ADC_STATE_ERROR) && (tmpHALStatus != HAL_ERROR))
1202 /* Change ADC state */
1203 hadc->State = HAL_ADC_STATE_READY;
1215 /* Process unlocked */
1218 /* Return function status */
1223 * @brief Gets the converted value from data register of regular channel.
1224 * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
1225 * the configuration information for the specified ADC.
1226 * @retval Converted value
1228 uint32_t HAL_ADC_GetValue(ADC_HandleTypeDef* hadc)
1230 /* Return the selected ADC converted value */
1231 return hadc->Instance->DR;
1235 * @brief Regular conversion complete callback in non blocking mode
1236 * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
1237 * the configuration information for the specified ADC.
1240 __weak void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
1242 /* NOTE : This function Should not be modified, when the callback is needed,
1243 the HAL_ADC_ConvCpltCallback could be implemented in the user file
1248 * @brief Regular conversion half DMA transfer callback in non blocking mode
1249 * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
1250 * the configuration information for the specified ADC.
1253 __weak void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc)
1255 /* NOTE : This function Should not be modified, when the callback is needed,
1256 the HAL_ADC_ConvHalfCpltCallback could be implemented in the user file
1261 * @brief Analog watchdog callback in non blocking mode
1262 * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
1263 * the configuration information for the specified ADC.
1266 __weak void HAL_ADC_LevelOutOfWindowCallback(ADC_HandleTypeDef* hadc)
1268 /* NOTE : This function Should not be modified, when the callback is needed,
1269 the HAL_ADC_LevelOoutOfWindowCallback could be implemented in the user file
1274 * @brief Error ADC callback.
1275 * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
1276 * the configuration information for the specified ADC.
1279 __weak void HAL_ADC_ErrorCallback(ADC_HandleTypeDef *hadc)
1281 /* NOTE : This function Should not be modified, when the callback is needed,
1282 the HAL_ADC_ErrorCallback could be implemented in the user file
1290 /** @defgroup ADC_Group3 Peripheral Control functions
1291 * @brief Peripheral Control functions
1294 ===============================================================================
1295 ##### Peripheral Control functions #####
1296 ===============================================================================
1297 [..] This section provides functions allowing to:
1298 (+) Configure channels.
1299 (+) Configure the analog watch dog.
1307 * @brief Configures the selected ADC regular channel: sampling time,
1309 * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
1310 * the configuration information for the specified ADC.
1311 * @param sConfig: ADC regular channel configuration structure.
1312 * @retval HAL status
1314 HAL_StatusTypeDef HAL_ADC_ConfigChannel(ADC_HandleTypeDef* hadc, ADC_ChannelConfTypeDef* sConfig)
1316 /* Check the parameters */
1317 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
1318 assert_param(IS_ADC_CHANNEL(sConfig->Channel));
1320 /* Process locked */
1323 /* Parameters update conditioned to ADC state: */
1324 /* Parameters that can be updated when ADC is disabled or enabled without */
1325 /* conversion on going : */
1326 /* - Channel number */
1327 /* - Management of internal measurement channels: Vbat/VrefInt/TempSensor */
1328 if (ADC_IS_CONVERSION_ONGOING(hadc) != RESET)
1330 /* Update ADC state machine to error */
1331 hadc->State = HAL_ADC_STATE_ERROR;
1332 /* Process unlocked */
1337 /* Enable selected channels */
1338 hadc->Instance->CHSELR |= (uint32_t)(sConfig->Channel & ADC_CHANNEL_MASK);
1340 /* Management of internal measurement channels: Vlcd/VrefInt/TempSensor */
1341 /* internal measurement paths enable: If internal channel selected, enable */
1342 /* dedicated internal buffers and path. */
1344 /* If Temperature sensor channel is selected, then enable the internal */
1345 /* buffers and path */
1346 if (((sConfig->Channel & ADC_CHANNEL_MASK) & ADC_CHANNEL_TEMPSENSOR ) == (ADC_CHANNEL_TEMPSENSOR & ADC_CHANNEL_MASK))
1348 ADC->CCR |= ADC_CCR_TSEN;
1350 /* Delay for temperature sensor stabilization time */
1351 ADC_DelayMicroSecond(ADC_TEMPSENSOR_DELAY_US);
1354 /* If VRefInt channel is selected, then enable the internal buffers and path */
1355 if (((sConfig->Channel & ADC_CHANNEL_MASK) & ADC_CHANNEL_VREFINT) == (ADC_CHANNEL_VREFINT & ADC_CHANNEL_MASK))
1357 ADC->CCR |= ADC_CCR_VREFEN;
1360 /* If Vlcd channel is selected, then enable the internal buffers and path */
1361 if (((sConfig->Channel & ADC_CHANNEL_MASK) & ADC_CHANNEL_VLCD) == (ADC_CHANNEL_VLCD & ADC_CHANNEL_MASK))
1363 ADC->CCR |= ADC_CCR_VLCDEN;
1366 /* Process unlocked */
1369 /* Return function status */
1374 * @brief Configures the analog watchdog.
1375 * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
1376 * the configuration information for the specified ADC.
1377 * @param AnalogWDGConfig : pointer to an ADC_AnalogWDGConfTypeDef structure
1378 * that contains the configuration information of ADC analog watchdog.
1379 * @retval HAL status
1381 HAL_StatusTypeDef HAL_ADC_AnalogWDGConfig(ADC_HandleTypeDef* hadc, ADC_AnalogWDGConfTypeDef* AnalogWDGConfig)
1383 uint32_t tmpAWDHighThresholdShifted;
1384 uint32_t tmpAWDLowThresholdShifted;
1386 /* Check the parameters */
1387 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
1388 assert_param(IS_ADC_ANALOG_WATCHDOG_MODE(AnalogWDGConfig->WatchdogMode));
1389 assert_param(IS_ADC_CHANNEL(AnalogWDGConfig->Channel));
1390 assert_param(IS_FUNCTIONAL_STATE(AnalogWDGConfig->ITMode));
1392 assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), AnalogWDGConfig->HighThreshold));
1393 assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), AnalogWDGConfig->LowThreshold));
1395 /* Process locked */
1398 /* Parameters update conditioned to ADC state: */
1399 /* Parameters that can be updated when ADC is disabled or enabled without */
1400 /* conversion on going : */
1401 /* - Analog watchdog channels */
1402 /* - Analog watchdog thresholds */
1403 if (ADC_IS_CONVERSION_ONGOING(hadc) != RESET)
1405 /* Update ADC state machine to error */
1406 hadc->State = HAL_ADC_STATE_ERROR;
1407 /* Process unlocked */
1412 /* Configure ADC Analog watchdog interrupt */
1413 if(AnalogWDGConfig->ITMode == ENABLE)
1415 /* Enable the ADC Analog watchdog interrupt */
1416 __HAL_ADC_ENABLE_IT(hadc, ADC_IT_AWD);
1420 /* Disable the ADC Analog watchdog interrupt */
1421 __HAL_ADC_DISABLE_IT(hadc, ADC_IT_AWD);
1424 /* Configuration of analog watchdog: */
1425 /* - Set the analog watchdog mode */
1426 /* - Set the Analog watchdog channel (is not used if watchdog */
1427 /* mode "all channels": ADC_CFGR1_AWD1SGL=0) */
1428 hadc->Instance->CFGR1 &= ~( ADC_CFGR1_AWDSGL |
1432 hadc->Instance->CFGR1 |= ( AnalogWDGConfig->WatchdogMode |
1433 (AnalogWDGConfig->Channel & ADC_CHANNEL_AWD_MASK));
1436 /* Shift the offset in function of the selected ADC resolution: Thresholds */
1437 /* have to be left-aligned on bit 11, the LSB (right bits) are set to 0 */
1438 tmpAWDHighThresholdShifted = ADC_AWD1THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->HighThreshold);
1439 tmpAWDLowThresholdShifted = ADC_AWD1THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->LowThreshold);
1441 /* Clear High & Low high thresholds */
1442 hadc->Instance->TR &= (uint32_t) ~ (ADC_TR_HT | ADC_TR_LT);
1444 /* Set the high threshold */
1445 hadc->Instance->TR = ADC_TRX_HIGHTHRESHOLD (tmpAWDHighThresholdShifted);
1446 /* Set the low threshold */
1447 hadc->Instance->TR |= tmpAWDLowThresholdShifted;
1449 /* Process unlocked */
1452 /* Return function status */
1460 /** @defgroup ADC_Group4 ADC Peripheral State functions
1461 * @brief ADC Peripheral State functions
1464 ===============================================================================
1465 ##### ADC Peripheral State functions #####
1466 ===============================================================================
1468 This subsection provides functions allowing to
1469 (+) Check the ADC state.
1470 (+) handle ADC interrupt request.
1477 * @brief return the ADC state
1478 * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
1479 * the configuration information for the specified ADC.
1482 HAL_ADC_StateTypeDef HAL_ADC_GetState(ADC_HandleTypeDef* hadc)
1484 /* Return ADC state */
1489 * @brief Return the ADC error code
1490 * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
1491 * the configuration information for the specified ADC.
1492 * @retval ADC Error Code
1494 uint32_t HAL_ADC_GetError(ADC_HandleTypeDef *hadc)
1496 return hadc->ErrorCode;
1505 * @brief Enable the selected ADC.
1506 * @note Prerequisite condition to use this function: ADC must be disabled
1507 * and voltage regulator must be enabled (done into HAL_ADC_Init()).
1508 * @note If low power mode AutoPowerOff is enabled, power-on/off phases are
1509 * performed automatically by hardware.
1510 * In this mode, this function is useless and must not be called because
1511 * flag ADC_FLAG_RDY is not usable.
1512 * Therefore, this function must be called under condition of
1513 * "if (hadc->Init.LowPowerAutoPowerOff != ENABLE)".
1514 * @param hadc: ADC handle
1515 * @retval HAL status.
1517 static HAL_StatusTypeDef ADC_Enable(ADC_HandleTypeDef* hadc)
1519 uint32_t tickstart = 0;
1521 /* ADC enable and wait for ADC ready (in case of ADC is disabled or */
1522 /* enabling phase not yet completed: flag ADC ready not yet set). */
1523 /* Timeout implemented to not be stuck if ADC cannot be enabled (possible */
1524 /* causes: ADC clock not running, ...). */
1525 if (ADC_IS_ENABLE(hadc) == RESET)
1527 /* Check if conditions to enable the ADC are fulfilled */
1528 if (ADC_ENABLING_CONDITIONS(hadc) == RESET)
1530 /* Update ADC state machine to error */
1531 hadc->State = HAL_ADC_STATE_ERROR;
1533 /* Set ADC error code to ADC IP internal error */
1534 hadc->ErrorCode |= HAL_ADC_ERROR_INTERNAL;
1539 /* Enable the ADC peripheral */
1540 __HAL_ADC_ENABLE(hadc);
1542 /* Delay for ADC stabilization time. */
1543 ADC_DelayMicroSecond(ADC_STAB_DELAY_US);
1545 /* Wait for ADC effectively enabled */
1547 tickstart = HAL_GetTick();
1549 /* Skip polling for RDY ADRDY when AutoOFF is enabled */
1550 if (hadc->Init.LowPowerAutoPowerOff != ENABLE)
1552 while(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_RDY) == RESET)
1554 /* Check for the Timeout */
1555 if(ADC_ENABLE_TIMEOUT != HAL_MAX_DELAY)
1557 if((HAL_GetTick() - tickstart ) > ADC_ENABLE_TIMEOUT)
1559 /* Update ADC state machine to error */
1560 hadc->State = HAL_ADC_STATE_ERROR;
1562 /* Set ADC error code to ADC IP internal error */
1563 hadc->ErrorCode |= HAL_ADC_ERROR_INTERNAL;
1572 /* Return HAL status */
1577 * @brief Disable the selected ADC.
1578 * @note Prerequisite condition to use this function: ADC conversions must be
1579 * stopped to disable the ADC.
1580 * @param hadc: ADC handle
1581 * @retval HAL status.
1583 static HAL_StatusTypeDef ADC_Disable(ADC_HandleTypeDef* hadc)
1585 uint32_t tickstart = 0;
1587 /* Verification if ADC is not already disabled: */
1588 /* forbidden to disable ADC (set bit ADC_CR_ADDIS) if ADC is already */
1590 if (ADC_IS_ENABLE(hadc) != RESET )
1592 /* Check if conditions to disable the ADC are fulfilled */
1593 if (ADC_DISABLING_CONDITIONS(hadc) != RESET)
1595 /* Disable the ADC peripheral */
1596 __HAL_ADC_DISABLE(hadc);
1600 /* Update ADC state machine to error */
1601 hadc->State = HAL_ADC_STATE_ERROR;
1603 /* Set ADC error code to ADC internal error */
1604 hadc->ErrorCode |= HAL_ADC_ERROR_INTERNAL;
1609 /* Wait for ADC effectively disabled */
1611 tickstart = HAL_GetTick();
1613 while(HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADEN))
1615 /* Check for the Timeout */
1616 if(ADC_ENABLE_TIMEOUT != HAL_MAX_DELAY)
1618 if((HAL_GetTick() - tickstart ) > ADC_DISABLE_TIMEOUT)
1620 /* Update ADC state machine to error */
1621 hadc->State = HAL_ADC_STATE_ERROR;
1623 /* Set ADC error code to ADC internal error */
1624 hadc->ErrorCode |= HAL_ADC_ERROR_INTERNAL;
1632 /* Return HAL status */
1637 * @brief Stop ADC conversion.
1638 * @note Prerequisite condition to use this function: ADC conversions must be
1639 * stopped to disable the ADC.
1640 * @param hadc: ADC handle
1641 * @param ConversionGroup: Only ADC group regular.
1642 * This parameter can be one of the following values:
1643 * @arg ADC_REGULAR_GROUP: ADC regular conversion type.
1644 * @retval HAL status.
1646 static HAL_StatusTypeDef ADC_ConversionStop(ADC_HandleTypeDef* hadc, uint32_t ConversionGroup)
1648 uint32_t tickstart = 0 ;
1650 /* Check the parameters */
1651 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
1652 assert_param(IS_ADC_CONVERSION_GROUP(ConversionGroup));
1654 /* Parameters update conditioned to ADC state: */
1655 /* Parameters that can be updated when ADC is disabled or enabled without */
1656 /* conversion on going : */
1657 if (ADC_IS_CONVERSION_ONGOING(hadc) != RESET)
1659 /* Update ADC state machine to error */
1660 hadc->State = HAL_ADC_STATE_ERROR;
1661 /* Process unlocked */
1665 /* Verification: if ADC is not already stopped, bypass this function */
1666 if (HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADSTART))
1668 /* Stop potential conversion on regular group */
1669 if (ConversionGroup == ADC_REGULAR_GROUP)
1671 /* Software is allowed to set ADSTP only when ADSTART=1 and ADDIS=0 */
1672 if (HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADSTART) && \
1673 HAL_IS_BIT_CLR(hadc->Instance->CR, ADC_CR_ADDIS) )
1675 /* Stop conversions on regular group */
1676 hadc->Instance->CR |= ADC_CR_ADSTP;
1680 /* Wait for conversion effectively stopped */
1682 tickstart = HAL_GetTick();
1684 while((hadc->Instance->CR & ADC_CR_ADSTART) != RESET)
1686 /* Check for the Timeout */
1687 if(ADC_STOP_CONVERSION_TIMEOUT != HAL_MAX_DELAY)
1689 if((HAL_GetTick() - tickstart ) > ADC_STOP_CONVERSION_TIMEOUT)
1691 /* Update ADC state machine to error */
1692 hadc->State = HAL_ADC_STATE_ERROR;
1694 /* Set ADC error code to ADC IP internal error */
1695 hadc->ErrorCode |= HAL_ADC_ERROR_INTERNAL;
1703 /* Return HAL status */
1708 * @brief DMA transfer complete callback.
1709 * @param hdma: pointer to DMA handle.
1712 static void ADC_DMAConvCplt(DMA_HandleTypeDef *hdma)
1714 ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1716 /* Change ADC state */
1717 hadc->State = HAL_ADC_STATE_EOC;
1719 HAL_ADC_ConvCpltCallback(hadc);
1723 * @brief DMA half transfer complete callback.
1724 * @param hdma: pointer to DMA handle.
1727 static void ADC_DMAHalfConvCplt(DMA_HandleTypeDef *hdma)
1729 ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1731 /* Conversion complete callback */
1732 HAL_ADC_ConvHalfCpltCallback(hadc);
1736 * @brief DMA error callback
1737 * @param hdma: pointer to DMA handle.
1740 static void ADC_DMAError(DMA_HandleTypeDef *hdma)
1742 ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1743 hadc->State= HAL_ADC_STATE_ERROR;
1744 /* Set ADC error code to DMA error */
1745 hadc->ErrorCode |= HAL_ADC_ERROR_DMA;
1746 HAL_ADC_ErrorCallback(hadc);
1750 * @brief Delay micro seconds
1751 * @param microSecond : delay
1754 static void ADC_DelayMicroSecond(uint32_t microSecond)
1756 /* Compute number of CPU cycles to wait for */
1757 __IO uint32_t waitLoopIndex = (microSecond * (SystemCoreClock / 1000000));
1759 while(waitLoopIndex != 0)
1769 #endif /* HAL_ADC_MODULE_ENABLED */
1778 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/