STM32L443xx HAL User Manual
stm32l4xx_hal_adc.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32l4xx_hal_adc.c
00004   * @author  MCD Application Team
00005   * @brief   This file provides firmware functions to manage the following
00006   *          functionalities of the Analog to Digital Converter (ADC)
00007   *          peripheral:
00008   *           + Initialization and de-initialization functions
00009   *           + Peripheral Control functions
00010   *           + Peripheral State functions
00011   *          Other functions (extended functions) are available in file
00012   *          "stm32l4xx_hal_adc_ex.c".
00013   *
00014   ******************************************************************************
00015   * @attention
00016   *
00017   * Copyright (c) 2017 STMicroelectronics.
00018   * All rights reserved.
00019   *
00020   * This software is licensed under terms that can be found in the LICENSE file
00021   * in the root directory of this software component.
00022   * If no LICENSE file comes with this software, it is provided AS-IS.
00023   *
00024   ******************************************************************************
00025   @verbatim
00026   ==============================================================================
00027                      ##### ADC peripheral features #####
00028   ==============================================================================
00029   [..]
00030   (+) 12-bit, 10-bit, 8-bit or 6-bit configurable resolution.
00031 
00032   (+) Interrupt generation at the end of regular conversion and in case of
00033       analog watchdog or overrun events.
00034 
00035   (+) Single and continuous conversion modes.
00036 
00037   (+) Scan mode for conversion of several channels sequentially.
00038 
00039   (+) Data alignment with in-built data coherency.
00040 
00041   (+) Programmable sampling time (channel wise)
00042 
00043   (+) External trigger (timer or EXTI) with configurable polarity
00044 
00045   (+) DMA request generation for transfer of conversions data of regular group.
00046 
00047   (+) Configurable delay between conversions in Dual interleaved mode.
00048 
00049   (+) ADC channels selectable single/differential input.
00050 
00051   (+) ADC offset shared on 4 offset instances.
00052   (+) ADC calibration
00053 
00054   (+) ADC conversion of regular group.
00055 
00056   (+) ADC supply requirements: 1.62 V to 3.6 V.
00057 
00058   (+) ADC input range: from Vref- (connected to Vssa) to Vref+ (connected to
00059       Vdda or to an external voltage reference).
00060 
00061 
00062                      ##### How to use this driver #####
00063   ==============================================================================
00064     [..]
00065 
00066      *** Configuration of top level parameters related to ADC ***
00067      ============================================================
00068      [..]
00069 
00070     (#) Enable the ADC interface
00071         (++) As prerequisite, ADC clock must be configured at RCC top level.
00072 
00073         (++) Two clock settings are mandatory:
00074              (+++) ADC clock (core clock, also possibly conversion clock).
00075 
00076              (+++) ADC clock (conversions clock).
00077                    Two possible clock sources: synchronous clock derived from APB clock
00078                    or asynchronous clock derived from system clock, PLLSAI1 or the PLLSAI2
00079                    running up to 80MHz.
00080 
00081              (+++) Example:
00082                    Into HAL_ADC_MspInit() (recommended code location) or with
00083                    other device clock parameters configuration:
00084                (+++) __HAL_RCC_ADC_CLK_ENABLE();                  (mandatory)
00085 
00086                RCC_ADCCLKSOURCE_PLL enable:                       (optional: if asynchronous clock selected)
00087                (+++) RCC_PeriphClkInitTypeDef   RCC_PeriphClkInit;
00088                (+++) PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
00089                (+++) PeriphClkInit.AdcClockSelection    = RCC_ADCCLKSOURCE_PLL;
00090                (+++) HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
00091 
00092         (++) ADC clock source and clock prescaler are configured at ADC level with
00093              parameter "ClockPrescaler" using function HAL_ADC_Init().
00094 
00095     (#) ADC pins configuration
00096          (++) Enable the clock for the ADC GPIOs
00097               using macro __HAL_RCC_GPIOx_CLK_ENABLE()
00098          (++) Configure these ADC pins in analog mode
00099               using function HAL_GPIO_Init()
00100 
00101     (#) Optionally, in case of usage of ADC with interruptions:
00102          (++) Configure the NVIC for ADC
00103               using function HAL_NVIC_EnableIRQ(ADCx_IRQn)
00104          (++) Insert the ADC interruption handler function HAL_ADC_IRQHandler()
00105               into the function of corresponding ADC interruption vector
00106               ADCx_IRQHandler().
00107 
00108     (#) Optionally, in case of usage of DMA:
00109          (++) Configure the DMA (DMA channel, mode normal or circular, ...)
00110               using function HAL_DMA_Init().
00111          (++) Configure the NVIC for DMA
00112               using function HAL_NVIC_EnableIRQ(DMAx_Channelx_IRQn)
00113          (++) Insert the ADC interruption handler function HAL_ADC_IRQHandler()
00114               into the function of corresponding DMA interruption vector
00115               DMAx_Channelx_IRQHandler().
00116 
00117      *** Configuration of ADC, group regular, channels parameters ***
00118      ================================================================
00119      [..]
00120 
00121     (#) Configure the ADC parameters (resolution, data alignment, ...)
00122         and regular group parameters (conversion trigger, sequencer, ...)
00123         using function HAL_ADC_Init().
00124 
00125     (#) Configure the channels for regular group parameters (channel number,
00126         channel rank into sequencer, ..., into regular group)
00127         using function HAL_ADC_ConfigChannel().
00128 
00129     (#) Optionally, configure the analog watchdog parameters (channels
00130         monitored, thresholds, ...)
00131         using function HAL_ADC_AnalogWDGConfig().
00132 
00133      *** Execution of ADC conversions ***
00134      ====================================
00135      [..]
00136 
00137     (#) Optionally, perform an automatic ADC calibration to improve the
00138         conversion accuracy
00139         using function HAL_ADCEx_Calibration_Start().
00140 
00141     (#) ADC driver can be used among three modes: polling, interruption,
00142         transfer by DMA.
00143 
00144         (++) ADC conversion by polling:
00145           (+++) Activate the ADC peripheral and start conversions
00146                 using function HAL_ADC_Start()
00147           (+++) Wait for ADC conversion completion
00148                 using function HAL_ADC_PollForConversion()
00149           (+++) Retrieve conversion results
00150                 using function HAL_ADC_GetValue()
00151           (+++) Stop conversion and disable the ADC peripheral
00152                 using function HAL_ADC_Stop()
00153 
00154         (++) ADC conversion by interruption:
00155           (+++) Activate the ADC peripheral and start conversions
00156                 using function HAL_ADC_Start_IT()
00157           (+++) Wait for ADC conversion completion by call of function
00158                 HAL_ADC_ConvCpltCallback()
00159                 (this function must be implemented in user program)
00160           (+++) Retrieve conversion results
00161                 using function HAL_ADC_GetValue()
00162           (+++) Stop conversion and disable the ADC peripheral
00163                 using function HAL_ADC_Stop_IT()
00164 
00165         (++) ADC conversion with transfer by DMA:
00166           (+++) Activate the ADC peripheral and start conversions
00167                 using function HAL_ADC_Start_DMA()
00168           (+++) Wait for ADC conversion completion by call of function
00169                 HAL_ADC_ConvCpltCallback() or HAL_ADC_ConvHalfCpltCallback()
00170                 (these functions must be implemented in user program)
00171           (+++) Conversion results are automatically transferred by DMA into
00172                 destination variable address.
00173           (+++) Stop conversion and disable the ADC peripheral
00174                 using function HAL_ADC_Stop_DMA()
00175 
00176      [..]
00177 
00178     (@) Callback functions must be implemented in user program:
00179       (+@) HAL_ADC_ErrorCallback()
00180       (+@) HAL_ADC_LevelOutOfWindowCallback() (callback of analog watchdog)
00181       (+@) HAL_ADC_ConvCpltCallback()
00182       (+@) HAL_ADC_ConvHalfCpltCallback
00183 
00184      *** Deinitialization of ADC ***
00185      ============================================================
00186      [..]
00187 
00188     (#) Disable the ADC interface
00189       (++) ADC clock can be hard reset and disabled at RCC top level.
00190         (++) Hard reset of ADC peripherals
00191              using macro __ADCx_FORCE_RESET(), __ADCx_RELEASE_RESET().
00192         (++) ADC clock disable
00193              using the equivalent macro/functions as configuration step.
00194              (+++) Example:
00195                    Into HAL_ADC_MspDeInit() (recommended code location) or with
00196                    other device clock parameters configuration:
00197                (+++) RCC_OscInitStructure.OscillatorType = RCC_OSCILLATORTYPE_HSI14;
00198                (+++) RCC_OscInitStructure.HSI14State = RCC_HSI14_OFF; (if not used for system clock)
00199                (+++) HAL_RCC_OscConfig(&RCC_OscInitStructure);
00200 
00201     (#) ADC pins configuration
00202          (++) Disable the clock for the ADC GPIOs
00203               using macro __HAL_RCC_GPIOx_CLK_DISABLE()
00204 
00205     (#) Optionally, in case of usage of ADC with interruptions:
00206          (++) Disable the NVIC for ADC
00207               using function HAL_NVIC_EnableIRQ(ADCx_IRQn)
00208 
00209     (#) Optionally, in case of usage of DMA:
00210          (++) Deinitialize the DMA
00211               using function HAL_DMA_Init().
00212          (++) Disable the NVIC for DMA
00213               using function HAL_NVIC_EnableIRQ(DMAx_Channelx_IRQn)
00214 
00215     [..]
00216 
00217     *** Callback registration ***
00218     =============================================
00219     [..]
00220 
00221      The compilation flag USE_HAL_ADC_REGISTER_CALLBACKS, when set to 1,
00222      allows the user to configure dynamically the driver callbacks.
00223      Use Functions HAL_ADC_RegisterCallback()
00224      to register an interrupt callback.
00225     [..]
00226 
00227      Function HAL_ADC_RegisterCallback() allows to register following callbacks:
00228        (+) ConvCpltCallback               : ADC conversion complete callback
00229        (+) ConvHalfCpltCallback           : ADC conversion DMA half-transfer callback
00230        (+) LevelOutOfWindowCallback       : ADC analog watchdog 1 callback
00231        (+) ErrorCallback                  : ADC error callback
00232        (+) InjectedConvCpltCallback       : ADC group injected conversion complete callback
00233        (+) InjectedQueueOverflowCallback  : ADC group injected context queue overflow callback
00234        (+) LevelOutOfWindow2Callback      : ADC analog watchdog 2 callback
00235        (+) LevelOutOfWindow3Callback      : ADC analog watchdog 3 callback
00236        (+) EndOfSamplingCallback          : ADC end of sampling callback
00237        (+) MspInitCallback                : ADC Msp Init callback
00238        (+) MspDeInitCallback              : ADC Msp DeInit callback
00239      This function takes as parameters the HAL peripheral handle, the Callback ID
00240      and a pointer to the user callback function.
00241     [..]
00242 
00243      Use function HAL_ADC_UnRegisterCallback to reset a callback to the default
00244      weak function.
00245     [..]
00246 
00247      HAL_ADC_UnRegisterCallback takes as parameters the HAL peripheral handle,
00248      and the Callback ID.
00249      This function allows to reset following callbacks:
00250        (+) ConvCpltCallback               : ADC conversion complete callback
00251        (+) ConvHalfCpltCallback           : ADC conversion DMA half-transfer callback
00252        (+) LevelOutOfWindowCallback       : ADC analog watchdog 1 callback
00253        (+) ErrorCallback                  : ADC error callback
00254        (+) InjectedConvCpltCallback       : ADC group injected conversion complete callback
00255        (+) InjectedQueueOverflowCallback  : ADC group injected context queue overflow callback
00256        (+) LevelOutOfWindow2Callback      : ADC analog watchdog 2 callback
00257        (+) LevelOutOfWindow3Callback      : ADC analog watchdog 3 callback
00258        (+) EndOfSamplingCallback          : ADC end of sampling callback
00259        (+) MspInitCallback                : ADC Msp Init callback
00260        (+) MspDeInitCallback              : ADC Msp DeInit callback
00261      [..]
00262 
00263      By default, after the HAL_ADC_Init() and when the state is HAL_ADC_STATE_RESET
00264      all callbacks are set to the corresponding weak functions:
00265      examples HAL_ADC_ConvCpltCallback(), HAL_ADC_ErrorCallback().
00266      Exception done for MspInit and MspDeInit functions that are
00267      reset to the legacy weak functions in the HAL_ADC_Init()/ HAL_ADC_DeInit() only when
00268      these callbacks are null (not registered beforehand).
00269     [..]
00270 
00271      If MspInit or MspDeInit are not null, the HAL_ADC_Init()/ HAL_ADC_DeInit()
00272      keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
00273      [..]
00274 
00275      Callbacks can be registered/unregistered in HAL_ADC_STATE_READY state only.
00276      Exception done MspInit/MspDeInit functions that can be registered/unregistered
00277      in HAL_ADC_STATE_READY or HAL_ADC_STATE_RESET state,
00278      thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
00279     [..]
00280 
00281      Then, the user first registers the MspInit/MspDeInit user callbacks
00282      using HAL_ADC_RegisterCallback() before calling HAL_ADC_DeInit()
00283      or HAL_ADC_Init() function.
00284      [..]
00285 
00286      When the compilation flag USE_HAL_ADC_REGISTER_CALLBACKS is set to 0 or
00287      not defined, the callback registration feature is not available and all callbacks
00288      are set to the corresponding weak functions.
00289 
00290   @endverbatim
00291   ******************************************************************************
00292   */
00293 
00294 /* Includes ------------------------------------------------------------------*/
00295 #include "stm32l4xx_hal.h"
00296 
00297 /** @addtogroup STM32L4xx_HAL_Driver
00298   * @{
00299   */
00300 
00301 /** @defgroup ADC ADC
00302   * @brief ADC HAL module driver
00303   * @{
00304   */
00305 
00306 #ifdef HAL_ADC_MODULE_ENABLED
00307 
00308 /* Private typedef -----------------------------------------------------------*/
00309 /* Private define ------------------------------------------------------------*/
00310 
00311 /** @defgroup ADC_Private_Constants ADC Private Constants
00312   * @{
00313   */
00314 
00315 #define ADC_CFGR_FIELDS_1  ((ADC_CFGR_RES    | ADC_CFGR_ALIGN   |\
00316                              ADC_CFGR_CONT   | ADC_CFGR_OVRMOD  |\
00317                              ADC_CFGR_DISCEN | ADC_CFGR_DISCNUM |\
00318                              ADC_CFGR_EXTEN  | ADC_CFGR_EXTSEL))   /*!< ADC_CFGR fields of parameters that can be updated when no regular conversion is on-going */
00319 
00320 /* Timeout values for ADC operations (enable settling time,                   */
00321 /*   disable settling time, ...).                                             */
00322 /*   Values defined to be higher than worst cases: low clock frequency,       */
00323 /*   maximum prescalers.                                                      */
00324 #define ADC_ENABLE_TIMEOUT              (2UL)    /*!< ADC enable time-out value  */
00325 #define ADC_DISABLE_TIMEOUT             (2UL)    /*!< ADC disable time-out value */
00326 
00327 /* Timeout to wait for current conversion on going to be completed.           */
00328 /* Timeout fixed to longest ADC conversion possible, for 1 channel:           */
00329 /*   - maximum sampling time (640.5 adc_clk)                                  */
00330 /*   - ADC resolution (Tsar 12 bits= 12.5 adc_clk)                            */
00331 /*   - System clock / ADC clock <= 4096 (hypothesis of maximum clock ratio)   */
00332 /*   - ADC oversampling ratio 256                                             */
00333 /*   Calculation: 653 * 4096 * 256 CPU clock cycles max                       */
00334 /* Unit: cycles of CPU clock.                                                 */
00335 #define ADC_CONVERSION_TIME_MAX_CPU_CYCLES (653UL * 4096UL * 256UL)  /*!< ADC conversion completion time-out value */
00336 
00337 
00338 /**
00339   * @}
00340   */
00341 
00342 /* Private macro -------------------------------------------------------------*/
00343 /* Private variables ---------------------------------------------------------*/
00344 /* Private function prototypes -----------------------------------------------*/
00345 /* Exported functions --------------------------------------------------------*/
00346 
00347 /** @defgroup ADC_Exported_Functions ADC Exported Functions
00348   * @{
00349   */
00350 
00351 /** @defgroup ADC_Exported_Functions_Group1 Initialization and de-initialization functions
00352   * @brief    ADC Initialization and Configuration functions
00353   *
00354 @verbatim
00355  ===============================================================================
00356               ##### Initialization and de-initialization functions #####
00357  ===============================================================================
00358     [..]  This section provides functions allowing to:
00359       (+) Initialize and configure the ADC.
00360       (+) De-initialize the ADC.
00361 @endverbatim
00362   * @{
00363   */
00364 
00365 /**
00366   * @brief  Initialize the ADC peripheral and regular group according to
00367   *         parameters specified in structure "ADC_InitTypeDef".
00368   * @note   As prerequisite, ADC clock must be configured at RCC top level
00369   *         (refer to description of RCC configuration for ADC
00370   *         in header of this file).
00371   * @note   Possibility to update parameters on the fly:
00372   *         This function initializes the ADC MSP (HAL_ADC_MspInit()) only when
00373   *         coming from ADC state reset. Following calls to this function can
00374   *         be used to reconfigure some parameters of ADC_InitTypeDef
00375   *         structure on the fly, without modifying MSP configuration. If ADC
00376   *         MSP has to be modified again, HAL_ADC_DeInit() must be called
00377   *         before HAL_ADC_Init().
00378   *         The setting of these parameters is conditioned to ADC state.
00379   *         For parameters constraints, see comments of structure
00380   *         "ADC_InitTypeDef".
00381   * @note   This function configures the ADC within 2 scopes: scope of entire
00382   *         ADC and scope of regular group. For parameters details, see comments
00383   *         of structure "ADC_InitTypeDef".
00384   * @note   Parameters related to common ADC registers (ADC clock mode) are set
00385   *         only if all ADCs are disabled.
00386   *         If this is not the case, these common parameters setting are
00387   *         bypassed without error reporting: it can be the intended behaviour in
00388   *         case of update of a parameter of ADC_InitTypeDef on the fly,
00389   *         without  disabling the other ADCs.
00390   * @param hadc ADC handle
00391   * @retval HAL status
00392   */
00393 HAL_StatusTypeDef HAL_ADC_Init(ADC_HandleTypeDef *hadc)
00394 {
00395   HAL_StatusTypeDef tmp_hal_status = HAL_OK;
00396   uint32_t tmpCFGR;
00397   uint32_t tmp_adc_reg_is_conversion_on_going;
00398   __IO uint32_t wait_loop_index = 0UL;
00399   uint32_t tmp_adc_is_conversion_on_going_regular;
00400   uint32_t tmp_adc_is_conversion_on_going_injected;
00401 
00402   /* Check ADC handle */
00403   if (hadc == NULL)
00404   {
00405     return HAL_ERROR;
00406   }
00407 
00408   /* Check the parameters */
00409   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
00410   assert_param(IS_ADC_CLOCKPRESCALER(hadc->Init.ClockPrescaler));
00411   assert_param(IS_ADC_RESOLUTION(hadc->Init.Resolution));
00412 #if defined(ADC_CFGR_DFSDMCFG) &&defined(DFSDM1_Channel0)
00413   assert_param(IS_ADC_DFSDMCFG_MODE(hadc));
00414 #endif
00415   assert_param(IS_ADC_DATA_ALIGN(hadc->Init.DataAlign));
00416   assert_param(IS_ADC_SCAN_MODE(hadc->Init.ScanConvMode));
00417   assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
00418   assert_param(IS_ADC_EXTTRIG_EDGE(hadc->Init.ExternalTrigConvEdge));
00419   assert_param(IS_ADC_EXTTRIG(hadc, hadc->Init.ExternalTrigConv));
00420   assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DMAContinuousRequests));
00421   assert_param(IS_ADC_EOC_SELECTION(hadc->Init.EOCSelection));
00422   assert_param(IS_ADC_OVERRUN(hadc->Init.Overrun));
00423   assert_param(IS_FUNCTIONAL_STATE(hadc->Init.LowPowerAutoWait));
00424   assert_param(IS_FUNCTIONAL_STATE(hadc->Init.OversamplingMode));
00425 
00426   if (hadc->Init.ScanConvMode != ADC_SCAN_DISABLE)
00427   {
00428     assert_param(IS_ADC_REGULAR_NB_CONV(hadc->Init.NbrOfConversion));
00429     assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DiscontinuousConvMode));
00430 
00431     if (hadc->Init.DiscontinuousConvMode == ENABLE)
00432     {
00433       assert_param(IS_ADC_REGULAR_DISCONT_NUMBER(hadc->Init.NbrOfDiscConversion));
00434     }
00435   }
00436 
00437   /* DISCEN and CONT bits cannot be set at the same time */
00438   assert_param(!((hadc->Init.DiscontinuousConvMode == ENABLE) && (hadc->Init.ContinuousConvMode == ENABLE)));
00439 
00440   /* Actions performed only if ADC is coming from state reset:                */
00441   /* - Initialization of ADC MSP                                              */
00442   if (hadc->State == HAL_ADC_STATE_RESET)
00443   {
00444 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
00445     /* Init the ADC Callback settings */
00446     hadc->ConvCpltCallback              = HAL_ADC_ConvCpltCallback;                 /* Legacy weak callback */
00447     hadc->ConvHalfCpltCallback          = HAL_ADC_ConvHalfCpltCallback;             /* Legacy weak callback */
00448     hadc->LevelOutOfWindowCallback      = HAL_ADC_LevelOutOfWindowCallback;         /* Legacy weak callback */
00449     hadc->ErrorCallback                 = HAL_ADC_ErrorCallback;                    /* Legacy weak callback */
00450     hadc->InjectedConvCpltCallback      = HAL_ADCEx_InjectedConvCpltCallback;       /* Legacy weak callback */
00451     hadc->InjectedQueueOverflowCallback = HAL_ADCEx_InjectedQueueOverflowCallback;  /* Legacy weak callback */
00452     hadc->LevelOutOfWindow2Callback     = HAL_ADCEx_LevelOutOfWindow2Callback;      /* Legacy weak callback */
00453     hadc->LevelOutOfWindow3Callback     = HAL_ADCEx_LevelOutOfWindow3Callback;      /* Legacy weak callback */
00454     hadc->EndOfSamplingCallback         = HAL_ADCEx_EndOfSamplingCallback;          /* Legacy weak callback */
00455 
00456     if (hadc->MspInitCallback == NULL)
00457     {
00458       hadc->MspInitCallback = HAL_ADC_MspInit; /* Legacy weak MspInit  */
00459     }
00460 
00461     /* Init the low level hardware */
00462     hadc->MspInitCallback(hadc);
00463 #else
00464     /* Init the low level hardware */
00465     HAL_ADC_MspInit(hadc);
00466 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
00467 
00468     /* Set ADC error code to none */
00469     ADC_CLEAR_ERRORCODE(hadc);
00470 
00471     /* Initialize Lock */
00472     hadc->Lock = HAL_UNLOCKED;
00473   }
00474 
00475   /* - Exit from deep-power-down mode and ADC voltage regulator enable        */
00476   if (LL_ADC_IsDeepPowerDownEnabled(hadc->Instance) != 0UL)
00477   {
00478     /* Disable ADC deep power down mode */
00479     LL_ADC_DisableDeepPowerDown(hadc->Instance);
00480 
00481     /* System was in deep power down mode, calibration must
00482      be relaunched or a previously saved calibration factor
00483      re-applied once the ADC voltage regulator is enabled */
00484   }
00485 
00486   if (LL_ADC_IsInternalRegulatorEnabled(hadc->Instance) == 0UL)
00487   {
00488     /* Enable ADC internal voltage regulator */
00489     LL_ADC_EnableInternalRegulator(hadc->Instance);
00490 
00491     /* Note: Variable divided by 2 to compensate partially              */
00492     /*       CPU processing cycles, scaling in us split to not          */
00493     /*       exceed 32 bits register capacity and handle low frequency. */
00494     wait_loop_index = ((LL_ADC_DELAY_INTERNAL_REGUL_STAB_US / 10UL) * ((SystemCoreClock / (100000UL * 2UL)) + 1UL));
00495     while (wait_loop_index != 0UL)
00496     {
00497       wait_loop_index--;
00498     }
00499   }
00500 
00501   /* Verification that ADC voltage regulator is correctly enabled, whether    */
00502   /* or not ADC is coming from state reset (if any potential problem of       */
00503   /* clocking, voltage regulator would not be enabled).                       */
00504   if (LL_ADC_IsInternalRegulatorEnabled(hadc->Instance) == 0UL)
00505   {
00506     /* Update ADC state machine to error */
00507     SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
00508 
00509     /* Set ADC error code to ADC peripheral internal error */
00510     SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
00511 
00512     tmp_hal_status = HAL_ERROR;
00513   }
00514 
00515   /* Configuration of ADC parameters if previous preliminary actions are      */
00516   /* correctly completed and if there is no conversion on going on regular    */
00517   /* group (ADC may already be enabled at this point if HAL_ADC_Init() is     */
00518   /* called to update a parameter on the fly).                                */
00519   tmp_adc_reg_is_conversion_on_going = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
00520 
00521   if (((hadc->State & HAL_ADC_STATE_ERROR_INTERNAL) == 0UL)
00522       && (tmp_adc_reg_is_conversion_on_going == 0UL)
00523      )
00524   {
00525     /* Set ADC state */
00526     ADC_STATE_CLR_SET(hadc->State,
00527                       HAL_ADC_STATE_REG_BUSY,
00528                       HAL_ADC_STATE_BUSY_INTERNAL);
00529 
00530     /* Configuration of common ADC parameters                                 */
00531 
00532     /* Parameters update conditioned to ADC state:                            */
00533     /* Parameters that can be updated only when ADC is disabled:              */
00534     /*  - clock configuration                                                 */
00535     if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
00536     {
00537       if (__LL_ADC_IS_ENABLED_ALL_COMMON_INSTANCE(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) == 0UL)
00538       {
00539         /* Reset configuration of ADC common register CCR:                      */
00540         /*                                                                      */
00541         /*   - ADC clock mode and ACC prescaler (CKMODE and PRESC bits)are set  */
00542         /*     according to adc->Init.ClockPrescaler. It selects the clock      */
00543         /*    source and sets the clock division factor.                        */
00544         /*                                                                      */
00545         /* Some parameters of this register are not reset, since they are set   */
00546         /* by other functions and must be kept in case of usage of this         */
00547         /* function on the fly (update of a parameter of ADC_InitTypeDef        */
00548         /* without needing to reconfigure all other ADC groups/channels         */
00549         /* parameters):                                                         */
00550         /*   - when multimode feature is available, multimode-related           */
00551         /*     parameters: MDMA, DMACFG, DELAY, DUAL (set by API                */
00552         /*     HAL_ADCEx_MultiModeConfigChannel() )                             */
00553         /*   - internal measurement paths: Vbat, temperature sensor, Vref       */
00554         /*     (set into HAL_ADC_ConfigChannel() or                             */
00555         /*     HAL_ADCEx_InjectedConfigChannel() )                              */
00556         LL_ADC_SetCommonClock(__LL_ADC_COMMON_INSTANCE(hadc->Instance), hadc->Init.ClockPrescaler);
00557       }
00558     }
00559 
00560     /* Configuration of ADC:                                                  */
00561     /*  - resolution                               Init.Resolution            */
00562     /*  - data alignment                           Init.DataAlign             */
00563     /*  - external trigger to start conversion     Init.ExternalTrigConv      */
00564     /*  - external trigger polarity                Init.ExternalTrigConvEdge  */
00565     /*  - continuous conversion mode               Init.ContinuousConvMode    */
00566     /*  - overrun                                  Init.Overrun               */
00567     /*  - discontinuous mode                       Init.DiscontinuousConvMode */
00568     /*  - discontinuous mode channel count         Init.NbrOfDiscConversion   */
00569     tmpCFGR  = (ADC_CFGR_CONTINUOUS((uint32_t)hadc->Init.ContinuousConvMode)           |
00570                 hadc->Init.Overrun                                                     |
00571                 hadc->Init.DataAlign                                                   |
00572                 hadc->Init.Resolution                                                  |
00573                 ADC_CFGR_REG_DISCONTINUOUS((uint32_t)hadc->Init.DiscontinuousConvMode));
00574 
00575     if (hadc->Init.DiscontinuousConvMode == ENABLE)
00576     {
00577       tmpCFGR |= ADC_CFGR_DISCONTINUOUS_NUM(hadc->Init.NbrOfDiscConversion);
00578     }
00579 
00580     /* Enable external trigger if trigger selection is different of software  */
00581     /* start.                                                                 */
00582     /* Note: This configuration keeps the hardware feature of parameter       */
00583     /*       ExternalTrigConvEdge "trigger edge none" equivalent to           */
00584     /*       software start.                                                  */
00585     if (hadc->Init.ExternalTrigConv != ADC_SOFTWARE_START)
00586     {
00587       tmpCFGR |= ((hadc->Init.ExternalTrigConv & ADC_CFGR_EXTSEL)
00588                   | hadc->Init.ExternalTrigConvEdge
00589                  );
00590     }
00591 
00592     /* Update Configuration Register CFGR */
00593     MODIFY_REG(hadc->Instance->CFGR, ADC_CFGR_FIELDS_1, tmpCFGR);
00594 
00595     /* Parameters update conditioned to ADC state:                            */
00596     /* Parameters that can be updated when ADC is disabled or enabled without */
00597     /* conversion on going on regular and injected groups:                    */
00598     /*  - DMA continuous request          Init.DMAContinuousRequests          */
00599     /*  - LowPowerAutoWait feature        Init.LowPowerAutoWait               */
00600     /*  - Oversampling parameters         Init.Oversampling                   */
00601     tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
00602     tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
00603     if ((tmp_adc_is_conversion_on_going_regular == 0UL)
00604         && (tmp_adc_is_conversion_on_going_injected == 0UL)
00605        )
00606     {
00607       tmpCFGR = (ADC_CFGR_DFSDM(hadc)                                            |
00608                  ADC_CFGR_AUTOWAIT((uint32_t)hadc->Init.LowPowerAutoWait)        |
00609                  ADC_CFGR_DMACONTREQ((uint32_t)hadc->Init.DMAContinuousRequests));
00610 
00611       MODIFY_REG(hadc->Instance->CFGR, ADC_CFGR_FIELDS_2, tmpCFGR);
00612 
00613       if (hadc->Init.OversamplingMode == ENABLE)
00614       {
00615         assert_param(IS_ADC_OVERSAMPLING_RATIO(hadc->Init.Oversampling.Ratio));
00616         assert_param(IS_ADC_RIGHT_BIT_SHIFT(hadc->Init.Oversampling.RightBitShift));
00617         assert_param(IS_ADC_TRIGGERED_OVERSAMPLING_MODE(hadc->Init.Oversampling.TriggeredMode));
00618         assert_param(IS_ADC_REGOVERSAMPLING_MODE(hadc->Init.Oversampling.OversamplingStopReset));
00619 
00620         /* Configuration of Oversampler:                                      */
00621         /*  - Oversampling Ratio                                              */
00622         /*  - Right bit shift                                                 */
00623         /*  - Triggered mode                                                  */
00624         /*  - Oversampling mode (continued/resumed)                           */
00625         MODIFY_REG(hadc->Instance->CFGR2,
00626                    ADC_CFGR2_OVSR  |
00627                    ADC_CFGR2_OVSS  |
00628                    ADC_CFGR2_TROVS |
00629                    ADC_CFGR2_ROVSM,
00630                    ADC_CFGR2_ROVSE                       |
00631                    hadc->Init.Oversampling.Ratio         |
00632                    hadc->Init.Oversampling.RightBitShift |
00633                    hadc->Init.Oversampling.TriggeredMode |
00634                    hadc->Init.Oversampling.OversamplingStopReset
00635                   );
00636       }
00637       else
00638       {
00639         /* Disable ADC oversampling scope on ADC group regular */
00640         CLEAR_BIT(hadc->Instance->CFGR2, ADC_CFGR2_ROVSE);
00641       }
00642 
00643     }
00644 
00645     /* Configuration of regular group sequencer:                              */
00646     /* - if scan mode is disabled, regular channels sequence length is set to */
00647     /*   0x00: 1 channel converted (channel on regular rank 1)                */
00648     /*   Parameter "NbrOfConversion" is discarded.                            */
00649     /*   Note: Scan mode is not present by hardware on this device, but       */
00650     /*   emulated by software for alignment over all STM32 devices.           */
00651     /* - if scan mode is enabled, regular channels sequence length is set to  */
00652     /*   parameter "NbrOfConversion".                                         */
00653 
00654     if (hadc->Init.ScanConvMode == ADC_SCAN_ENABLE)
00655     {
00656       /* Set number of ranks in regular group sequencer */
00657       MODIFY_REG(hadc->Instance->SQR1, ADC_SQR1_L, (hadc->Init.NbrOfConversion - (uint8_t)1));
00658     }
00659     else
00660     {
00661       CLEAR_BIT(hadc->Instance->SQR1, ADC_SQR1_L);
00662     }
00663 
00664     /* Initialize the ADC state */
00665     /* Clear HAL_ADC_STATE_BUSY_INTERNAL bit, set HAL_ADC_STATE_READY bit */
00666     ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_BUSY_INTERNAL, HAL_ADC_STATE_READY);
00667   }
00668   else
00669   {
00670     /* Update ADC state machine to error */
00671     SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
00672 
00673     tmp_hal_status = HAL_ERROR;
00674   }
00675 
00676   /* Return function status */
00677   return tmp_hal_status;
00678 }
00679 
00680 /**
00681   * @brief  Deinitialize the ADC peripheral registers to their default reset
00682   *         values, with deinitialization of the ADC MSP.
00683   * @note   For devices with several ADCs: reset of ADC common registers is done
00684   *         only if all ADCs sharing the same common group are disabled.
00685   *         (function "HAL_ADC_MspDeInit()" is also called under the same conditions:
00686   *         all ADC instances use the same core clock at RCC level, disabling
00687   *         the core clock reset all ADC instances).
00688   *         If this is not the case, reset of these common parameters reset is
00689   *         bypassed without error reporting: it can be the intended behavior in
00690   *         case of reset of a single ADC while the other ADCs sharing the same
00691   *         common group is still running.
00692   * @note   By default, HAL_ADC_DeInit() set ADC in mode deep power-down:
00693   *         this saves more power by reducing leakage currents
00694   *         and is particularly interesting before entering MCU low-power modes.
00695   * @param hadc ADC handle
00696   * @retval HAL status
00697   */
00698 HAL_StatusTypeDef HAL_ADC_DeInit(ADC_HandleTypeDef *hadc)
00699 {
00700   HAL_StatusTypeDef tmp_hal_status;
00701 
00702   /* Check ADC handle */
00703   if (hadc == NULL)
00704   {
00705     return HAL_ERROR;
00706   }
00707 
00708   /* Check the parameters */
00709   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
00710 
00711   /* Set ADC state */
00712   SET_BIT(hadc->State, HAL_ADC_STATE_BUSY_INTERNAL);
00713 
00714   /* Stop potential conversion on going */
00715   tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_INJECTED_GROUP);
00716 
00717   /* Disable ADC peripheral if conversions are effectively stopped            */
00718   /* Flush register JSQR: reset the queue sequencer when injected             */
00719   /* queue sequencer is enabled and ADC disabled.                             */
00720   /* The software and hardware triggers of the injected sequence are both     */
00721   /* internally disabled just after the completion of the last valid          */
00722   /* injected sequence.                                                       */
00723   SET_BIT(hadc->Instance->CFGR, ADC_CFGR_JQM);
00724 
00725   /* Disable ADC peripheral if conversions are effectively stopped */
00726   if (tmp_hal_status == HAL_OK)
00727   {
00728     /* Disable the ADC peripheral */
00729     tmp_hal_status = ADC_Disable(hadc);
00730 
00731     /* Check if ADC is effectively disabled */
00732     if (tmp_hal_status == HAL_OK)
00733     {
00734       /* Change ADC state */
00735       hadc->State = HAL_ADC_STATE_READY;
00736     }
00737   }
00738 
00739   /* Note: HAL ADC deInit is done independently of ADC conversion stop        */
00740   /*       and disable return status. In case of status fail, attempt to      */
00741   /*       perform deinitialization anyway and it is up user code in          */
00742   /*       in HAL_ADC_MspDeInit() to reset the ADC peripheral using           */
00743   /*       system RCC hard reset.                                             */
00744 
00745   /* ========== Reset ADC registers ========== */
00746   /* Reset register IER */
00747   __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_AWD3  | ADC_IT_AWD2 | ADC_IT_AWD1 |
00748                               ADC_IT_JQOVF | ADC_IT_OVR  |
00749                               ADC_IT_JEOS  | ADC_IT_JEOC |
00750                               ADC_IT_EOS   | ADC_IT_EOC  |
00751                               ADC_IT_EOSMP | ADC_IT_RDY));
00752 
00753   /* Reset register ISR */
00754   __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_AWD3  | ADC_FLAG_AWD2 | ADC_FLAG_AWD1 |
00755                               ADC_FLAG_JQOVF | ADC_FLAG_OVR  |
00756                               ADC_FLAG_JEOS  | ADC_FLAG_JEOC |
00757                               ADC_FLAG_EOS   | ADC_FLAG_EOC  |
00758                               ADC_FLAG_EOSMP | ADC_FLAG_RDY));
00759 
00760   /* Reset register CR */
00761   /* Bits ADC_CR_JADSTP, ADC_CR_ADSTP, ADC_CR_JADSTART, ADC_CR_ADSTART,
00762      ADC_CR_ADCAL, ADC_CR_ADDIS and ADC_CR_ADEN are in access mode "read-set":
00763      no direct reset applicable.
00764      Update CR register to reset value where doable by software */
00765   CLEAR_BIT(hadc->Instance->CR, ADC_CR_ADVREGEN | ADC_CR_ADCALDIF);
00766   SET_BIT(hadc->Instance->CR, ADC_CR_DEEPPWD);
00767 
00768   /* Reset register CFGR */
00769   CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_FIELDS);
00770   SET_BIT(hadc->Instance->CFGR, ADC_CFGR_JQDIS);
00771 
00772   /* Reset register CFGR2 */
00773   CLEAR_BIT(hadc->Instance->CFGR2, ADC_CFGR2_ROVSM  | ADC_CFGR2_TROVS   | ADC_CFGR2_OVSS |
00774             ADC_CFGR2_OVSR  | ADC_CFGR2_JOVSE | ADC_CFGR2_ROVSE);
00775 
00776   /* Reset register SMPR1 */
00777   CLEAR_BIT(hadc->Instance->SMPR1, ADC_SMPR1_FIELDS);
00778 
00779   /* Reset register SMPR2 */
00780   CLEAR_BIT(hadc->Instance->SMPR2, ADC_SMPR2_SMP18 | ADC_SMPR2_SMP17 | ADC_SMPR2_SMP16 |
00781             ADC_SMPR2_SMP15 | ADC_SMPR2_SMP14 | ADC_SMPR2_SMP13 |
00782             ADC_SMPR2_SMP12 | ADC_SMPR2_SMP11 | ADC_SMPR2_SMP10);
00783 
00784   /* Reset register TR1 */
00785   CLEAR_BIT(hadc->Instance->TR1, ADC_TR1_HT1 | ADC_TR1_LT1);
00786 
00787   /* Reset register TR2 */
00788   CLEAR_BIT(hadc->Instance->TR2, ADC_TR2_HT2 | ADC_TR2_LT2);
00789 
00790   /* Reset register TR3 */
00791   CLEAR_BIT(hadc->Instance->TR3, ADC_TR3_HT3 | ADC_TR3_LT3);
00792 
00793   /* Reset register SQR1 */
00794   CLEAR_BIT(hadc->Instance->SQR1, ADC_SQR1_SQ4 | ADC_SQR1_SQ3 | ADC_SQR1_SQ2 |
00795             ADC_SQR1_SQ1 | ADC_SQR1_L);
00796 
00797   /* Reset register SQR2 */
00798   CLEAR_BIT(hadc->Instance->SQR2, ADC_SQR2_SQ9 | ADC_SQR2_SQ8 | ADC_SQR2_SQ7 |
00799             ADC_SQR2_SQ6 | ADC_SQR2_SQ5);
00800 
00801   /* Reset register SQR3 */
00802   CLEAR_BIT(hadc->Instance->SQR3, ADC_SQR3_SQ14 | ADC_SQR3_SQ13 | ADC_SQR3_SQ12 |
00803             ADC_SQR3_SQ11 | ADC_SQR3_SQ10);
00804 
00805   /* Reset register SQR4 */
00806   CLEAR_BIT(hadc->Instance->SQR4, ADC_SQR4_SQ16 | ADC_SQR4_SQ15);
00807 
00808   /* Register JSQR was reset when the ADC was disabled */
00809 
00810   /* Reset register DR */
00811   /* bits in access mode read only, no direct reset applicable*/
00812 
00813   /* Reset register OFR1 */
00814   CLEAR_BIT(hadc->Instance->OFR1, ADC_OFR1_OFFSET1_EN | ADC_OFR1_OFFSET1_CH | ADC_OFR1_OFFSET1);
00815   /* Reset register OFR2 */
00816   CLEAR_BIT(hadc->Instance->OFR2, ADC_OFR2_OFFSET2_EN | ADC_OFR2_OFFSET2_CH | ADC_OFR2_OFFSET2);
00817   /* Reset register OFR3 */
00818   CLEAR_BIT(hadc->Instance->OFR3, ADC_OFR3_OFFSET3_EN | ADC_OFR3_OFFSET3_CH | ADC_OFR3_OFFSET3);
00819   /* Reset register OFR4 */
00820   CLEAR_BIT(hadc->Instance->OFR4, ADC_OFR4_OFFSET4_EN | ADC_OFR4_OFFSET4_CH | ADC_OFR4_OFFSET4);
00821 
00822   /* Reset registers JDR1, JDR2, JDR3, JDR4 */
00823   /* bits in access mode read only, no direct reset applicable*/
00824 
00825   /* Reset register AWD2CR */
00826   CLEAR_BIT(hadc->Instance->AWD2CR, ADC_AWD2CR_AWD2CH);
00827 
00828   /* Reset register AWD3CR */
00829   CLEAR_BIT(hadc->Instance->AWD3CR, ADC_AWD3CR_AWD3CH);
00830 
00831   /* Reset register DIFSEL */
00832   CLEAR_BIT(hadc->Instance->DIFSEL, ADC_DIFSEL_DIFSEL);
00833 
00834   /* Reset register CALFACT */
00835   CLEAR_BIT(hadc->Instance->CALFACT, ADC_CALFACT_CALFACT_D | ADC_CALFACT_CALFACT_S);
00836 
00837 
00838   /* ========== Reset common ADC registers ========== */
00839 
00840   /* Software is allowed to change common parameters only when all the other
00841      ADCs are disabled.   */
00842   if (__LL_ADC_IS_ENABLED_ALL_COMMON_INSTANCE(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) == 0UL)
00843   {
00844     /* Reset configuration of ADC common register CCR:
00845       - clock mode: CKMODE, PRESCEN
00846       - multimode related parameters (when this feature is available): MDMA,
00847         DMACFG, DELAY, DUAL (set by HAL_ADCEx_MultiModeConfigChannel() API)
00848       - internal measurement paths: Vbat, temperature sensor, Vref (set into
00849         HAL_ADC_ConfigChannel() or HAL_ADCEx_InjectedConfigChannel() )
00850     */
00851     ADC_CLEAR_COMMON_CONTROL_REGISTER(hadc);
00852 
00853     /* ========== Hard reset ADC peripheral ========== */
00854     /* Performs a global reset of the entire ADC peripherals instances        */
00855     /* sharing the same common ADC instance: ADC state is forced to           */
00856     /* a similar state as after device power-on.                              */
00857     /* Note: A possible implementation is to add RCC bus reset of ADC         */
00858     /* (for example, using macro                                              */
00859     /*  __HAL_RCC_ADC..._FORCE_RESET()/..._RELEASE_RESET()/..._CLK_DISABLE()) */
00860     /* in function "void HAL_ADC_MspDeInit(ADC_HandleTypeDef *hadc)":         */
00861 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
00862     if (hadc->MspDeInitCallback == NULL)
00863     {
00864       hadc->MspDeInitCallback = HAL_ADC_MspDeInit; /* Legacy weak MspDeInit  */
00865     }
00866 
00867     /* DeInit the low level hardware */
00868     hadc->MspDeInitCallback(hadc);
00869 #else
00870     /* DeInit the low level hardware */
00871     HAL_ADC_MspDeInit(hadc);
00872 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
00873   }
00874 
00875   /* Set ADC error code to none */
00876   ADC_CLEAR_ERRORCODE(hadc);
00877 
00878   /* Reset injected channel configuration parameters */
00879   hadc->InjectionConfig.ContextQueue = 0;
00880   hadc->InjectionConfig.ChannelCount = 0;
00881 
00882   /* Set ADC state */
00883   hadc->State = HAL_ADC_STATE_RESET;
00884 
00885   /* Process unlocked */
00886   __HAL_UNLOCK(hadc);
00887 
00888   /* Return function status */
00889   return tmp_hal_status;
00890 }
00891 
00892 /**
00893   * @brief  Initialize the ADC MSP.
00894   * @param hadc ADC handle
00895   * @retval None
00896   */
00897 __weak void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc)
00898 {
00899   /* Prevent unused argument(s) compilation warning */
00900   UNUSED(hadc);
00901 
00902   /* NOTE : This function should not be modified. When the callback is needed,
00903             function HAL_ADC_MspInit must be implemented in the user file.
00904    */
00905 }
00906 
00907 /**
00908   * @brief  DeInitialize the ADC MSP.
00909   * @param hadc ADC handle
00910   * @note   All ADC instances use the same core clock at RCC level, disabling
00911   *         the core clock reset all ADC instances).
00912   * @retval None
00913   */
00914 __weak void HAL_ADC_MspDeInit(ADC_HandleTypeDef *hadc)
00915 {
00916   /* Prevent unused argument(s) compilation warning */
00917   UNUSED(hadc);
00918 
00919   /* NOTE : This function should not be modified. When the callback is needed,
00920             function HAL_ADC_MspDeInit must be implemented in the user file.
00921    */
00922 }
00923 
00924 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
00925 /**
00926   * @brief  Register a User ADC Callback
00927   *         To be used instead of the weak predefined callback
00928   * @param  hadc Pointer to a ADC_HandleTypeDef structure that contains
00929   *                the configuration information for the specified ADC.
00930   * @param  CallbackID ID of the callback to be registered
00931   *         This parameter can be one of the following values:
00932   *          @arg @ref HAL_ADC_CONVERSION_COMPLETE_CB_ID      ADC conversion complete callback ID
00933   *          @arg @ref HAL_ADC_CONVERSION_HALF_CB_ID          ADC conversion DMA half-transfer callback ID
00934   *          @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID    ADC analog watchdog 1 callback ID
00935   *          @arg @ref HAL_ADC_ERROR_CB_ID                    ADC error callback ID
00936   *          @arg @ref HAL_ADC_INJ_CONVERSION_COMPLETE_CB_ID  ADC group injected conversion complete callback ID
00937   *          @arg @ref HAL_ADC_INJ_QUEUE_OVEFLOW_CB_ID        ADC group injected context queue overflow callback ID
00938   *          @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_2_CB_ID    ADC analog watchdog 2 callback ID
00939   *          @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_3_CB_ID    ADC analog watchdog 3 callback ID
00940   *          @arg @ref HAL_ADC_END_OF_SAMPLING_CB_ID          ADC end of sampling callback ID
00941   *          @arg @ref HAL_ADC_MSPINIT_CB_ID                  ADC Msp Init callback ID
00942   *          @arg @ref HAL_ADC_MSPDEINIT_CB_ID                ADC Msp DeInit callback ID
00943   *          @arg @ref HAL_ADC_MSPINIT_CB_ID MspInit callback ID
00944   *          @arg @ref HAL_ADC_MSPDEINIT_CB_ID MspDeInit callback ID
00945   * @param  pCallback pointer to the Callback function
00946   * @retval HAL status
00947   */
00948 HAL_StatusTypeDef HAL_ADC_RegisterCallback(ADC_HandleTypeDef *hadc, HAL_ADC_CallbackIDTypeDef CallbackID,
00949                                            pADC_CallbackTypeDef pCallback)
00950 {
00951   HAL_StatusTypeDef status = HAL_OK;
00952 
00953   if (pCallback == NULL)
00954   {
00955     /* Update the error code */
00956     hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
00957 
00958     return HAL_ERROR;
00959   }
00960 
00961   if ((hadc->State & HAL_ADC_STATE_READY) != 0UL)
00962   {
00963     switch (CallbackID)
00964     {
00965       case HAL_ADC_CONVERSION_COMPLETE_CB_ID :
00966         hadc->ConvCpltCallback = pCallback;
00967         break;
00968 
00969       case HAL_ADC_CONVERSION_HALF_CB_ID :
00970         hadc->ConvHalfCpltCallback = pCallback;
00971         break;
00972 
00973       case HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID :
00974         hadc->LevelOutOfWindowCallback = pCallback;
00975         break;
00976 
00977       case HAL_ADC_ERROR_CB_ID :
00978         hadc->ErrorCallback = pCallback;
00979         break;
00980 
00981       case HAL_ADC_INJ_CONVERSION_COMPLETE_CB_ID :
00982         hadc->InjectedConvCpltCallback = pCallback;
00983         break;
00984 
00985       case HAL_ADC_INJ_QUEUE_OVEFLOW_CB_ID :
00986         hadc->InjectedQueueOverflowCallback = pCallback;
00987         break;
00988 
00989       case HAL_ADC_LEVEL_OUT_OF_WINDOW_2_CB_ID :
00990         hadc->LevelOutOfWindow2Callback = pCallback;
00991         break;
00992 
00993       case HAL_ADC_LEVEL_OUT_OF_WINDOW_3_CB_ID :
00994         hadc->LevelOutOfWindow3Callback = pCallback;
00995         break;
00996 
00997       case HAL_ADC_END_OF_SAMPLING_CB_ID :
00998         hadc->EndOfSamplingCallback = pCallback;
00999         break;
01000 
01001       case HAL_ADC_MSPINIT_CB_ID :
01002         hadc->MspInitCallback = pCallback;
01003         break;
01004 
01005       case HAL_ADC_MSPDEINIT_CB_ID :
01006         hadc->MspDeInitCallback = pCallback;
01007         break;
01008 
01009       default :
01010         /* Update the error code */
01011         hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
01012 
01013         /* Return error status */
01014         status = HAL_ERROR;
01015         break;
01016     }
01017   }
01018   else if (HAL_ADC_STATE_RESET == hadc->State)
01019   {
01020     switch (CallbackID)
01021     {
01022       case HAL_ADC_MSPINIT_CB_ID :
01023         hadc->MspInitCallback = pCallback;
01024         break;
01025 
01026       case HAL_ADC_MSPDEINIT_CB_ID :
01027         hadc->MspDeInitCallback = pCallback;
01028         break;
01029 
01030       default :
01031         /* Update the error code */
01032         hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
01033 
01034         /* Return error status */
01035         status = HAL_ERROR;
01036         break;
01037     }
01038   }
01039   else
01040   {
01041     /* Update the error code */
01042     hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
01043 
01044     /* Return error status */
01045     status =  HAL_ERROR;
01046   }
01047 
01048   return status;
01049 }
01050 
01051 /**
01052   * @brief  Unregister a ADC Callback
01053   *         ADC callback is redirected to the weak predefined callback
01054   * @param  hadc Pointer to a ADC_HandleTypeDef structure that contains
01055   *                the configuration information for the specified ADC.
01056   * @param  CallbackID ID of the callback to be unregistered
01057   *         This parameter can be one of the following values:
01058   *          @arg @ref HAL_ADC_CONVERSION_COMPLETE_CB_ID      ADC conversion complete callback ID
01059   *          @arg @ref HAL_ADC_CONVERSION_HALF_CB_ID          ADC conversion DMA half-transfer callback ID
01060   *          @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID    ADC analog watchdog 1 callback ID
01061   *          @arg @ref HAL_ADC_ERROR_CB_ID                    ADC error callback ID
01062   *          @arg @ref HAL_ADC_INJ_CONVERSION_COMPLETE_CB_ID  ADC group injected conversion complete callback ID
01063   *          @arg @ref HAL_ADC_INJ_QUEUE_OVEFLOW_CB_ID        ADC group injected context queue overflow callback ID
01064   *          @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_2_CB_ID    ADC analog watchdog 2 callback ID
01065   *          @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_3_CB_ID    ADC analog watchdog 3 callback ID
01066   *          @arg @ref HAL_ADC_END_OF_SAMPLING_CB_ID          ADC end of sampling callback ID
01067   *          @arg @ref HAL_ADC_MSPINIT_CB_ID                  ADC Msp Init callback ID
01068   *          @arg @ref HAL_ADC_MSPDEINIT_CB_ID                ADC Msp DeInit callback ID
01069   *          @arg @ref HAL_ADC_MSPINIT_CB_ID MspInit callback ID
01070   *          @arg @ref HAL_ADC_MSPDEINIT_CB_ID MspDeInit callback ID
01071   * @retval HAL status
01072   */
01073 HAL_StatusTypeDef HAL_ADC_UnRegisterCallback(ADC_HandleTypeDef *hadc, HAL_ADC_CallbackIDTypeDef CallbackID)
01074 {
01075   HAL_StatusTypeDef status = HAL_OK;
01076 
01077   if ((hadc->State & HAL_ADC_STATE_READY) != 0UL)
01078   {
01079     switch (CallbackID)
01080     {
01081       case HAL_ADC_CONVERSION_COMPLETE_CB_ID :
01082         hadc->ConvCpltCallback = HAL_ADC_ConvCpltCallback;
01083         break;
01084 
01085       case HAL_ADC_CONVERSION_HALF_CB_ID :
01086         hadc->ConvHalfCpltCallback = HAL_ADC_ConvHalfCpltCallback;
01087         break;
01088 
01089       case HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID :
01090         hadc->LevelOutOfWindowCallback = HAL_ADC_LevelOutOfWindowCallback;
01091         break;
01092 
01093       case HAL_ADC_ERROR_CB_ID :
01094         hadc->ErrorCallback = HAL_ADC_ErrorCallback;
01095         break;
01096 
01097       case HAL_ADC_INJ_CONVERSION_COMPLETE_CB_ID :
01098         hadc->InjectedConvCpltCallback = HAL_ADCEx_InjectedConvCpltCallback;
01099         break;
01100 
01101       case HAL_ADC_INJ_QUEUE_OVEFLOW_CB_ID :
01102         hadc->InjectedQueueOverflowCallback = HAL_ADCEx_InjectedQueueOverflowCallback;
01103         break;
01104 
01105       case HAL_ADC_LEVEL_OUT_OF_WINDOW_2_CB_ID :
01106         hadc->LevelOutOfWindow2Callback = HAL_ADCEx_LevelOutOfWindow2Callback;
01107         break;
01108 
01109       case HAL_ADC_LEVEL_OUT_OF_WINDOW_3_CB_ID :
01110         hadc->LevelOutOfWindow3Callback = HAL_ADCEx_LevelOutOfWindow3Callback;
01111         break;
01112 
01113       case HAL_ADC_END_OF_SAMPLING_CB_ID :
01114         hadc->EndOfSamplingCallback = HAL_ADCEx_EndOfSamplingCallback;
01115         break;
01116 
01117       case HAL_ADC_MSPINIT_CB_ID :
01118         hadc->MspInitCallback = HAL_ADC_MspInit; /* Legacy weak MspInit              */
01119         break;
01120 
01121       case HAL_ADC_MSPDEINIT_CB_ID :
01122         hadc->MspDeInitCallback = HAL_ADC_MspDeInit; /* Legacy weak MspDeInit            */
01123         break;
01124 
01125       default :
01126         /* Update the error code */
01127         hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
01128 
01129         /* Return error status */
01130         status =  HAL_ERROR;
01131         break;
01132     }
01133   }
01134   else if (HAL_ADC_STATE_RESET == hadc->State)
01135   {
01136     switch (CallbackID)
01137     {
01138       case HAL_ADC_MSPINIT_CB_ID :
01139         hadc->MspInitCallback = HAL_ADC_MspInit;                   /* Legacy weak MspInit              */
01140         break;
01141 
01142       case HAL_ADC_MSPDEINIT_CB_ID :
01143         hadc->MspDeInitCallback = HAL_ADC_MspDeInit;               /* Legacy weak MspDeInit            */
01144         break;
01145 
01146       default :
01147         /* Update the error code */
01148         hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
01149 
01150         /* Return error status */
01151         status =  HAL_ERROR;
01152         break;
01153     }
01154   }
01155   else
01156   {
01157     /* Update the error code */
01158     hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
01159 
01160     /* Return error status */
01161     status =  HAL_ERROR;
01162   }
01163 
01164   return status;
01165 }
01166 
01167 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
01168 
01169 /**
01170   * @}
01171   */
01172 
01173 /** @defgroup ADC_Exported_Functions_Group2 ADC Input and Output operation functions
01174   * @brief    ADC IO operation functions
01175   *
01176 @verbatim
01177  ===============================================================================
01178                       ##### IO operation functions #####
01179  ===============================================================================
01180     [..]  This section provides functions allowing to:
01181       (+) Start conversion of regular group.
01182       (+) Stop conversion of regular group.
01183       (+) Poll for conversion complete on regular group.
01184       (+) Poll for conversion event.
01185       (+) Get result of regular channel conversion.
01186       (+) Start conversion of regular group and enable interruptions.
01187       (+) Stop conversion of regular group and disable interruptions.
01188       (+) Handle ADC interrupt request
01189       (+) Start conversion of regular group and enable DMA transfer.
01190       (+) Stop conversion of regular group and disable ADC DMA transfer.
01191 @endverbatim
01192   * @{
01193   */
01194 
01195 /**
01196   * @brief  Enable ADC, start conversion of regular group.
01197   * @note   Interruptions enabled in this function: None.
01198   * @note   Case of multimode enabled (when multimode feature is available):
01199   *           if ADC is Slave, ADC is enabled but conversion is not started,
01200   *           if ADC is master, ADC is enabled and multimode conversion is started.
01201   * @param hadc ADC handle
01202   * @retval HAL status
01203   */
01204 HAL_StatusTypeDef HAL_ADC_Start(ADC_HandleTypeDef *hadc)
01205 {
01206   HAL_StatusTypeDef tmp_hal_status;
01207 #if defined(ADC_MULTIMODE_SUPPORT)
01208   const ADC_TypeDef *tmpADC_Master;
01209   uint32_t tmp_multimode_config = LL_ADC_GetMultimode(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
01210 #endif
01211 
01212   /* Check the parameters */
01213   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
01214 
01215   /* Perform ADC enable and conversion start if no conversion is on going */
01216   if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
01217   {
01218     /* Process locked */
01219     __HAL_LOCK(hadc);
01220 
01221     /* Enable the ADC peripheral */
01222     tmp_hal_status = ADC_Enable(hadc);
01223 
01224     /* Start conversion if ADC is effectively enabled */
01225     if (tmp_hal_status == HAL_OK)
01226     {
01227       /* Set ADC state                                                        */
01228       /* - Clear state bitfield related to regular group conversion results   */
01229       /* - Set state bitfield related to regular operation                    */
01230       ADC_STATE_CLR_SET(hadc->State,
01231                         HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP,
01232                         HAL_ADC_STATE_REG_BUSY);
01233 
01234 #if defined(ADC_MULTIMODE_SUPPORT)
01235       /* Reset HAL_ADC_STATE_MULTIMODE_SLAVE bit
01236         - if ADC instance is master or if multimode feature is not available
01237         - if multimode setting is disabled (ADC instance slave in independent mode) */
01238       if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
01239           || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
01240          )
01241       {
01242         CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
01243       }
01244 #endif
01245 
01246       /* Set ADC error code */
01247       /* Check if a conversion is on going on ADC group injected */
01248       if (HAL_IS_BIT_SET(hadc->State, HAL_ADC_STATE_INJ_BUSY))
01249       {
01250         /* Reset ADC error code fields related to regular conversions only */
01251         CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR | HAL_ADC_ERROR_DMA));
01252       }
01253       else
01254       {
01255         /* Reset all ADC error code fields */
01256         ADC_CLEAR_ERRORCODE(hadc);
01257       }
01258 
01259       /* Clear ADC group regular conversion flag and overrun flag               */
01260       /* (To ensure of no unknown state from potential previous ADC operations) */
01261       __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
01262 
01263       /* Process unlocked */
01264       /* Unlock before starting ADC conversions: in case of potential         */
01265       /* interruption, to let the process to ADC IRQ Handler.                 */
01266       __HAL_UNLOCK(hadc);
01267 
01268       /* Enable conversion of regular group.                                  */
01269       /* If software start has been selected, conversion starts immediately.  */
01270       /* If external trigger has been selected, conversion will start at next */
01271       /* trigger event.                                                       */
01272       /* Case of multimode enabled (when multimode feature is available):     */
01273       /*  - if ADC is slave and dual regular conversions are enabled, ADC is  */
01274       /*    enabled only (conversion is not started),                         */
01275       /*  - if ADC is master, ADC is enabled and conversion is started.       */
01276 #if defined(ADC_MULTIMODE_SUPPORT)
01277       if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
01278           || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
01279           || (tmp_multimode_config == LL_ADC_MULTI_DUAL_INJ_SIMULT)
01280           || (tmp_multimode_config == LL_ADC_MULTI_DUAL_INJ_ALTERN)
01281          )
01282       {
01283         /* ADC instance is not a multimode slave instance with multimode regular conversions enabled */
01284         if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_JAUTO) != 0UL)
01285         {
01286           ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
01287         }
01288 
01289         /* Start ADC group regular conversion */
01290         LL_ADC_REG_StartConversion(hadc->Instance);
01291       }
01292       else
01293       {
01294         /* ADC instance is a multimode slave instance with multimode regular conversions enabled */
01295         SET_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
01296         /* if Master ADC JAUTO bit is set, update Slave State in setting
01297            HAL_ADC_STATE_INJ_BUSY bit and in resetting HAL_ADC_STATE_INJ_EOC bit */
01298         tmpADC_Master = __LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance);
01299         if (READ_BIT(tmpADC_Master->CFGR, ADC_CFGR_JAUTO) != 0UL)
01300         {
01301           ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
01302         }
01303 
01304       }
01305 #else
01306       if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_JAUTO) != 0UL)
01307       {
01308         ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
01309       }
01310 
01311       /* Start ADC group regular conversion */
01312       LL_ADC_REG_StartConversion(hadc->Instance);
01313 #endif
01314     }
01315     else
01316     {
01317       /* Process unlocked */
01318       __HAL_UNLOCK(hadc);
01319     }
01320   }
01321   else
01322   {
01323     tmp_hal_status = HAL_BUSY;
01324   }
01325 
01326   /* Return function status */
01327   return tmp_hal_status;
01328 }
01329 
01330 /**
01331   * @brief  Stop ADC conversion of regular group (and injected channels in
01332   *         case of auto_injection mode), disable ADC peripheral.
01333   * @note:  ADC peripheral disable is forcing stop of potential
01334   *         conversion on injected group. If injected group is under use, it
01335   *         should be preliminarily stopped using HAL_ADCEx_InjectedStop function.
01336   * @param hadc ADC handle
01337   * @retval HAL status.
01338   */
01339 HAL_StatusTypeDef HAL_ADC_Stop(ADC_HandleTypeDef *hadc)
01340 {
01341   HAL_StatusTypeDef tmp_hal_status;
01342 
01343   /* Check the parameters */
01344   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
01345 
01346   /* Process locked */
01347   __HAL_LOCK(hadc);
01348 
01349   /* 1. Stop potential conversion on going, on ADC groups regular and injected */
01350   tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_INJECTED_GROUP);
01351 
01352   /* Disable ADC peripheral if conversions are effectively stopped */
01353   if (tmp_hal_status == HAL_OK)
01354   {
01355     /* 2. Disable the ADC peripheral */
01356     tmp_hal_status = ADC_Disable(hadc);
01357 
01358     /* Check if ADC is effectively disabled */
01359     if (tmp_hal_status == HAL_OK)
01360     {
01361       /* Set ADC state */
01362       ADC_STATE_CLR_SET(hadc->State,
01363                         HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
01364                         HAL_ADC_STATE_READY);
01365     }
01366   }
01367 
01368   /* Process unlocked */
01369   __HAL_UNLOCK(hadc);
01370 
01371   /* Return function status */
01372   return tmp_hal_status;
01373 }
01374 
01375 /**
01376   * @brief  Wait for regular group conversion to be completed.
01377   * @note   ADC conversion flags EOS (end of sequence) and EOC (end of
01378   *         conversion) are cleared by this function, with an exception:
01379   *         if low power feature "LowPowerAutoWait" is enabled, flags are
01380   *         not cleared to not interfere with this feature until data register
01381   *         is read using function HAL_ADC_GetValue().
01382   * @note   This function cannot be used in a particular setup: ADC configured
01383   *         in DMA mode and polling for end of each conversion (ADC init
01384   *         parameter "EOCSelection" set to ADC_EOC_SINGLE_CONV).
01385   *         In this case, DMA resets the flag EOC and polling cannot be
01386   *         performed on each conversion. Nevertheless, polling can still
01387   *         be performed on the complete sequence (ADC init
01388   *         parameter "EOCSelection" set to ADC_EOC_SEQ_CONV).
01389   * @param hadc ADC handle
01390   * @param Timeout Timeout value in millisecond.
01391   * @retval HAL status
01392   */
01393 HAL_StatusTypeDef HAL_ADC_PollForConversion(ADC_HandleTypeDef *hadc, uint32_t Timeout)
01394 {
01395   uint32_t tickstart;
01396   uint32_t tmp_Flag_End;
01397   uint32_t tmp_cfgr;
01398 #if defined(ADC_MULTIMODE_SUPPORT)
01399   const ADC_TypeDef *tmpADC_Master;
01400   uint32_t tmp_multimode_config = LL_ADC_GetMultimode(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
01401 #endif
01402 
01403   /* Check the parameters */
01404   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
01405 
01406   /* If end of conversion selected to end of sequence conversions */
01407   if (hadc->Init.EOCSelection == ADC_EOC_SEQ_CONV)
01408   {
01409     tmp_Flag_End = ADC_FLAG_EOS;
01410   }
01411   /* If end of conversion selected to end of unitary conversion */
01412   else /* ADC_EOC_SINGLE_CONV */
01413   {
01414     /* Verification that ADC configuration is compliant with polling for      */
01415     /* each conversion:                                                       */
01416     /* Particular case is ADC configured in DMA mode and ADC sequencer with   */
01417     /* several ranks and polling for end of each conversion.                  */
01418     /* For code simplicity sake, this particular case is generalized to       */
01419     /* ADC configured in DMA mode and and polling for end of each conversion. */
01420 #if defined(ADC_MULTIMODE_SUPPORT)
01421     if ((tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
01422         || (tmp_multimode_config == LL_ADC_MULTI_DUAL_INJ_SIMULT)
01423         || (tmp_multimode_config == LL_ADC_MULTI_DUAL_INJ_ALTERN)
01424        )
01425     {
01426       /* Check ADC DMA mode in independent mode on ADC group regular */
01427       if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_DMAEN) != 0UL)
01428       {
01429         SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
01430         return HAL_ERROR;
01431       }
01432       else
01433       {
01434         tmp_Flag_End = (ADC_FLAG_EOC);
01435       }
01436     }
01437     else
01438     {
01439       /* Check ADC DMA mode in multimode on ADC group regular */
01440       if (LL_ADC_GetMultiDMATransfer(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) != LL_ADC_MULTI_REG_DMA_EACH_ADC)
01441       {
01442         SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
01443         return HAL_ERROR;
01444       }
01445       else
01446       {
01447         tmp_Flag_End = (ADC_FLAG_EOC);
01448       }
01449     }
01450 #else
01451     /* Check ADC DMA mode */
01452     if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_DMAEN) != 0UL)
01453     {
01454       SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
01455       return HAL_ERROR;
01456     }
01457     else
01458     {
01459       tmp_Flag_End = (ADC_FLAG_EOC);
01460     }
01461 #endif
01462   }
01463 
01464   /* Get tick count */
01465   tickstart = HAL_GetTick();
01466 
01467   /* Wait until End of unitary conversion or sequence conversions flag is raised */
01468   while ((hadc->Instance->ISR & tmp_Flag_End) == 0UL)
01469   {
01470     /* Check if timeout is disabled (set to infinite wait) */
01471     if (Timeout != HAL_MAX_DELAY)
01472     {
01473       if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0UL))
01474       {
01475         /* New check to avoid false timeout detection in case of preemption */
01476         if ((hadc->Instance->ISR & tmp_Flag_End) == 0UL)
01477         {
01478           /* Update ADC state machine to timeout */
01479           SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
01480 
01481           /* Process unlocked */
01482           __HAL_UNLOCK(hadc);
01483 
01484           return HAL_TIMEOUT;
01485         }
01486       }
01487     }
01488   }
01489 
01490   /* Update ADC state machine */
01491   SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
01492 
01493   /* Determine whether any further conversion upcoming on group regular       */
01494   /* by external trigger, continuous mode or scan sequence on going.          */
01495   if ((LL_ADC_REG_IsTriggerSourceSWStart(hadc->Instance) != 0UL)
01496       && (hadc->Init.ContinuousConvMode == DISABLE)
01497      )
01498   {
01499     /* Check whether end of sequence is reached */
01500     if (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOS))
01501     {
01502       /* Set ADC state */
01503       CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
01504 
01505       if ((hadc->State & HAL_ADC_STATE_INJ_BUSY) == 0UL)
01506       {
01507         SET_BIT(hadc->State, HAL_ADC_STATE_READY);
01508       }
01509     }
01510   }
01511 
01512   /* Get relevant register CFGR in ADC instance of ADC master or slave        */
01513   /* in function of multimode state (for devices with multimode               */
01514   /* available).                                                              */
01515 #if defined(ADC_MULTIMODE_SUPPORT)
01516   if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
01517       || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
01518       || (tmp_multimode_config == LL_ADC_MULTI_DUAL_INJ_SIMULT)
01519       || (tmp_multimode_config == LL_ADC_MULTI_DUAL_INJ_ALTERN)
01520      )
01521   {
01522     /* Retrieve handle ADC CFGR register */
01523     tmp_cfgr = READ_REG(hadc->Instance->CFGR);
01524   }
01525   else
01526   {
01527     /* Retrieve Master ADC CFGR register */
01528     tmpADC_Master = __LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance);
01529     tmp_cfgr = READ_REG(tmpADC_Master->CFGR);
01530   }
01531 #else
01532   /* Retrieve handle ADC CFGR register */
01533   tmp_cfgr = READ_REG(hadc->Instance->CFGR);
01534 #endif
01535 
01536   /* Clear polled flag */
01537   if (tmp_Flag_End == ADC_FLAG_EOS)
01538   {
01539     __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOS);
01540   }
01541   else
01542   {
01543     /* Clear end of conversion EOC flag of regular group if low power feature */
01544     /* "LowPowerAutoWait " is disabled, to not interfere with this feature    */
01545     /* until data register is read using function HAL_ADC_GetValue().         */
01546     if (READ_BIT(tmp_cfgr, ADC_CFGR_AUTDLY) == 0UL)
01547     {
01548       __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS));
01549     }
01550   }
01551 
01552   /* Return function status */
01553   return HAL_OK;
01554 }
01555 
01556 /**
01557   * @brief  Poll for ADC event.
01558   * @param hadc ADC handle
01559   * @param EventType the ADC event type.
01560   *          This parameter can be one of the following values:
01561   *            @arg @ref ADC_EOSMP_EVENT  ADC End of Sampling event
01562   *            @arg @ref ADC_AWD1_EVENT   ADC Analog watchdog 1 event (main analog watchdog, present on all STM32 devices)
01563   *            @arg @ref ADC_AWD2_EVENT   ADC Analog watchdog 2 event (additional analog watchdog, not present on all STM32 families)
01564   *            @arg @ref ADC_AWD3_EVENT   ADC Analog watchdog 3 event (additional analog watchdog, not present on all STM32 families)
01565   *            @arg @ref ADC_OVR_EVENT    ADC Overrun event
01566   *            @arg @ref ADC_JQOVF_EVENT  ADC Injected context queue overflow event
01567   * @param Timeout Timeout value in millisecond.
01568   * @note   The relevant flag is cleared if found to be set, except for ADC_FLAG_OVR.
01569   *         Indeed, the latter is reset only if hadc->Init.Overrun field is set
01570   *         to ADC_OVR_DATA_OVERWRITTEN. Otherwise, data register may be potentially overwritten
01571   *         by a new converted data as soon as OVR is cleared.
01572   *         To reset OVR flag once the preserved data is retrieved, the user can resort
01573   *         to macro __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
01574   * @retval HAL status
01575   */
01576 HAL_StatusTypeDef HAL_ADC_PollForEvent(ADC_HandleTypeDef *hadc, uint32_t EventType, uint32_t Timeout)
01577 {
01578   uint32_t tickstart;
01579 
01580   /* Check the parameters */
01581   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
01582   assert_param(IS_ADC_EVENT_TYPE(EventType));
01583 
01584   /* Get tick count */
01585   tickstart = HAL_GetTick();
01586 
01587   /* Check selected event flag */
01588   while (__HAL_ADC_GET_FLAG(hadc, EventType) == 0UL)
01589   {
01590     /* Check if timeout is disabled (set to infinite wait) */
01591     if (Timeout != HAL_MAX_DELAY)
01592     {
01593       if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0UL))
01594       {
01595         /* New check to avoid false timeout detection in case of preemption */
01596         if (__HAL_ADC_GET_FLAG(hadc, EventType) == 0UL)
01597         {
01598           /* Update ADC state machine to timeout */
01599           SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
01600 
01601           /* Process unlocked */
01602           __HAL_UNLOCK(hadc);
01603 
01604           return HAL_TIMEOUT;
01605         }
01606       }
01607     }
01608   }
01609 
01610   switch (EventType)
01611   {
01612     /* End Of Sampling event */
01613     case ADC_EOSMP_EVENT:
01614       /* Set ADC state */
01615       SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOSMP);
01616 
01617       /* Clear the End Of Sampling flag */
01618       __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOSMP);
01619 
01620       break;
01621 
01622     /* Analog watchdog (level out of window) event */
01623     /* Note: In case of several analog watchdog enabled, if needed to know      */
01624     /* which one triggered and on which ADCx, test ADC state of analog watchdog */
01625     /* flags HAL_ADC_STATE_AWD1/2/3 using function "HAL_ADC_GetState()".        */
01626     /* For example:                                                             */
01627     /*  " if ((HAL_ADC_GetState(hadc1) & HAL_ADC_STATE_AWD1) != 0UL) "          */
01628     /*  " if ((HAL_ADC_GetState(hadc1) & HAL_ADC_STATE_AWD2) != 0UL) "          */
01629     /*  " if ((HAL_ADC_GetState(hadc1) & HAL_ADC_STATE_AWD3) != 0UL) "          */
01630 
01631     /* Check analog watchdog 1 flag */
01632     case ADC_AWD_EVENT:
01633       /* Set ADC state */
01634       SET_BIT(hadc->State, HAL_ADC_STATE_AWD1);
01635 
01636       /* Clear ADC analog watchdog flag */
01637       __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD1);
01638 
01639       break;
01640 
01641     /* Check analog watchdog 2 flag */
01642     case ADC_AWD2_EVENT:
01643       /* Set ADC state */
01644       SET_BIT(hadc->State, HAL_ADC_STATE_AWD2);
01645 
01646       /* Clear ADC analog watchdog flag */
01647       __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD2);
01648 
01649       break;
01650 
01651     /* Check analog watchdog 3 flag */
01652     case ADC_AWD3_EVENT:
01653       /* Set ADC state */
01654       SET_BIT(hadc->State, HAL_ADC_STATE_AWD3);
01655 
01656       /* Clear ADC analog watchdog flag */
01657       __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD3);
01658 
01659       break;
01660 
01661     /* Injected context queue overflow event */
01662     case ADC_JQOVF_EVENT:
01663       /* Set ADC state */
01664       SET_BIT(hadc->State, HAL_ADC_STATE_INJ_JQOVF);
01665 
01666       /* Set ADC error code to Injected context queue overflow */
01667       SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_JQOVF);
01668 
01669       /* Clear ADC Injected context queue overflow flag */
01670       __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JQOVF);
01671 
01672       break;
01673 
01674     /* Overrun event */
01675     default: /* Case ADC_OVR_EVENT */
01676       /* If overrun is set to overwrite previous data, overrun event is not     */
01677       /* considered as an error.                                                */
01678       /* (cf ref manual "Managing conversions without using the DMA and without */
01679       /* overrun ")                                                             */
01680       if (hadc->Init.Overrun == ADC_OVR_DATA_PRESERVED)
01681       {
01682         /* Set ADC state */
01683         SET_BIT(hadc->State, HAL_ADC_STATE_REG_OVR);
01684 
01685         /* Set ADC error code to overrun */
01686         SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_OVR);
01687       }
01688       else
01689       {
01690         /* Clear ADC Overrun flag only if Overrun is set to ADC_OVR_DATA_OVERWRITTEN
01691            otherwise, data register is potentially overwritten by new converted data as soon
01692            as OVR is cleared. */
01693         __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
01694       }
01695       break;
01696   }
01697 
01698   /* Return function status */
01699   return HAL_OK;
01700 }
01701 
01702 /**
01703   * @brief  Enable ADC, start conversion of regular group with interruption.
01704   * @note   Interruptions enabled in this function according to initialization
01705   *         setting : EOC (end of conversion), EOS (end of sequence),
01706   *         OVR overrun.
01707   *         Each of these interruptions has its dedicated callback function.
01708   * @note   Case of multimode enabled (when multimode feature is available):
01709   *         HAL_ADC_Start_IT() must be called for ADC Slave first, then for
01710   *         ADC Master.
01711   *         For ADC Slave, ADC is enabled only (conversion is not started).
01712   *         For ADC Master, ADC is enabled and multimode conversion is started.
01713   * @note   To guarantee a proper reset of all interruptions once all the needed
01714   *         conversions are obtained, HAL_ADC_Stop_IT() must be called to ensure
01715   *         a correct stop of the IT-based conversions.
01716   * @note   By default, HAL_ADC_Start_IT() does not enable the End Of Sampling
01717   *         interruption. If required (e.g. in case of oversampling with trigger
01718   *         mode), the user must:
01719   *          1. first clear the EOSMP flag if set with macro __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOSMP)
01720   *          2. then enable the EOSMP interrupt with macro __HAL_ADC_ENABLE_IT(hadc, ADC_IT_EOSMP)
01721   *          before calling HAL_ADC_Start_IT().
01722   * @param hadc ADC handle
01723   * @retval HAL status
01724   */
01725 HAL_StatusTypeDef HAL_ADC_Start_IT(ADC_HandleTypeDef *hadc)
01726 {
01727   HAL_StatusTypeDef tmp_hal_status;
01728 #if defined(ADC_MULTIMODE_SUPPORT)
01729   const ADC_TypeDef *tmpADC_Master;
01730   uint32_t tmp_multimode_config = LL_ADC_GetMultimode(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
01731 #endif
01732 
01733   /* Check the parameters */
01734   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
01735 
01736   /* Perform ADC enable and conversion start if no conversion is on going */
01737   if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
01738   {
01739     /* Process locked */
01740     __HAL_LOCK(hadc);
01741 
01742     /* Enable the ADC peripheral */
01743     tmp_hal_status = ADC_Enable(hadc);
01744 
01745     /* Start conversion if ADC is effectively enabled */
01746     if (tmp_hal_status == HAL_OK)
01747     {
01748       /* Set ADC state                                                        */
01749       /* - Clear state bitfield related to regular group conversion results   */
01750       /* - Set state bitfield related to regular operation                    */
01751       ADC_STATE_CLR_SET(hadc->State,
01752                         HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP,
01753                         HAL_ADC_STATE_REG_BUSY);
01754 
01755 #if defined(ADC_MULTIMODE_SUPPORT)
01756       /* Reset HAL_ADC_STATE_MULTIMODE_SLAVE bit
01757         - if ADC instance is master or if multimode feature is not available
01758         - if multimode setting is disabled (ADC instance slave in independent mode) */
01759       if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
01760           || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
01761          )
01762       {
01763         CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
01764       }
01765 #endif
01766 
01767       /* Set ADC error code */
01768       /* Check if a conversion is on going on ADC group injected */
01769       if ((hadc->State & HAL_ADC_STATE_INJ_BUSY) != 0UL)
01770       {
01771         /* Reset ADC error code fields related to regular conversions only */
01772         CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR | HAL_ADC_ERROR_DMA));
01773       }
01774       else
01775       {
01776         /* Reset all ADC error code fields */
01777         ADC_CLEAR_ERRORCODE(hadc);
01778       }
01779 
01780       /* Clear ADC group regular conversion flag and overrun flag               */
01781       /* (To ensure of no unknown state from potential previous ADC operations) */
01782       __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
01783 
01784       /* Process unlocked */
01785       /* Unlock before starting ADC conversions: in case of potential         */
01786       /* interruption, to let the process to ADC IRQ Handler.                 */
01787       __HAL_UNLOCK(hadc);
01788 
01789       /* Disable all interruptions before enabling the desired ones */
01790       __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_EOS | ADC_IT_OVR));
01791 
01792       /* Enable ADC end of conversion interrupt */
01793       switch (hadc->Init.EOCSelection)
01794       {
01795         case ADC_EOC_SEQ_CONV:
01796           __HAL_ADC_ENABLE_IT(hadc, ADC_IT_EOS);
01797           break;
01798         /* case ADC_EOC_SINGLE_CONV */
01799         default:
01800           __HAL_ADC_ENABLE_IT(hadc, ADC_IT_EOC);
01801           break;
01802       }
01803 
01804       /* Enable ADC overrun interrupt */
01805       /* If hadc->Init.Overrun is set to ADC_OVR_DATA_PRESERVED, only then is
01806          ADC_IT_OVR enabled; otherwise data overwrite is considered as normal
01807          behavior and no CPU time is lost for a non-processed interruption */
01808       if (hadc->Init.Overrun == ADC_OVR_DATA_PRESERVED)
01809       {
01810         __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);
01811       }
01812 
01813       /* Enable conversion of regular group.                                  */
01814       /* If software start has been selected, conversion starts immediately.  */
01815       /* If external trigger has been selected, conversion will start at next */
01816       /* trigger event.                                                       */
01817       /* Case of multimode enabled (when multimode feature is available):     */
01818       /*  - if ADC is slave and dual regular conversions are enabled, ADC is  */
01819       /*    enabled only (conversion is not started),                         */
01820       /*  - if ADC is master, ADC is enabled and conversion is started.       */
01821 #if defined(ADC_MULTIMODE_SUPPORT)
01822       if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
01823           || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
01824           || (tmp_multimode_config == LL_ADC_MULTI_DUAL_INJ_SIMULT)
01825           || (tmp_multimode_config == LL_ADC_MULTI_DUAL_INJ_ALTERN)
01826          )
01827       {
01828         /* ADC instance is not a multimode slave instance with multimode regular conversions enabled */
01829         if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_JAUTO) != 0UL)
01830         {
01831           ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
01832 
01833           /* Enable as well injected interruptions in case
01834            HAL_ADCEx_InjectedStart_IT() has not been called beforehand. This
01835            allows to start regular and injected conversions when JAUTO is
01836            set with a single call to HAL_ADC_Start_IT() */
01837           switch (hadc->Init.EOCSelection)
01838           {
01839             case ADC_EOC_SEQ_CONV:
01840               __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC);
01841               __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOS);
01842               break;
01843             /* case ADC_EOC_SINGLE_CONV */
01844             default:
01845               __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOS);
01846               __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOC);
01847               break;
01848           }
01849         }
01850 
01851         /* Start ADC group regular conversion */
01852         LL_ADC_REG_StartConversion(hadc->Instance);
01853       }
01854       else
01855       {
01856         /* ADC instance is a multimode slave instance with multimode regular conversions enabled */
01857         SET_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
01858         /* if Master ADC JAUTO bit is set, Slave injected interruptions
01859            are enabled nevertheless (for same reason as above) */
01860         tmpADC_Master = __LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance);
01861         if (READ_BIT(tmpADC_Master->CFGR, ADC_CFGR_JAUTO) != 0UL)
01862         {
01863           /* First, update Slave State in setting HAL_ADC_STATE_INJ_BUSY bit
01864              and in resetting HAL_ADC_STATE_INJ_EOC bit */
01865           ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
01866           /* Next, set Slave injected interruptions */
01867           switch (hadc->Init.EOCSelection)
01868           {
01869             case ADC_EOC_SEQ_CONV:
01870               __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC);
01871               __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOS);
01872               break;
01873             /* case ADC_EOC_SINGLE_CONV */
01874             default:
01875               __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOS);
01876               __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOC);
01877               break;
01878           }
01879         }
01880       }
01881 #else
01882       /* ADC instance is not a multimode slave instance with multimode regular conversions enabled */
01883       if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_JAUTO) != 0UL)
01884       {
01885         ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
01886 
01887         /* Enable as well injected interruptions in case
01888          HAL_ADCEx_InjectedStart_IT() has not been called beforehand. This
01889          allows to start regular and injected conversions when JAUTO is
01890          set with a single call to HAL_ADC_Start_IT() */
01891         switch (hadc->Init.EOCSelection)
01892         {
01893           case ADC_EOC_SEQ_CONV:
01894             __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC);
01895             __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOS);
01896             break;
01897           /* case ADC_EOC_SINGLE_CONV */
01898           default:
01899             __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOS);
01900             __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOC);
01901             break;
01902         }
01903       }
01904 
01905       /* Start ADC group regular conversion */
01906       LL_ADC_REG_StartConversion(hadc->Instance);
01907 #endif
01908     }
01909     else
01910     {
01911       /* Process unlocked */
01912       __HAL_UNLOCK(hadc);
01913     }
01914 
01915   }
01916   else
01917   {
01918     tmp_hal_status = HAL_BUSY;
01919   }
01920 
01921   /* Return function status */
01922   return tmp_hal_status;
01923 }
01924 
01925 /**
01926   * @brief  Stop ADC conversion of regular group (and injected group in
01927   *         case of auto_injection mode), disable interrution of
01928   *         end-of-conversion, disable ADC peripheral.
01929   * @param hadc ADC handle
01930   * @retval HAL status.
01931   */
01932 HAL_StatusTypeDef HAL_ADC_Stop_IT(ADC_HandleTypeDef *hadc)
01933 {
01934   HAL_StatusTypeDef tmp_hal_status;
01935 
01936   /* Check the parameters */
01937   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
01938 
01939   /* Process locked */
01940   __HAL_LOCK(hadc);
01941 
01942   /* 1. Stop potential conversion on going, on ADC groups regular and injected */
01943   tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_INJECTED_GROUP);
01944 
01945   /* Disable ADC peripheral if conversions are effectively stopped */
01946   if (tmp_hal_status == HAL_OK)
01947   {
01948     /* Disable ADC end of conversion interrupt for regular group */
01949     /* Disable ADC overrun interrupt */
01950     __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_EOS | ADC_IT_OVR));
01951 
01952     /* 2. Disable the ADC peripheral */
01953     tmp_hal_status = ADC_Disable(hadc);
01954 
01955     /* Check if ADC is effectively disabled */
01956     if (tmp_hal_status == HAL_OK)
01957     {
01958       /* Set ADC state */
01959       ADC_STATE_CLR_SET(hadc->State,
01960                         HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
01961                         HAL_ADC_STATE_READY);
01962     }
01963   }
01964 
01965   /* Process unlocked */
01966   __HAL_UNLOCK(hadc);
01967 
01968   /* Return function status */
01969   return tmp_hal_status;
01970 }
01971 
01972 /**
01973   * @brief  Enable ADC, start conversion of regular group and transfer result through DMA.
01974   * @note   Interruptions enabled in this function:
01975   *         overrun (if applicable), DMA half transfer, DMA transfer complete.
01976   *         Each of these interruptions has its dedicated callback function.
01977   * @note   Case of multimode enabled (when multimode feature is available): HAL_ADC_Start_DMA()
01978   *         is designed for single-ADC mode only. For multimode, the dedicated
01979   *         HAL_ADCEx_MultiModeStart_DMA() function must be used.
01980   * @param hadc ADC handle
01981   * @param pData Destination Buffer address.
01982   * @param Length Number of data to be transferred from ADC peripheral to memory
01983   * @retval HAL status.
01984   */
01985 HAL_StatusTypeDef HAL_ADC_Start_DMA(ADC_HandleTypeDef *hadc, uint32_t *pData, uint32_t Length)
01986 {
01987   HAL_StatusTypeDef tmp_hal_status;
01988 #if defined(ADC_MULTIMODE_SUPPORT)
01989   uint32_t tmp_multimode_config = LL_ADC_GetMultimode(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
01990 #endif
01991 
01992   /* Check the parameters */
01993   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
01994 
01995   /* Perform ADC enable and conversion start if no conversion is on going */
01996   if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
01997   {
01998     /* Process locked */
01999     __HAL_LOCK(hadc);
02000 
02001 #if defined(ADC_MULTIMODE_SUPPORT)
02002     /* Ensure that multimode regular conversions are not enabled.   */
02003     /* Otherwise, dedicated API HAL_ADCEx_MultiModeStart_DMA() must be used.  */
02004     if ((ADC_IS_INDEPENDENT(hadc) != RESET)
02005         || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
02006         || (tmp_multimode_config == LL_ADC_MULTI_DUAL_INJ_SIMULT)
02007         || (tmp_multimode_config == LL_ADC_MULTI_DUAL_INJ_ALTERN)
02008        )
02009 #endif /* ADC_MULTIMODE_SUPPORT */
02010     {
02011       /* Enable the ADC peripheral */
02012       tmp_hal_status = ADC_Enable(hadc);
02013 
02014       /* Start conversion if ADC is effectively enabled */
02015       if (tmp_hal_status == HAL_OK)
02016       {
02017         /* Set ADC state                                                        */
02018         /* - Clear state bitfield related to regular group conversion results   */
02019         /* - Set state bitfield related to regular operation                    */
02020         ADC_STATE_CLR_SET(hadc->State,
02021                           HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP,
02022                           HAL_ADC_STATE_REG_BUSY);
02023 
02024 #if defined(ADC_MULTIMODE_SUPPORT)
02025         /* Reset HAL_ADC_STATE_MULTIMODE_SLAVE bit
02026           - if ADC instance is master or if multimode feature is not available
02027           - if multimode setting is disabled (ADC instance slave in independent mode) */
02028         if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
02029             || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
02030            )
02031         {
02032           CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
02033         }
02034 #endif
02035 
02036         /* Check if a conversion is on going on ADC group injected */
02037         if ((hadc->State & HAL_ADC_STATE_INJ_BUSY) != 0UL)
02038         {
02039           /* Reset ADC error code fields related to regular conversions only */
02040           CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR | HAL_ADC_ERROR_DMA));
02041         }
02042         else
02043         {
02044           /* Reset all ADC error code fields */
02045           ADC_CLEAR_ERRORCODE(hadc);
02046         }
02047 
02048         /* Set the DMA transfer complete callback */
02049         hadc->DMA_Handle->XferCpltCallback = ADC_DMAConvCplt;
02050 
02051         /* Set the DMA half transfer complete callback */
02052         hadc->DMA_Handle->XferHalfCpltCallback = ADC_DMAHalfConvCplt;
02053 
02054         /* Set the DMA error callback */
02055         hadc->DMA_Handle->XferErrorCallback = ADC_DMAError;
02056 
02057 
02058         /* Manage ADC and DMA start: ADC overrun interruption, DMA start,     */
02059         /* ADC start (in case of SW start):                                   */
02060 
02061         /* Clear regular group conversion flag and overrun flag               */
02062         /* (To ensure of no unknown state from potential previous ADC         */
02063         /* operations)                                                        */
02064         __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
02065 
02066         /* Process unlocked */
02067         /* Unlock before starting ADC conversions: in case of potential         */
02068         /* interruption, to let the process to ADC IRQ Handler.                 */
02069         __HAL_UNLOCK(hadc);
02070 
02071         /* With DMA, overrun event is always considered as an error even if
02072            hadc->Init.Overrun is set to ADC_OVR_DATA_OVERWRITTEN. Therefore,
02073            ADC_IT_OVR is enabled. */
02074         __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);
02075 
02076         /* Enable ADC DMA mode */
02077         SET_BIT(hadc->Instance->CFGR, ADC_CFGR_DMAEN);
02078 
02079         /* Start the DMA channel */
02080         tmp_hal_status = HAL_DMA_Start_IT(hadc->DMA_Handle, (uint32_t)&hadc->Instance->DR, (uint32_t)pData, Length);
02081 
02082         /* Enable conversion of regular group.                                  */
02083         /* If software start has been selected, conversion starts immediately.  */
02084         /* If external trigger has been selected, conversion will start at next */
02085         /* trigger event.                                                       */
02086         /* Start ADC group regular conversion */
02087         LL_ADC_REG_StartConversion(hadc->Instance);
02088       }
02089       else
02090       {
02091         /* Process unlocked */
02092         __HAL_UNLOCK(hadc);
02093       }
02094 
02095     }
02096 #if defined(ADC_MULTIMODE_SUPPORT)
02097     else
02098     {
02099       tmp_hal_status = HAL_ERROR;
02100       /* Process unlocked */
02101       __HAL_UNLOCK(hadc);
02102     }
02103 #endif
02104   }
02105   else
02106   {
02107     tmp_hal_status = HAL_BUSY;
02108   }
02109 
02110   /* Return function status */
02111   return tmp_hal_status;
02112 }
02113 
02114 /**
02115   * @brief  Stop ADC conversion of regular group (and injected group in
02116   *         case of auto_injection mode), disable ADC DMA transfer, disable
02117   *         ADC peripheral.
02118   * @note:  ADC peripheral disable is forcing stop of potential
02119   *         conversion on ADC group injected. If ADC group injected is under use, it
02120   *         should be preliminarily stopped using HAL_ADCEx_InjectedStop function.
02121   * @note   Case of multimode enabled (when multimode feature is available):
02122   *         HAL_ADC_Stop_DMA() function is dedicated to single-ADC mode only.
02123   *         For multimode, the dedicated HAL_ADCEx_MultiModeStop_DMA() API must be used.
02124   * @param hadc ADC handle
02125   * @retval HAL status.
02126   */
02127 HAL_StatusTypeDef HAL_ADC_Stop_DMA(ADC_HandleTypeDef *hadc)
02128 {
02129   HAL_StatusTypeDef tmp_hal_status;
02130 
02131   /* Check the parameters */
02132   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
02133 
02134   /* Process locked */
02135   __HAL_LOCK(hadc);
02136 
02137   /* 1. Stop potential ADC group regular conversion on going */
02138   tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_INJECTED_GROUP);
02139 
02140   /* Disable ADC peripheral if conversions are effectively stopped */
02141   if (tmp_hal_status == HAL_OK)
02142   {
02143     /* Disable ADC DMA (ADC DMA configuration of continuous requests is kept) */
02144     CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_DMAEN);
02145 
02146     /* Disable the DMA channel (in case of DMA in circular mode or stop       */
02147     /* while DMA transfer is on going)                                        */
02148     if (hadc->DMA_Handle->State == HAL_DMA_STATE_BUSY)
02149     {
02150       tmp_hal_status = HAL_DMA_Abort(hadc->DMA_Handle);
02151 
02152       /* Check if DMA channel effectively disabled */
02153       if (tmp_hal_status != HAL_OK)
02154       {
02155         /* Update ADC state machine to error */
02156         SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
02157       }
02158     }
02159 
02160     /* Disable ADC overrun interrupt */
02161     __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);
02162 
02163     /* 2. Disable the ADC peripheral */
02164     /* Update "tmp_hal_status" only if DMA channel disabling passed,          */
02165     /* to keep in memory a potential failing status.                          */
02166     if (tmp_hal_status == HAL_OK)
02167     {
02168       tmp_hal_status = ADC_Disable(hadc);
02169     }
02170     else
02171     {
02172       (void)ADC_Disable(hadc);
02173     }
02174 
02175     /* Check if ADC is effectively disabled */
02176     if (tmp_hal_status == HAL_OK)
02177     {
02178       /* Set ADC state */
02179       ADC_STATE_CLR_SET(hadc->State,
02180                         HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
02181                         HAL_ADC_STATE_READY);
02182     }
02183 
02184   }
02185 
02186   /* Process unlocked */
02187   __HAL_UNLOCK(hadc);
02188 
02189   /* Return function status */
02190   return tmp_hal_status;
02191 }
02192 
02193 /**
02194   * @brief  Get ADC regular group conversion result.
02195   * @note   Reading register DR automatically clears ADC flag EOC
02196   *         (ADC group regular end of unitary conversion).
02197   * @note   This function does not clear ADC flag EOS
02198   *         (ADC group regular end of sequence conversion).
02199   *         Occurrence of flag EOS rising:
02200   *          - If sequencer is composed of 1 rank, flag EOS is equivalent
02201   *            to flag EOC.
02202   *          - If sequencer is composed of several ranks, during the scan
02203   *            sequence flag EOC only is raised, at the end of the scan sequence
02204   *            both flags EOC and EOS are raised.
02205   *         To clear this flag, either use function:
02206   *         in programming model IT: @ref HAL_ADC_IRQHandler(), in programming
02207   *         model polling: @ref HAL_ADC_PollForConversion()
02208   *         or @ref __HAL_ADC_CLEAR_FLAG(&hadc, ADC_FLAG_EOS).
02209   * @param hadc ADC handle
02210   * @retval ADC group regular conversion data
02211   */
02212 uint32_t HAL_ADC_GetValue(ADC_HandleTypeDef *hadc)
02213 {
02214   /* Check the parameters */
02215   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
02216 
02217   /* Note: EOC flag is not cleared here by software because automatically     */
02218   /*       cleared by hardware when reading register DR.                      */
02219 
02220   /* Return ADC converted value */
02221   return hadc->Instance->DR;
02222 }
02223 
02224 /**
02225   * @brief  Handle ADC interrupt request.
02226   * @param hadc ADC handle
02227   * @retval None
02228   */
02229 void HAL_ADC_IRQHandler(ADC_HandleTypeDef *hadc)
02230 {
02231   uint32_t overrun_error = 0UL; /* flag set if overrun occurrence has to be considered as an error */
02232   uint32_t tmp_isr = hadc->Instance->ISR;
02233   uint32_t tmp_ier = hadc->Instance->IER;
02234   uint32_t tmp_adc_inj_is_trigger_source_sw_start;
02235   uint32_t tmp_adc_reg_is_trigger_source_sw_start;
02236   uint32_t tmp_cfgr;
02237 #if defined(ADC_MULTIMODE_SUPPORT)
02238   const ADC_TypeDef *tmpADC_Master;
02239   uint32_t tmp_multimode_config = LL_ADC_GetMultimode(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
02240 #endif
02241 
02242   /* Check the parameters */
02243   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
02244   assert_param(IS_ADC_EOC_SELECTION(hadc->Init.EOCSelection));
02245 
02246   /* ========== Check End of Sampling flag for ADC group regular ========== */
02247   if (((tmp_isr & ADC_FLAG_EOSMP) == ADC_FLAG_EOSMP) && ((tmp_ier & ADC_IT_EOSMP) == ADC_IT_EOSMP))
02248   {
02249     /* Update state machine on end of sampling status if not in error state */
02250     if ((hadc->State & HAL_ADC_STATE_ERROR_INTERNAL) == 0UL)
02251     {
02252       /* Set ADC state */
02253       SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOSMP);
02254     }
02255 
02256     /* End Of Sampling callback */
02257 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
02258     hadc->EndOfSamplingCallback(hadc);
02259 #else
02260     HAL_ADCEx_EndOfSamplingCallback(hadc);
02261 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
02262 
02263     /* Clear regular group conversion flag */
02264     __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOSMP);
02265   }
02266 
02267   /* ====== Check ADC group regular end of unitary conversion sequence conversions ===== */
02268   if ((((tmp_isr & ADC_FLAG_EOC) == ADC_FLAG_EOC) && ((tmp_ier & ADC_IT_EOC) == ADC_IT_EOC)) ||
02269       (((tmp_isr & ADC_FLAG_EOS) == ADC_FLAG_EOS) && ((tmp_ier & ADC_IT_EOS) == ADC_IT_EOS)))
02270   {
02271     /* Update state machine on conversion status if not in error state */
02272     if ((hadc->State & HAL_ADC_STATE_ERROR_INTERNAL) == 0UL)
02273     {
02274       /* Set ADC state */
02275       SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
02276     }
02277 
02278     /* Determine whether any further conversion upcoming on group regular     */
02279     /* by external trigger, continuous mode or scan sequence on going         */
02280     /* to disable interruption.                                               */
02281     if (LL_ADC_REG_IsTriggerSourceSWStart(hadc->Instance) != 0UL)
02282     {
02283       /* Get relevant register CFGR in ADC instance of ADC master or slave    */
02284       /* in function of multimode state (for devices with multimode           */
02285       /* available).                                                          */
02286 #if defined(ADC_MULTIMODE_SUPPORT)
02287       if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
02288           || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
02289           || (tmp_multimode_config == LL_ADC_MULTI_DUAL_INJ_SIMULT)
02290           || (tmp_multimode_config == LL_ADC_MULTI_DUAL_INJ_ALTERN)
02291          )
02292       {
02293         /* check CONT bit directly in handle ADC CFGR register */
02294         tmp_cfgr = READ_REG(hadc->Instance->CFGR);
02295       }
02296       else
02297       {
02298         /* else need to check Master ADC CONT bit */
02299         tmpADC_Master = __LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance);
02300         tmp_cfgr = READ_REG(tmpADC_Master->CFGR);
02301       }
02302 #else
02303       tmp_cfgr = READ_REG(hadc->Instance->CFGR);
02304 #endif
02305 
02306       /* Carry on if continuous mode is disabled */
02307       if (READ_BIT(tmp_cfgr, ADC_CFGR_CONT) != ADC_CFGR_CONT)
02308       {
02309         /* If End of Sequence is reached, disable interrupts */
02310         if (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOS))
02311         {
02312           /* Allowed to modify bits ADC_IT_EOC/ADC_IT_EOS only if bit         */
02313           /* ADSTART==0 (no conversion on going)                              */
02314           if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
02315           {
02316             /* Disable ADC end of sequence conversion interrupt */
02317             /* Note: Overrun interrupt was enabled with EOC interrupt in      */
02318             /* HAL_Start_IT(), but is not disabled here because can be used   */
02319             /* by overrun IRQ process below.                                  */
02320             __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC | ADC_IT_EOS);
02321 
02322             /* Set ADC state */
02323             CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
02324 
02325             if ((hadc->State & HAL_ADC_STATE_INJ_BUSY) == 0UL)
02326             {
02327               SET_BIT(hadc->State, HAL_ADC_STATE_READY);
02328             }
02329           }
02330           else
02331           {
02332             /* Change ADC state to error state */
02333             SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
02334 
02335             /* Set ADC error code to ADC peripheral internal error */
02336             SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
02337           }
02338         }
02339       }
02340     }
02341 
02342     /* Conversion complete callback */
02343     /* Note: Into callback function "HAL_ADC_ConvCpltCallback()",             */
02344     /*       to determine if conversion has been triggered from EOC or EOS,   */
02345     /*       possibility to use:                                              */
02346     /*        " if ( __HAL_ADC_GET_FLAG(&hadc, ADC_FLAG_EOS)) "               */
02347 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
02348     hadc->ConvCpltCallback(hadc);
02349 #else
02350     HAL_ADC_ConvCpltCallback(hadc);
02351 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
02352 
02353     /* Clear regular group conversion flag */
02354     /* Note: in case of overrun set to ADC_OVR_DATA_PRESERVED, end of         */
02355     /*       conversion flags clear induces the release of the preserved data.*/
02356     /*       Therefore, if the preserved data value is needed, it must be     */
02357     /*       read preliminarily into HAL_ADC_ConvCpltCallback().              */
02358     __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS));
02359   }
02360 
02361   /* ====== Check ADC group injected end of unitary conversion sequence conversions ===== */
02362   if ((((tmp_isr & ADC_FLAG_JEOC) == ADC_FLAG_JEOC) && ((tmp_ier & ADC_IT_JEOC) == ADC_IT_JEOC)) ||
02363       (((tmp_isr & ADC_FLAG_JEOS) == ADC_FLAG_JEOS) && ((tmp_ier & ADC_IT_JEOS) == ADC_IT_JEOS)))
02364   {
02365     /* Update state machine on conversion status if not in error state */
02366     if ((hadc->State & HAL_ADC_STATE_ERROR_INTERNAL) == 0UL)
02367     {
02368       /* Set ADC state */
02369       SET_BIT(hadc->State, HAL_ADC_STATE_INJ_EOC);
02370     }
02371 
02372     /* Retrieve ADC configuration */
02373     tmp_adc_inj_is_trigger_source_sw_start = LL_ADC_INJ_IsTriggerSourceSWStart(hadc->Instance);
02374     tmp_adc_reg_is_trigger_source_sw_start = LL_ADC_REG_IsTriggerSourceSWStart(hadc->Instance);
02375     /* Get relevant register CFGR in ADC instance of ADC master or slave  */
02376     /* in function of multimode state (for devices with multimode         */
02377     /* available).                                                        */
02378 #if defined(ADC_MULTIMODE_SUPPORT)
02379     if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
02380         || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
02381         || (tmp_multimode_config == LL_ADC_MULTI_DUAL_REG_SIMULT)
02382         || (tmp_multimode_config == LL_ADC_MULTI_DUAL_REG_INTERL)
02383        )
02384     {
02385       tmp_cfgr = READ_REG(hadc->Instance->CFGR);
02386     }
02387     else
02388     {
02389       tmpADC_Master = __LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance);
02390       tmp_cfgr = READ_REG(tmpADC_Master->CFGR);
02391     }
02392 #else
02393     tmp_cfgr = READ_REG(hadc->Instance->CFGR);
02394 #endif
02395 
02396     /* Disable interruption if no further conversion upcoming by injected     */
02397     /* external trigger or by automatic injected conversion with regular      */
02398     /* group having no further conversion upcoming (same conditions as        */
02399     /* regular group interruption disabling above),                           */
02400     /* and if injected scan sequence is completed.                            */
02401     if (tmp_adc_inj_is_trigger_source_sw_start != 0UL)
02402     {
02403       if ((READ_BIT(tmp_cfgr, ADC_CFGR_JAUTO) == 0UL) ||
02404           ((tmp_adc_reg_is_trigger_source_sw_start != 0UL) &&
02405            (READ_BIT(tmp_cfgr, ADC_CFGR_CONT) == 0UL)))
02406       {
02407         /* If End of Sequence is reached, disable interrupts */
02408         if (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_JEOS))
02409         {
02410           /* Particular case if injected contexts queue is enabled:             */
02411           /* when the last context has been fully processed, JSQR is reset      */
02412           /* by the hardware. Even if no injected conversion is planned to come */
02413           /* (queue empty, triggers are ignored), it can start again            */
02414           /* immediately after setting a new context (JADSTART is still set).   */
02415           /* Therefore, state of HAL ADC injected group is kept to busy.        */
02416           if (READ_BIT(tmp_cfgr, ADC_CFGR_JQM) == 0UL)
02417           {
02418             /* Allowed to modify bits ADC_IT_JEOC/ADC_IT_JEOS only if bit       */
02419             /* JADSTART==0 (no conversion on going)                             */
02420             if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) == 0UL)
02421             {
02422               /* Disable ADC end of sequence conversion interrupt  */
02423               __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC | ADC_IT_JEOS);
02424 
02425               /* Set ADC state */
02426               CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
02427 
02428               if ((hadc->State & HAL_ADC_STATE_REG_BUSY) == 0UL)
02429               {
02430                 SET_BIT(hadc->State, HAL_ADC_STATE_READY);
02431               }
02432             }
02433             else
02434             {
02435               /* Update ADC state machine to error */
02436               SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
02437 
02438               /* Set ADC error code to ADC peripheral internal error */
02439               SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
02440             }
02441           }
02442         }
02443       }
02444     }
02445 
02446     /* Injected Conversion complete callback */
02447     /* Note:  HAL_ADCEx_InjectedConvCpltCallback can resort to
02448               if (__HAL_ADC_GET_FLAG(&hadc, ADC_FLAG_JEOS)) or
02449               if (__HAL_ADC_GET_FLAG(&hadc, ADC_FLAG_JEOC)) to determine whether
02450               interruption has been triggered by end of conversion or end of
02451               sequence.    */
02452 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
02453     hadc->InjectedConvCpltCallback(hadc);
02454 #else
02455     HAL_ADCEx_InjectedConvCpltCallback(hadc);
02456 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
02457 
02458     /* Clear injected group conversion flag */
02459     __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOC | ADC_FLAG_JEOS);
02460   }
02461 
02462   /* ========== Check Analog watchdog 1 flag ========== */
02463   if (((tmp_isr & ADC_FLAG_AWD1) == ADC_FLAG_AWD1) && ((tmp_ier & ADC_IT_AWD1) == ADC_IT_AWD1))
02464   {
02465     /* Set ADC state */
02466     SET_BIT(hadc->State, HAL_ADC_STATE_AWD1);
02467 
02468     /* Level out of window 1 callback */
02469 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
02470     hadc->LevelOutOfWindowCallback(hadc);
02471 #else
02472     HAL_ADC_LevelOutOfWindowCallback(hadc);
02473 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
02474 
02475     /* Clear ADC analog watchdog flag */
02476     __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD1);
02477   }
02478 
02479   /* ========== Check analog watchdog 2 flag ========== */
02480   if (((tmp_isr & ADC_FLAG_AWD2) == ADC_FLAG_AWD2) && ((tmp_ier & ADC_IT_AWD2) == ADC_IT_AWD2))
02481   {
02482     /* Set ADC state */
02483     SET_BIT(hadc->State, HAL_ADC_STATE_AWD2);
02484 
02485     /* Level out of window 2 callback */
02486 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
02487     hadc->LevelOutOfWindow2Callback(hadc);
02488 #else
02489     HAL_ADCEx_LevelOutOfWindow2Callback(hadc);
02490 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
02491 
02492     /* Clear ADC analog watchdog flag */
02493     __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD2);
02494   }
02495 
02496   /* ========== Check analog watchdog 3 flag ========== */
02497   if (((tmp_isr & ADC_FLAG_AWD3) == ADC_FLAG_AWD3) && ((tmp_ier & ADC_IT_AWD3) == ADC_IT_AWD3))
02498   {
02499     /* Set ADC state */
02500     SET_BIT(hadc->State, HAL_ADC_STATE_AWD3);
02501 
02502     /* Level out of window 3 callback */
02503 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
02504     hadc->LevelOutOfWindow3Callback(hadc);
02505 #else
02506     HAL_ADCEx_LevelOutOfWindow3Callback(hadc);
02507 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
02508 
02509     /* Clear ADC analog watchdog flag */
02510     __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD3);
02511   }
02512 
02513   /* ========== Check Overrun flag ========== */
02514   if (((tmp_isr & ADC_FLAG_OVR) == ADC_FLAG_OVR) && ((tmp_ier & ADC_IT_OVR) == ADC_IT_OVR))
02515   {
02516     /* If overrun is set to overwrite previous data (default setting),        */
02517     /* overrun event is not considered as an error.                           */
02518     /* (cf ref manual "Managing conversions without using the DMA and without */
02519     /* overrun ")                                                             */
02520     /* Exception for usage with DMA overrun event always considered as an     */
02521     /* error.                                                                 */
02522     if (hadc->Init.Overrun == ADC_OVR_DATA_PRESERVED)
02523     {
02524       overrun_error = 1UL;
02525     }
02526     else
02527     {
02528       /* Check DMA configuration */
02529 #if defined(ADC_MULTIMODE_SUPPORT)
02530       if (tmp_multimode_config != LL_ADC_MULTI_INDEPENDENT)
02531       {
02532         /* Multimode (when feature is available) is enabled,
02533            Common Control Register MDMA bits must be checked. */
02534         if (LL_ADC_GetMultiDMATransfer(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) != LL_ADC_MULTI_REG_DMA_EACH_ADC)
02535         {
02536           overrun_error = 1UL;
02537         }
02538       }
02539       else
02540 #endif
02541       {
02542         /* Multimode not set or feature not available or ADC independent */
02543         if ((hadc->Instance->CFGR & ADC_CFGR_DMAEN) != 0UL)
02544         {
02545           overrun_error = 1UL;
02546         }
02547       }
02548     }
02549 
02550     if (overrun_error == 1UL)
02551     {
02552       /* Change ADC state to error state */
02553       SET_BIT(hadc->State, HAL_ADC_STATE_REG_OVR);
02554 
02555       /* Set ADC error code to overrun */
02556       SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_OVR);
02557 
02558       /* Error callback */
02559       /* Note: In case of overrun, ADC conversion data is preserved until     */
02560       /*       flag OVR is reset.                                             */
02561       /*       Therefore, old ADC conversion data can be retrieved in         */
02562       /*       function "HAL_ADC_ErrorCallback()".                            */
02563 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
02564       hadc->ErrorCallback(hadc);
02565 #else
02566       HAL_ADC_ErrorCallback(hadc);
02567 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
02568     }
02569 
02570     /* Clear ADC overrun flag */
02571     __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
02572   }
02573 
02574   /* ========== Check Injected context queue overflow flag ========== */
02575   if (((tmp_isr & ADC_FLAG_JQOVF) == ADC_FLAG_JQOVF) && ((tmp_ier & ADC_IT_JQOVF) == ADC_IT_JQOVF))
02576   {
02577     /* Change ADC state to overrun state */
02578     SET_BIT(hadc->State, HAL_ADC_STATE_INJ_JQOVF);
02579 
02580     /* Set ADC error code to Injected context queue overflow */
02581     SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_JQOVF);
02582 
02583     /* Clear the Injected context queue overflow flag */
02584     __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JQOVF);
02585 
02586     /* Injected context queue overflow callback */
02587 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
02588     hadc->InjectedQueueOverflowCallback(hadc);
02589 #else
02590     HAL_ADCEx_InjectedQueueOverflowCallback(hadc);
02591 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
02592   }
02593 
02594 }
02595 
02596 /**
02597   * @brief  Conversion complete callback in non-blocking mode.
02598   * @param hadc ADC handle
02599   * @retval None
02600   */
02601 __weak void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
02602 {
02603   /* Prevent unused argument(s) compilation warning */
02604   UNUSED(hadc);
02605 
02606   /* NOTE : This function should not be modified. When the callback is needed,
02607             function HAL_ADC_ConvCpltCallback must be implemented in the user file.
02608    */
02609 }
02610 
02611 /**
02612   * @brief  Conversion DMA half-transfer callback in non-blocking mode.
02613   * @param hadc ADC handle
02614   * @retval None
02615   */
02616 __weak void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef *hadc)
02617 {
02618   /* Prevent unused argument(s) compilation warning */
02619   UNUSED(hadc);
02620 
02621   /* NOTE : This function should not be modified. When the callback is needed,
02622             function HAL_ADC_ConvHalfCpltCallback must be implemented in the user file.
02623   */
02624 }
02625 
02626 /**
02627   * @brief  Analog watchdog 1 callback in non-blocking mode.
02628   * @param hadc ADC handle
02629   * @retval None
02630   */
02631 __weak void HAL_ADC_LevelOutOfWindowCallback(ADC_HandleTypeDef *hadc)
02632 {
02633   /* Prevent unused argument(s) compilation warning */
02634   UNUSED(hadc);
02635 
02636   /* NOTE : This function should not be modified. When the callback is needed,
02637             function HAL_ADC_LevelOutOfWindowCallback must be implemented in the user file.
02638   */
02639 }
02640 
02641 /**
02642   * @brief  ADC error callback in non-blocking mode
02643   *         (ADC conversion with interruption or transfer by DMA).
02644   * @note   In case of error due to overrun when using ADC with DMA transfer
02645   *         (HAL ADC handle parameter "ErrorCode" to state "HAL_ADC_ERROR_OVR"):
02646   *         - Reinitialize the DMA using function "HAL_ADC_Stop_DMA()".
02647   *         - If needed, restart a new ADC conversion using function
02648   *           "HAL_ADC_Start_DMA()"
02649   *           (this function is also clearing overrun flag)
02650   * @param hadc ADC handle
02651   * @retval None
02652   */
02653 __weak void HAL_ADC_ErrorCallback(ADC_HandleTypeDef *hadc)
02654 {
02655   /* Prevent unused argument(s) compilation warning */
02656   UNUSED(hadc);
02657 
02658   /* NOTE : This function should not be modified. When the callback is needed,
02659             function HAL_ADC_ErrorCallback must be implemented in the user file.
02660   */
02661 }
02662 
02663 /**
02664   * @}
02665   */
02666 
02667 /** @defgroup ADC_Exported_Functions_Group3 Peripheral Control functions
02668   * @brief    Peripheral Control functions
02669   *
02670 @verbatim
02671  ===============================================================================
02672              ##### Peripheral Control functions #####
02673  ===============================================================================
02674     [..]  This section provides functions allowing to:
02675       (+) Configure channels on regular group
02676       (+) Configure the analog watchdog
02677 
02678 @endverbatim
02679   * @{
02680   */
02681 
02682 /**
02683   * @brief  Configure a channel to be assigned to ADC group regular.
02684   * @note   In case of usage of internal measurement channels:
02685   *         Vbat/VrefInt/TempSensor.
02686   *         These internal paths can be disabled using function
02687   *         HAL_ADC_DeInit().
02688   * @note   Possibility to update parameters on the fly:
02689   *         This function initializes channel into ADC group regular,
02690   *         following calls to this function can be used to reconfigure
02691   *         some parameters of structure "ADC_ChannelConfTypeDef" on the fly,
02692   *         without resetting the ADC.
02693   *         The setting of these parameters is conditioned to ADC state:
02694   *         Refer to comments of structure "ADC_ChannelConfTypeDef".
02695   * @param hadc ADC handle
02696   * @param sConfig Structure of ADC channel assigned to ADC group regular.
02697   * @retval HAL status
02698   */
02699 HAL_StatusTypeDef HAL_ADC_ConfigChannel(ADC_HandleTypeDef *hadc, ADC_ChannelConfTypeDef *sConfig)
02700 {
02701   HAL_StatusTypeDef tmp_hal_status = HAL_OK;
02702   uint32_t tmpOffsetShifted;
02703   uint32_t tmp_config_internal_channel;
02704   __IO uint32_t wait_loop_index = 0UL;
02705   uint32_t tmp_adc_is_conversion_on_going_regular;
02706   uint32_t tmp_adc_is_conversion_on_going_injected;
02707 
02708   /* Check the parameters */
02709   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
02710   assert_param(IS_ADC_REGULAR_RANK(sConfig->Rank));
02711   assert_param(IS_ADC_SAMPLE_TIME(sConfig->SamplingTime));
02712   assert_param(IS_ADC_SINGLE_DIFFERENTIAL(sConfig->SingleDiff));
02713   assert_param(IS_ADC_OFFSET_NUMBER(sConfig->OffsetNumber));
02714   assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), sConfig->Offset));
02715 
02716   /* if ROVSE is set, the value of the OFFSETy_EN bit in ADCx_OFRy register is
02717      ignored (considered as reset) */
02718   assert_param(!((sConfig->OffsetNumber != ADC_OFFSET_NONE) && (hadc->Init.OversamplingMode == ENABLE)));
02719 
02720   /* Verification of channel number */
02721   if (sConfig->SingleDiff != ADC_DIFFERENTIAL_ENDED)
02722   {
02723     assert_param(IS_ADC_CHANNEL(hadc, sConfig->Channel));
02724   }
02725   else
02726   {
02727     assert_param(IS_ADC_DIFF_CHANNEL(hadc, sConfig->Channel));
02728   }
02729 
02730   /* Process locked */
02731   __HAL_LOCK(hadc);
02732 
02733   /* Parameters update conditioned to ADC state:                              */
02734   /* Parameters that can be updated when ADC is disabled or enabled without   */
02735   /* conversion on going on regular group:                                    */
02736   /*  - Channel number                                                        */
02737   /*  - Channel rank                                                          */
02738   if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
02739   {
02740 #if !defined (USE_FULL_ASSERT)
02741     /* Correspondence for compatibility with legacy definition of             */
02742     /* sequencer ranks in direct number format. This correspondence can       */
02743     /* be done only on ranks 1 to 5 due to literal values.                    */
02744     /* Note: Sequencer ranks in direct number format are no more used         */
02745     /*       and are detected by activating USE_FULL_ASSERT feature.          */
02746     if (sConfig->Rank <= 5U)
02747     {
02748       switch (sConfig->Rank)
02749       {
02750         case 2U:
02751           sConfig->Rank = ADC_REGULAR_RANK_2;
02752           break;
02753         case 3U:
02754           sConfig->Rank = ADC_REGULAR_RANK_3;
02755           break;
02756         case 4U:
02757           sConfig->Rank = ADC_REGULAR_RANK_4;
02758           break;
02759         case 5U:
02760           sConfig->Rank = ADC_REGULAR_RANK_5;
02761           break;
02762         /* case 1U */
02763         default:
02764           sConfig->Rank = ADC_REGULAR_RANK_1;
02765           break;
02766       }
02767     }
02768 #endif
02769 
02770     /* Set ADC group regular sequence: channel on the selected scan sequence rank */
02771     LL_ADC_REG_SetSequencerRanks(hadc->Instance, sConfig->Rank, sConfig->Channel);
02772 
02773     /* Parameters update conditioned to ADC state:                              */
02774     /* Parameters that can be updated when ADC is disabled or enabled without   */
02775     /* conversion on going on regular group:                                    */
02776     /*  - Channel sampling time                                                 */
02777     /*  - Channel offset                                                        */
02778     tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
02779     tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
02780     if ((tmp_adc_is_conversion_on_going_regular == 0UL)
02781         && (tmp_adc_is_conversion_on_going_injected == 0UL)
02782        )
02783     {
02784 #if defined(ADC_SMPR1_SMPPLUS)
02785       /* Manage specific case of sampling time 3.5 cycles replacing 2.5 cyles */
02786       if (sConfig->SamplingTime == ADC_SAMPLETIME_3CYCLES_5)
02787       {
02788         /* Set sampling time of the selected ADC channel */
02789         LL_ADC_SetChannelSamplingTime(hadc->Instance, sConfig->Channel, LL_ADC_SAMPLINGTIME_2CYCLES_5);
02790 
02791         /* Set ADC sampling time common configuration */
02792         LL_ADC_SetSamplingTimeCommonConfig(hadc->Instance, LL_ADC_SAMPLINGTIME_COMMON_3C5_REPL_2C5);
02793       }
02794       else
02795       {
02796         /* Set sampling time of the selected ADC channel */
02797         LL_ADC_SetChannelSamplingTime(hadc->Instance, sConfig->Channel, sConfig->SamplingTime);
02798 
02799         /* Set ADC sampling time common configuration */
02800         LL_ADC_SetSamplingTimeCommonConfig(hadc->Instance, LL_ADC_SAMPLINGTIME_COMMON_DEFAULT);
02801       }
02802 #else
02803       /* Set sampling time of the selected ADC channel */
02804       LL_ADC_SetChannelSamplingTime(hadc->Instance, sConfig->Channel, sConfig->SamplingTime);
02805 #endif
02806 
02807       /* Configure the offset: offset enable/disable, channel, offset value */
02808 
02809       /* Shift the offset with respect to the selected ADC resolution. */
02810       /* Offset has to be left-aligned on bit 11, the LSB (right bits) are set to 0 */
02811       tmpOffsetShifted = ADC_OFFSET_SHIFT_RESOLUTION(hadc, (uint32_t)sConfig->Offset);
02812 
02813       if (sConfig->OffsetNumber != ADC_OFFSET_NONE)
02814       {
02815         /* Set ADC selected offset number */
02816         LL_ADC_SetOffset(hadc->Instance, sConfig->OffsetNumber, sConfig->Channel, tmpOffsetShifted);
02817 
02818       }
02819       else
02820       {
02821         /* Scan each offset register to check if the selected channel is targeted. */
02822         /* If this is the case, the corresponding offset number is disabled.       */
02823         if (__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_1))
02824             == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfig->Channel))
02825         {
02826           LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_1, LL_ADC_OFFSET_DISABLE);
02827         }
02828         if (__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_2))
02829             == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfig->Channel))
02830         {
02831           LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_2, LL_ADC_OFFSET_DISABLE);
02832         }
02833         if (__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_3))
02834             == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfig->Channel))
02835         {
02836           LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_3, LL_ADC_OFFSET_DISABLE);
02837         }
02838         if (__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_4))
02839             == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfig->Channel))
02840         {
02841           LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_4, LL_ADC_OFFSET_DISABLE);
02842         }
02843       }
02844     }
02845 
02846     /* Parameters update conditioned to ADC state:                              */
02847     /* Parameters that can be updated only when ADC is disabled:                */
02848     /*  - Single or differential mode                                           */
02849     if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
02850     {
02851       /* Set mode single-ended or differential input of the selected ADC channel */
02852       LL_ADC_SetChannelSingleDiff(hadc->Instance, sConfig->Channel, sConfig->SingleDiff);
02853 
02854       /* Configuration of differential mode */
02855       if (sConfig->SingleDiff == ADC_DIFFERENTIAL_ENDED)
02856       {
02857         /* Set sampling time of the selected ADC channel */
02858         /* Note: ADC channel number masked with value "0x1F" to ensure shift value within 32 bits range */
02859         LL_ADC_SetChannelSamplingTime(hadc->Instance,
02860                                       (uint32_t)(__LL_ADC_DECIMAL_NB_TO_CHANNEL((__LL_ADC_CHANNEL_TO_DECIMAL_NB((uint32_t)sConfig->Channel) + 1UL) & 0x1FUL)),
02861                                       sConfig->SamplingTime);
02862       }
02863 
02864     }
02865 
02866     /* Management of internal measurement channels: Vbat/VrefInt/TempSensor.  */
02867     /* If internal channel selected, enable dedicated internal buffers and    */
02868     /* paths.                                                                 */
02869     /* Note: these internal measurement paths can be disabled using           */
02870     /* HAL_ADC_DeInit().                                                      */
02871 
02872     if (__LL_ADC_IS_CHANNEL_INTERNAL(sConfig->Channel))
02873     {
02874       tmp_config_internal_channel = LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
02875 
02876       /* If the requested internal measurement path has already been enabled, */
02877       /* bypass the configuration processing.                                 */
02878       if ((sConfig->Channel == ADC_CHANNEL_TEMPSENSOR)
02879           && ((tmp_config_internal_channel & LL_ADC_PATH_INTERNAL_TEMPSENSOR) == 0UL))
02880       {
02881         if (ADC_TEMPERATURE_SENSOR_INSTANCE(hadc))
02882         {
02883           LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance),
02884                                          LL_ADC_PATH_INTERNAL_TEMPSENSOR | tmp_config_internal_channel);
02885 
02886           /* Delay for temperature sensor stabilization time */
02887           /* Wait loop initialization and execution */
02888           /* Note: Variable divided by 2 to compensate partially              */
02889           /*       CPU processing cycles, scaling in us split to not          */
02890           /*       exceed 32 bits register capacity and handle low frequency. */
02891           wait_loop_index = ((LL_ADC_DELAY_TEMPSENSOR_STAB_US / 10UL) * ((SystemCoreClock / (100000UL * 2UL)) + 1UL));
02892           while (wait_loop_index != 0UL)
02893           {
02894             wait_loop_index--;
02895           }
02896         }
02897       }
02898       else if ((sConfig->Channel == ADC_CHANNEL_VBAT) && ((tmp_config_internal_channel & LL_ADC_PATH_INTERNAL_VBAT) == 0UL))
02899       {
02900         if (ADC_BATTERY_VOLTAGE_INSTANCE(hadc))
02901         {
02902           LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance),
02903                                          LL_ADC_PATH_INTERNAL_VBAT | tmp_config_internal_channel);
02904         }
02905       }
02906       else if ((sConfig->Channel == ADC_CHANNEL_VREFINT)
02907                && ((tmp_config_internal_channel & LL_ADC_PATH_INTERNAL_VREFINT) == 0UL))
02908       {
02909         if (ADC_VREFINT_INSTANCE(hadc))
02910         {
02911           LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance),
02912                                          LL_ADC_PATH_INTERNAL_VREFINT | tmp_config_internal_channel);
02913         }
02914       }
02915       else
02916       {
02917         /* nothing to do */
02918       }
02919     }
02920   }
02921 
02922   /* If a conversion is on going on regular group, no update on regular       */
02923   /* channel could be done on neither of the channel configuration structure  */
02924   /* parameters.                                                              */
02925   else
02926   {
02927     /* Update ADC state machine to error */
02928     SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
02929 
02930     tmp_hal_status = HAL_ERROR;
02931   }
02932 
02933   /* Process unlocked */
02934   __HAL_UNLOCK(hadc);
02935 
02936   /* Return function status */
02937   return tmp_hal_status;
02938 }
02939 
02940 /**
02941   * @brief  Configure the analog watchdog.
02942   * @note   Possibility to update parameters on the fly:
02943   *         This function initializes the selected analog watchdog, successive
02944   *         calls to this function can be used to reconfigure some parameters
02945   *         of structure "ADC_AnalogWDGConfTypeDef" on the fly, without resetting
02946   *         the ADC.
02947   *         The setting of these parameters is conditioned to ADC state.
02948   *         For parameters constraints, see comments of structure
02949   *         "ADC_AnalogWDGConfTypeDef".
02950   * @note   On this STM32 series, analog watchdog thresholds cannot be modified
02951   *         while ADC conversion is on going.
02952   * @param hadc ADC handle
02953   * @param AnalogWDGConfig Structure of ADC analog watchdog configuration
02954   * @retval HAL status
02955   */
02956 HAL_StatusTypeDef HAL_ADC_AnalogWDGConfig(ADC_HandleTypeDef *hadc, ADC_AnalogWDGConfTypeDef *AnalogWDGConfig)
02957 {
02958   HAL_StatusTypeDef tmp_hal_status = HAL_OK;
02959   uint32_t tmpAWDHighThresholdShifted;
02960   uint32_t tmpAWDLowThresholdShifted;
02961   uint32_t tmp_adc_is_conversion_on_going_regular;
02962   uint32_t tmp_adc_is_conversion_on_going_injected;
02963 
02964   /* Check the parameters */
02965   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
02966   assert_param(IS_ADC_ANALOG_WATCHDOG_NUMBER(AnalogWDGConfig->WatchdogNumber));
02967   assert_param(IS_ADC_ANALOG_WATCHDOG_MODE(AnalogWDGConfig->WatchdogMode));
02968   assert_param(IS_FUNCTIONAL_STATE(AnalogWDGConfig->ITMode));
02969 
02970   if ((AnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_REG)     ||
02971       (AnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_INJEC)   ||
02972       (AnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_REGINJEC))
02973   {
02974     assert_param(IS_ADC_CHANNEL(hadc, AnalogWDGConfig->Channel));
02975   }
02976 
02977   /* Verify thresholds range */
02978   if (hadc->Init.OversamplingMode == ENABLE)
02979   {
02980     /* Case of oversampling enabled: depending on ratio and shift configuration,
02981        analog watchdog thresholds can be higher than ADC resolution.
02982        Verify if thresholds are within maximum thresholds range. */
02983     assert_param(IS_ADC_RANGE(ADC_RESOLUTION_12B, AnalogWDGConfig->HighThreshold));
02984     assert_param(IS_ADC_RANGE(ADC_RESOLUTION_12B, AnalogWDGConfig->LowThreshold));
02985   }
02986   else
02987   {
02988     /* Verify if thresholds are within the selected ADC resolution */
02989     assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), AnalogWDGConfig->HighThreshold));
02990     assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), AnalogWDGConfig->LowThreshold));
02991   }
02992 
02993   /* Process locked */
02994   __HAL_LOCK(hadc);
02995 
02996   /* Parameters update conditioned to ADC state:                              */
02997   /* Parameters that can be updated when ADC is disabled or enabled without   */
02998   /* conversion on going on ADC groups regular and injected:                  */
02999   /*  - Analog watchdog channels                                              */
03000   /*  - Analog watchdog thresholds                                            */
03001   tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
03002   tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
03003   if ((tmp_adc_is_conversion_on_going_regular == 0UL)
03004       && (tmp_adc_is_conversion_on_going_injected == 0UL)
03005      )
03006   {
03007     /* Analog watchdog configuration */
03008     if (AnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_1)
03009     {
03010       /* Configuration of analog watchdog:                                    */
03011       /*  - Set the analog watchdog enable mode: one or overall group of      */
03012       /*    channels, on groups regular and-or injected.                      */
03013       switch (AnalogWDGConfig->WatchdogMode)
03014       {
03015         case ADC_ANALOGWATCHDOG_SINGLE_REG:
03016           LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, __LL_ADC_ANALOGWD_CHANNEL_GROUP(AnalogWDGConfig->Channel,
03017                                           LL_ADC_GROUP_REGULAR));
03018           break;
03019 
03020         case ADC_ANALOGWATCHDOG_SINGLE_INJEC:
03021           LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, __LL_ADC_ANALOGWD_CHANNEL_GROUP(AnalogWDGConfig->Channel,
03022                                           LL_ADC_GROUP_INJECTED));
03023           break;
03024 
03025         case ADC_ANALOGWATCHDOG_SINGLE_REGINJEC:
03026           LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, __LL_ADC_ANALOGWD_CHANNEL_GROUP(AnalogWDGConfig->Channel,
03027                                           LL_ADC_GROUP_REGULAR_INJECTED));
03028           break;
03029 
03030         case ADC_ANALOGWATCHDOG_ALL_REG:
03031           LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, LL_ADC_AWD_ALL_CHANNELS_REG);
03032           break;
03033 
03034         case ADC_ANALOGWATCHDOG_ALL_INJEC:
03035           LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, LL_ADC_AWD_ALL_CHANNELS_INJ);
03036           break;
03037 
03038         case ADC_ANALOGWATCHDOG_ALL_REGINJEC:
03039           LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, LL_ADC_AWD_ALL_CHANNELS_REG_INJ);
03040           break;
03041 
03042         default: /* ADC_ANALOGWATCHDOG_NONE */
03043           LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, LL_ADC_AWD_DISABLE);
03044           break;
03045       }
03046 
03047       /* Shift the offset in function of the selected ADC resolution:         */
03048       /* Thresholds have to be left-aligned on bit 11, the LSB (right bits)   */
03049       /* are set to 0                                                         */
03050       tmpAWDHighThresholdShifted = ADC_AWD1THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->HighThreshold);
03051       tmpAWDLowThresholdShifted  = ADC_AWD1THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->LowThreshold);
03052 
03053       /* Set ADC analog watchdog thresholds value of both thresholds high and low */
03054       LL_ADC_ConfigAnalogWDThresholds(hadc->Instance, AnalogWDGConfig->WatchdogNumber, tmpAWDHighThresholdShifted,
03055                                       tmpAWDLowThresholdShifted);
03056 
03057       /* Update state, clear previous result related to AWD1 */
03058       CLEAR_BIT(hadc->State, HAL_ADC_STATE_AWD1);
03059 
03060       /* Clear flag ADC analog watchdog */
03061       /* Note: Flag cleared Clear the ADC Analog watchdog flag to be ready  */
03062       /* to use for HAL_ADC_IRQHandler() or HAL_ADC_PollForEvent()          */
03063       /* (in case left enabled by previous ADC operations).                 */
03064       LL_ADC_ClearFlag_AWD1(hadc->Instance);
03065 
03066       /* Configure ADC analog watchdog interrupt */
03067       if (AnalogWDGConfig->ITMode == ENABLE)
03068       {
03069         LL_ADC_EnableIT_AWD1(hadc->Instance);
03070       }
03071       else
03072       {
03073         LL_ADC_DisableIT_AWD1(hadc->Instance);
03074       }
03075     }
03076     /* Case of ADC_ANALOGWATCHDOG_2 or ADC_ANALOGWATCHDOG_3 */
03077     else
03078     {
03079       switch (AnalogWDGConfig->WatchdogMode)
03080       {
03081         case ADC_ANALOGWATCHDOG_SINGLE_REG:
03082         case ADC_ANALOGWATCHDOG_SINGLE_INJEC:
03083         case ADC_ANALOGWATCHDOG_SINGLE_REGINJEC:
03084           /* Update AWD by bitfield to keep the possibility to monitor        */
03085           /* several channels by successive calls of this function.           */
03086           if (AnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_2)
03087           {
03088             SET_BIT(hadc->Instance->AWD2CR, (1UL << (__LL_ADC_CHANNEL_TO_DECIMAL_NB(AnalogWDGConfig->Channel) & 0x1FUL)));
03089           }
03090           else
03091           {
03092             SET_BIT(hadc->Instance->AWD3CR, (1UL << (__LL_ADC_CHANNEL_TO_DECIMAL_NB(AnalogWDGConfig->Channel) & 0x1FUL)));
03093           }
03094           break;
03095 
03096         case ADC_ANALOGWATCHDOG_ALL_REG:
03097         case ADC_ANALOGWATCHDOG_ALL_INJEC:
03098         case ADC_ANALOGWATCHDOG_ALL_REGINJEC:
03099           LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, AnalogWDGConfig->WatchdogNumber, LL_ADC_AWD_ALL_CHANNELS_REG_INJ);
03100           break;
03101 
03102         default: /* ADC_ANALOGWATCHDOG_NONE */
03103           LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, AnalogWDGConfig->WatchdogNumber, LL_ADC_AWD_DISABLE);
03104           break;
03105       }
03106 
03107       /* Shift the thresholds in function of the selected ADC resolution      */
03108       /* have to be left-aligned on bit 7, the LSB (right bits) are set to 0  */
03109       tmpAWDHighThresholdShifted = ADC_AWD23THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->HighThreshold);
03110       tmpAWDLowThresholdShifted  = ADC_AWD23THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->LowThreshold);
03111 
03112       /* Set ADC analog watchdog thresholds value of both thresholds high and low */
03113       LL_ADC_ConfigAnalogWDThresholds(hadc->Instance, AnalogWDGConfig->WatchdogNumber, tmpAWDHighThresholdShifted,
03114                                       tmpAWDLowThresholdShifted);
03115 
03116       if (AnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_2)
03117       {
03118         /* Update state, clear previous result related to AWD2 */
03119         CLEAR_BIT(hadc->State, HAL_ADC_STATE_AWD2);
03120 
03121         /* Clear flag ADC analog watchdog */
03122         /* Note: Flag cleared Clear the ADC Analog watchdog flag to be ready  */
03123         /* to use for HAL_ADC_IRQHandler() or HAL_ADC_PollForEvent()          */
03124         /* (in case left enabled by previous ADC operations).                 */
03125         LL_ADC_ClearFlag_AWD2(hadc->Instance);
03126 
03127         /* Configure ADC analog watchdog interrupt */
03128         if (AnalogWDGConfig->ITMode == ENABLE)
03129         {
03130           LL_ADC_EnableIT_AWD2(hadc->Instance);
03131         }
03132         else
03133         {
03134           LL_ADC_DisableIT_AWD2(hadc->Instance);
03135         }
03136       }
03137       /* (AnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_3) */
03138       else
03139       {
03140         /* Update state, clear previous result related to AWD3 */
03141         CLEAR_BIT(hadc->State, HAL_ADC_STATE_AWD3);
03142 
03143         /* Clear flag ADC analog watchdog */
03144         /* Note: Flag cleared Clear the ADC Analog watchdog flag to be ready  */
03145         /* to use for HAL_ADC_IRQHandler() or HAL_ADC_PollForEvent()          */
03146         /* (in case left enabled by previous ADC operations).                 */
03147         LL_ADC_ClearFlag_AWD3(hadc->Instance);
03148 
03149         /* Configure ADC analog watchdog interrupt */
03150         if (AnalogWDGConfig->ITMode == ENABLE)
03151         {
03152           LL_ADC_EnableIT_AWD3(hadc->Instance);
03153         }
03154         else
03155         {
03156           LL_ADC_DisableIT_AWD3(hadc->Instance);
03157         }
03158       }
03159     }
03160 
03161   }
03162   /* If a conversion is on going on ADC group regular or injected, no update  */
03163   /* could be done on neither of the AWD configuration structure parameters.  */
03164   else
03165   {
03166     /* Update ADC state machine to error */
03167     SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
03168 
03169     tmp_hal_status = HAL_ERROR;
03170   }
03171   /* Process unlocked */
03172   __HAL_UNLOCK(hadc);
03173 
03174   /* Return function status */
03175   return tmp_hal_status;
03176 }
03177 
03178 
03179 /**
03180   * @}
03181   */
03182 
03183 /** @defgroup ADC_Exported_Functions_Group4 Peripheral State functions
03184   *  @brief    ADC Peripheral State functions
03185   *
03186 @verbatim
03187  ===============================================================================
03188             ##### Peripheral state and errors functions #####
03189  ===============================================================================
03190     [..]
03191     This subsection provides functions to get in run-time the status of the
03192     peripheral.
03193       (+) Check the ADC state
03194       (+) Check the ADC error code
03195 
03196 @endverbatim
03197   * @{
03198   */
03199 
03200 /**
03201   * @brief  Return the ADC handle state.
03202   * @note   ADC state machine is managed by bitfields, ADC status must be
03203   *         compared with states bits.
03204   *         For example:
03205   *           " if ((HAL_ADC_GetState(hadc1) & HAL_ADC_STATE_REG_BUSY) != 0UL) "
03206   *           " if ((HAL_ADC_GetState(hadc1) & HAL_ADC_STATE_AWD1) != 0UL) "
03207   * @param hadc ADC handle
03208   * @retval ADC handle state (bitfield on 32 bits)
03209   */
03210 uint32_t HAL_ADC_GetState(ADC_HandleTypeDef *hadc)
03211 {
03212   /* Check the parameters */
03213   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
03214 
03215   /* Return ADC handle state */
03216   return hadc->State;
03217 }
03218 
03219 /**
03220   * @brief  Return the ADC error code.
03221   * @param hadc ADC handle
03222   * @retval ADC error code (bitfield on 32 bits)
03223   */
03224 uint32_t HAL_ADC_GetError(ADC_HandleTypeDef *hadc)
03225 {
03226   /* Check the parameters */
03227   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
03228 
03229   return hadc->ErrorCode;
03230 }
03231 
03232 /**
03233   * @}
03234   */
03235 
03236 /**
03237   * @}
03238   */
03239 
03240 /** @defgroup ADC_Private_Functions ADC Private Functions
03241   * @{
03242   */
03243 
03244 /**
03245   * @brief  Stop ADC conversion.
03246   * @param hadc ADC handle
03247   * @param ConversionGroup ADC group regular and/or injected.
03248   *          This parameter can be one of the following values:
03249   *            @arg @ref ADC_REGULAR_GROUP           ADC regular conversion type.
03250   *            @arg @ref ADC_INJECTED_GROUP          ADC injected conversion type.
03251   *            @arg @ref ADC_REGULAR_INJECTED_GROUP  ADC regular and injected conversion type.
03252   * @retval HAL status.
03253   */
03254 HAL_StatusTypeDef ADC_ConversionStop(ADC_HandleTypeDef *hadc, uint32_t ConversionGroup)
03255 {
03256   uint32_t tickstart;
03257   uint32_t Conversion_Timeout_CPU_cycles = 0UL;
03258   uint32_t conversion_group_reassigned = ConversionGroup;
03259   uint32_t tmp_ADC_CR_ADSTART_JADSTART;
03260   uint32_t tmp_adc_is_conversion_on_going_regular;
03261   uint32_t tmp_adc_is_conversion_on_going_injected;
03262 
03263   /* Check the parameters */
03264   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
03265   assert_param(IS_ADC_CONVERSION_GROUP(ConversionGroup));
03266 
03267   /* Verification if ADC is not already stopped (on regular and injected      */
03268   /* groups) to bypass this function if not needed.                           */
03269   tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
03270   tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
03271   if ((tmp_adc_is_conversion_on_going_regular != 0UL)
03272       || (tmp_adc_is_conversion_on_going_injected != 0UL)
03273      )
03274   {
03275     /* Particular case of continuous auto-injection mode combined with        */
03276     /* auto-delay mode.                                                       */
03277     /* In auto-injection mode, regular group stop ADC_CR_ADSTP is used (not   */
03278     /* injected group stop ADC_CR_JADSTP).                                    */
03279     /* Procedure to be followed: Wait until JEOS=1, clear JEOS, set ADSTP=1   */
03280     /* (see reference manual).                                                */
03281     if (((hadc->Instance->CFGR & ADC_CFGR_JAUTO) != 0UL)
03282         && (hadc->Init.ContinuousConvMode == ENABLE)
03283         && (hadc->Init.LowPowerAutoWait == ENABLE)
03284        )
03285     {
03286       /* Use stop of regular group */
03287       conversion_group_reassigned = ADC_REGULAR_GROUP;
03288 
03289       /* Wait until JEOS=1 (maximum Timeout: 4 injected conversions) */
03290       while (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_JEOS) == 0UL)
03291       {
03292         if (Conversion_Timeout_CPU_cycles >= (ADC_CONVERSION_TIME_MAX_CPU_CYCLES * 4UL))
03293         {
03294           /* Update ADC state machine to error */
03295           SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
03296 
03297           /* Set ADC error code to ADC peripheral internal error */
03298           SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
03299 
03300           return HAL_ERROR;
03301         }
03302         Conversion_Timeout_CPU_cycles ++;
03303       }
03304 
03305       /* Clear JEOS */
03306       __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOS);
03307     }
03308 
03309     /* Stop potential conversion on going on ADC group regular */
03310     if (conversion_group_reassigned != ADC_INJECTED_GROUP)
03311     {
03312       /* Software is allowed to set ADSTP only when ADSTART=1 and ADDIS=0 */
03313       if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) != 0UL)
03314       {
03315         if (LL_ADC_IsDisableOngoing(hadc->Instance) == 0UL)
03316         {
03317           /* Stop ADC group regular conversion */
03318           LL_ADC_REG_StopConversion(hadc->Instance);
03319         }
03320       }
03321     }
03322 
03323     /* Stop potential conversion on going on ADC group injected */
03324     if (conversion_group_reassigned != ADC_REGULAR_GROUP)
03325     {
03326       /* Software is allowed to set JADSTP only when JADSTART=1 and ADDIS=0 */
03327       if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) != 0UL)
03328       {
03329         if (LL_ADC_IsDisableOngoing(hadc->Instance) == 0UL)
03330         {
03331           /* Stop ADC group injected conversion */
03332           LL_ADC_INJ_StopConversion(hadc->Instance);
03333         }
03334       }
03335     }
03336 
03337     /* Selection of start and stop bits with respect to the regular or injected group */
03338     switch (conversion_group_reassigned)
03339     {
03340       case ADC_REGULAR_INJECTED_GROUP:
03341         tmp_ADC_CR_ADSTART_JADSTART = (ADC_CR_ADSTART | ADC_CR_JADSTART);
03342         break;
03343       case ADC_INJECTED_GROUP:
03344         tmp_ADC_CR_ADSTART_JADSTART = ADC_CR_JADSTART;
03345         break;
03346       /* Case ADC_REGULAR_GROUP only*/
03347       default:
03348         tmp_ADC_CR_ADSTART_JADSTART = ADC_CR_ADSTART;
03349         break;
03350     }
03351 
03352     /* Wait for conversion effectively stopped */
03353     tickstart = HAL_GetTick();
03354 
03355     while ((hadc->Instance->CR & tmp_ADC_CR_ADSTART_JADSTART) != 0UL)
03356     {
03357       if ((HAL_GetTick() - tickstart) > ADC_STOP_CONVERSION_TIMEOUT)
03358       {
03359         /* New check to avoid false timeout detection in case of preemption */
03360         if ((hadc->Instance->CR & tmp_ADC_CR_ADSTART_JADSTART) != 0UL)
03361         {
03362           /* Update ADC state machine to error */
03363           SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
03364 
03365           /* Set ADC error code to ADC peripheral internal error */
03366           SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
03367 
03368           return HAL_ERROR;
03369         }
03370       }
03371     }
03372 
03373   }
03374 
03375   /* Return HAL status */
03376   return HAL_OK;
03377 }
03378 
03379 /**
03380   * @brief  Enable the selected ADC.
03381   * @note   Prerequisite condition to use this function: ADC must be disabled
03382   *         and voltage regulator must be enabled (done into HAL_ADC_Init()).
03383   * @param hadc ADC handle
03384   * @retval HAL status.
03385   */
03386 HAL_StatusTypeDef ADC_Enable(ADC_HandleTypeDef *hadc)
03387 {
03388   uint32_t tickstart;
03389 
03390   /* ADC enable and wait for ADC ready (in case of ADC is disabled or         */
03391   /* enabling phase not yet completed: flag ADC ready not yet set).           */
03392   /* Timeout implemented to not be stuck if ADC cannot be enabled (possible   */
03393   /* causes: ADC clock not running, ...).                                     */
03394   if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
03395   {
03396     /* Check if conditions to enable the ADC are fulfilled */
03397     if ((hadc->Instance->CR & (ADC_CR_ADCAL | ADC_CR_JADSTP | ADC_CR_ADSTP | ADC_CR_JADSTART | ADC_CR_ADSTART
03398                                | ADC_CR_ADDIS | ADC_CR_ADEN)) != 0UL)
03399     {
03400       /* Update ADC state machine to error */
03401       SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
03402 
03403       /* Set ADC error code to ADC peripheral internal error */
03404       SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
03405 
03406       return HAL_ERROR;
03407     }
03408 
03409     /* Enable the ADC peripheral */
03410     LL_ADC_Enable(hadc->Instance);
03411 
03412     /* Wait for ADC effectively enabled */
03413     tickstart = HAL_GetTick();
03414 
03415     while (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_RDY) == 0UL)
03416     {
03417       /*  If ADEN bit is set less than 4 ADC clock cycles after the ADCAL bit
03418           has been cleared (after a calibration), ADEN bit is reset by the
03419           calibration logic.
03420           The workaround is to continue setting ADEN until ADRDY is becomes 1.
03421           Additionally, ADC_ENABLE_TIMEOUT is defined to encompass this
03422           4 ADC clock cycle duration */
03423       /* Note: Test of ADC enabled required due to hardware constraint to     */
03424       /*       not enable ADC if already enabled.                             */
03425       if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
03426       {
03427         LL_ADC_Enable(hadc->Instance);
03428       }
03429 
03430       if ((HAL_GetTick() - tickstart) > ADC_ENABLE_TIMEOUT)
03431       {
03432         /* New check to avoid false timeout detection in case of preemption */
03433         if (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_RDY) == 0UL)
03434         {
03435           /* Update ADC state machine to error */
03436           SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
03437 
03438           /* Set ADC error code to ADC peripheral internal error */
03439           SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
03440 
03441           return HAL_ERROR;
03442         }
03443       }
03444     }
03445   }
03446 
03447   /* Return HAL status */
03448   return HAL_OK;
03449 }
03450 
03451 /**
03452   * @brief  Disable the selected ADC.
03453   * @note   Prerequisite condition to use this function: ADC conversions must be
03454   *         stopped.
03455   * @param hadc ADC handle
03456   * @retval HAL status.
03457   */
03458 HAL_StatusTypeDef ADC_Disable(ADC_HandleTypeDef *hadc)
03459 {
03460   uint32_t tickstart;
03461   const uint32_t tmp_adc_is_disable_on_going = LL_ADC_IsDisableOngoing(hadc->Instance);
03462 
03463   /* Verification if ADC is not already disabled:                             */
03464   /* Note: forbidden to disable ADC (set bit ADC_CR_ADDIS) if ADC is already  */
03465   /*       disabled.                                                          */
03466   if ((LL_ADC_IsEnabled(hadc->Instance) != 0UL)
03467       && (tmp_adc_is_disable_on_going == 0UL)
03468      )
03469   {
03470     /* Check if conditions to disable the ADC are fulfilled */
03471     if ((hadc->Instance->CR & (ADC_CR_JADSTART | ADC_CR_ADSTART | ADC_CR_ADEN)) == ADC_CR_ADEN)
03472     {
03473       /* Disable the ADC peripheral */
03474       LL_ADC_Disable(hadc->Instance);
03475       __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOSMP | ADC_FLAG_RDY));
03476     }
03477     else
03478     {
03479       /* Update ADC state machine to error */
03480       SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
03481 
03482       /* Set ADC error code to ADC peripheral internal error */
03483       SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
03484 
03485       return HAL_ERROR;
03486     }
03487 
03488     /* Wait for ADC effectively disabled */
03489     /* Get tick count */
03490     tickstart = HAL_GetTick();
03491 
03492     while ((hadc->Instance->CR & ADC_CR_ADEN) != 0UL)
03493     {
03494       if ((HAL_GetTick() - tickstart) > ADC_DISABLE_TIMEOUT)
03495       {
03496         /* New check to avoid false timeout detection in case of preemption */
03497         if ((hadc->Instance->CR & ADC_CR_ADEN) != 0UL)
03498         {
03499           /* Update ADC state machine to error */
03500           SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
03501 
03502           /* Set ADC error code to ADC peripheral internal error */
03503           SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
03504 
03505           return HAL_ERROR;
03506         }
03507       }
03508     }
03509   }
03510 
03511   /* Return HAL status */
03512   return HAL_OK;
03513 }
03514 
03515 /**
03516   * @brief  DMA transfer complete callback.
03517   * @param hdma pointer to DMA handle.
03518   * @retval None
03519   */
03520 void ADC_DMAConvCplt(DMA_HandleTypeDef *hdma)
03521 {
03522   /* Retrieve ADC handle corresponding to current DMA handle */
03523   ADC_HandleTypeDef *hadc = (ADC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
03524 
03525   /* Update state machine on conversion status if not in error state */
03526   if ((hadc->State & (HAL_ADC_STATE_ERROR_INTERNAL | HAL_ADC_STATE_ERROR_DMA)) == 0UL)
03527   {
03528     /* Set ADC state */
03529     SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
03530 
03531     /* Determine whether any further conversion upcoming on group regular     */
03532     /* by external trigger, continuous mode or scan sequence on going         */
03533     /* to disable interruption.                                               */
03534     /* Is it the end of the regular sequence ? */
03535     if ((hadc->Instance->ISR & ADC_FLAG_EOS) != 0UL)
03536     {
03537       /* Are conversions software-triggered ? */
03538       if (LL_ADC_REG_IsTriggerSourceSWStart(hadc->Instance) != 0UL)
03539       {
03540         /* Is CONT bit set ? */
03541         if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_CONT) == 0UL)
03542         {
03543           /* CONT bit is not set, no more conversions expected */
03544           CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
03545           if ((hadc->State & HAL_ADC_STATE_INJ_BUSY) == 0UL)
03546           {
03547             SET_BIT(hadc->State, HAL_ADC_STATE_READY);
03548           }
03549         }
03550       }
03551     }
03552     else
03553     {
03554       /* DMA End of Transfer interrupt was triggered but conversions sequence
03555          is not over. If DMACFG is set to 0, conversions are stopped. */
03556       if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_DMACFG) == 0UL)
03557       {
03558         /* DMACFG bit is not set, conversions are stopped. */
03559         CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
03560         if ((hadc->State & HAL_ADC_STATE_INJ_BUSY) == 0UL)
03561         {
03562           SET_BIT(hadc->State, HAL_ADC_STATE_READY);
03563         }
03564       }
03565     }
03566 
03567     /* Conversion complete callback */
03568 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
03569     hadc->ConvCpltCallback(hadc);
03570 #else
03571     HAL_ADC_ConvCpltCallback(hadc);
03572 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
03573   }
03574   else /* DMA and-or internal error occurred */
03575   {
03576     if ((hadc->State & HAL_ADC_STATE_ERROR_INTERNAL) != 0UL)
03577     {
03578       /* Call HAL ADC Error Callback function */
03579 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
03580       hadc->ErrorCallback(hadc);
03581 #else
03582       HAL_ADC_ErrorCallback(hadc);
03583 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
03584     }
03585     else
03586     {
03587       /* Call ADC DMA error callback */
03588       hadc->DMA_Handle->XferErrorCallback(hdma);
03589     }
03590   }
03591 }
03592 
03593 /**
03594   * @brief  DMA half transfer complete callback.
03595   * @param hdma pointer to DMA handle.
03596   * @retval None
03597   */
03598 void ADC_DMAHalfConvCplt(DMA_HandleTypeDef *hdma)
03599 {
03600   /* Retrieve ADC handle corresponding to current DMA handle */
03601   ADC_HandleTypeDef *hadc = (ADC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
03602 
03603   /* Half conversion callback */
03604 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
03605   hadc->ConvHalfCpltCallback(hadc);
03606 #else
03607   HAL_ADC_ConvHalfCpltCallback(hadc);
03608 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
03609 }
03610 
03611 /**
03612   * @brief  DMA error callback.
03613   * @param hdma pointer to DMA handle.
03614   * @retval None
03615   */
03616 void ADC_DMAError(DMA_HandleTypeDef *hdma)
03617 {
03618   /* Retrieve ADC handle corresponding to current DMA handle */
03619   ADC_HandleTypeDef *hadc = (ADC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
03620 
03621   /* Set ADC state */
03622   SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
03623 
03624   /* Set ADC error code to DMA error */
03625   SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_DMA);
03626 
03627   /* Error callback */
03628 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
03629   hadc->ErrorCallback(hadc);
03630 #else
03631   HAL_ADC_ErrorCallback(hadc);
03632 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
03633 }
03634 
03635 /**
03636   * @}
03637   */
03638 
03639 #endif /* HAL_ADC_MODULE_ENABLED */
03640 /**
03641   * @}
03642   */
03643 
03644 /**
03645   * @}
03646   */