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