Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:06

0001 /**
0002   ******************************************************************************
0003   * @file    stm32h7xx_hal_adc.c
0004   * @author  MCD Application Team
0005   * @brief   This file provides firmware functions to manage the following
0006   *          functionalities of the Analog to Digital Converter (ADC)
0007   *          peripheral:
0008   *           + Peripheral Control functions
0009   *           + Peripheral State functions
0010   *          Other functions (extended functions) are available in file
0011   *          "stm32h7xx_hal_adc_ex.c".
0012   *
0013   ******************************************************************************
0014   * @attention
0015   *
0016   * Copyright (c) 2017 STMicroelectronics.
0017   * All rights reserved.
0018   *
0019   * This software is licensed under terms that can be found in the LICENSE file
0020   * in the root directory of this software component.
0021   * If no LICENSE file comes with this software, it is provided AS-IS.
0022   *
0023   ******************************************************************************
0024   @verbatim
0025   ==============================================================================
0026                      ##### ADC peripheral features #####
0027   ==============================================================================
0028   [..]
0029   (+) 16-bit, 14-bit, 12-bit, 10-bit or 8-bit configurable resolution.
0030        Note: On devices STM32H72xx and STM32H73xx, these resolution are applicable to instances ADC1 and ADC2. 
0031        ADC3 is featuring resolutions 12-bit, 10-bit, 8-bit, 6-bit.
0032 
0033   (+) Interrupt generation at the end of regular conversion and in case of
0034       analog watchdog or overrun events.
0035 
0036   (+) Single and continuous conversion modes.
0037 
0038   (+) Scan mode for conversion of several channels sequentially.
0039 
0040   (+) Data alignment with in-built data coherency.
0041 
0042   (+) Programmable sampling time (channel wise)
0043 
0044   (+) External trigger (timer or EXTI) with configurable polarity
0045 
0046   (+) DMA request generation for transfer of conversions data of regular group.
0047 
0048   (+) Configurable delay between conversions in Dual interleaved mode.
0049 
0050   (+) ADC channels selectable single/differential input.
0051 
0052   (+) ADC offset shared on 4 offset instances.
0053   (+) ADC calibration
0054 
0055   (+) ADC conversion of regular group.
0056 
0057   (+) ADC supply requirements: 1.62 V to 3.6 V.
0058 
0059   (+) ADC input range: from Vref- (connected to Vssa) to Vref+ (connected to
0060       Vdda or to an external voltage reference).
0061 
0062 
0063                      ##### How to use this driver #####
0064   ==============================================================================
0065     [..]
0066 
0067      *** Configuration of top level parameters related to ADC ***
0068      ============================================================
0069      [..]
0070 
0071     (#) Enable the ADC interface
0072         (++) As prerequisite, ADC clock must be configured at RCC top level.
0073 
0074         (++) Two clock settings are mandatory:
0075              (+++) ADC clock (core clock, also possibly conversion clock).
0076 
0077              (+++) ADC clock (conversions clock).
0078                    Two possible clock sources: synchronous clock derived from AHB clock
0079                    or asynchronous clock derived from system clock, the PLL2 or the PLL3 running up to 400MHz.
0080 
0081              (+++) Example:
0082                    Into HAL_ADC_MspInit() (recommended code location) or with
0083                    other device clock parameters configuration:
0084                (+++) __HAL_RCC_ADC_CLK_ENABLE();                  (mandatory)
0085 
0086                RCC_ADCCLKSOURCE_PLL2 enable:                   (optional: if asynchronous clock selected)
0087                (+++) RCC_PeriphClkInitTypeDef   RCC_PeriphClkInit;
0088                (+++) PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
0089                (+++) PeriphClkInit.AdcClockSelection    = RCC_ADCCLKSOURCE_PLL2;
0090                (+++) HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
0091 
0092         (++) ADC clock source and clock prescaler are configured at ADC level with
0093              parameter "ClockPrescaler" using function HAL_ADC_Init().
0094 
0095     (#) ADC pins configuration
0096          (++) Enable the clock for the ADC GPIOs
0097               using macro __HAL_RCC_GPIOx_CLK_ENABLE()
0098          (++) Configure these ADC pins in analog mode
0099               using function HAL_GPIO_Init()
0100 
0101     (#) Optionally, in case of usage of ADC with interruptions:
0102          (++) Configure the NVIC for ADC
0103               using function HAL_NVIC_EnableIRQ(ADCx_IRQn)
0104          (++) Insert the ADC interruption handler function HAL_ADC_IRQHandler()
0105               into the function of corresponding ADC interruption vector
0106               ADCx_IRQHandler().
0107 
0108     (#) Optionally, in case of usage of DMA:
0109          (++) Configure the DMA (DMA channel, mode normal or circular, ...)
0110               using function HAL_DMA_Init().
0111          (++) Configure the NVIC for DMA
0112               using function HAL_NVIC_EnableIRQ(DMAx_Channelx_IRQn)
0113          (++) Insert the ADC interruption handler function HAL_ADC_IRQHandler()
0114               into the function of corresponding DMA interruption vector
0115               DMAx_Channelx_IRQHandler().
0116 
0117      *** Configuration of ADC, group regular, channels parameters ***
0118      ================================================================
0119      [..]
0120 
0121     (#) Configure the ADC parameters (resolution, data alignment, ...)
0122         and regular group parameters (conversion trigger, sequencer, ...)
0123         using function HAL_ADC_Init().
0124 
0125     (#) Configure the channels for regular group parameters (channel number,
0126         channel rank into sequencer, ..., into regular group)
0127         using function HAL_ADC_ConfigChannel().
0128 
0129     (#) Optionally, configure the analog watchdog parameters (channels
0130         monitored, thresholds, ...)
0131         using function HAL_ADC_AnalogWDGConfig().
0132 
0133      *** Execution of ADC conversions ***
0134      ====================================
0135      [..]
0136 
0137     (#) Optionally, perform an automatic ADC calibration to improve the
0138         conversion accuracy
0139         using function HAL_ADCEx_Calibration_Start().
0140 
0141     (#) ADC driver can be used among three modes: polling, interruption,
0142         transfer by DMA.
0143 
0144         (++) ADC conversion by polling:
0145           (+++) Activate the ADC peripheral and start conversions
0146                 using function HAL_ADC_Start()
0147           (+++) Wait for ADC conversion completion
0148                 using function HAL_ADC_PollForConversion()
0149           (+++) Retrieve conversion results
0150                 using function HAL_ADC_GetValue()
0151           (+++) Stop conversion and disable the ADC peripheral
0152                 using function HAL_ADC_Stop()
0153 
0154         (++) ADC conversion by interruption:
0155           (+++) Activate the ADC peripheral and start conversions
0156                 using function HAL_ADC_Start_IT()
0157           (+++) Wait for ADC conversion completion by call of function
0158                 HAL_ADC_ConvCpltCallback()
0159                 (this function must be implemented in user program)
0160           (+++) Retrieve conversion results
0161                 using function HAL_ADC_GetValue()
0162           (+++) Stop conversion and disable the ADC peripheral
0163                 using function HAL_ADC_Stop_IT()
0164 
0165         (++) ADC conversion with transfer by DMA:
0166           (+++) Activate the ADC peripheral and start conversions
0167                 using function HAL_ADC_Start_DMA()
0168           (+++) Wait for ADC conversion completion by call of function
0169                 HAL_ADC_ConvCpltCallback() or HAL_ADC_ConvHalfCpltCallback()
0170                 (these functions must be implemented in user program)
0171           (+++) Conversion results are automatically transferred by DMA into
0172                 destination variable address.
0173           (+++) Stop conversion and disable the ADC peripheral
0174                 using function HAL_ADC_Stop_DMA()
0175 
0176      [..]
0177 
0178     (@) Callback functions must be implemented in user program:
0179       (+@) HAL_ADC_ErrorCallback()
0180       (+@) HAL_ADC_LevelOutOfWindowCallback() (callback of analog watchdog)
0181       (+@) HAL_ADC_ConvCpltCallback()
0182       (+@) HAL_ADC_ConvHalfCpltCallback
0183 
0184      *** Deinitialization of ADC ***
0185      ============================================================
0186      [..]
0187 
0188     (#) Disable the ADC interface
0189       (++) ADC clock can be hard reset and disabled at RCC top level.
0190         (++) Hard reset of ADC peripherals
0191              using macro __HAL_RCC_ADCx_FORCE_RESET(), __HAL_RCC_ADCx_RELEASE_RESET().
0192         (++) ADC clock disable
0193              using the equivalent macro/functions as configuration step.
0194              (+++) Example:
0195                    Into HAL_ADC_MspDeInit() (recommended code location) or with
0196                    other device clock parameters configuration:
0197                (+++) __HAL_RCC_ADC_CLK_DISABLE();                  (if not used anymore)
0198                RCC_ADCCLKSOURCE_CLKP restore:                      (optional)
0199                (+++) RCC_PeriphClkInitTypeDef   RCC_PeriphClkInit;
0200                (+++) PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
0201                (+++) PeriphClkInit.AdcClockSelection    = RCC_ADCCLKSOURCE_CLKP;
0202                (+++) HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
0203 
0204     (#) ADC pins configuration
0205          (++) Disable the clock for the ADC GPIOs
0206               using macro __HAL_RCC_GPIOx_CLK_DISABLE()
0207 
0208     (#) Optionally, in case of usage of ADC with interruptions:
0209          (++) Disable the NVIC for ADC
0210               using function HAL_NVIC_EnableIRQ(ADCx_IRQn)
0211 
0212     (#) Optionally, in case of usage of DMA:
0213          (++) Deinitialize the DMA
0214               using function HAL_DMA_Init().
0215          (++) Disable the NVIC for DMA
0216               using function HAL_NVIC_EnableIRQ(DMAx_Channelx_IRQn)
0217 
0218     [..]
0219 
0220     *** Callback registration ***
0221     =============================================
0222     [..]
0223 
0224      The compilation flag USE_HAL_ADC_REGISTER_CALLBACKS, when set to 1,
0225      allows the user to configure dynamically the driver callbacks.
0226      Use Functions HAL_ADC_RegisterCallback()
0227      to register an interrupt callback.
0228     [..]
0229 
0230      Function HAL_ADC_RegisterCallback() allows to register following callbacks:
0231        (+) ConvCpltCallback               : ADC conversion complete callback
0232        (+) ConvHalfCpltCallback           : ADC conversion DMA half-transfer callback
0233        (+) LevelOutOfWindowCallback       : ADC analog watchdog 1 callback
0234        (+) ErrorCallback                  : ADC error callback
0235        (+) InjectedConvCpltCallback       : ADC group injected conversion complete callback
0236        (+) InjectedQueueOverflowCallback  : ADC group injected context queue overflow callback
0237        (+) LevelOutOfWindow2Callback      : ADC analog watchdog 2 callback
0238        (+) LevelOutOfWindow3Callback      : ADC analog watchdog 3 callback
0239        (+) EndOfSamplingCallback          : ADC end of sampling callback
0240        (+) MspInitCallback                : ADC Msp Init callback
0241        (+) MspDeInitCallback              : ADC Msp DeInit callback
0242      This function takes as parameters the HAL peripheral handle, the Callback ID
0243      and a pointer to the user callback function.
0244     [..]
0245 
0246      Use function HAL_ADC_UnRegisterCallback to reset a callback to the default
0247      weak function.
0248     [..]
0249 
0250      HAL_ADC_UnRegisterCallback takes as parameters the HAL peripheral handle,
0251      and the Callback ID.
0252      This function allows to reset following callbacks:
0253        (+) ConvCpltCallback               : ADC conversion complete callback
0254        (+) ConvHalfCpltCallback           : ADC conversion DMA half-transfer callback
0255        (+) LevelOutOfWindowCallback       : ADC analog watchdog 1 callback
0256        (+) ErrorCallback                  : ADC error callback
0257        (+) InjectedConvCpltCallback       : ADC group injected conversion complete callback
0258        (+) InjectedQueueOverflowCallback  : ADC group injected context queue overflow callback
0259        (+) LevelOutOfWindow2Callback      : ADC analog watchdog 2 callback
0260        (+) LevelOutOfWindow3Callback      : ADC analog watchdog 3 callback
0261        (+) EndOfSamplingCallback          : ADC end of sampling callback
0262        (+) MspInitCallback                : ADC Msp Init callback
0263        (+) MspDeInitCallback              : ADC Msp DeInit callback
0264      [..]
0265 
0266      By default, after the HAL_ADC_Init() and when the state is HAL_ADC_STATE_RESET
0267      all callbacks are set to the corresponding weak functions:
0268      examples HAL_ADC_ConvCpltCallback(), HAL_ADC_ErrorCallback().
0269      Exception done for MspInit and MspDeInit functions that are
0270      reset to the legacy weak functions in the HAL_ADC_Init()/ HAL_ADC_DeInit() only when
0271      these callbacks are null (not registered beforehand).
0272     [..]
0273 
0274      If MspInit or MspDeInit are not null, the HAL_ADC_Init()/ HAL_ADC_DeInit()
0275      keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
0276      [..]
0277 
0278      Callbacks can be registered/unregistered in HAL_ADC_STATE_READY state only.
0279      Exception done MspInit/MspDeInit functions that can be registered/unregistered
0280      in HAL_ADC_STATE_READY or HAL_ADC_STATE_RESET state,
0281      thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
0282     [..]
0283 
0284      Then, the user first registers the MspInit/MspDeInit user callbacks
0285      using HAL_ADC_RegisterCallback() before calling HAL_ADC_DeInit()
0286      or HAL_ADC_Init() function.
0287      [..]
0288 
0289      When the compilation flag USE_HAL_ADC_REGISTER_CALLBACKS is set to 0 or
0290      not defined, the callback registration feature is not available and all callbacks
0291      are set to the corresponding weak functions.
0292 
0293   @endverbatim
0294   ******************************************************************************
0295   */
0296 
0297 /* Includes ------------------------------------------------------------------*/
0298 #include "stm32h7xx_hal.h"
0299 
0300 /** @addtogroup STM32H7xx_HAL_Driver
0301   * @{
0302   */
0303 
0304 /** @defgroup ADC ADC
0305   * @ingroup RTEMSBSPsARMSTM32H7
0306   * @brief ADC HAL module driver
0307   * @{
0308   */
0309 
0310 #ifdef HAL_ADC_MODULE_ENABLED
0311 
0312 /* Private typedef -----------------------------------------------------------*/
0313 /* Private define ------------------------------------------------------------*/
0314 
0315 /** @defgroup ADC_Private_Constants ADC Private Constants
0316   * @ingroup RTEMSBSPsARMSTM32H7
0317   * @{
0318   */
0319 #define ADC_CFGR_FIELDS_1  ((uint32_t)(ADC_CFGR_RES    |\
0320                                        ADC_CFGR_CONT   | ADC_CFGR_OVRMOD  |\
0321                                        ADC_CFGR_DISCEN | ADC_CFGR_DISCNUM |\
0322                                        ADC_CFGR_EXTEN  | ADC_CFGR_EXTSEL)) /*!< ADC_CFGR fields of parameters that can be updated
0323                                                                                   when no regular conversion is on-going */
0324 
0325 #if defined(ADC_VER_V5_V90)
0326 #define ADC3_CFGR_FIELDS_1  ((ADC3_CFGR_RES    | ADC3_CFGR_ALIGN   |\
0327                              ADC_CFGR_CONT   | ADC_CFGR_OVRMOD  |\
0328                              ADC_CFGR_DISCEN | ADC_CFGR_DISCNUM |\
0329                              ADC_CFGR_EXTEN  | ADC_CFGR_EXTSEL))   /*!< ADC_CFGR fields of parameters that can be updated 
0330                                                                         when no regular conversion is on-going */
0331 #endif
0332 
0333 #define ADC_CFGR2_FIELDS  ((uint32_t)(ADC_CFGR2_ROVSE | ADC_CFGR2_OVSR  |\
0334                                        ADC_CFGR2_OVSS | ADC_CFGR2_TROVS |\
0335                                        ADC_CFGR2_ROVSM))                     /*!< ADC_CFGR2 fields of parameters that can be updated when no conversion
0336                                                                                  (neither regular nor injected) is on-going  */
0337 
0338 /* Timeout values for ADC operations (enable settling time,                   */
0339 /*   disable settling time, ...).                                             */
0340 /*   Values defined to be higher than worst cases: low clock frequency,       */
0341 /*   maximum prescalers.                                                      */
0342 #define ADC_ENABLE_TIMEOUT              (2UL)    /*!< ADC enable time-out value  */
0343 #define ADC_DISABLE_TIMEOUT             (2UL)    /*!< ADC disable time-out value */
0344 
0345 /* Timeout to wait for current conversion on going to be completed.           */
0346 /* Timeout fixed to worst case, for 1 channel.                                */
0347 /*   - maximum sampling time (830.5 adc_clk)                                  */
0348 /*   - ADC resolution (Tsar 16 bits= 16.5 adc_clk)                            */
0349 /*   - ADC clock with prescaler 256                                           */
0350 /*     823 * 256 = 210688 clock cycles max                                    */
0351 /* Unit: cycles of CPU clock.                                                 */
0352 #define ADC_CONVERSION_TIME_MAX_CPU_CYCLES (210688UL)  /*!< ADC conversion completion time-out value */
0353 
0354 /**
0355   * @}
0356   */
0357 
0358 /* Private macro -------------------------------------------------------------*/
0359 /* Private variables ---------------------------------------------------------*/
0360 /* Private function prototypes -----------------------------------------------*/
0361 /* Exported functions --------------------------------------------------------*/
0362 
0363 /** @defgroup ADC_Exported_Functions ADC Exported Functions
0364   * @ingroup RTEMSBSPsARMSTM32H7
0365   * @{
0366   */
0367 
0368 /** @defgroup ADC_Exported_Functions_Group1 Initialization and de-initialization functions
0369   * @ingroup RTEMSBSPsARMSTM32H7
0370   * @brief    ADC Initialization and Configuration functions
0371   *
0372 @verbatim
0373  ===============================================================================
0374               ##### Initialization and de-initialization functions #####
0375  ===============================================================================
0376     [..]  This section provides functions allowing to:
0377       (+) Initialize and configure the ADC.
0378       (+) De-initialize the ADC.
0379 @endverbatim
0380   * @{
0381   */
0382 
0383 /**
0384   * @brief  Initialize the ADC peripheral and regular group according to
0385   *         parameters specified in structure "ADC_InitTypeDef".
0386   * @note   As prerequisite, ADC clock must be configured at RCC top level
0387   *         (refer to description of RCC configuration for ADC
0388   *         in header of this file).
0389   * @note   Possibility to update parameters on the fly:
0390   *         This function initializes the ADC MSP (HAL_ADC_MspInit()) only when
0391   *         coming from ADC state reset. Following calls to this function can
0392   *         be used to reconfigure some parameters of ADC_InitTypeDef
0393   *         structure on the fly, without modifying MSP configuration. If ADC
0394   *         MSP has to be modified again, HAL_ADC_DeInit() must be called
0395   *         before HAL_ADC_Init().
0396   *         The setting of these parameters is conditioned to ADC state.
0397   *         For parameters constraints, see comments of structure
0398   *         "ADC_InitTypeDef".
0399   * @note   This function configures the ADC within 2 scopes: scope of entire
0400   *         ADC and scope of regular group. For parameters details, see comments
0401   *         of structure "ADC_InitTypeDef".
0402   * @note   Parameters related to common ADC registers (ADC clock mode) are set
0403   *         only if all ADCs are disabled.
0404   *         If this is not the case, these common parameters setting are
0405   *         bypassed without error reporting: it can be the intended behaviour in
0406   *         case of update of a parameter of ADC_InitTypeDef on the fly,
0407   *         without  disabling the other ADCs.
0408   * @param hadc ADC handle
0409   * @retval HAL status
0410   */
0411 HAL_StatusTypeDef HAL_ADC_Init(ADC_HandleTypeDef *hadc)
0412 {
0413   HAL_StatusTypeDef tmp_hal_status = HAL_OK;
0414   uint32_t tmpCFGR;
0415   uint32_t tmp_adc_reg_is_conversion_on_going;
0416   __IO uint32_t wait_loop_index = 0UL;
0417   uint32_t tmp_adc_is_conversion_on_going_regular;
0418   uint32_t tmp_adc_is_conversion_on_going_injected;
0419 
0420   /* Check ADC handle */
0421   if (hadc == NULL)
0422   {
0423     return HAL_ERROR;
0424   }
0425 
0426   /* Check the parameters */
0427   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
0428   assert_param(IS_ADC_CLOCKPRESCALER(hadc->Init.ClockPrescaler));
0429   assert_param(IS_ADC_RESOLUTION(hadc->Init.Resolution));
0430   assert_param(IS_ADC_SCAN_MODE(hadc->Init.ScanConvMode));
0431   assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
0432   assert_param(IS_ADC_EXTTRIG_EDGE(hadc->Init.ExternalTrigConvEdge));
0433   assert_param(IS_ADC_EXTTRIG(hadc->Init.ExternalTrigConv));
0434   assert_param(IS_ADC_CONVERSIONDATAMGT(hadc->Init.ConversionDataManagement));
0435   assert_param(IS_ADC_EOC_SELECTION(hadc->Init.EOCSelection));
0436   assert_param(IS_ADC_OVERRUN(hadc->Init.Overrun));
0437   assert_param(IS_FUNCTIONAL_STATE(hadc->Init.LowPowerAutoWait));
0438   assert_param(IS_FUNCTIONAL_STATE(hadc->Init.OversamplingMode));
0439 
0440   if (hadc->Init.ScanConvMode != ADC_SCAN_DISABLE)
0441   {
0442     assert_param(IS_ADC_REGULAR_NB_CONV(hadc->Init.NbrOfConversion));
0443     assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DiscontinuousConvMode));
0444 
0445     if (hadc->Init.DiscontinuousConvMode == ENABLE)
0446     {
0447       assert_param(IS_ADC_REGULAR_DISCONT_NUMBER(hadc->Init.NbrOfDiscConversion));
0448     }
0449   }
0450 
0451   /* DISCEN and CONT bits cannot be set at the same time */
0452   assert_param(!((hadc->Init.DiscontinuousConvMode == ENABLE) && (hadc->Init.ContinuousConvMode == ENABLE)));
0453 
0454   /* Actions performed only if ADC is coming from state reset:                */
0455   /* - Initialization of ADC MSP                                              */
0456   if (hadc->State == HAL_ADC_STATE_RESET)
0457   {
0458 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
0459     /* Init the ADC Callback settings */
0460     hadc->ConvCpltCallback              = HAL_ADC_ConvCpltCallback;                 /* Legacy weak callback */
0461     hadc->ConvHalfCpltCallback          = HAL_ADC_ConvHalfCpltCallback;             /* Legacy weak callback */
0462     hadc->LevelOutOfWindowCallback      = HAL_ADC_LevelOutOfWindowCallback;         /* Legacy weak callback */
0463     hadc->ErrorCallback                 = HAL_ADC_ErrorCallback;                    /* Legacy weak callback */
0464     hadc->InjectedConvCpltCallback      = HAL_ADCEx_InjectedConvCpltCallback;       /* Legacy weak callback */
0465     hadc->InjectedQueueOverflowCallback = HAL_ADCEx_InjectedQueueOverflowCallback;  /* Legacy weak callback */
0466     hadc->LevelOutOfWindow2Callback     = HAL_ADCEx_LevelOutOfWindow2Callback;      /* Legacy weak callback */
0467     hadc->LevelOutOfWindow3Callback     = HAL_ADCEx_LevelOutOfWindow3Callback;      /* Legacy weak callback */
0468     hadc->EndOfSamplingCallback         = HAL_ADCEx_EndOfSamplingCallback;          /* Legacy weak callback */
0469 
0470     if (hadc->MspInitCallback == NULL)
0471     {
0472       hadc->MspInitCallback = HAL_ADC_MspInit; /* Legacy weak MspInit  */
0473     }
0474 
0475     /* Init the low level hardware */
0476     hadc->MspInitCallback(hadc);
0477 #else
0478     /* Init the low level hardware */
0479     HAL_ADC_MspInit(hadc);
0480 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
0481 
0482     /* Set ADC error code to none */
0483     ADC_CLEAR_ERRORCODE(hadc);
0484 
0485     /* Initialize Lock */
0486     hadc->Lock = HAL_UNLOCKED;
0487   }
0488 
0489   /* - Exit from deep-power-down mode and ADC voltage regulator enable        */
0490   if (LL_ADC_IsDeepPowerDownEnabled(hadc->Instance) != 0UL)
0491   {
0492     /* Disable ADC deep power down mode */
0493     LL_ADC_DisableDeepPowerDown(hadc->Instance);
0494 
0495     /* System was in deep power down mode, calibration must
0496      be relaunched or a previously saved calibration factor
0497      re-applied once the ADC voltage regulator is enabled */
0498   }
0499 
0500   if (LL_ADC_IsInternalRegulatorEnabled(hadc->Instance) == 0UL)
0501   {
0502     /* Enable ADC internal voltage regulator */
0503     LL_ADC_EnableInternalRegulator(hadc->Instance);
0504 
0505     /* Note: Variable divided by 2 to compensate partially              */
0506     /*       CPU processing cycles, scaling in us split to not          */
0507     /*       exceed 32 bits register capacity and handle low frequency. */
0508     wait_loop_index = ((LL_ADC_DELAY_INTERNAL_REGUL_STAB_US / 10UL) * ((SystemCoreClock / (100000UL * 2UL)) + 1UL));
0509     while (wait_loop_index != 0UL)
0510     {
0511       wait_loop_index--;
0512     }
0513   }
0514 
0515   /* Verification that ADC voltage regulator is correctly enabled, whether    */
0516   /* or not ADC is coming from state reset (if any potential problem of       */
0517   /* clocking, voltage regulator would not be enabled).                       */
0518   if (LL_ADC_IsInternalRegulatorEnabled(hadc->Instance) == 0UL)
0519   {
0520     /* Update ADC state machine to error */
0521     SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
0522 
0523     /* Set ADC error code to ADC peripheral internal error */
0524     SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
0525 
0526     tmp_hal_status = HAL_ERROR;
0527   }
0528 
0529   /* Configuration of ADC parameters if previous preliminary actions are      */
0530   /* correctly completed and if there is no conversion on going on regular    */
0531   /* group (ADC may already be enabled at this point if HAL_ADC_Init() is     */
0532   /* called to update a parameter on the fly).                                */
0533   tmp_adc_reg_is_conversion_on_going = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
0534 
0535   if (((hadc->State & HAL_ADC_STATE_ERROR_INTERNAL) == 0UL)
0536       && (tmp_adc_reg_is_conversion_on_going == 0UL)
0537      )
0538   {
0539     /* Set ADC state */
0540     ADC_STATE_CLR_SET(hadc->State,
0541                       HAL_ADC_STATE_REG_BUSY,
0542                       HAL_ADC_STATE_BUSY_INTERNAL);
0543 
0544     /* Configuration of common ADC parameters                                 */
0545 
0546     /* Parameters update conditioned to ADC state:                            */
0547     /* Parameters that can be updated only when ADC is disabled:              */
0548     /*  - clock configuration                                                 */
0549     if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
0550     {
0551       if (__LL_ADC_IS_ENABLED_ALL_COMMON_INSTANCE(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) == 0UL)
0552       {
0553         /* Reset configuration of ADC common register CCR:                      */
0554         /*                                                                      */
0555         /*   - ADC clock mode and ACC prescaler (CKMODE and PRESC bits)are set  */
0556         /*     according to adc->Init.ClockPrescaler. It selects the clock      */
0557         /*    source and sets the clock division factor.                        */
0558         /*                                                                      */
0559         /* Some parameters of this register are not reset, since they are set   */
0560         /* by other functions and must be kept in case of usage of this         */
0561         /* function on the fly (update of a parameter of ADC_InitTypeDef        */
0562         /* without needing to reconfigure all other ADC groups/channels         */
0563         /* parameters):                                                         */
0564         /*   - when multimode feature is available, multimode-related           */
0565         /*     parameters: MDMA, DMACFG, DELAY, DUAL (set by API                */
0566         /*     HAL_ADCEx_MultiModeConfigChannel() )                             */
0567         /*   - internal measurement paths: Vbat, temperature sensor, Vref       */
0568         /*     (set into HAL_ADC_ConfigChannel() or                             */
0569         /*     HAL_ADCEx_InjectedConfigChannel() )                              */
0570         LL_ADC_SetCommonClock(__LL_ADC_COMMON_INSTANCE(hadc->Instance), hadc->Init.ClockPrescaler);
0571       }
0572     }
0573 
0574     /* Configuration of ADC:                                                  */
0575     /*  - resolution                               Init.Resolution            */
0576     /*  - external trigger to start conversion     Init.ExternalTrigConv      */
0577     /*  - external trigger polarity                Init.ExternalTrigConvEdge  */
0578     /*  - continuous conversion mode               Init.ContinuousConvMode    */
0579     /*  - overrun                                  Init.Overrun               */
0580     /*  - discontinuous mode                       Init.DiscontinuousConvMode */
0581     /*  - discontinuous mode channel count         Init.NbrOfDiscConversion   */
0582 #if defined(ADC_VER_V5_3)
0583 
0584     tmpCFGR  = (ADC_CFGR_CONTINUOUS((uint32_t)hadc->Init.ContinuousConvMode)           |
0585                 hadc->Init.Overrun                                                    |
0586                 hadc->Init.Resolution                                                 |
0587                 ADC_CFGR_REG_DISCONTINUOUS((uint32_t)hadc->Init.DiscontinuousConvMode));
0588 
0589 #elif defined(ADC_VER_V5_V90)
0590     if (hadc->Instance == ADC3)
0591     {
0592       tmpCFGR  = (ADC_CFGR_CONTINUOUS((uint32_t)hadc->Init.ContinuousConvMode)         |
0593                   hadc->Init.Overrun                                                     |
0594                   hadc->Init.DataAlign                                                   |
0595                   ((__LL_ADC12_RESOLUTION_TO_ADC3(hadc->Init.Resolution)  & (ADC_CFGR_RES_1 | ADC_CFGR_RES_0)) << 1UL)                                                   |
0596                   ADC_CFGR_REG_DISCONTINUOUS((uint32_t)hadc->Init.DiscontinuousConvMode));
0597     }
0598     else
0599     {
0600       tmpCFGR  = (ADC_CFGR_CONTINUOUS((uint32_t)hadc->Init.ContinuousConvMode)          |
0601                   hadc->Init.Overrun                                                    |
0602                   hadc->Init.Resolution                                                 |
0603                   ADC_CFGR_REG_DISCONTINUOUS((uint32_t)hadc->Init.DiscontinuousConvMode));
0604     }
0605 
0606 #else
0607 
0608     if ((HAL_GetREVID() > REV_ID_Y) && (ADC_RESOLUTION_8B == hadc->Init.Resolution))
0609     {
0610       /* for STM32H7 silicon rev.B and above , ADC_CFGR_RES value for 8bits resolution is : b111 */
0611       tmpCFGR  = (ADC_CFGR_CONTINUOUS((uint32_t)hadc->Init.ContinuousConvMode)          |
0612                   hadc->Init.Overrun                                                    |
0613                   hadc->Init.Resolution | (ADC_CFGR_RES_1 | ADC_CFGR_RES_0)                |
0614                   ADC_CFGR_REG_DISCONTINUOUS((uint32_t)hadc->Init.DiscontinuousConvMode));
0615     }
0616     else
0617     {
0618 
0619       tmpCFGR  = (ADC_CFGR_CONTINUOUS((uint32_t)hadc->Init.ContinuousConvMode)          |
0620                   hadc->Init.Overrun                                                    |
0621                   hadc->Init.Resolution                                                 |
0622                   ADC_CFGR_REG_DISCONTINUOUS((uint32_t)hadc->Init.DiscontinuousConvMode));
0623     }
0624 
0625 #endif /* ADC_VER_V5_3 */
0626 
0627     if (hadc->Init.DiscontinuousConvMode == ENABLE)
0628     {
0629       tmpCFGR |= ADC_CFGR_DISCONTINUOUS_NUM(hadc->Init.NbrOfDiscConversion);
0630     }
0631 
0632     /* Enable external trigger if trigger selection is different of software  */
0633     /* start.                                                                 */
0634     /* Note: This configuration keeps the hardware feature of parameter       */
0635     /*       ExternalTrigConvEdge "trigger edge none" equivalent to           */
0636     /*       software start.                                                  */
0637     if (hadc->Init.ExternalTrigConv != ADC_SOFTWARE_START)
0638     {
0639       tmpCFGR |= ((hadc->Init.ExternalTrigConv & ADC_CFGR_EXTSEL)
0640                   | hadc->Init.ExternalTrigConvEdge
0641                  );
0642     }
0643 
0644 
0645 #if defined(ADC_VER_V5_V90)
0646     if (hadc->Instance == ADC3)
0647     {
0648       /* Update Configuration Register CFGR */
0649       MODIFY_REG(hadc->Instance->CFGR, ADC3_CFGR_FIELDS_1, tmpCFGR);
0650       /* Configuration of sampling mode */
0651       MODIFY_REG(hadc->Instance->CFGR2, ADC3_CFGR2_BULB | ADC3_CFGR2_SMPTRIG, hadc->Init.SamplingMode);
0652     }
0653     else
0654     {
0655       /* Update Configuration Register CFGR */
0656       MODIFY_REG(hadc->Instance->CFGR, ADC_CFGR_FIELDS_1, tmpCFGR);
0657     }
0658 #else
0659     /* Update Configuration Register CFGR */
0660     MODIFY_REG(hadc->Instance->CFGR, ADC_CFGR_FIELDS_1, tmpCFGR);
0661 #endif
0662 
0663     /* Parameters update conditioned to ADC state:                            */
0664     /* Parameters that can be updated when ADC is disabled or enabled without */
0665     /* conversion on going on regular and injected groups:                    */
0666     /*  - Conversion data management      Init.ConversionDataManagement       */
0667     /*  - LowPowerAutoWait feature        Init.LowPowerAutoWait               */
0668     /*  - Oversampling parameters         Init.Oversampling                   */
0669     tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
0670     tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
0671     if ((tmp_adc_is_conversion_on_going_regular == 0UL)
0672         && (tmp_adc_is_conversion_on_going_injected == 0UL)
0673        )
0674     {
0675 #if defined(ADC_VER_V5_V90)
0676       if (hadc->Instance == ADC3)
0677       {
0678         tmpCFGR = (
0679                     ADC_CFGR_AUTOWAIT((uint32_t)hadc->Init.LowPowerAutoWait)        |
0680                     ADC3_CFGR_DMACONTREQ((uint32_t)hadc->Init.DMAContinuousRequests));
0681       }
0682       else
0683       {
0684         tmpCFGR = (
0685                     ADC_CFGR_AUTOWAIT((uint32_t)hadc->Init.LowPowerAutoWait)        |
0686                     ADC_CFGR_DMACONTREQ((uint32_t)hadc->Init.ConversionDataManagement));
0687       }
0688 #else
0689       tmpCFGR = (
0690                   ADC_CFGR_AUTOWAIT((uint32_t)hadc->Init.LowPowerAutoWait)        |
0691                   ADC_CFGR_DMACONTREQ((uint32_t)hadc->Init.ConversionDataManagement));
0692 #endif
0693 
0694       MODIFY_REG(hadc->Instance->CFGR, ADC_CFGR_FIELDS_2, tmpCFGR);
0695 
0696       if (hadc->Init.OversamplingMode == ENABLE)
0697       {
0698 #if defined(ADC_VER_V5_V90)
0699         if (hadc->Instance == ADC3)
0700         {
0701           assert_param(IS_ADC_OVERSAMPLING_RATIO_ADC3(hadc->Init.Oversampling.Ratio));
0702         }
0703         else
0704         {
0705           assert_param(IS_ADC_OVERSAMPLING_RATIO(hadc->Init.Oversampling.Ratio));
0706         }
0707 #else
0708         assert_param(IS_ADC_OVERSAMPLING_RATIO(hadc->Init.Oversampling.Ratio));
0709 #endif
0710         assert_param(IS_ADC_RIGHT_BIT_SHIFT(hadc->Init.Oversampling.RightBitShift));
0711         assert_param(IS_ADC_TRIGGERED_OVERSAMPLING_MODE(hadc->Init.Oversampling.TriggeredMode));
0712         assert_param(IS_ADC_REGOVERSAMPLING_MODE(hadc->Init.Oversampling.OversamplingStopReset));
0713 
0714         if ((hadc->Init.ExternalTrigConv == ADC_SOFTWARE_START)
0715             || (hadc->Init.ExternalTrigConvEdge == ADC_EXTERNALTRIGCONVEDGE_NONE))
0716         {
0717           /* Multi trigger is not applicable to software-triggered conversions */
0718           assert_param((hadc->Init.Oversampling.TriggeredMode == ADC_TRIGGEREDMODE_SINGLE_TRIGGER));
0719         }
0720 
0721 #if defined(ADC_VER_V5_V90)
0722         if (hadc->Instance == ADC3)
0723         {
0724           /* Configuration of Oversampler:                                      */
0725           /*  - Oversampling Ratio                                              */
0726           /*  - Right bit shift                                                 */
0727           /*  - Triggered mode                                                  */
0728           /*  - Oversampling mode (continued/resumed)                           */
0729           MODIFY_REG(hadc->Instance->CFGR2,
0730                      ADC_CFGR2_OVSR  |
0731                      ADC_CFGR2_OVSS  |
0732                      ADC_CFGR2_TROVS |
0733                      ADC_CFGR2_ROVSM,
0734                      ADC_CFGR2_ROVSE                       |
0735                      hadc->Init.Oversampling.Ratio         |
0736                      hadc->Init.Oversampling.RightBitShift |
0737                      hadc->Init.Oversampling.TriggeredMode |
0738                      hadc->Init.Oversampling.OversamplingStopReset
0739                     );
0740         }
0741         else
0742         {
0743 
0744           /* Configuration of Oversampler:                                       */
0745           /*  - Oversampling Ratio                                               */
0746           /*  - Right bit shift                                                  */
0747           /*  - Left bit shift                                                   */
0748           /*  - Triggered mode                                                   */
0749           /*  - Oversampling mode (continued/resumed)                            */
0750           MODIFY_REG(hadc->Instance->CFGR2, ADC_CFGR2_FIELDS,
0751                      ADC_CFGR2_ROVSE                       |
0752                      ((hadc->Init.Oversampling.Ratio - 1UL)  << ADC_CFGR2_OVSR_Pos) |
0753                      hadc->Init.Oversampling.RightBitShift |
0754                      hadc->Init.Oversampling.TriggeredMode |
0755                      hadc->Init.Oversampling.OversamplingStopReset);
0756         }
0757 #else
0758         /* Configuration of Oversampler:                                       */
0759         /*  - Oversampling Ratio                                               */
0760         /*  - Right bit shift                                                  */
0761         /*  - Left bit shift                                                   */
0762         /*  - Triggered mode                                                   */
0763         /*  - Oversampling mode (continued/resumed)                            */
0764         MODIFY_REG(hadc->Instance->CFGR2, ADC_CFGR2_FIELDS,
0765                    ADC_CFGR2_ROVSE                       |
0766                    ((hadc->Init.Oversampling.Ratio - 1UL) << ADC_CFGR2_OVSR_Pos) |
0767                    hadc->Init.Oversampling.RightBitShift |
0768                    hadc->Init.Oversampling.TriggeredMode |
0769                    hadc->Init.Oversampling.OversamplingStopReset);
0770 #endif
0771 
0772       }
0773       else
0774       {
0775         /* Disable ADC oversampling scope on ADC group regular */
0776         CLEAR_BIT(hadc->Instance->CFGR2, ADC_CFGR2_ROVSE);
0777       }
0778 
0779       /* Set the LeftShift parameter: it is applied to the final result with or without oversampling */
0780       MODIFY_REG(hadc->Instance->CFGR2, ADC_CFGR2_LSHIFT, hadc->Init.LeftBitShift);
0781 #if defined(ADC_VER_V5_V90)
0782       if (hadc->Instance != ADC3)
0783       {
0784         /* Configure the BOOST Mode */
0785         ADC_ConfigureBoostMode(hadc);
0786       }
0787 #else
0788       /* Configure the BOOST Mode */
0789       ADC_ConfigureBoostMode(hadc);
0790 #endif
0791     }
0792 
0793     /* Configuration of regular group sequencer:                              */
0794     /* - if scan mode is disabled, regular channels sequence length is set to */
0795     /*   0x00: 1 channel converted (channel on regular rank 1)                */
0796     /*   Parameter "NbrOfConversion" is discarded.                            */
0797     /*   Note: Scan mode is not present by hardware on this device, but       */
0798     /*   emulated by software for alignment over all STM32 devices.           */
0799     /* - if scan mode is enabled, regular channels sequence length is set to  */
0800     /*   parameter "NbrOfConversion".                                         */
0801 
0802     if (hadc->Init.ScanConvMode == ADC_SCAN_ENABLE)
0803     {
0804       /* Set number of ranks in regular group sequencer */
0805       MODIFY_REG(hadc->Instance->SQR1, ADC_SQR1_L, (hadc->Init.NbrOfConversion - (uint8_t)1));
0806     }
0807     else
0808     {
0809       CLEAR_BIT(hadc->Instance->SQR1, ADC_SQR1_L);
0810     }
0811 
0812     /* Initialize the ADC state */
0813     /* Clear HAL_ADC_STATE_BUSY_INTERNAL bit, set HAL_ADC_STATE_READY bit */
0814     ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_BUSY_INTERNAL, HAL_ADC_STATE_READY);
0815   }
0816   else
0817   {
0818     /* Update ADC state machine to error */
0819     SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
0820 
0821     tmp_hal_status = HAL_ERROR;
0822   }
0823 
0824   /* Return function status */
0825   return tmp_hal_status;
0826 }
0827 
0828 /**
0829   * @brief  Deinitialize the ADC peripheral registers to their default reset
0830   *         values, with deinitialization of the ADC MSP.
0831   * @note   For devices with several ADCs: reset of ADC common registers is done
0832   *         only if all ADCs sharing the same common group are disabled.
0833   *         (function "HAL_ADC_MspDeInit()" is also called under the same conditions:
0834   *         all ADC instances use the same core clock at RCC level, disabling
0835   *         the core clock reset all ADC instances).
0836   *         If this is not the case, reset of these common parameters reset is
0837   *         bypassed without error reporting: it can be the intended behavior in
0838   *         case of reset of a single ADC while the other ADCs sharing the same
0839   *         common group is still running.
0840   * @note   By default, HAL_ADC_DeInit() set ADC in mode deep power-down:
0841   *         this saves more power by reducing leakage currents
0842   *         and is particularly interesting before entering MCU low-power modes.
0843   * @param hadc ADC handle
0844   * @retval HAL status
0845   */
0846 HAL_StatusTypeDef HAL_ADC_DeInit(ADC_HandleTypeDef *hadc)
0847 {
0848   HAL_StatusTypeDef tmp_hal_status;
0849 
0850   /* Check ADC handle */
0851   if (hadc == NULL)
0852   {
0853     return HAL_ERROR;
0854   }
0855 
0856   /* Check the parameters */
0857   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
0858 
0859   /* Set ADC state */
0860   SET_BIT(hadc->State, HAL_ADC_STATE_BUSY_INTERNAL);
0861 
0862   /* Stop potential conversion on going */
0863   tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_INJECTED_GROUP);
0864 
0865   /* Disable ADC peripheral if conversions are effectively stopped            */
0866   /* Flush register JSQR: reset the queue sequencer when injected             */
0867   /* queue sequencer is enabled and ADC disabled.                             */
0868   /* The software and hardware triggers of the injected sequence are both     */
0869   /* internally disabled just after the completion of the last valid          */
0870   /* injected sequence.                                                       */
0871   SET_BIT(hadc->Instance->CFGR, ADC_CFGR_JQM);
0872 
0873   /* Disable ADC peripheral if conversions are effectively stopped */
0874   if (tmp_hal_status == HAL_OK)
0875   {
0876     /* Disable the ADC peripheral */
0877     tmp_hal_status = ADC_Disable(hadc);
0878 
0879     /* Check if ADC is effectively disabled */
0880     if (tmp_hal_status == HAL_OK)
0881     {
0882       /* Change ADC state */
0883       hadc->State = HAL_ADC_STATE_READY;
0884     }
0885   }
0886 
0887   /* Note: HAL ADC deInit is done independently of ADC conversion stop        */
0888   /*       and disable return status. In case of status fail, attempt to      */
0889   /*       perform deinitialization anyway and it is up user code in          */
0890   /*       in HAL_ADC_MspDeInit() to reset the ADC peripheral using           */
0891   /*       system RCC hard reset.                                             */
0892 
0893   /* ========== Reset ADC registers ========== */
0894   /* Reset register IER */
0895   __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_AWD3  | ADC_IT_AWD2 | ADC_IT_AWD1 |
0896                               ADC_IT_JQOVF | ADC_IT_OVR  |
0897                               ADC_IT_JEOS  | ADC_IT_JEOC |
0898                               ADC_IT_EOS   | ADC_IT_EOC  |
0899                               ADC_IT_EOSMP | ADC_IT_RDY));
0900 
0901   /* Reset register ISR */
0902   __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_AWD3  | ADC_FLAG_AWD2 | ADC_FLAG_AWD1 |
0903                               ADC_FLAG_JQOVF | ADC_FLAG_OVR  |
0904                               ADC_FLAG_JEOS  | ADC_FLAG_JEOC |
0905                               ADC_FLAG_EOS   | ADC_FLAG_EOC  |
0906                               ADC_FLAG_EOSMP | ADC_FLAG_RDY));
0907 
0908   /* Reset register CR */
0909   /* Bits ADC_CR_JADSTP, ADC_CR_ADSTP, ADC_CR_JADSTART, ADC_CR_ADSTART,
0910      ADC_CR_ADCAL, ADC_CR_ADDIS and ADC_CR_ADEN are in access mode "read-set":
0911      no direct reset applicable.
0912      Update CR register to reset value where doable by software */
0913   CLEAR_BIT(hadc->Instance->CR, ADC_CR_ADVREGEN | ADC_CR_ADCALDIF);
0914   SET_BIT(hadc->Instance->CR, ADC_CR_DEEPPWD);
0915 
0916   /* Reset register CFGR */
0917   CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_AWD1CH  | ADC_CFGR_JAUTO   | ADC_CFGR_JAWD1EN |
0918             ADC_CFGR_AWD1EN  | ADC_CFGR_AWD1SGL | ADC_CFGR_JQM     |
0919             ADC_CFGR_JDISCEN | ADC_CFGR_DISCNUM | ADC_CFGR_DISCEN  |
0920             ADC_CFGR_AUTDLY  | ADC_CFGR_CONT    | ADC_CFGR_OVRMOD  |
0921             ADC_CFGR_EXTEN   | ADC_CFGR_EXTSEL  |
0922             ADC_CFGR_RES     | ADC_CFGR_DMNGT);
0923   SET_BIT(hadc->Instance->CFGR, ADC_CFGR_JQDIS);
0924 
0925   /* Reset register CFGR2 */
0926   CLEAR_BIT(hadc->Instance->CFGR2, ADC_CFGR2_ROVSM  | ADC_CFGR2_TROVS   | ADC_CFGR2_OVSS |
0927             ADC_CFGR2_OVSR  | ADC_CFGR2_JOVSE | ADC_CFGR2_ROVSE);
0928 
0929   /* Reset register SMPR1 */
0930   CLEAR_BIT(hadc->Instance->SMPR1, ADC_SMPR1_FIELDS);
0931 
0932   /* Reset register SMPR2 */
0933   CLEAR_BIT(hadc->Instance->SMPR2, ADC_SMPR2_SMP18 | ADC_SMPR2_SMP17 | ADC_SMPR2_SMP16 |
0934             ADC_SMPR2_SMP15 | ADC_SMPR2_SMP14 | ADC_SMPR2_SMP13 |
0935             ADC_SMPR2_SMP12 | ADC_SMPR2_SMP11 | ADC_SMPR2_SMP10);
0936 
0937 #if defined(ADC_VER_V5_V90)
0938   if (hadc->Instance == ADC3)
0939   {  
0940     /* Reset register LTR1 and HTR1 */
0941     CLEAR_BIT(hadc->Instance->LTR1_TR1, ADC3_TR1_HT1 | ADC3_TR1_LT1);
0942     CLEAR_BIT(hadc->Instance->HTR1_TR2, ADC3_TR2_HT2 | ADC3_TR2_LT2);
0943 
0944     /* Reset register LTR3 and HTR3 */
0945     CLEAR_BIT(hadc->Instance->RES1_TR3, ADC3_TR3_HT3 | ADC3_TR3_LT3);
0946   }
0947   else
0948   {  
0949     CLEAR_BIT(hadc->Instance->LTR1_TR1, ADC_LTR_LT);
0950     CLEAR_BIT(hadc->Instance->HTR1_TR2, ADC_HTR_HT);
0951 
0952     /* Reset register LTR2 and HTR2*/
0953     CLEAR_BIT(hadc->Instance->LTR2_DIFSEL, ADC_LTR_LT);
0954     CLEAR_BIT(hadc->Instance->HTR2_CALFACT, ADC_HTR_HT);
0955 
0956     /* Reset register LTR3 and HTR3 */
0957     CLEAR_BIT(hadc->Instance->LTR3_RES10, ADC_LTR_LT);
0958     CLEAR_BIT(hadc->Instance->HTR3_RES11, ADC_HTR_HT);
0959   }
0960 #else
0961   /* Reset register LTR1 and HTR1 */
0962   CLEAR_BIT(hadc->Instance->LTR1, ADC_LTR_LT);
0963   CLEAR_BIT(hadc->Instance->HTR1, ADC_HTR_HT);
0964 
0965   /* Reset register LTR2 and HTR2*/
0966   CLEAR_BIT(hadc->Instance->LTR2, ADC_LTR_LT);
0967   CLEAR_BIT(hadc->Instance->HTR2, ADC_HTR_HT);
0968 
0969   /* Reset register LTR3 and HTR3 */
0970   CLEAR_BIT(hadc->Instance->LTR3, ADC_LTR_LT);
0971   CLEAR_BIT(hadc->Instance->HTR3, ADC_HTR_HT);
0972 #endif /* ADC_VER_V5_V90 */
0973 
0974 
0975   /* Reset register SQR1 */
0976   CLEAR_BIT(hadc->Instance->SQR1, ADC_SQR1_SQ4 | ADC_SQR1_SQ3 | ADC_SQR1_SQ2 |
0977             ADC_SQR1_SQ1 | ADC_SQR1_L);
0978 
0979   /* Reset register SQR2 */
0980   CLEAR_BIT(hadc->Instance->SQR2, ADC_SQR2_SQ9 | ADC_SQR2_SQ8 | ADC_SQR2_SQ7 |
0981             ADC_SQR2_SQ6 | ADC_SQR2_SQ5);
0982 
0983   /* Reset register SQR3 */
0984   CLEAR_BIT(hadc->Instance->SQR3, ADC_SQR3_SQ14 | ADC_SQR3_SQ13 | ADC_SQR3_SQ12 |
0985             ADC_SQR3_SQ11 | ADC_SQR3_SQ10);
0986 
0987   /* Reset register SQR4 */
0988   CLEAR_BIT(hadc->Instance->SQR4, ADC_SQR4_SQ16 | ADC_SQR4_SQ15);
0989 
0990   /* Register JSQR was reset when the ADC was disabled */
0991 
0992   /* Reset register DR */
0993   /* bits in access mode read only, no direct reset applicable*/
0994 
0995   /* Reset register OFR1 */
0996   CLEAR_BIT(hadc->Instance->OFR1, ADC_OFR1_SSATE | ADC_OFR1_OFFSET1_CH | ADC_OFR1_OFFSET1);
0997   /* Reset register OFR2 */
0998   CLEAR_BIT(hadc->Instance->OFR2, ADC_OFR2_SSATE | ADC_OFR2_OFFSET2_CH | ADC_OFR2_OFFSET2);
0999   /* Reset register OFR3 */
1000   CLEAR_BIT(hadc->Instance->OFR3, ADC_OFR3_SSATE | ADC_OFR3_OFFSET3_CH | ADC_OFR3_OFFSET3);
1001   /* Reset register OFR4 */
1002   CLEAR_BIT(hadc->Instance->OFR4, ADC_OFR4_SSATE | ADC_OFR4_OFFSET4_CH | ADC_OFR4_OFFSET4);
1003 
1004   /* Reset registers JDR1, JDR2, JDR3, JDR4 */
1005   /* bits in access mode read only, no direct reset applicable*/
1006 
1007   /* Reset register AWD2CR */
1008   CLEAR_BIT(hadc->Instance->AWD2CR, ADC_AWD2CR_AWD2CH);
1009 
1010   /* Reset register AWD3CR */
1011   CLEAR_BIT(hadc->Instance->AWD3CR, ADC_AWD3CR_AWD3CH);
1012 
1013 #if defined(ADC_VER_V5_V90)
1014   if (hadc->Instance == ADC3)
1015   {
1016   /* Reset register DIFSEL */
1017   CLEAR_BIT(hadc->Instance->LTR2_DIFSEL, ADC_DIFSEL_DIFSEL);
1018 
1019   /* Reset register CALFACT */
1020   CLEAR_BIT(hadc->Instance->HTR2_CALFACT, ADC_CALFACT_CALFACT_D | ADC_CALFACT_CALFACT_S);
1021   }
1022   else
1023   {
1024     /* Reset register DIFSEL */
1025     CLEAR_BIT(hadc->Instance->DIFSEL_RES12, ADC_DIFSEL_DIFSEL);
1026 
1027     /* Reset register CALFACT */
1028     CLEAR_BIT(hadc->Instance->CALFACT_RES13, ADC_CALFACT_CALFACT_D | ADC_CALFACT_CALFACT_S);
1029   }
1030 #else
1031   /* Reset register DIFSEL */
1032   CLEAR_BIT(hadc->Instance->DIFSEL, ADC_DIFSEL_DIFSEL);
1033 
1034   /* Reset register CALFACT */
1035   CLEAR_BIT(hadc->Instance->CALFACT, ADC_CALFACT_CALFACT_D | ADC_CALFACT_CALFACT_S);
1036 #endif /* ADC_VER_V5_V90 */
1037 
1038   /* ========== Reset common ADC registers ========== */
1039 
1040   /* Software is allowed to change common parameters only when all the other
1041      ADCs are disabled.   */
1042   if (__LL_ADC_IS_ENABLED_ALL_COMMON_INSTANCE(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) == 0UL)
1043   {
1044     /* Reset configuration of ADC common register CCR:
1045       - clock mode: CKMODE, PRESCEN
1046       - multimode related parameters(when this feature is available): DELAY, DUAL
1047        (set into  HAL_ADCEx_MultiModeConfigChannel() API)
1048       - internal measurement paths: Vbat, temperature sensor, Vref (set into
1049         HAL_ADC_ConfigChannel() or HAL_ADCEx_InjectedConfigChannel() )
1050     */
1051     ADC_CLEAR_COMMON_CONTROL_REGISTER(hadc);
1052 
1053     /* ========== Hard reset ADC peripheral ========== */
1054     /* Performs a global reset of the entire ADC peripherals instances        */
1055     /* sharing the same common ADC instance: ADC state is forced to           */
1056     /* a similar state as after device power-on.                              */
1057     /* Note: A possible implementation is to add RCC bus reset of ADC         */
1058     /* (for example, using macro                                              */
1059     /*  __HAL_RCC_ADC..._FORCE_RESET()/..._RELEASE_RESET()/..._CLK_DISABLE()) */
1060     /* in function "void HAL_ADC_MspDeInit(ADC_HandleTypeDef *hadc)":         */
1061 
1062 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
1063     if (hadc->MspDeInitCallback == NULL)
1064     {
1065       hadc->MspDeInitCallback = HAL_ADC_MspDeInit; /* Legacy weak MspDeInit  */
1066     }
1067 
1068     /* DeInit the low level hardware: RCC clock, NVIC */
1069     hadc->MspDeInitCallback(hadc);
1070 #else
1071     /* DeInit the low level hardware: RCC clock, NVIC */
1072     HAL_ADC_MspDeInit(hadc);
1073 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
1074 
1075   }
1076 
1077   /* Set ADC error code to none */
1078   ADC_CLEAR_ERRORCODE(hadc);
1079 
1080   /* Reset injected channel configuration parameters */
1081   hadc->InjectionConfig.ContextQueue = 0;
1082   hadc->InjectionConfig.ChannelCount = 0;
1083 
1084   /* Set ADC state */
1085   hadc->State = HAL_ADC_STATE_RESET;
1086 
1087   /* Process unlocked */
1088   __HAL_UNLOCK(hadc);
1089 
1090   /* Return function status */
1091   return tmp_hal_status;
1092 }
1093 
1094 /**
1095   * @brief  Initialize the ADC MSP.
1096   * @param hadc ADC handle
1097   * @retval None
1098   */
1099 __weak void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc)
1100 {
1101   /* Prevent unused argument(s) compilation warning */
1102   UNUSED(hadc);
1103 
1104   /* NOTE : This function should not be modified. When the callback is needed,
1105             function HAL_ADC_MspInit must be implemented in the user file.
1106    */
1107 }
1108 
1109 /**
1110   * @brief  DeInitialize the ADC MSP.
1111   * @param hadc ADC handle
1112   * @note   All ADC instances use the same core clock at RCC level, disabling
1113   *         the core clock reset all ADC instances).
1114   * @retval None
1115   */
1116 __weak void HAL_ADC_MspDeInit(ADC_HandleTypeDef *hadc)
1117 {
1118   /* Prevent unused argument(s) compilation warning */
1119   UNUSED(hadc);
1120 
1121   /* NOTE : This function should not be modified. When the callback is needed,
1122             function HAL_ADC_MspDeInit must be implemented in the user file.
1123    */
1124 }
1125 
1126 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
1127 /**
1128   * @brief  Register a User ADC Callback
1129   *         To be used instead of the weak predefined callback
1130   * @param  hadc Pointer to a ADC_HandleTypeDef structure that contains
1131   *                the configuration information for the specified ADC.
1132   * @param  CallbackID ID of the callback to be registered
1133   *         This parameter can be one of the following values:
1134   *          @arg @ref HAL_ADC_CONVERSION_COMPLETE_CB_ID      ADC conversion complete callback ID
1135   *          @arg @ref HAL_ADC_CONVERSION_HALF_CB_ID          ADC conversion DMA half-transfer callback ID
1136   *          @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID    ADC analog watchdog 1 callback ID
1137   *          @arg @ref HAL_ADC_ERROR_CB_ID                    ADC error callback ID
1138   *          @arg @ref HAL_ADC_INJ_CONVERSION_COMPLETE_CB_ID  ADC group injected conversion complete callback ID
1139   *          @arg @ref HAL_ADC_INJ_QUEUE_OVEFLOW_CB_ID        ADC group injected context queue overflow callback ID
1140   *          @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_2_CB_ID    ADC analog watchdog 2 callback ID
1141   *          @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_3_CB_ID    ADC analog watchdog 3 callback ID
1142   *          @arg @ref HAL_ADC_END_OF_SAMPLING_CB_ID          ADC end of sampling callback ID
1143   *          @arg @ref HAL_ADC_MSPINIT_CB_ID                  ADC Msp Init callback ID
1144   *          @arg @ref HAL_ADC_MSPDEINIT_CB_ID                ADC Msp DeInit callback ID
1145   * @param  pCallback pointer to the Callback function
1146   * @retval HAL status
1147   */
1148 HAL_StatusTypeDef HAL_ADC_RegisterCallback(ADC_HandleTypeDef *hadc, HAL_ADC_CallbackIDTypeDef CallbackID, pADC_CallbackTypeDef pCallback)
1149 {
1150   HAL_StatusTypeDef status = HAL_OK;
1151 
1152   if (pCallback == NULL)
1153   {
1154     /* Update the error code */
1155     hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
1156 
1157     return HAL_ERROR;
1158   }
1159 
1160   if ((hadc->State & HAL_ADC_STATE_READY) != 0UL)
1161   {
1162     switch (CallbackID)
1163     {
1164       case HAL_ADC_CONVERSION_COMPLETE_CB_ID :
1165         hadc->ConvCpltCallback = pCallback;
1166         break;
1167 
1168       case HAL_ADC_CONVERSION_HALF_CB_ID :
1169         hadc->ConvHalfCpltCallback = pCallback;
1170         break;
1171 
1172       case HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID :
1173         hadc->LevelOutOfWindowCallback = pCallback;
1174         break;
1175 
1176       case HAL_ADC_ERROR_CB_ID :
1177         hadc->ErrorCallback = pCallback;
1178         break;
1179 
1180       case HAL_ADC_INJ_CONVERSION_COMPLETE_CB_ID :
1181         hadc->InjectedConvCpltCallback = pCallback;
1182         break;
1183 
1184       case HAL_ADC_INJ_QUEUE_OVEFLOW_CB_ID :
1185         hadc->InjectedQueueOverflowCallback = pCallback;
1186         break;
1187 
1188       case HAL_ADC_LEVEL_OUT_OF_WINDOW_2_CB_ID :
1189         hadc->LevelOutOfWindow2Callback = pCallback;
1190         break;
1191 
1192       case HAL_ADC_LEVEL_OUT_OF_WINDOW_3_CB_ID :
1193         hadc->LevelOutOfWindow3Callback = pCallback;
1194         break;
1195 
1196       case HAL_ADC_END_OF_SAMPLING_CB_ID :
1197         hadc->EndOfSamplingCallback = pCallback;
1198         break;
1199 
1200       case HAL_ADC_MSPINIT_CB_ID :
1201         hadc->MspInitCallback = pCallback;
1202         break;
1203 
1204       case HAL_ADC_MSPDEINIT_CB_ID :
1205         hadc->MspDeInitCallback = pCallback;
1206         break;
1207 
1208       default :
1209         /* Update the error code */
1210         hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
1211 
1212         /* Return error status */
1213         status = HAL_ERROR;
1214         break;
1215     }
1216   }
1217   else if (HAL_ADC_STATE_RESET == hadc->State)
1218   {
1219     switch (CallbackID)
1220     {
1221       case HAL_ADC_MSPINIT_CB_ID :
1222         hadc->MspInitCallback = pCallback;
1223         break;
1224 
1225       case HAL_ADC_MSPDEINIT_CB_ID :
1226         hadc->MspDeInitCallback = pCallback;
1227         break;
1228 
1229       default :
1230         /* Update the error code */
1231         hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
1232 
1233         /* Return error status */
1234         status = HAL_ERROR;
1235         break;
1236     }
1237   }
1238   else
1239   {
1240     /* Update the error code */
1241     hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
1242 
1243     /* Return error status */
1244     status =  HAL_ERROR;
1245   }
1246 
1247   return status;
1248 }
1249 
1250 /**
1251   * @brief  Unregister a ADC Callback
1252   *         ADC callback is redirected to the weak predefined callback
1253   * @param  hadc Pointer to a ADC_HandleTypeDef structure that contains
1254   *                the configuration information for the specified ADC.
1255   * @param  CallbackID ID of the callback to be unregistered
1256   *         This parameter can be one of the following values:
1257   *          @arg @ref HAL_ADC_CONVERSION_COMPLETE_CB_ID      ADC conversion complete callback ID
1258   *          @arg @ref HAL_ADC_CONVERSION_HALF_CB_ID          ADC conversion DMA half-transfer callback ID
1259   *          @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID    ADC analog watchdog 1 callback ID
1260   *          @arg @ref HAL_ADC_ERROR_CB_ID                    ADC error callback ID
1261   *          @arg @ref HAL_ADC_INJ_CONVERSION_COMPLETE_CB_ID  ADC group injected conversion complete callback ID
1262   *          @arg @ref HAL_ADC_INJ_QUEUE_OVEFLOW_CB_ID        ADC group injected context queue overflow callback ID
1263   *          @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_2_CB_ID    ADC analog watchdog 2 callback ID
1264   *          @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_3_CB_ID    ADC analog watchdog 3 callback ID
1265   *          @arg @ref HAL_ADC_END_OF_SAMPLING_CB_ID          ADC end of sampling callback ID
1266   *          @arg @ref HAL_ADC_MSPINIT_CB_ID                  ADC Msp Init callback ID
1267   *          @arg @ref HAL_ADC_MSPDEINIT_CB_ID                ADC Msp DeInit callback ID
1268   * @retval HAL status
1269   */
1270 HAL_StatusTypeDef HAL_ADC_UnRegisterCallback(ADC_HandleTypeDef *hadc, HAL_ADC_CallbackIDTypeDef CallbackID)
1271 {
1272   HAL_StatusTypeDef status = HAL_OK;
1273 
1274   if ((hadc->State & HAL_ADC_STATE_READY) != 0UL)
1275   {
1276     switch (CallbackID)
1277     {
1278       case HAL_ADC_CONVERSION_COMPLETE_CB_ID :
1279         hadc->ConvCpltCallback = HAL_ADC_ConvCpltCallback;
1280         break;
1281 
1282       case HAL_ADC_CONVERSION_HALF_CB_ID :
1283         hadc->ConvHalfCpltCallback = HAL_ADC_ConvHalfCpltCallback;
1284         break;
1285 
1286       case HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID :
1287         hadc->LevelOutOfWindowCallback = HAL_ADC_LevelOutOfWindowCallback;
1288         break;
1289 
1290       case HAL_ADC_ERROR_CB_ID :
1291         hadc->ErrorCallback = HAL_ADC_ErrorCallback;
1292         break;
1293 
1294       case HAL_ADC_INJ_CONVERSION_COMPLETE_CB_ID :
1295         hadc->InjectedConvCpltCallback = HAL_ADCEx_InjectedConvCpltCallback;
1296         break;
1297 
1298       case HAL_ADC_INJ_QUEUE_OVEFLOW_CB_ID :
1299         hadc->InjectedQueueOverflowCallback = HAL_ADCEx_InjectedQueueOverflowCallback;
1300         break;
1301 
1302       case HAL_ADC_LEVEL_OUT_OF_WINDOW_2_CB_ID :
1303         hadc->LevelOutOfWindow2Callback = HAL_ADCEx_LevelOutOfWindow2Callback;
1304         break;
1305 
1306       case HAL_ADC_LEVEL_OUT_OF_WINDOW_3_CB_ID :
1307         hadc->LevelOutOfWindow3Callback = HAL_ADCEx_LevelOutOfWindow3Callback;
1308         break;
1309 
1310       case HAL_ADC_END_OF_SAMPLING_CB_ID :
1311         hadc->EndOfSamplingCallback = HAL_ADCEx_EndOfSamplingCallback;
1312         break;
1313 
1314       case HAL_ADC_MSPINIT_CB_ID :
1315         hadc->MspInitCallback = HAL_ADC_MspInit; /* Legacy weak MspInit              */
1316         break;
1317 
1318       case HAL_ADC_MSPDEINIT_CB_ID :
1319         hadc->MspDeInitCallback = HAL_ADC_MspDeInit; /* Legacy weak MspDeInit            */
1320         break;
1321 
1322       default :
1323         /* Update the error code */
1324         hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
1325 
1326         /* Return error status */
1327         status =  HAL_ERROR;
1328         break;
1329     }
1330   }
1331   else if (HAL_ADC_STATE_RESET == hadc->State)
1332   {
1333     switch (CallbackID)
1334     {
1335       case HAL_ADC_MSPINIT_CB_ID :
1336         hadc->MspInitCallback = HAL_ADC_MspInit;                   /* Legacy weak MspInit              */
1337         break;
1338 
1339       case HAL_ADC_MSPDEINIT_CB_ID :
1340         hadc->MspDeInitCallback = HAL_ADC_MspDeInit;               /* Legacy weak MspDeInit            */
1341         break;
1342 
1343       default :
1344         /* Update the error code */
1345         hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
1346 
1347         /* Return error status */
1348         status =  HAL_ERROR;
1349         break;
1350     }
1351   }
1352   else
1353   {
1354     /* Update the error code */
1355     hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
1356 
1357     /* Return error status */
1358     status =  HAL_ERROR;
1359   }
1360 
1361   return status;
1362 }
1363 
1364 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
1365 
1366 /**
1367   * @}
1368   */
1369 
1370 /** @defgroup ADC_Exported_Functions_Group2 ADC Input and Output operation functions
1371   * @ingroup RTEMSBSPsARMSTM32H7
1372   * @brief    ADC IO operation functions
1373   *
1374 @verbatim
1375  ===============================================================================
1376                       ##### IO operation functions #####
1377  ===============================================================================
1378     [..]  This section provides functions allowing to:
1379       (+) Start conversion of regular group.
1380       (+) Stop conversion of regular group.
1381       (+) Poll for conversion complete on regular group.
1382       (+) Poll for conversion event.
1383       (+) Get result of regular channel conversion.
1384       (+) Start conversion of regular group and enable interruptions.
1385       (+) Stop conversion of regular group and disable interruptions.
1386       (+) Handle ADC interrupt request
1387       (+) Start conversion of regular group and enable DMA transfer.
1388       (+) Stop conversion of regular group and disable ADC DMA transfer.
1389 @endverbatim
1390   * @{
1391   */
1392 
1393 /**
1394   * @brief  Enable ADC, start conversion of regular group.
1395   * @note   Interruptions enabled in this function: None.
1396   * @note   Case of multimode enabled (when multimode feature is available):
1397   *           if ADC is Slave, ADC is enabled but conversion is not started,
1398   *           if ADC is master, ADC is enabled and multimode conversion is started.
1399   * @param hadc ADC handle
1400   * @retval HAL status
1401   */
1402 HAL_StatusTypeDef HAL_ADC_Start(ADC_HandleTypeDef *hadc)
1403 {
1404   HAL_StatusTypeDef tmp_hal_status;
1405   const ADC_TypeDef *tmpADC_Master;
1406   uint32_t tmp_multimode_config = LL_ADC_GetMultimode(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
1407 
1408   /* Check the parameters */
1409   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
1410 
1411   /* Perform ADC enable and conversion start if no conversion is on going */
1412   if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
1413   {
1414     /* Process locked */
1415     __HAL_LOCK(hadc);
1416 
1417     /* Enable the ADC peripheral */
1418     tmp_hal_status = ADC_Enable(hadc);
1419 
1420     /* Start conversion if ADC is effectively enabled */
1421     if (tmp_hal_status == HAL_OK)
1422     {
1423       /* Set ADC state                                                        */
1424       /* - Clear state bitfield related to regular group conversion results   */
1425       /* - Set state bitfield related to regular operation                    */
1426       ADC_STATE_CLR_SET(hadc->State,
1427                         HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP,
1428                         HAL_ADC_STATE_REG_BUSY);
1429 
1430       /* Reset HAL_ADC_STATE_MULTIMODE_SLAVE bit
1431         - if ADC instance is master or if multimode feature is not available
1432         - if multimode setting is disabled (ADC instance slave in independent mode) */
1433       if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
1434           || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
1435          )
1436       {
1437         CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
1438       }
1439 
1440       /* Set ADC error code */
1441       /* Check if a conversion is on going on ADC group injected */
1442       if (HAL_IS_BIT_SET(hadc->State, HAL_ADC_STATE_INJ_BUSY))
1443       {
1444         /* Reset ADC error code fields related to regular conversions only */
1445         CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR | HAL_ADC_ERROR_DMA));
1446       }
1447       else
1448       {
1449         /* Reset all ADC error code fields */
1450         ADC_CLEAR_ERRORCODE(hadc);
1451       }
1452 
1453       /* Clear ADC group regular conversion flag and overrun flag               */
1454       /* (To ensure of no unknown state from potential previous ADC operations) */
1455       __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
1456 
1457       /* Process unlocked */
1458       /* Unlock before starting ADC conversions: in case of potential         */
1459       /* interruption, to let the process to ADC IRQ Handler.                 */
1460       __HAL_UNLOCK(hadc);
1461 
1462       /* Enable conversion of regular group.                                  */
1463       /* If software start has been selected, conversion starts immediately.  */
1464       /* If external trigger has been selected, conversion will start at next */
1465       /* trigger event.                                                       */
1466       /* Case of multimode enabled (when multimode feature is available):     */
1467       /*  - if ADC is slave and dual regular conversions are enabled, ADC is  */
1468       /*    enabled only (conversion is not started),                         */
1469       /*  - if ADC is master, ADC is enabled and conversion is started.       */
1470       if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
1471           || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
1472           || (tmp_multimode_config == LL_ADC_MULTI_DUAL_INJ_SIMULT)
1473           || (tmp_multimode_config == LL_ADC_MULTI_DUAL_INJ_ALTERN)
1474          )
1475       {
1476         /* ADC instance is not a multimode slave instance with multimode regular conversions enabled */
1477         if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_JAUTO) != 0UL)
1478         {
1479           ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
1480         }
1481 
1482         /* Start ADC group regular conversion */
1483         LL_ADC_REG_StartConversion(hadc->Instance);
1484       }
1485       else
1486       {
1487         /* ADC instance is a multimode slave instance with multimode regular conversions enabled */
1488         SET_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
1489         /* if Master ADC JAUTO bit is set, update Slave State in setting
1490            HAL_ADC_STATE_INJ_BUSY bit and in resetting HAL_ADC_STATE_INJ_EOC bit */
1491         tmpADC_Master = __LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance);
1492         if (READ_BIT(tmpADC_Master->CFGR, ADC_CFGR_JAUTO) != 0UL)
1493         {
1494           ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
1495         }
1496 
1497       }
1498     }
1499     else
1500     {
1501       /* Process unlocked */
1502       __HAL_UNLOCK(hadc);
1503     }
1504   }
1505   else
1506   {
1507     tmp_hal_status = HAL_BUSY;
1508   }
1509 
1510   /* Return function status */
1511   return tmp_hal_status;
1512 }
1513 
1514 /**
1515   * @brief  Stop ADC conversion of regular group (and injected channels in
1516   *         case of auto_injection mode), disable ADC peripheral.
1517   * @note:  ADC peripheral disable is forcing stop of potential
1518   *         conversion on injected group. If injected group is under use, it
1519   *         should be preliminarily stopped using HAL_ADCEx_InjectedStop function.
1520   * @param hadc ADC handle
1521   * @retval HAL status.
1522   */
1523 HAL_StatusTypeDef HAL_ADC_Stop(ADC_HandleTypeDef *hadc)
1524 {
1525   HAL_StatusTypeDef tmp_hal_status;
1526 
1527   /* Check the parameters */
1528   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
1529 
1530   /* Process locked */
1531   __HAL_LOCK(hadc);
1532 
1533   /* 1. Stop potential conversion on going, on ADC groups regular and injected */
1534   tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_INJECTED_GROUP);
1535 
1536   /* Disable ADC peripheral if conversions are effectively stopped */
1537   if (tmp_hal_status == HAL_OK)
1538   {
1539     /* 2. Disable the ADC peripheral */
1540     tmp_hal_status = ADC_Disable(hadc);
1541 
1542     /* Check if ADC is effectively disabled */
1543     if (tmp_hal_status == HAL_OK)
1544     {
1545       /* Set ADC state */
1546       ADC_STATE_CLR_SET(hadc->State,
1547                         HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
1548                         HAL_ADC_STATE_READY);
1549     }
1550   }
1551 
1552   /* Process unlocked */
1553   __HAL_UNLOCK(hadc);
1554 
1555   /* Return function status */
1556   return tmp_hal_status;
1557 }
1558 
1559 /**
1560   * @brief  Wait for regular group conversion to be completed.
1561   * @note   ADC conversion flags EOS (end of sequence) and EOC (end of
1562   *         conversion) are cleared by this function, with an exception:
1563   *         if low power feature "LowPowerAutoWait" is enabled, flags are
1564   *         not cleared to not interfere with this feature until data register
1565   *         is read using function HAL_ADC_GetValue().
1566   * @note   This function cannot be used in a particular setup: ADC configured
1567   *         in DMA mode and polling for end of each conversion (ADC init
1568   *         parameter "EOCSelection" set to ADC_EOC_SINGLE_CONV).
1569   *         In this case, DMA resets the flag EOC and polling cannot be
1570   *         performed on each conversion. Nevertheless, polling can still
1571   *         be performed on the complete sequence (ADC init
1572   *         parameter "EOCSelection" set to ADC_EOC_SEQ_CONV).
1573   * @param hadc ADC handle
1574   * @param Timeout Timeout value in millisecond.
1575   * @retval HAL status
1576   */
1577 HAL_StatusTypeDef HAL_ADC_PollForConversion(ADC_HandleTypeDef *hadc, uint32_t Timeout)
1578 {
1579   uint32_t tickstart;
1580   uint32_t tmp_Flag_End;
1581   uint32_t tmp_cfgr;
1582   const ADC_TypeDef *tmpADC_Master;
1583   uint32_t tmp_multimode_config = LL_ADC_GetMultimode(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
1584 
1585   /* Check the parameters */
1586   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
1587 
1588   /* If end of conversion selected to end of sequence conversions */
1589   if (hadc->Init.EOCSelection == ADC_EOC_SEQ_CONV)
1590   {
1591     tmp_Flag_End = ADC_FLAG_EOS;
1592   }
1593   /* If end of conversion selected to end of unitary conversion */
1594   else /* ADC_EOC_SINGLE_CONV */
1595   {
1596     /* Verification that ADC configuration is compliant with polling for      */
1597     /* each conversion:                                                       */
1598     /* Particular case is ADC configured in DMA mode and ADC sequencer with   */
1599     /* several ranks and polling for end of each conversion.                  */
1600     /* For code simplicity sake, this particular case is generalized to       */
1601     /* ADC configured in DMA mode and and polling for end of each conversion. */
1602     if ((tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
1603         || (tmp_multimode_config == LL_ADC_MULTI_DUAL_INJ_SIMULT)
1604         || (tmp_multimode_config == LL_ADC_MULTI_DUAL_INJ_ALTERN)
1605        )
1606     {
1607       /* Check DMNGT bit in handle ADC CFGR register */
1608       if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_DMNGT_0) != 0UL)
1609       {
1610         SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
1611         return HAL_ERROR;
1612       }
1613       else
1614       {
1615         tmp_Flag_End = (ADC_FLAG_EOC);
1616       }
1617     }
1618     else
1619     {
1620       /* Check ADC DMA mode in multimode on ADC group regular */
1621       if (LL_ADC_GetMultiDMATransfer(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) != LL_ADC_MULTI_REG_DMA_EACH_ADC)
1622       {
1623         SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
1624         return HAL_ERROR;
1625       }
1626       else
1627       {
1628         tmp_Flag_End = (ADC_FLAG_EOC);
1629       }
1630     }
1631   }
1632 
1633   /* Get tick count */
1634   tickstart = HAL_GetTick();
1635 
1636   /* Wait until End of unitary conversion or sequence conversions flag is raised */
1637   while ((hadc->Instance->ISR & tmp_Flag_End) == 0UL)
1638   {
1639     /* Check if timeout is disabled (set to infinite wait) */
1640     if (Timeout != HAL_MAX_DELAY)
1641     {
1642       if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0UL))
1643       {
1644         /* New check to avoid false timeout detection in case of preemption */
1645         if((hadc->Instance->ISR & tmp_Flag_End) == 0UL)
1646         {
1647           /* Update ADC state machine to timeout */
1648           SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
1649 
1650           /* Process unlocked */
1651           __HAL_UNLOCK(hadc);
1652 
1653           return HAL_TIMEOUT;
1654         }
1655       }
1656     }
1657   }
1658 
1659   /* Update ADC state machine */
1660   SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
1661 
1662   /* Determine whether any further conversion upcoming on group regular       */
1663   /* by external trigger, continuous mode or scan sequence on going.          */
1664   if ((LL_ADC_REG_IsTriggerSourceSWStart(hadc->Instance) != 0UL)
1665       && (hadc->Init.ContinuousConvMode == DISABLE)
1666      )
1667   {
1668     /* Check whether end of sequence is reached */
1669     if (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOS))
1670     {
1671       /* Set ADC state */
1672       CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
1673 
1674       if ((hadc->State & HAL_ADC_STATE_INJ_BUSY) == 0UL)
1675       {
1676         SET_BIT(hadc->State, HAL_ADC_STATE_READY);
1677       }
1678     }
1679   }
1680 
1681   /* Get relevant register CFGR in ADC instance of ADC master or slave        */
1682   /* in function of multimode state (for devices with multimode               */
1683   /* available).                                                              */
1684   if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
1685       || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
1686       || (tmp_multimode_config == LL_ADC_MULTI_DUAL_INJ_SIMULT)
1687       || (tmp_multimode_config == LL_ADC_MULTI_DUAL_INJ_ALTERN)
1688      )
1689   {
1690     /* Retrieve handle ADC CFGR register */
1691     tmp_cfgr = READ_REG(hadc->Instance->CFGR);
1692   }
1693   else
1694   {
1695     /* Retrieve Master ADC CFGR register */
1696     tmpADC_Master = __LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance);
1697     tmp_cfgr = READ_REG(tmpADC_Master->CFGR);
1698   }
1699 
1700   /* Clear polled flag */
1701   if (tmp_Flag_End == ADC_FLAG_EOS)
1702   {
1703     __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOS);
1704   }
1705   else
1706   {
1707     /* Clear end of conversion EOC flag of regular group if low power feature */
1708     /* "LowPowerAutoWait " is disabled, to not interfere with this feature    */
1709     /* until data register is read using function HAL_ADC_GetValue().         */
1710     if (READ_BIT(tmp_cfgr, ADC_CFGR_AUTDLY) == 0UL)
1711     {
1712       __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS));
1713     }
1714   }
1715 
1716   /* Return function status */
1717   return HAL_OK;
1718 }
1719 
1720 /**
1721   * @brief  Poll for ADC event.
1722   * @param hadc ADC handle
1723   * @param EventType the ADC event type.
1724   *          This parameter can be one of the following values:
1725   *            @arg @ref ADC_EOSMP_EVENT  ADC End of Sampling event
1726   *            @arg @ref ADC_AWD1_EVENT   ADC Analog watchdog 1 event (main analog watchdog, present on all STM32 devices)
1727   *            @arg @ref ADC_AWD2_EVENT   ADC Analog watchdog 2 event (additional analog watchdog, not present on all STM32 families)
1728   *            @arg @ref ADC_AWD3_EVENT   ADC Analog watchdog 3 event (additional analog watchdog, not present on all STM32 families)
1729   *            @arg @ref ADC_OVR_EVENT    ADC Overrun event
1730   *            @arg @ref ADC_JQOVF_EVENT  ADC Injected context queue overflow event
1731   * @param Timeout Timeout value in millisecond.
1732   * @note   The relevant flag is cleared if found to be set, except for ADC_FLAG_OVR.
1733   *         Indeed, the latter is reset only if hadc->Init.Overrun field is set
1734   *         to ADC_OVR_DATA_OVERWRITTEN. Otherwise, data register may be potentially overwritten
1735   *         by a new converted data as soon as OVR is cleared.
1736   *         To reset OVR flag once the preserved data is retrieved, the user can resort
1737   *         to macro __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
1738   * @retval HAL status
1739   */
1740 HAL_StatusTypeDef HAL_ADC_PollForEvent(ADC_HandleTypeDef *hadc, uint32_t EventType, uint32_t Timeout)
1741 {
1742   uint32_t tickstart;
1743 
1744   /* Check the parameters */
1745   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
1746   assert_param(IS_ADC_EVENT_TYPE(EventType));
1747 
1748   /* Get tick count */
1749   tickstart = HAL_GetTick();
1750 
1751   /* Check selected event flag */
1752   while (__HAL_ADC_GET_FLAG(hadc, EventType) == 0UL)
1753   {
1754     /* Check if timeout is disabled (set to infinite wait) */
1755     if (Timeout != HAL_MAX_DELAY)
1756     {
1757       if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0UL))
1758       {
1759         /* New check to avoid false timeout detection in case of preemption */
1760         if(__HAL_ADC_GET_FLAG(hadc, EventType) == 0UL)
1761         {
1762           /* Update ADC state machine to timeout */
1763           SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
1764 
1765           /* Process unlocked */
1766           __HAL_UNLOCK(hadc);
1767 
1768           return HAL_TIMEOUT;
1769         }
1770       }
1771     }
1772   }
1773 
1774   switch (EventType)
1775   {
1776     /* End Of Sampling event */
1777     case ADC_EOSMP_EVENT:
1778       /* Set ADC state */
1779       SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOSMP);
1780 
1781       /* Clear the End Of Sampling flag */
1782       __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOSMP);
1783 
1784       break;
1785 
1786     /* Analog watchdog (level out of window) event */
1787     /* Note: In case of several analog watchdog enabled, if needed to know      */
1788     /* which one triggered and on which ADCx, test ADC state of analog watchdog */
1789     /* flags HAL_ADC_STATE_AWD1/2/3 using function "HAL_ADC_GetState()".        */
1790     /* For example:                                                             */
1791     /*  " if ((HAL_ADC_GetState(hadc1) & HAL_ADC_STATE_AWD1) != 0UL) "          */
1792     /*  " if ((HAL_ADC_GetState(hadc1) & HAL_ADC_STATE_AWD2) != 0UL) "          */
1793     /*  " if ((HAL_ADC_GetState(hadc1) & HAL_ADC_STATE_AWD3) != 0UL) "          */
1794 
1795     /* Check analog watchdog 1 flag */
1796     case ADC_AWD_EVENT:
1797       /* Set ADC state */
1798       SET_BIT(hadc->State, HAL_ADC_STATE_AWD1);
1799 
1800       /* Clear ADC analog watchdog flag */
1801       __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD1);
1802 
1803       break;
1804 
1805     /* Check analog watchdog 2 flag */
1806     case ADC_AWD2_EVENT:
1807       /* Set ADC state */
1808       SET_BIT(hadc->State, HAL_ADC_STATE_AWD2);
1809 
1810       /* Clear ADC analog watchdog flag */
1811       __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD2);
1812 
1813       break;
1814 
1815     /* Check analog watchdog 3 flag */
1816     case ADC_AWD3_EVENT:
1817       /* Set ADC state */
1818       SET_BIT(hadc->State, HAL_ADC_STATE_AWD3);
1819 
1820       /* Clear ADC analog watchdog flag */
1821       __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD3);
1822 
1823       break;
1824 
1825     /* Injected context queue overflow event */
1826     case ADC_JQOVF_EVENT:
1827       /* Set ADC state */
1828       SET_BIT(hadc->State, HAL_ADC_STATE_INJ_JQOVF);
1829 
1830       /* Set ADC error code to Injected context queue overflow */
1831       SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_JQOVF);
1832 
1833       /* Clear ADC Injected context queue overflow flag */
1834       __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JQOVF);
1835 
1836       break;
1837 
1838     /* Overrun event */
1839     default: /* Case ADC_OVR_EVENT */
1840       /* If overrun is set to overwrite previous data, overrun event is not     */
1841       /* considered as an error.                                                */
1842       /* (cf ref manual "Managing conversions without using the DMA and without */
1843       /* overrun ")                                                             */
1844       if (hadc->Init.Overrun == ADC_OVR_DATA_PRESERVED)
1845       {
1846         /* Set ADC state */
1847         SET_BIT(hadc->State, HAL_ADC_STATE_REG_OVR);
1848 
1849         /* Set ADC error code to overrun */
1850         SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_OVR);
1851       }
1852       else
1853       {
1854         /* Clear ADC Overrun flag only if Overrun is set to ADC_OVR_DATA_OVERWRITTEN
1855            otherwise, data register is potentially overwritten by new converted data as soon
1856            as OVR is cleared. */
1857         __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
1858       }
1859       break;
1860   }
1861 
1862   /* Return function status */
1863   return HAL_OK;
1864 }
1865 
1866 /**
1867   * @brief  Enable ADC, start conversion of regular group with interruption.
1868   * @note   Interruptions enabled in this function according to initialization
1869   *         setting : EOC (end of conversion), EOS (end of sequence),
1870   *         OVR overrun.
1871   *         Each of these interruptions has its dedicated callback function.
1872   * @note   Case of multimode enabled (when multimode feature is available):
1873   *         HAL_ADC_Start_IT() must be called for ADC Slave first, then for
1874   *         ADC Master.
1875   *         For ADC Slave, ADC is enabled only (conversion is not started).
1876   *         For ADC Master, ADC is enabled and multimode conversion is started.
1877   * @note   To guarantee a proper reset of all interruptions once all the needed
1878   *         conversions are obtained, HAL_ADC_Stop_IT() must be called to ensure
1879   *         a correct stop of the IT-based conversions.
1880   * @note   By default, HAL_ADC_Start_IT() does not enable the End Of Sampling
1881   *         interruption. If required (e.g. in case of oversampling with trigger
1882   *         mode), the user must:
1883   *          1. first clear the EOSMP flag if set with macro __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOSMP)
1884   *          2. then enable the EOSMP interrupt with macro __HAL_ADC_ENABLE_IT(hadc, ADC_IT_EOSMP)
1885   *          before calling HAL_ADC_Start_IT().
1886   * @param hadc ADC handle
1887   * @retval HAL status
1888   */
1889 HAL_StatusTypeDef HAL_ADC_Start_IT(ADC_HandleTypeDef *hadc)
1890 {
1891   HAL_StatusTypeDef tmp_hal_status;
1892   const ADC_TypeDef *tmpADC_Master;
1893   uint32_t tmp_multimode_config = LL_ADC_GetMultimode(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
1894 
1895   /* Check the parameters */
1896   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
1897 
1898   /* Perform ADC enable and conversion start if no conversion is on going */
1899   if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
1900   {
1901     /* Process locked */
1902     __HAL_LOCK(hadc);
1903 
1904     /* Enable the ADC peripheral */
1905     tmp_hal_status = ADC_Enable(hadc);
1906 
1907     /* Start conversion if ADC is effectively enabled */
1908     if (tmp_hal_status == HAL_OK)
1909     {
1910       /* Set ADC state                                                        */
1911       /* - Clear state bitfield related to regular group conversion results   */
1912       /* - Set state bitfield related to regular operation                    */
1913       ADC_STATE_CLR_SET(hadc->State,
1914                         HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP,
1915                         HAL_ADC_STATE_REG_BUSY);
1916 
1917       /* Reset HAL_ADC_STATE_MULTIMODE_SLAVE bit
1918         - if ADC instance is master or if multimode feature is not available
1919         - if multimode setting is disabled (ADC instance slave in independent mode) */
1920       if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
1921           || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
1922          )
1923       {
1924         CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
1925       }
1926 
1927       /* Set ADC error code */
1928       /* Check if a conversion is on going on ADC group injected */
1929       if ((hadc->State & HAL_ADC_STATE_INJ_BUSY) != 0UL)
1930       {
1931         /* Reset ADC error code fields related to regular conversions only */
1932         CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR | HAL_ADC_ERROR_DMA));
1933       }
1934       else
1935       {
1936         /* Reset all ADC error code fields */
1937         ADC_CLEAR_ERRORCODE(hadc);
1938       }
1939 
1940       /* Clear ADC group regular conversion flag and overrun flag               */
1941       /* (To ensure of no unknown state from potential previous ADC operations) */
1942       __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
1943 
1944       /* Process unlocked */
1945       /* Unlock before starting ADC conversions: in case of potential         */
1946       /* interruption, to let the process to ADC IRQ Handler.                 */
1947       __HAL_UNLOCK(hadc);
1948 
1949       /* Disable all interruptions before enabling the desired ones */
1950       __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_EOS | ADC_IT_OVR));
1951 
1952       /* Enable ADC end of conversion interrupt */
1953       switch (hadc->Init.EOCSelection)
1954       {
1955         case ADC_EOC_SEQ_CONV:
1956           __HAL_ADC_ENABLE_IT(hadc, ADC_IT_EOS);
1957           break;
1958         /* case ADC_EOC_SINGLE_CONV */
1959         default:
1960           __HAL_ADC_ENABLE_IT(hadc, ADC_IT_EOC);
1961           break;
1962       }
1963 
1964       /* Enable ADC overrun interrupt */
1965       /* If hadc->Init.Overrun is set to ADC_OVR_DATA_PRESERVED, only then is
1966          ADC_IT_OVR enabled; otherwise data overwrite is considered as normal
1967          behavior and no CPU time is lost for a non-processed interruption */
1968       if (hadc->Init.Overrun == ADC_OVR_DATA_PRESERVED)
1969       {
1970         __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);
1971       }
1972 
1973       /* Enable conversion of regular group.                                  */
1974       /* If software start has been selected, conversion starts immediately.  */
1975       /* If external trigger has been selected, conversion will start at next */
1976       /* trigger event.                                                       */
1977       /* Case of multimode enabled (when multimode feature is available):     */
1978       /*  - if ADC is slave and dual regular conversions are enabled, ADC is  */
1979       /*    enabled only (conversion is not started),                         */
1980       /*  - if ADC is master, ADC is enabled and conversion is started.       */
1981       if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
1982           || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
1983           || (tmp_multimode_config == LL_ADC_MULTI_DUAL_INJ_SIMULT)
1984           || (tmp_multimode_config == LL_ADC_MULTI_DUAL_INJ_ALTERN)
1985          )
1986       {
1987         /* ADC instance is not a multimode slave instance with multimode regular conversions enabled */
1988         if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_JAUTO) != 0UL)
1989         {
1990           ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
1991 
1992           /* Enable as well injected interruptions in case
1993            HAL_ADCEx_InjectedStart_IT() has not been called beforehand. This
1994            allows to start regular and injected conversions when JAUTO is
1995            set with a single call to HAL_ADC_Start_IT() */
1996           switch (hadc->Init.EOCSelection)
1997           {
1998             case ADC_EOC_SEQ_CONV:
1999               __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC);
2000               __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOS);
2001               break;
2002             /* case ADC_EOC_SINGLE_CONV */
2003             default:
2004               __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOS);
2005               __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOC);
2006               break;
2007           }
2008         }
2009 
2010         /* Start ADC group regular conversion */
2011         LL_ADC_REG_StartConversion(hadc->Instance);
2012       }
2013       else
2014       {
2015         /* ADC instance is a multimode slave instance with multimode regular conversions enabled */
2016         SET_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
2017         /* if Master ADC JAUTO bit is set, Slave injected interruptions
2018            are enabled nevertheless (for same reason as above) */
2019         tmpADC_Master = __LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance);
2020         if (READ_BIT(tmpADC_Master->CFGR, ADC_CFGR_JAUTO) != 0UL)
2021         {
2022           /* First, update Slave State in setting HAL_ADC_STATE_INJ_BUSY bit
2023              and in resetting HAL_ADC_STATE_INJ_EOC bit */
2024           ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
2025           /* Next, set Slave injected interruptions */
2026           switch (hadc->Init.EOCSelection)
2027           {
2028             case ADC_EOC_SEQ_CONV:
2029               __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC);
2030               __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOS);
2031               break;
2032             /* case ADC_EOC_SINGLE_CONV */
2033             default:
2034               __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOS);
2035               __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOC);
2036               break;
2037           }
2038         }
2039       }
2040     }
2041     else
2042     {
2043       /* Process unlocked */
2044       __HAL_UNLOCK(hadc);
2045     }
2046 
2047   }
2048   else
2049   {
2050     tmp_hal_status = HAL_BUSY;
2051   }
2052 
2053   /* Return function status */
2054   return tmp_hal_status;
2055 }
2056 
2057 /**
2058   * @brief  Stop ADC conversion of regular group (and injected group in
2059   *         case of auto_injection mode), disable interrution of
2060   *         end-of-conversion, disable ADC peripheral.
2061   * @param hadc ADC handle
2062   * @retval HAL status.
2063   */
2064 HAL_StatusTypeDef HAL_ADC_Stop_IT(ADC_HandleTypeDef *hadc)
2065 {
2066   HAL_StatusTypeDef tmp_hal_status;
2067 
2068   /* Check the parameters */
2069   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
2070 
2071   /* Process locked */
2072   __HAL_LOCK(hadc);
2073 
2074   /* 1. Stop potential conversion on going, on ADC groups regular and injected */
2075   tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_INJECTED_GROUP);
2076 
2077   /* Disable ADC peripheral if conversions are effectively stopped */
2078   if (tmp_hal_status == HAL_OK)
2079   {
2080     /* Disable ADC end of conversion interrupt for regular group */
2081     /* Disable ADC overrun interrupt */
2082     __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_EOS | ADC_IT_OVR));
2083 
2084     /* 2. Disable the ADC peripheral */
2085     tmp_hal_status = ADC_Disable(hadc);
2086 
2087     /* Check if ADC is effectively disabled */
2088     if (tmp_hal_status == HAL_OK)
2089     {
2090       /* Set ADC state */
2091       ADC_STATE_CLR_SET(hadc->State,
2092                         HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
2093                         HAL_ADC_STATE_READY);
2094     }
2095   }
2096 
2097   /* Process unlocked */
2098   __HAL_UNLOCK(hadc);
2099 
2100   /* Return function status */
2101   return tmp_hal_status;
2102 }
2103 
2104 /**
2105   * @brief  Enable ADC, start conversion of regular group and transfer result through DMA.
2106   * @note   Interruptions enabled in this function:
2107   *         overrun (if applicable), DMA half transfer, DMA transfer complete.
2108   *         Each of these interruptions has its dedicated callback function.
2109   * @note   Case of multimode enabled (when multimode feature is available): HAL_ADC_Start_DMA()
2110   *         is designed for single-ADC mode only. For multimode, the dedicated
2111   *         HAL_ADCEx_MultiModeStart_DMA() function must be used.
2112   * @param hadc ADC handle
2113   * @param pData Destination Buffer address.
2114   * @param Length Number of data to be transferred from ADC peripheral to memory
2115   * @retval HAL status.
2116   */
2117 HAL_StatusTypeDef HAL_ADC_Start_DMA(ADC_HandleTypeDef *hadc, uint32_t *pData, uint32_t Length)
2118 {
2119   HAL_StatusTypeDef tmp_hal_status;
2120   uint32_t tmp_multimode_config = LL_ADC_GetMultimode(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
2121 
2122   /* Check the parameters */
2123   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
2124 
2125   /* Perform ADC enable and conversion start if no conversion is on going */
2126   if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
2127   {
2128     /* Process locked */
2129     __HAL_LOCK(hadc);
2130 
2131     /* Ensure that multimode regular conversions are not enabled.   */
2132     /* Otherwise, dedicated API HAL_ADCEx_MultiModeStart_DMA() must be used.  */
2133     if ((tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
2134         || (tmp_multimode_config == LL_ADC_MULTI_DUAL_INJ_SIMULT)
2135         || (tmp_multimode_config == LL_ADC_MULTI_DUAL_INJ_ALTERN)
2136        )
2137     {
2138       /* Enable the ADC peripheral */
2139       tmp_hal_status = ADC_Enable(hadc);
2140 
2141       /* Start conversion if ADC is effectively enabled */
2142       if (tmp_hal_status == HAL_OK)
2143       {
2144         /* Set ADC state                                                        */
2145         /* - Clear state bitfield related to regular group conversion results   */
2146         /* - Set state bitfield related to regular operation                    */
2147         ADC_STATE_CLR_SET(hadc->State,
2148                           HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP,
2149                           HAL_ADC_STATE_REG_BUSY);
2150 
2151         /* Reset HAL_ADC_STATE_MULTIMODE_SLAVE bit
2152           - if ADC instance is master or if multimode feature is not available
2153           - if multimode setting is disabled (ADC instance slave in independent mode) */
2154         if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
2155             || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
2156            )
2157         {
2158           CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
2159         }
2160 
2161         /* Check if a conversion is on going on ADC group injected */
2162         if ((hadc->State & HAL_ADC_STATE_INJ_BUSY) != 0UL)
2163         {
2164           /* Reset ADC error code fields related to regular conversions only */
2165           CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR | HAL_ADC_ERROR_DMA));
2166         }
2167         else
2168         {
2169           /* Reset all ADC error code fields */
2170           ADC_CLEAR_ERRORCODE(hadc);
2171         }
2172 
2173         /* Set the DMA transfer complete callback */
2174         hadc->DMA_Handle->XferCpltCallback = ADC_DMAConvCplt;
2175 
2176         /* Set the DMA half transfer complete callback */
2177         hadc->DMA_Handle->XferHalfCpltCallback = ADC_DMAHalfConvCplt;
2178 
2179         /* Set the DMA error callback */
2180         hadc->DMA_Handle->XferErrorCallback = ADC_DMAError;
2181 
2182 
2183         /* Manage ADC and DMA start: ADC overrun interruption, DMA start,     */
2184         /* ADC start (in case of SW start):                                   */
2185 
2186         /* Clear regular group conversion flag and overrun flag               */
2187         /* (To ensure of no unknown state from potential previous ADC         */
2188         /* operations)                                                        */
2189         __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
2190 
2191         /* Process unlocked */
2192         /* Unlock before starting ADC conversions: in case of potential         */
2193         /* interruption, to let the process to ADC IRQ Handler.                 */
2194         __HAL_UNLOCK(hadc);
2195 
2196         /* With DMA, overrun event is always considered as an error even if
2197            hadc->Init.Overrun is set to ADC_OVR_DATA_OVERWRITTEN. Therefore,
2198            ADC_IT_OVR is enabled. */
2199         __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);
2200 
2201         /* Enable ADC DMA  mode*/
2202 #if defined(ADC_VER_V5_V90)
2203         if (hadc->Instance == ADC3)
2204         {
2205           LL_ADC_REG_SetDMATransferMode(hadc->Instance, ADC3_CFGR_DMACONTREQ((uint32_t)hadc->Init.DMAContinuousRequests));
2206           LL_ADC_EnableDMAReq(hadc->Instance);
2207         }
2208         else
2209         {
2210           LL_ADC_REG_SetDataTransferMode(hadc->Instance, ADC_CFGR_DMACONTREQ((uint32_t)hadc->Init.ConversionDataManagement));
2211         }
2212 
2213 #else
2214         LL_ADC_REG_SetDataTransferMode(hadc->Instance, (uint32_t)hadc->Init.ConversionDataManagement);
2215 #endif
2216 
2217 
2218         /* Start the DMA channel */
2219         tmp_hal_status = HAL_DMA_Start_IT(hadc->DMA_Handle, (uint32_t)&hadc->Instance->DR, (uint32_t)pData, Length);
2220 
2221         /* Enable conversion of regular group.                                  */
2222         /* If software start has been selected, conversion starts immediately.  */
2223         /* If external trigger has been selected, conversion will start at next */
2224         /* trigger event.                                                       */
2225         /* Start ADC group regular conversion */
2226         LL_ADC_REG_StartConversion(hadc->Instance);
2227       }
2228       else
2229       {
2230         /* Process unlocked */
2231         __HAL_UNLOCK(hadc);
2232       }
2233 
2234     }
2235     else
2236     {
2237       tmp_hal_status = HAL_ERROR;
2238       /* Process unlocked */
2239       __HAL_UNLOCK(hadc);
2240     }
2241   }
2242   else
2243   {
2244     tmp_hal_status = HAL_BUSY;
2245   }
2246 
2247   /* Return function status */
2248   return tmp_hal_status;
2249 }
2250 
2251 /**
2252   * @brief  Stop ADC conversion of regular group (and injected group in
2253   *         case of auto_injection mode), disable ADC DMA transfer, disable
2254   *         ADC peripheral.
2255   * @note:  ADC peripheral disable is forcing stop of potential
2256   *         conversion on ADC group injected. If ADC group injected is under use, it
2257   *         should be preliminarily stopped using HAL_ADCEx_InjectedStop function.
2258   * @note   Case of multimode enabled (when multimode feature is available):
2259   *         HAL_ADC_Stop_DMA() function is dedicated to single-ADC mode only.
2260   *         For multimode, the dedicated HAL_ADCEx_MultiModeStop_DMA() API must be used.
2261   * @param hadc ADC handle
2262   * @retval HAL status.
2263   */
2264 HAL_StatusTypeDef HAL_ADC_Stop_DMA(ADC_HandleTypeDef *hadc)
2265 {
2266   HAL_StatusTypeDef tmp_hal_status;
2267 
2268   /* Check the parameters */
2269   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
2270 
2271   /* Process locked */
2272   __HAL_LOCK(hadc);
2273 
2274   /* 1. Stop potential ADC group regular conversion on going */
2275   tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_INJECTED_GROUP);
2276 
2277   /* Disable ADC peripheral if conversions are effectively stopped */
2278   if (tmp_hal_status == HAL_OK)
2279   {
2280     /* Disable ADC DMA (ADC DMA configuration of continuous requests is kept) */
2281     MODIFY_REG(hadc->Instance->CFGR, ADC_CFGR_DMNGT_0 | ADC_CFGR_DMNGT_1, 0UL);
2282 
2283     /* Disable the DMA channel (in case of DMA in circular mode or stop       */
2284     /* while DMA transfer is on going)                                        */
2285     if (hadc->DMA_Handle->State == HAL_DMA_STATE_BUSY)
2286     {
2287       tmp_hal_status = HAL_DMA_Abort(hadc->DMA_Handle);
2288 
2289       /* Check if DMA channel effectively disabled */
2290       if (tmp_hal_status != HAL_OK)
2291       {
2292         /* Update ADC state machine to error */
2293         SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
2294       }
2295     }
2296 
2297     /* Disable ADC overrun interrupt */
2298     __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);
2299 
2300     /* 2. Disable the ADC peripheral */
2301     /* Update "tmp_hal_status" only if DMA channel disabling passed,          */
2302     /* to keep in memory a potential failing status.                          */
2303     if (tmp_hal_status == HAL_OK)
2304     {
2305       tmp_hal_status = ADC_Disable(hadc);
2306     }
2307     else
2308     {
2309       (void)ADC_Disable(hadc);
2310     }
2311 
2312     /* Check if ADC is effectively disabled */
2313     if (tmp_hal_status == HAL_OK)
2314     {
2315       /* Set ADC state */
2316       ADC_STATE_CLR_SET(hadc->State,
2317                         HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
2318                         HAL_ADC_STATE_READY);
2319     }
2320 
2321   }
2322 
2323   /* Process unlocked */
2324   __HAL_UNLOCK(hadc);
2325 
2326   /* Return function status */
2327   return tmp_hal_status;
2328 }
2329 
2330 /**
2331   * @brief  Get ADC regular group conversion result.
2332   * @note   Reading register DR automatically clears ADC flag EOC
2333   *         (ADC group regular end of unitary conversion).
2334   * @note   This function does not clear ADC flag EOS
2335   *         (ADC group regular end of sequence conversion).
2336   *         Occurrence of flag EOS rising:
2337   *          - If sequencer is composed of 1 rank, flag EOS is equivalent
2338   *            to flag EOC.
2339   *          - If sequencer is composed of several ranks, during the scan
2340   *            sequence flag EOC only is raised, at the end of the scan sequence
2341   *            both flags EOC and EOS are raised.
2342   *         To clear this flag, either use function:
2343   *         in programming model IT: @ref HAL_ADC_IRQHandler(), in programming
2344   *         model polling: @ref HAL_ADC_PollForConversion()
2345   *         or @ref __HAL_ADC_CLEAR_FLAG(&hadc, ADC_FLAG_EOS).
2346   * @param hadc ADC handle
2347   * @retval ADC group regular conversion data
2348   */
2349 uint32_t HAL_ADC_GetValue(ADC_HandleTypeDef *hadc)
2350 {
2351   /* Check the parameters */
2352   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
2353 
2354   /* Note: EOC flag is not cleared here by software because automatically     */
2355   /*       cleared by hardware when reading register DR.                      */
2356 
2357   /* Return ADC converted value */
2358   return hadc->Instance->DR;
2359 }
2360 
2361 /**
2362   * @brief  Handle ADC interrupt request.
2363   * @param hadc ADC handle
2364   * @retval None
2365   */
2366 void HAL_ADC_IRQHandler(ADC_HandleTypeDef *hadc)
2367 {
2368   uint32_t overrun_error = 0UL; /* flag set if overrun occurrence has to be considered as an error */
2369   uint32_t tmp_isr = hadc->Instance->ISR;
2370   uint32_t tmp_ier = hadc->Instance->IER;
2371   uint32_t tmp_adc_inj_is_trigger_source_sw_start;
2372   uint32_t tmp_adc_reg_is_trigger_source_sw_start;
2373   uint32_t tmp_cfgr;
2374   const ADC_TypeDef *tmpADC_Master;
2375   uint32_t tmp_multimode_config = LL_ADC_GetMultimode(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
2376 
2377   /* Check the parameters */
2378   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
2379   assert_param(IS_ADC_EOC_SELECTION(hadc->Init.EOCSelection));
2380 
2381   /* ========== Check End of Sampling flag for ADC group regular ========== */
2382   if (((tmp_isr & ADC_FLAG_EOSMP) == ADC_FLAG_EOSMP) && ((tmp_ier & ADC_IT_EOSMP) == ADC_IT_EOSMP))
2383   {
2384     /* Update state machine on end of sampling status if not in error state */
2385     if ((hadc->State & HAL_ADC_STATE_ERROR_INTERNAL) == 0UL)
2386     {
2387       /* Set ADC state */
2388       SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOSMP);
2389     }
2390 
2391     /* End Of Sampling callback */
2392 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
2393     hadc->EndOfSamplingCallback(hadc);
2394 #else
2395     HAL_ADCEx_EndOfSamplingCallback(hadc);
2396 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
2397 
2398     /* Clear regular group conversion flag */
2399     __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOSMP);
2400   }
2401 
2402   /* ====== Check ADC group regular end of unitary conversion sequence conversions ===== */
2403   if ((((tmp_isr & ADC_FLAG_EOC) == ADC_FLAG_EOC) && ((tmp_ier & ADC_IT_EOC) == ADC_IT_EOC)) ||
2404       (((tmp_isr & ADC_FLAG_EOS) == ADC_FLAG_EOS) && ((tmp_ier & ADC_IT_EOS) == ADC_IT_EOS)))
2405   {
2406     /* Update state machine on conversion status if not in error state */
2407     if ((hadc->State & HAL_ADC_STATE_ERROR_INTERNAL) == 0UL)
2408     {
2409       /* Set ADC state */
2410       SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
2411     }
2412 
2413     /* Determine whether any further conversion upcoming on group regular     */
2414     /* by external trigger, continuous mode or scan sequence on going         */
2415     /* to disable interruption.                                               */
2416     if (LL_ADC_REG_IsTriggerSourceSWStart(hadc->Instance) != 0UL)
2417     {
2418       /* Get relevant register CFGR in ADC instance of ADC master or slave    */
2419       /* in function of multimode state (for devices with multimode           */
2420       /* available).                                                          */
2421       if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
2422           || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
2423           || (tmp_multimode_config == LL_ADC_MULTI_DUAL_INJ_SIMULT)
2424           || (tmp_multimode_config == LL_ADC_MULTI_DUAL_INJ_ALTERN)
2425          )
2426       {
2427         /* check CONT bit directly in handle ADC CFGR register */
2428         tmp_cfgr = READ_REG(hadc->Instance->CFGR);
2429       }
2430       else
2431       {
2432         /* else need to check Master ADC CONT bit */
2433         tmpADC_Master = __LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance);
2434         tmp_cfgr = READ_REG(tmpADC_Master->CFGR);
2435       }
2436 
2437       /* Carry on if continuous mode is disabled */
2438       if (READ_BIT(tmp_cfgr, ADC_CFGR_CONT) != ADC_CFGR_CONT)
2439       {
2440         /* If End of Sequence is reached, disable interrupts */
2441         if (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOS))
2442         {
2443           /* Allowed to modify bits ADC_IT_EOC/ADC_IT_EOS only if bit         */
2444           /* ADSTART==0 (no conversion on going)                              */
2445           if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
2446           {
2447             /* Disable ADC end of sequence conversion interrupt */
2448             /* Note: Overrun interrupt was enabled with EOC interrupt in      */
2449             /* HAL_Start_IT(), but is not disabled here because can be used   */
2450             /* by overrun IRQ process below.                                  */
2451             __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC | ADC_IT_EOS);
2452 
2453             /* Set ADC state */
2454             CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
2455 
2456             if ((hadc->State & HAL_ADC_STATE_INJ_BUSY) == 0UL)
2457             {
2458               SET_BIT(hadc->State, HAL_ADC_STATE_READY);
2459             }
2460           }
2461           else
2462           {
2463             /* Change ADC state to error state */
2464             SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
2465 
2466             /* Set ADC error code to ADC peripheral internal error */
2467             SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
2468           }
2469         }
2470       }
2471     }
2472 
2473     /* Conversion complete callback */
2474     /* Note: Into callback function "HAL_ADC_ConvCpltCallback()",             */
2475     /*       to determine if conversion has been triggered from EOC or EOS,   */
2476     /*       possibility to use:                                              */
2477     /*        " if( __HAL_ADC_GET_FLAG(&hadc, ADC_FLAG_EOS)) "                */
2478 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
2479     hadc->ConvCpltCallback(hadc);
2480 #else
2481     HAL_ADC_ConvCpltCallback(hadc);
2482 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
2483 
2484     /* Clear regular group conversion flag */
2485     /* Note: in case of overrun set to ADC_OVR_DATA_PRESERVED, end of         */
2486     /*       conversion flags clear induces the release of the preserved data.*/
2487     /*       Therefore, if the preserved data value is needed, it must be     */
2488     /*       read preliminarily into HAL_ADC_ConvCpltCallback().              */
2489     __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS));
2490   }
2491 
2492   /* ====== Check ADC group injected end of unitary conversion sequence conversions ===== */
2493   if ((((tmp_isr & ADC_FLAG_JEOC) == ADC_FLAG_JEOC) && ((tmp_ier & ADC_IT_JEOC) == ADC_IT_JEOC)) ||
2494       (((tmp_isr & ADC_FLAG_JEOS) == ADC_FLAG_JEOS) && ((tmp_ier & ADC_IT_JEOS) == ADC_IT_JEOS)))
2495   {
2496     /* Update state machine on conversion status if not in error state */
2497     if ((hadc->State & HAL_ADC_STATE_ERROR_INTERNAL) == 0UL)
2498     {
2499       /* Set ADC state */
2500       SET_BIT(hadc->State, HAL_ADC_STATE_INJ_EOC);
2501     }
2502 
2503     /* Retrieve ADC configuration */
2504     tmp_adc_inj_is_trigger_source_sw_start = LL_ADC_INJ_IsTriggerSourceSWStart(hadc->Instance);
2505     tmp_adc_reg_is_trigger_source_sw_start = LL_ADC_REG_IsTriggerSourceSWStart(hadc->Instance);
2506     /* Get relevant register CFGR in ADC instance of ADC master or slave  */
2507     /* in function of multimode state (for devices with multimode         */
2508     /* available).                                                        */
2509     if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
2510         || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
2511         || (tmp_multimode_config == LL_ADC_MULTI_DUAL_REG_SIMULT)
2512         || (tmp_multimode_config == LL_ADC_MULTI_DUAL_REG_INTERL)
2513        )
2514     {
2515       tmp_cfgr = READ_REG(hadc->Instance->CFGR);
2516     }
2517     else
2518     {
2519       tmpADC_Master = __LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance);
2520       tmp_cfgr = READ_REG(tmpADC_Master->CFGR);
2521     }
2522 
2523     /* Disable interruption if no further conversion upcoming by injected     */
2524     /* external trigger or by automatic injected conversion with regular      */
2525     /* group having no further conversion upcoming (same conditions as        */
2526     /* regular group interruption disabling above),                           */
2527     /* and if injected scan sequence is completed.                            */
2528     if (tmp_adc_inj_is_trigger_source_sw_start != 0UL)
2529     {
2530       if ((READ_BIT(tmp_cfgr, ADC_CFGR_JAUTO) == 0UL) ||
2531           ((tmp_adc_reg_is_trigger_source_sw_start != 0UL) &&
2532            (READ_BIT(tmp_cfgr, ADC_CFGR_CONT) == 0UL)))
2533       {
2534         /* If End of Sequence is reached, disable interrupts */
2535         if (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_JEOS))
2536         {
2537           /* Particular case if injected contexts queue is enabled:             */
2538           /* when the last context has been fully processed, JSQR is reset      */
2539           /* by the hardware. Even if no injected conversion is planned to come */
2540           /* (queue empty, triggers are ignored), it can start again            */
2541           /* immediately after setting a new context (JADSTART is still set).   */
2542           /* Therefore, state of HAL ADC injected group is kept to busy.        */
2543           if (READ_BIT(tmp_cfgr, ADC_CFGR_JQM) == 0UL)
2544           {
2545             /* Allowed to modify bits ADC_IT_JEOC/ADC_IT_JEOS only if bit       */
2546             /* JADSTART==0 (no conversion on going)                             */
2547             if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) == 0UL)
2548             {
2549               /* Disable ADC end of sequence conversion interrupt  */
2550               __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC | ADC_IT_JEOS);
2551 
2552               /* Set ADC state */
2553               CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
2554 
2555               if ((hadc->State & HAL_ADC_STATE_REG_BUSY) == 0UL)
2556               {
2557                 SET_BIT(hadc->State, HAL_ADC_STATE_READY);
2558               }
2559             }
2560             else
2561             {
2562               /* Update ADC state machine to error */
2563               SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
2564 
2565               /* Set ADC error code to ADC peripheral internal error */
2566               SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
2567             }
2568           }
2569         }
2570       }
2571     }
2572 
2573     /* Injected Conversion complete callback */
2574     /* Note:  HAL_ADCEx_InjectedConvCpltCallback can resort to
2575               if( __HAL_ADC_GET_FLAG(&hadc, ADC_FLAG_JEOS)) or
2576               if( __HAL_ADC_GET_FLAG(&hadc, ADC_FLAG_JEOC)) to determine whether
2577               interruption has been triggered by end of conversion or end of
2578               sequence.    */
2579 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
2580     hadc->InjectedConvCpltCallback(hadc);
2581 #else
2582     HAL_ADCEx_InjectedConvCpltCallback(hadc);
2583 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
2584 
2585     /* Clear injected group conversion flag */
2586     __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOC | ADC_FLAG_JEOS);
2587   }
2588 
2589   /* ========== Check Analog watchdog 1 flag ========== */
2590   if (((tmp_isr & ADC_FLAG_AWD1) == ADC_FLAG_AWD1) && ((tmp_ier & ADC_IT_AWD1) == ADC_IT_AWD1))
2591   {
2592     /* Set ADC state */
2593     SET_BIT(hadc->State, HAL_ADC_STATE_AWD1);
2594 
2595     /* Level out of window 1 callback */
2596 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
2597     hadc->LevelOutOfWindowCallback(hadc);
2598 #else
2599     HAL_ADC_LevelOutOfWindowCallback(hadc);
2600 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
2601 
2602     /* Clear ADC analog watchdog flag */
2603     __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD1);
2604   }
2605 
2606   /* ========== Check analog watchdog 2 flag ========== */
2607   if (((tmp_isr & ADC_FLAG_AWD2) == ADC_FLAG_AWD2) && ((tmp_ier & ADC_IT_AWD2) == ADC_IT_AWD2))
2608   {
2609     /* Set ADC state */
2610     SET_BIT(hadc->State, HAL_ADC_STATE_AWD2);
2611 
2612     /* Level out of window 2 callback */
2613 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
2614     hadc->LevelOutOfWindow2Callback(hadc);
2615 #else
2616     HAL_ADCEx_LevelOutOfWindow2Callback(hadc);
2617 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
2618 
2619     /* Clear ADC analog watchdog flag */
2620     __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD2);
2621   }
2622 
2623   /* ========== Check analog watchdog 3 flag ========== */
2624   if (((tmp_isr & ADC_FLAG_AWD3) == ADC_FLAG_AWD3) && ((tmp_ier & ADC_IT_AWD3) == ADC_IT_AWD3))
2625   {
2626     /* Set ADC state */
2627     SET_BIT(hadc->State, HAL_ADC_STATE_AWD3);
2628 
2629     /* Level out of window 3 callback */
2630 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
2631     hadc->LevelOutOfWindow3Callback(hadc);
2632 #else
2633     HAL_ADCEx_LevelOutOfWindow3Callback(hadc);
2634 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
2635 
2636     /* Clear ADC analog watchdog flag */
2637     __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD3);
2638   }
2639 
2640   /* ========== Check Overrun flag ========== */
2641   if (((tmp_isr & ADC_FLAG_OVR) == ADC_FLAG_OVR) && ((tmp_ier & ADC_IT_OVR) == ADC_IT_OVR))
2642   {
2643     /* If overrun is set to overwrite previous data (default setting),        */
2644     /* overrun event is not considered as an error.                           */
2645     /* (cf ref manual "Managing conversions without using the DMA and without */
2646     /* overrun ")                                                             */
2647     /* Exception for usage with DMA overrun event always considered as an     */
2648     /* error.                                                                 */
2649     if (hadc->Init.Overrun == ADC_OVR_DATA_PRESERVED)
2650     {
2651       overrun_error = 1UL;
2652     }
2653     else
2654     {
2655       /* Check DMA configuration */
2656       if (tmp_multimode_config != LL_ADC_MULTI_INDEPENDENT)
2657       {
2658         /* Multimode (when feature is available) is enabled,
2659            Common Control Register MDMA bits must be checked. */
2660         if (LL_ADC_GetMultiDMATransfer(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) != LL_ADC_MULTI_REG_DMA_EACH_ADC)
2661         {
2662           overrun_error = 1UL;
2663         }
2664       }
2665       else
2666       {
2667         /* Multimode not set or feature not available or ADC independent */
2668         if ((hadc->Instance->CFGR & ADC_CFGR_DMNGT) != 0UL)
2669         {
2670           overrun_error = 1UL;
2671         }
2672       }
2673     }
2674 
2675     if (overrun_error == 1UL)
2676     {
2677       /* Change ADC state to error state */
2678       SET_BIT(hadc->State, HAL_ADC_STATE_REG_OVR);
2679 
2680       /* Set ADC error code to overrun */
2681       SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_OVR);
2682 
2683       /* Error callback */
2684       /* Note: In case of overrun, ADC conversion data is preserved until     */
2685       /*       flag OVR is reset.                                             */
2686       /*       Therefore, old ADC conversion data can be retrieved in         */
2687       /*       function "HAL_ADC_ErrorCallback()".                            */
2688 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
2689       hadc->ErrorCallback(hadc);
2690 #else
2691       HAL_ADC_ErrorCallback(hadc);
2692 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
2693     }
2694 
2695     /* Clear ADC overrun flag */
2696     __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
2697   }
2698 
2699   /* ========== Check Injected context queue overflow flag ========== */
2700   if (((tmp_isr & ADC_FLAG_JQOVF) == ADC_FLAG_JQOVF) && ((tmp_ier & ADC_IT_JQOVF) == ADC_IT_JQOVF))
2701   {
2702     /* Change ADC state to overrun state */
2703     SET_BIT(hadc->State, HAL_ADC_STATE_INJ_JQOVF);
2704 
2705     /* Set ADC error code to Injected context queue overflow */
2706     SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_JQOVF);
2707 
2708     /* Clear the Injected context queue overflow flag */
2709     __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JQOVF);
2710 
2711     /* Injected context queue overflow callback */
2712 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
2713     hadc->InjectedQueueOverflowCallback(hadc);
2714 #else
2715     HAL_ADCEx_InjectedQueueOverflowCallback(hadc);
2716 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
2717   }
2718 
2719 }
2720 
2721 /**
2722   * @brief  Conversion complete callback in non-blocking mode.
2723   * @param hadc ADC handle
2724   * @retval None
2725   */
2726 __weak void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
2727 {
2728   /* Prevent unused argument(s) compilation warning */
2729   UNUSED(hadc);
2730 
2731   /* NOTE : This function should not be modified. When the callback is needed,
2732             function HAL_ADC_ConvCpltCallback must be implemented in the user file.
2733    */
2734 }
2735 
2736 /**
2737   * @brief  Conversion DMA half-transfer callback in non-blocking mode.
2738   * @param hadc ADC handle
2739   * @retval None
2740   */
2741 __weak void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef *hadc)
2742 {
2743   /* Prevent unused argument(s) compilation warning */
2744   UNUSED(hadc);
2745 
2746   /* NOTE : This function should not be modified. When the callback is needed,
2747             function HAL_ADC_ConvHalfCpltCallback must be implemented in the user file.
2748   */
2749 }
2750 
2751 /**
2752   * @brief  Analog watchdog 1 callback in non-blocking mode.
2753   * @param hadc ADC handle
2754   * @retval None
2755   */
2756 __weak void HAL_ADC_LevelOutOfWindowCallback(ADC_HandleTypeDef *hadc)
2757 {
2758   /* Prevent unused argument(s) compilation warning */
2759   UNUSED(hadc);
2760 
2761   /* NOTE : This function should not be modified. When the callback is needed,
2762             function HAL_ADC_LevelOutOfWindowCallback must be implemented in the user file.
2763   */
2764 }
2765 
2766 /**
2767   * @brief  ADC error callback in non-blocking mode
2768   *         (ADC conversion with interruption or transfer by DMA).
2769   * @note   In case of error due to overrun when using ADC with DMA transfer
2770   *         (HAL ADC handle parameter "ErrorCode" to state "HAL_ADC_ERROR_OVR"):
2771   *         - Reinitialize the DMA using function "HAL_ADC_Stop_DMA()".
2772   *         - If needed, restart a new ADC conversion using function
2773   *           "HAL_ADC_Start_DMA()"
2774   *           (this function is also clearing overrun flag)
2775   * @param hadc ADC handle
2776   * @retval None
2777   */
2778 __weak void HAL_ADC_ErrorCallback(ADC_HandleTypeDef *hadc)
2779 {
2780   /* Prevent unused argument(s) compilation warning */
2781   UNUSED(hadc);
2782 
2783   /* NOTE : This function should not be modified. When the callback is needed,
2784             function HAL_ADC_ErrorCallback must be implemented in the user file.
2785   */
2786 }
2787 
2788 /**
2789   * @}
2790   */
2791 
2792 /** @defgroup ADC_Exported_Functions_Group3 Peripheral Control functions
2793   * @ingroup RTEMSBSPsARMSTM32H7
2794   * @brief    Peripheral Control functions
2795   *
2796 @verbatim
2797  ===============================================================================
2798              ##### Peripheral Control functions #####
2799  ===============================================================================
2800     [..]  This section provides functions allowing to:
2801       (+) Configure channels on regular group
2802       (+) Configure the analog watchdog
2803 
2804 @endverbatim
2805   * @{
2806   */
2807 
2808 /**
2809   * @brief  Configure a channel to be assigned to ADC group regular.
2810   * @note   In case of usage of internal measurement channels:
2811   *         Vbat/VrefInt/TempSensor.
2812   *         These internal paths can be disabled using function
2813   *         HAL_ADC_DeInit().
2814   * @note   Possibility to update parameters on the fly:
2815   *         This function initializes channel into ADC group regular,
2816   *         following calls to this function can be used to reconfigure
2817   *         some parameters of structure "ADC_ChannelConfTypeDef" on the fly,
2818   *         without resetting the ADC.
2819   *         The setting of these parameters is conditioned to ADC state:
2820   *         Refer to comments of structure "ADC_ChannelConfTypeDef".
2821   * @param hadc ADC handle
2822   * @param sConfig Structure of ADC channel assigned to ADC group regular.
2823   * @retval HAL status
2824   */
2825 HAL_StatusTypeDef HAL_ADC_ConfigChannel(ADC_HandleTypeDef *hadc, ADC_ChannelConfTypeDef *sConfig)
2826 {
2827   HAL_StatusTypeDef tmp_hal_status = HAL_OK;
2828   uint32_t tmpOffsetShifted;
2829   uint32_t tmp_config_internal_channel;
2830   __IO uint32_t wait_loop_index = 0;
2831   uint32_t tmp_adc_is_conversion_on_going_regular;
2832   uint32_t tmp_adc_is_conversion_on_going_injected;
2833 
2834   /* Check the parameters */
2835   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
2836   assert_param(IS_ADC_REGULAR_RANK(sConfig->Rank));
2837   assert_param(IS_ADC_SAMPLE_TIME(sConfig->SamplingTime));
2838   assert_param(IS_ADC_SINGLE_DIFFERENTIAL(sConfig->SingleDiff));
2839   assert_param(IS_ADC_OFFSET_NUMBER(sConfig->OffsetNumber));
2840   /* Check offset range according to oversampling setting */
2841   if (hadc->Init.OversamplingMode == ENABLE)
2842   {
2843     assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), sConfig->Offset / (hadc->Init.Oversampling.Ratio + 1U)));
2844   }
2845   else
2846   {
2847 #if defined(ADC_VER_V5_V90)
2848     if (hadc->Instance == ADC3)
2849     {
2850       assert_param(IS_ADC3_RANGE(ADC_GET_RESOLUTION(hadc), sConfig->Offset));
2851     }
2852     else
2853 #endif /* ADC_VER_V5_V90 */
2854     {
2855       assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), sConfig->Offset));
2856     }
2857   }
2858 
2859   /* if ROVSE is set, the value of the OFFSETy_EN bit in ADCx_OFRy register is
2860      ignored (considered as reset) */
2861   assert_param(!((sConfig->OffsetNumber != ADC_OFFSET_NONE) && (hadc->Init.OversamplingMode == ENABLE)));
2862 
2863   /* Verification of channel number */
2864   if (sConfig->SingleDiff != ADC_DIFFERENTIAL_ENDED)
2865   {
2866     assert_param(IS_ADC_CHANNEL(sConfig->Channel));
2867   }
2868   else
2869   {
2870     if (hadc->Instance == ADC1)
2871     {
2872       assert_param(IS_ADC1_DIFF_CHANNEL(sConfig->Channel));
2873     }
2874     if (hadc->Instance == ADC2)
2875     {
2876       assert_param(IS_ADC2_DIFF_CHANNEL(sConfig->Channel));
2877     }
2878 #if defined(ADC3)
2879     /* ADC3 is not available on some STM32H7 products */
2880     if (hadc->Instance == ADC3)
2881     {
2882       assert_param(IS_ADC3_DIFF_CHANNEL(sConfig->Channel));
2883     }
2884 #endif
2885   }
2886 
2887   /* Process locked */
2888   __HAL_LOCK(hadc);
2889 
2890   /* Parameters update conditioned to ADC state:                              */
2891   /* Parameters that can be updated when ADC is disabled or enabled without   */
2892   /* conversion on going on regular group:                                    */
2893   /*  - Channel number                                                        */
2894   /*  - Channel rank                                                          */
2895   if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
2896   {
2897     if (!(__LL_ADC_IS_CHANNEL_INTERNAL(sConfig->Channel)))
2898     {
2899 #if defined(ADC_VER_V5_V90)
2900       if (hadc->Instance != ADC3)
2901       {
2902         /* ADC channels preselection */
2903         hadc->Instance->PCSEL_RES0 |= (1UL << (__LL_ADC_CHANNEL_TO_DECIMAL_NB((uint32_t)sConfig->Channel) & 0x1FUL));
2904       }
2905 #else
2906       /* ADC channels preselection */
2907       hadc->Instance->PCSEL |= (1UL << (__LL_ADC_CHANNEL_TO_DECIMAL_NB((uint32_t)sConfig->Channel) & 0x1FUL));
2908 #endif /* ADC_VER_V5_V90 */
2909     }
2910 
2911     /* Set ADC group regular sequence: channel on the selected scan sequence rank */
2912     LL_ADC_REG_SetSequencerRanks(hadc->Instance, sConfig->Rank, sConfig->Channel);
2913 
2914     /* Parameters update conditioned to ADC state:                              */
2915     /* Parameters that can be updated when ADC is disabled or enabled without   */
2916     /* conversion on going on regular group:                                    */
2917     /*  - Channel sampling time                                                 */
2918     /*  - Channel offset                                                        */
2919     tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
2920     tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
2921     if ((tmp_adc_is_conversion_on_going_regular == 0UL)
2922         && (tmp_adc_is_conversion_on_going_injected == 0UL)
2923        )
2924     {
2925       /* Set sampling time of the selected ADC channel */
2926       LL_ADC_SetChannelSamplingTime(hadc->Instance, sConfig->Channel, sConfig->SamplingTime);
2927 
2928       /* Configure the offset: offset enable/disable, channel, offset value */
2929 
2930       /* Shift the offset with respect to the selected ADC resolution. */
2931       /* Offset has to be left-aligned on bit 11, the LSB (right bits) are set to 0 */
2932 #if defined(ADC_VER_V5_V90)
2933       if (hadc->Instance == ADC3)
2934       {
2935         tmpOffsetShifted = ADC3_OFFSET_SHIFT_RESOLUTION(hadc, (uint32_t)sConfig->Offset);
2936       }
2937       else
2938 #endif /* ADC_VER_V5_V90 */
2939       {
2940         tmpOffsetShifted = ADC_OFFSET_SHIFT_RESOLUTION(hadc, (uint32_t)sConfig->Offset);
2941       }
2942       
2943       if (sConfig->OffsetNumber != ADC_OFFSET_NONE)
2944       {
2945         /* Set ADC selected offset number */
2946         LL_ADC_SetOffset(hadc->Instance, sConfig->OffsetNumber, sConfig->Channel, tmpOffsetShifted);
2947 
2948 #if defined(ADC_VER_V5_V90)
2949         if (hadc->Instance == ADC3)
2950         {
2951           assert_param(IS_ADC3_OFFSET_SIGN(sConfig->OffsetSign));
2952           assert_param(IS_FUNCTIONAL_STATE(sConfig->OffsetSaturation));
2953           /* Set ADC selected offset sign & saturation */
2954           LL_ADC_SetOffsetSign(hadc->Instance, sConfig->OffsetNumber, sConfig->OffsetSign);
2955           LL_ADC_SetOffsetSaturation(hadc->Instance, sConfig->OffsetNumber, (sConfig->OffsetSaturation == ENABLE) ? LL_ADC_OFFSET_SATURATION_ENABLE : LL_ADC_OFFSET_SATURATION_DISABLE);
2956         }
2957         else
2958 #endif /* ADC_VER_V5_V90 */
2959         {
2960           assert_param(IS_FUNCTIONAL_STATE(sConfig->OffsetSignedSaturation));
2961           /* Set ADC selected offset signed saturation */
2962           LL_ADC_SetOffsetSignedSaturation(hadc->Instance, sConfig->OffsetNumber, (sConfig->OffsetSignedSaturation == ENABLE) ? LL_ADC_OFFSET_SIGNED_SATURATION_ENABLE : LL_ADC_OFFSET_SIGNED_SATURATION_DISABLE);
2963 
2964           assert_param(IS_FUNCTIONAL_STATE(sConfig->OffsetRightShift));
2965           /* Set ADC selected offset right shift */
2966           LL_ADC_SetDataRightShift(hadc->Instance, sConfig->OffsetNumber, (sConfig->OffsetRightShift == ENABLE) ? LL_ADC_OFFSET_RSHIFT_ENABLE : LL_ADC_OFFSET_RSHIFT_DISABLE);
2967         }
2968 
2969       }
2970       else
2971       {
2972         /* Scan OFR1, OFR2, OFR3, OFR4 to check if the selected channel is enabled.
2973           If this is the case, offset OFRx is disabled since
2974           sConfig->OffsetNumber = ADC_OFFSET_NONE. */
2975 #if defined(ADC_VER_V5_V90)
2976         if (hadc->Instance == ADC3)
2977         {
2978           if (__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_1)) == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfig->Channel))
2979           {
2980             LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_1, LL_ADC_OFFSET_DISABLE);
2981           }
2982           if (__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_2)) == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfig->Channel))
2983           {
2984             LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_2, LL_ADC_OFFSET_DISABLE);
2985           }
2986           if (__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_3)) == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfig->Channel))
2987           {
2988             LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_3, LL_ADC_OFFSET_DISABLE);
2989           }
2990           if (__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_4)) == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfig->Channel))
2991           {
2992             LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_4, LL_ADC_OFFSET_DISABLE);
2993           }
2994         }
2995         else
2996 #endif /* ADC_VER_V5_V90 */
2997         {
2998           if (((hadc->Instance->OFR1) & ADC_OFR1_OFFSET1_CH) == ADC_OFR_CHANNEL(sConfig->Channel))
2999           {
3000             CLEAR_BIT(hadc->Instance->OFR1, ADC_OFR1_SSATE);
3001           }
3002           if (((hadc->Instance->OFR2) & ADC_OFR2_OFFSET2_CH) == ADC_OFR_CHANNEL(sConfig->Channel))
3003           {
3004             CLEAR_BIT(hadc->Instance->OFR2, ADC_OFR2_SSATE);
3005           }
3006           if (((hadc->Instance->OFR3) & ADC_OFR3_OFFSET3_CH) == ADC_OFR_CHANNEL(sConfig->Channel))
3007           {
3008             CLEAR_BIT(hadc->Instance->OFR3, ADC_OFR3_SSATE);
3009           }
3010           if (((hadc->Instance->OFR4) & ADC_OFR4_OFFSET4_CH) == ADC_OFR_CHANNEL(sConfig->Channel))
3011           {
3012             CLEAR_BIT(hadc->Instance->OFR4, ADC_OFR4_SSATE);
3013           }
3014         }
3015         
3016      }
3017     }
3018 
3019     /* Parameters update conditioned to ADC state:                              */
3020     /* Parameters that can be updated only when ADC is disabled:                */
3021     /*  - Single or differential mode                                           */
3022     /*  - Internal measurement channels: Vbat/VrefInt/TempSensor                */
3023     if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
3024     {
3025       /* Set mode single-ended or differential input of the selected ADC channel */
3026       LL_ADC_SetChannelSingleDiff(hadc->Instance, sConfig->Channel, sConfig->SingleDiff);
3027 
3028       /* Configuration of differential mode */
3029       if (sConfig->SingleDiff == ADC_DIFFERENTIAL_ENDED)
3030       {
3031         /* Set sampling time of the selected ADC channel */
3032         /* Note: ADC channel number masked with value "0x1F" to ensure shift value within 32 bits range */
3033         LL_ADC_SetChannelSamplingTime(hadc->Instance,
3034                                       (uint32_t)(__LL_ADC_DECIMAL_NB_TO_CHANNEL((__LL_ADC_CHANNEL_TO_DECIMAL_NB((uint32_t)sConfig->Channel) + 1UL) & 0x1FUL)),
3035                                       sConfig->SamplingTime);
3036       }
3037 
3038       /* Management of internal measurement channels: Vbat/VrefInt/TempSensor.  */
3039       /* If internal channel selected, enable dedicated internal buffers and    */
3040       /* paths.                                                                 */
3041       /* Note: these internal measurement paths can be disabled using           */
3042       /* HAL_ADC_DeInit().                                                      */
3043 
3044       if (__LL_ADC_IS_CHANNEL_INTERNAL(sConfig->Channel))
3045       {
3046         /* Configuration of common ADC parameters                                 */
3047 
3048         tmp_config_internal_channel = LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
3049 
3050         /* Software is allowed to change common parameters only when all ADCs   */
3051         /* of the common group are disabled.                                    */
3052         if (__LL_ADC_IS_ENABLED_ALL_COMMON_INSTANCE(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) == 0UL)
3053         {
3054           /* If the requested internal measurement path has already been enabled, */
3055           /* bypass the configuration processing.                                 */
3056           if ((sConfig->Channel == ADC_CHANNEL_TEMPSENSOR) && ((tmp_config_internal_channel & LL_ADC_PATH_INTERNAL_TEMPSENSOR) == 0UL))
3057           {
3058             if (ADC_TEMPERATURE_SENSOR_INSTANCE(hadc))
3059             {
3060               LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), LL_ADC_PATH_INTERNAL_TEMPSENSOR | tmp_config_internal_channel);
3061 
3062               /* Delay for temperature sensor stabilization time */
3063               /* Wait loop initialization and execution */
3064               /* Note: Variable divided by 2 to compensate partially              */
3065               /*       CPU processing cycles, scaling in us split to not          */
3066               /*       exceed 32 bits register capacity and handle low frequency. */
3067               wait_loop_index = ((LL_ADC_DELAY_TEMPSENSOR_STAB_US / 10UL) * ((SystemCoreClock / (100000UL * 2UL)) + 1UL));
3068               while (wait_loop_index != 0UL)
3069               {
3070                 wait_loop_index--;
3071               }
3072             }
3073           }
3074           else if ((sConfig->Channel == ADC_CHANNEL_VBAT) && ((tmp_config_internal_channel & LL_ADC_PATH_INTERNAL_VBAT) == 0UL))
3075           {
3076             if (ADC_BATTERY_VOLTAGE_INSTANCE(hadc))
3077             {
3078               LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), LL_ADC_PATH_INTERNAL_VBAT | tmp_config_internal_channel);
3079             }
3080           }
3081           else if ((sConfig->Channel == ADC_CHANNEL_VREFINT) && ((tmp_config_internal_channel & LL_ADC_PATH_INTERNAL_VREFINT) == 0UL))
3082           {
3083             if (ADC_VREFINT_INSTANCE(hadc))
3084             {
3085               LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), LL_ADC_PATH_INTERNAL_VREFINT | tmp_config_internal_channel);
3086             }
3087           }
3088           else
3089           {
3090             /* nothing to do */
3091           }
3092         }
3093         /* If the requested internal measurement path has already been          */
3094         /* enabled and other ADC of the common group are enabled, internal      */
3095         /* measurement paths cannot be enabled.                                 */
3096         else
3097         {
3098           /* Update ADC state machine to error */
3099           SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
3100 
3101           tmp_hal_status = HAL_ERROR;
3102         }
3103       }
3104     }
3105   }
3106 
3107   /* If a conversion is on going on regular group, no update on regular       */
3108   /* channel could be done on neither of the channel configuration structure  */
3109   /* parameters.                                                              */
3110   else
3111   {
3112     /* Update ADC state machine to error */
3113     SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
3114 
3115     tmp_hal_status = HAL_ERROR;
3116   }
3117 
3118   /* Process unlocked */
3119   __HAL_UNLOCK(hadc);
3120 
3121   /* Return function status */
3122   return tmp_hal_status;
3123 }
3124 
3125 /**
3126   * @brief  Configure the analog watchdog.
3127   * @note   Possibility to update parameters on the fly:
3128   *         This function initializes the selected analog watchdog, successive
3129   *         calls to this function can be used to reconfigure some parameters
3130   *         of structure "ADC_AnalogWDGConfTypeDef" on the fly, without resetting
3131   *         the ADC.
3132   *         The setting of these parameters is conditioned to ADC state.
3133   *         For parameters constraints, see comments of structure
3134   *         "ADC_AnalogWDGConfTypeDef".
3135   * @note   On this STM32 series, analog watchdog thresholds cannot be modified
3136   *         while ADC conversion is on going.
3137   * @param hadc ADC handle
3138   * @param AnalogWDGConfig Structure of ADC analog watchdog configuration
3139   * @retval HAL status
3140   */
3141 HAL_StatusTypeDef HAL_ADC_AnalogWDGConfig(ADC_HandleTypeDef *hadc, ADC_AnalogWDGConfTypeDef *AnalogWDGConfig)
3142 {
3143   HAL_StatusTypeDef tmp_hal_status = HAL_OK;
3144   uint32_t tmpAWDHighThresholdShifted;
3145   uint32_t tmpAWDLowThresholdShifted;
3146   uint32_t tmp_adc_is_conversion_on_going_regular;
3147   uint32_t tmp_adc_is_conversion_on_going_injected;
3148 
3149   /* Check the parameters */
3150   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
3151   assert_param(IS_ADC_ANALOG_WATCHDOG_NUMBER(AnalogWDGConfig->WatchdogNumber));
3152   assert_param(IS_ADC_ANALOG_WATCHDOG_MODE(AnalogWDGConfig->WatchdogMode));
3153   assert_param(IS_FUNCTIONAL_STATE(AnalogWDGConfig->ITMode));
3154 
3155   if ((AnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_REG)     ||
3156       (AnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_INJEC)   ||
3157       (AnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_REGINJEC))
3158   {
3159     assert_param(IS_ADC_CHANNEL(AnalogWDGConfig->Channel));
3160   }
3161 
3162 #if defined(ADC_VER_V5_V90)
3163 
3164   if (hadc->Instance == ADC3)
3165   {
3166     /* Verify thresholds range */
3167     if (hadc->Init.OversamplingMode == ENABLE)
3168     {
3169       /* Case of oversampling enabled: thresholds are compared to oversampling
3170          intermediate computation (after ratio, before shift application) */
3171       assert_param(IS_ADC3_RANGE(ADC_GET_RESOLUTION(hadc), AnalogWDGConfig->HighThreshold / (hadc->Init.Oversampling.Ratio + 1UL)));
3172       assert_param(IS_ADC3_RANGE(ADC_GET_RESOLUTION(hadc), AnalogWDGConfig->LowThreshold / (hadc->Init.Oversampling.Ratio + 1UL)));
3173     }
3174     else
3175     {
3176       /* Verify if thresholds are within the selected ADC resolution */
3177       assert_param(IS_ADC3_RANGE(ADC_GET_RESOLUTION(hadc), AnalogWDGConfig->HighThreshold));
3178       assert_param(IS_ADC3_RANGE(ADC_GET_RESOLUTION(hadc), AnalogWDGConfig->LowThreshold));
3179     }
3180   }
3181   else
3182 #endif /* ADC_VER_V5_V90 */
3183   {
3184     /* Verify thresholds range */
3185     if (hadc->Init.OversamplingMode == ENABLE)
3186     {
3187       /* Case of oversampling enabled: thresholds are compared to oversampling
3188          intermediate computation (after ratio, before shift application) */
3189       assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), AnalogWDGConfig->HighThreshold / (hadc->Init.Oversampling.Ratio + 1UL)));
3190       assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), AnalogWDGConfig->LowThreshold / (hadc->Init.Oversampling.Ratio + 1UL)));
3191     }
3192     else
3193     {
3194       /* Verify if thresholds are within the selected ADC resolution */
3195       assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), AnalogWDGConfig->HighThreshold));
3196       assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), AnalogWDGConfig->LowThreshold));
3197     }
3198   }
3199 
3200   /* Process locked */
3201   __HAL_LOCK(hadc);
3202 
3203   /* Parameters update conditioned to ADC state:                              */
3204   /* Parameters that can be updated when ADC is disabled or enabled without   */
3205   /* conversion on going on ADC groups regular and injected:                  */
3206   /*  - Analog watchdog channels                                              */
3207   /*  - Analog watchdog thresholds                                            */
3208   tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
3209   tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
3210   if ((tmp_adc_is_conversion_on_going_regular == 0UL)
3211       && (tmp_adc_is_conversion_on_going_injected == 0UL)
3212      )
3213   {
3214     /* Analog watchdog configuration */
3215     if (AnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_1)
3216     {
3217       /* Configuration of analog watchdog:                                    */
3218       /*  - Set the analog watchdog enable mode: one or overall group of      */
3219       /*    channels, on groups regular and-or injected.                      */
3220       switch (AnalogWDGConfig->WatchdogMode)
3221       {
3222         case ADC_ANALOGWATCHDOG_SINGLE_REG:
3223           LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, __LL_ADC_ANALOGWD_CHANNEL_GROUP(AnalogWDGConfig->Channel,
3224                                           LL_ADC_GROUP_REGULAR));
3225           break;
3226 
3227         case ADC_ANALOGWATCHDOG_SINGLE_INJEC:
3228           LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, __LL_ADC_ANALOGWD_CHANNEL_GROUP(AnalogWDGConfig->Channel,
3229                                           LL_ADC_GROUP_INJECTED));
3230           break;
3231 
3232         case ADC_ANALOGWATCHDOG_SINGLE_REGINJEC:
3233           LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, __LL_ADC_ANALOGWD_CHANNEL_GROUP(AnalogWDGConfig->Channel,
3234                                           LL_ADC_GROUP_REGULAR_INJECTED));
3235           break;
3236 
3237         case ADC_ANALOGWATCHDOG_ALL_REG:
3238           LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, LL_ADC_AWD_ALL_CHANNELS_REG);
3239           break;
3240 
3241         case ADC_ANALOGWATCHDOG_ALL_INJEC:
3242           LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, LL_ADC_AWD_ALL_CHANNELS_INJ);
3243           break;
3244 
3245         case ADC_ANALOGWATCHDOG_ALL_REGINJEC:
3246           LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, LL_ADC_AWD_ALL_CHANNELS_REG_INJ);
3247           break;
3248 
3249         default: /* ADC_ANALOGWATCHDOG_NONE */
3250           LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, LL_ADC_AWD_DISABLE);
3251           break;
3252       }
3253 
3254       /* Shift the offset in function of the selected ADC resolution:         */
3255       /* Thresholds have to be left-aligned on bit 11, the LSB (right bits)   */
3256       /* are set to 0                                                         */
3257       tmpAWDHighThresholdShifted = ADC_AWD1THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->HighThreshold);
3258       tmpAWDLowThresholdShifted  = ADC_AWD1THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->LowThreshold);
3259 
3260       /* Set the high and low thresholds */
3261 #if defined(ADC_VER_V5_V90)
3262       if (hadc->Instance == ADC3)
3263       {
3264         MODIFY_REG(hadc->Instance->LTR1_TR1,
3265                    ADC3_TR1_AWDFILT,
3266                    AnalogWDGConfig->FilteringConfig);
3267         MODIFY_REG(hadc->Instance->LTR1_TR1,  ADC3_TR1_LT1, tmpAWDLowThresholdShifted);
3268         MODIFY_REG(hadc->Instance->LTR1_TR1,  ADC3_TR1_HT1, (tmpAWDHighThresholdShifted << ADC3_TR1_HT1_Pos));
3269       }
3270       else
3271       {
3272 
3273         MODIFY_REG(hadc->Instance->LTR1_TR1,  ADC_LTR_LT, tmpAWDLowThresholdShifted);
3274         MODIFY_REG(hadc->Instance->HTR1_TR2,  ADC_HTR_HT, tmpAWDHighThresholdShifted);
3275       }
3276 #else
3277       MODIFY_REG(hadc->Instance->LTR1,  ADC_LTR_LT, tmpAWDLowThresholdShifted);
3278       MODIFY_REG(hadc->Instance->HTR1,  ADC_HTR_HT, tmpAWDHighThresholdShifted);
3279 #endif
3280 
3281       /* Update state, clear previous result related to AWD1 */
3282       CLEAR_BIT(hadc->State, HAL_ADC_STATE_AWD1);
3283 
3284       /* Clear flag ADC analog watchdog */
3285       /* Note: Flag cleared Clear the ADC Analog watchdog flag to be ready  */
3286       /* to use for HAL_ADC_IRQHandler() or HAL_ADC_PollForEvent()          */
3287       /* (in case left enabled by previous ADC operations).                 */
3288       LL_ADC_ClearFlag_AWD1(hadc->Instance);
3289 
3290       /* Configure ADC analog watchdog interrupt */
3291       if (AnalogWDGConfig->ITMode == ENABLE)
3292       {
3293         LL_ADC_EnableIT_AWD1(hadc->Instance);
3294       }
3295       else
3296       {
3297         LL_ADC_DisableIT_AWD1(hadc->Instance);
3298       }
3299     }
3300     /* Case of ADC_ANALOGWATCHDOG_2 or ADC_ANALOGWATCHDOG_3 */
3301     else
3302     {
3303       switch (AnalogWDGConfig->WatchdogMode)
3304       {
3305         case ADC_ANALOGWATCHDOG_SINGLE_REG:
3306         case ADC_ANALOGWATCHDOG_SINGLE_INJEC:
3307         case ADC_ANALOGWATCHDOG_SINGLE_REGINJEC:
3308           /* Update AWD by bitfield to keep the possibility to monitor        */
3309           /* several channels by successive calls of this function.           */
3310           if (AnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_2)
3311           {
3312             SET_BIT(hadc->Instance->AWD2CR, (1UL << (__LL_ADC_CHANNEL_TO_DECIMAL_NB(AnalogWDGConfig->Channel) & 0x1FUL)));
3313           }
3314           else
3315           {
3316             SET_BIT(hadc->Instance->AWD3CR, (1UL << (__LL_ADC_CHANNEL_TO_DECIMAL_NB(AnalogWDGConfig->Channel) & 0x1FUL)));
3317           }
3318           break;
3319 
3320         case ADC_ANALOGWATCHDOG_ALL_REG:
3321         case ADC_ANALOGWATCHDOG_ALL_INJEC:
3322         case ADC_ANALOGWATCHDOG_ALL_REGINJEC:
3323 
3324 #if defined(ADC_VER_V5_V90)
3325           if (hadc->Instance == ADC3)
3326           {
3327 
3328             LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, AnalogWDGConfig->WatchdogNumber, LL_ADC_AWD_ALL_CHANNELS_REG_INJ);
3329 
3330           }
3331           else
3332           {
3333 #endif  /*ADC_VER_V5_V90*/
3334             /* Update AWD by bitfield to keep the possibility to monitor        */
3335             /* several channels by successive calls of this function.           */
3336             if (AnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_2)
3337             {
3338               SET_BIT(hadc->Instance->AWD2CR, (1UL << (__LL_ADC_CHANNEL_TO_DECIMAL_NB(AnalogWDGConfig->Channel) & 0x1FUL)));
3339             }
3340             else
3341             {
3342               SET_BIT(hadc->Instance->AWD3CR, (1UL << (__LL_ADC_CHANNEL_TO_DECIMAL_NB(AnalogWDGConfig->Channel) & 0x1FUL)));
3343             }
3344 #if defined(ADC_VER_V5_V90)
3345           }
3346 #endif  /*ADC_VER_V5_V90*/
3347           break;
3348 
3349         default: /* ADC_ANALOGWATCHDOG_NONE */
3350           LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, AnalogWDGConfig->WatchdogNumber, LL_ADC_AWD_DISABLE);
3351           break;
3352       }
3353 
3354       /* Shift the thresholds in function of the selected ADC resolution      */
3355       /* have to be left-aligned on bit 15, the LSB (right bits) are set to 0 */
3356       tmpAWDHighThresholdShifted = ADC_AWD23THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->HighThreshold);
3357       tmpAWDLowThresholdShifted  = ADC_AWD23THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->LowThreshold);
3358 
3359 #if defined(ADC_VER_V5_V90)
3360       if (hadc->Instance == ADC3)
3361       {
3362 
3363         /* Analog watchdog thresholds configuration */
3364         if (AnalogWDGConfig->WatchdogNumber != ADC_ANALOGWATCHDOG_1)
3365         {
3366           /* Shift the offset with respect to the selected ADC resolution:        */
3367           /* Thresholds have to be left-aligned on bit 7, the LSB (right bits)    */
3368           /* are set to 0.                                                        */
3369           tmpAWDHighThresholdShifted = ADC_AWD23THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->HighThreshold);
3370           tmpAWDLowThresholdShifted  = ADC_AWD23THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->LowThreshold);
3371         }
3372 
3373         /* Set ADC analog watchdog thresholds value of both thresholds high and low */
3374         LL_ADC_ConfigAnalogWDThresholds(hadc->Instance, AnalogWDGConfig->WatchdogNumber, tmpAWDHighThresholdShifted, tmpAWDLowThresholdShifted);
3375 
3376 
3377       }
3378       else
3379       {
3380 
3381         if (AnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_2)
3382         {
3383           /* Set ADC analog watchdog thresholds value of both thresholds high and low */
3384           MODIFY_REG(hadc->Instance->LTR2_DIFSEL,  ADC_LTR_LT, tmpAWDLowThresholdShifted);
3385           MODIFY_REG(hadc->Instance->HTR2_CALFACT,  ADC_HTR_HT, tmpAWDHighThresholdShifted);
3386         }
3387         else
3388         {
3389           /* Set ADC analog watchdog thresholds value of both thresholds high and low */
3390           MODIFY_REG(hadc->Instance->LTR3_RES10,  ADC_LTR_LT, tmpAWDLowThresholdShifted);
3391           MODIFY_REG(hadc->Instance->HTR3_RES11,  ADC_HTR_HT, tmpAWDHighThresholdShifted);
3392         }
3393       }
3394 #else
3395       if (AnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_2)
3396       {
3397         /* Set ADC analog watchdog thresholds value of both thresholds high and low */
3398         MODIFY_REG(hadc->Instance->LTR2,  ADC_LTR_LT, tmpAWDLowThresholdShifted);
3399         MODIFY_REG(hadc->Instance->HTR2,  ADC_HTR_HT, tmpAWDHighThresholdShifted);
3400       }
3401       else
3402       {
3403         /* Set ADC analog watchdog thresholds value of both thresholds high and low */
3404         MODIFY_REG(hadc->Instance->LTR3,  ADC_LTR_LT, tmpAWDLowThresholdShifted);
3405         MODIFY_REG(hadc->Instance->HTR3,  ADC_HTR_HT, tmpAWDHighThresholdShifted);
3406       }
3407 
3408 #endif
3409       if (AnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_2)
3410       {
3411         /* Update state, clear previous result related to AWD2 */
3412         CLEAR_BIT(hadc->State, HAL_ADC_STATE_AWD2);
3413 
3414         /* Clear flag ADC analog watchdog */
3415         /* Note: Flag cleared Clear the ADC Analog watchdog flag to be ready  */
3416         /* to use for HAL_ADC_IRQHandler() or HAL_ADC_PollForEvent()          */
3417         /* (in case left enabled by previous ADC operations).                 */
3418         LL_ADC_ClearFlag_AWD2(hadc->Instance);
3419 
3420         /* Configure ADC analog watchdog interrupt */
3421         if (AnalogWDGConfig->ITMode == ENABLE)
3422         {
3423           LL_ADC_EnableIT_AWD2(hadc->Instance);
3424         }
3425         else
3426         {
3427           LL_ADC_DisableIT_AWD2(hadc->Instance);
3428         }
3429       }
3430       /* (AnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_3) */
3431       else
3432       {
3433         /* Update state, clear previous result related to AWD3 */
3434         CLEAR_BIT(hadc->State, HAL_ADC_STATE_AWD3);
3435 
3436         /* Clear flag ADC analog watchdog */
3437         /* Note: Flag cleared Clear the ADC Analog watchdog flag to be ready  */
3438         /* to use for HAL_ADC_IRQHandler() or HAL_ADC_PollForEvent()          */
3439         /* (in case left enabled by previous ADC operations).                 */
3440         LL_ADC_ClearFlag_AWD3(hadc->Instance);
3441 
3442         /* Configure ADC analog watchdog interrupt */
3443         if (AnalogWDGConfig->ITMode == ENABLE)
3444         {
3445           LL_ADC_EnableIT_AWD3(hadc->Instance);
3446         }
3447         else
3448         {
3449           LL_ADC_DisableIT_AWD3(hadc->Instance);
3450         }
3451       }
3452     }
3453 
3454   }
3455   /* If a conversion is on going on ADC group regular or injected, no update  */
3456   /* could be done on neither of the AWD configuration structure parameters.  */
3457   else
3458   {
3459     /* Update ADC state machine to error */
3460     SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
3461 
3462     tmp_hal_status = HAL_ERROR;
3463   }
3464   /* Process unlocked */
3465   __HAL_UNLOCK(hadc);
3466 
3467   /* Return function status */
3468   return tmp_hal_status;
3469 }
3470 
3471 
3472 /**
3473   * @}
3474   */
3475 
3476 /** @defgroup ADC_Exported_Functions_Group4 Peripheral State functions
3477   * @ingroup RTEMSBSPsARMSTM32H7
3478   *  @brief    ADC Peripheral State functions
3479   *
3480 @verbatim
3481  ===============================================================================
3482             ##### Peripheral state and errors functions #####
3483  ===============================================================================
3484     [..]
3485     This subsection provides functions to get in run-time the status of the
3486     peripheral.
3487       (+) Check the ADC state
3488       (+) Check the ADC error code
3489 
3490 @endverbatim
3491   * @{
3492   */
3493 
3494 /**
3495   * @brief  Return the ADC handle state.
3496   * @note   ADC state machine is managed by bitfields, ADC status must be
3497   *         compared with states bits.
3498   *         For example:
3499   *           " if ((HAL_ADC_GetState(hadc1) & HAL_ADC_STATE_REG_BUSY) != 0UL) "
3500   *           " if ((HAL_ADC_GetState(hadc1) & HAL_ADC_STATE_AWD1) != 0UL) "
3501   * @param hadc ADC handle
3502   * @retval ADC handle state (bitfield on 32 bits)
3503   */
3504 uint32_t HAL_ADC_GetState(ADC_HandleTypeDef *hadc)
3505 {
3506   /* Check the parameters */
3507   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
3508 
3509   /* Return ADC handle state */
3510   return hadc->State;
3511 }
3512 
3513 /**
3514   * @brief  Return the ADC error code.
3515   * @param hadc ADC handle
3516   * @retval ADC error code (bitfield on 32 bits)
3517   */
3518 uint32_t HAL_ADC_GetError(ADC_HandleTypeDef *hadc)
3519 {
3520   /* Check the parameters */
3521   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
3522 
3523   return hadc->ErrorCode;
3524 }
3525 
3526 /**
3527   * @}
3528   */
3529 
3530 /**
3531   * @}
3532   */
3533 
3534 /** @defgroup ADC_Private_Functions ADC Private Functions
3535   * @ingroup RTEMSBSPsARMSTM32H7
3536   * @{
3537   */
3538 
3539 /**
3540   * @brief  Stop ADC conversion.
3541   * @param hadc ADC handle
3542   * @param ConversionGroup ADC group regular and/or injected.
3543   *          This parameter can be one of the following values:
3544   *            @arg @ref ADC_REGULAR_GROUP           ADC regular conversion type.
3545   *            @arg @ref ADC_INJECTED_GROUP          ADC injected conversion type.
3546   *            @arg @ref ADC_REGULAR_INJECTED_GROUP  ADC regular and injected conversion type.
3547   * @retval HAL status.
3548   */
3549 HAL_StatusTypeDef ADC_ConversionStop(ADC_HandleTypeDef *hadc, uint32_t ConversionGroup)
3550 {
3551   uint32_t tickstart;
3552   uint32_t Conversion_Timeout_CPU_cycles = 0UL;
3553   uint32_t conversion_group_reassigned = ConversionGroup;
3554   uint32_t tmp_ADC_CR_ADSTART_JADSTART;
3555   uint32_t tmp_adc_is_conversion_on_going_regular;
3556   uint32_t tmp_adc_is_conversion_on_going_injected;
3557 
3558   /* Check the parameters */
3559   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
3560   assert_param(IS_ADC_CONVERSION_GROUP(ConversionGroup));
3561 
3562   /* Verification if ADC is not already stopped (on regular and injected      */
3563   /* groups) to bypass this function if not needed.                           */
3564   tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
3565   tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
3566   if ((tmp_adc_is_conversion_on_going_regular != 0UL)
3567       || (tmp_adc_is_conversion_on_going_injected != 0UL)
3568      )
3569   {
3570     /* Particular case of continuous auto-injection mode combined with        */
3571     /* auto-delay mode.                                                       */
3572     /* In auto-injection mode, regular group stop ADC_CR_ADSTP is used (not   */
3573     /* injected group stop ADC_CR_JADSTP).                                    */
3574     /* Procedure to be followed: Wait until JEOS=1, clear JEOS, set ADSTP=1   */
3575     /* (see reference manual).                                                */
3576     if (((hadc->Instance->CFGR & ADC_CFGR_JAUTO) != 0UL)
3577         && (hadc->Init.ContinuousConvMode == ENABLE)
3578         && (hadc->Init.LowPowerAutoWait == ENABLE)
3579        )
3580     {
3581       /* Use stop of regular group */
3582       conversion_group_reassigned = ADC_REGULAR_GROUP;
3583 
3584       /* Wait until JEOS=1 (maximum Timeout: 4 injected conversions) */
3585       while (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_JEOS) == 0UL)
3586       {
3587         if (Conversion_Timeout_CPU_cycles >= (ADC_CONVERSION_TIME_MAX_CPU_CYCLES * 4UL))
3588         {
3589           /* Update ADC state machine to error */
3590           SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
3591 
3592           /* Set ADC error code to ADC peripheral internal error */
3593           SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
3594 
3595           return HAL_ERROR;
3596         }
3597         Conversion_Timeout_CPU_cycles ++;
3598       }
3599 
3600       /* Clear JEOS */
3601       __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOS);
3602     }
3603 
3604     /* Stop potential conversion on going on ADC group regular */
3605     if (conversion_group_reassigned != ADC_INJECTED_GROUP)
3606     {
3607       /* Software is allowed to set ADSTP only when ADSTART=1 and ADDIS=0 */
3608       if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) != 0UL)
3609       {
3610         if (LL_ADC_IsDisableOngoing(hadc->Instance) == 0UL)
3611         {
3612           /* Stop ADC group regular conversion */
3613           LL_ADC_REG_StopConversion(hadc->Instance);
3614         }
3615       }
3616     }
3617 
3618     /* Stop potential conversion on going on ADC group injected */
3619     if (conversion_group_reassigned != ADC_REGULAR_GROUP)
3620     {
3621       /* Software is allowed to set JADSTP only when JADSTART=1 and ADDIS=0 */
3622       if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) != 0UL)
3623       {
3624         if (LL_ADC_IsDisableOngoing(hadc->Instance) == 0UL)
3625         {
3626           /* Stop ADC group injected conversion */
3627           LL_ADC_INJ_StopConversion(hadc->Instance);
3628         }
3629       }
3630     }
3631 
3632     /* Selection of start and stop bits with respect to the regular or injected group */
3633     switch (conversion_group_reassigned)
3634     {
3635       case ADC_REGULAR_INJECTED_GROUP:
3636         tmp_ADC_CR_ADSTART_JADSTART = (ADC_CR_ADSTART | ADC_CR_JADSTART);
3637         break;
3638       case ADC_INJECTED_GROUP:
3639         tmp_ADC_CR_ADSTART_JADSTART = ADC_CR_JADSTART;
3640         break;
3641       /* Case ADC_REGULAR_GROUP only*/
3642       default:
3643         tmp_ADC_CR_ADSTART_JADSTART = ADC_CR_ADSTART;
3644         break;
3645     }
3646 
3647     /* Wait for conversion effectively stopped */
3648     tickstart = HAL_GetTick();
3649 
3650     while ((hadc->Instance->CR & tmp_ADC_CR_ADSTART_JADSTART) != 0UL)
3651     {
3652       if ((HAL_GetTick() - tickstart) > ADC_STOP_CONVERSION_TIMEOUT)
3653       {
3654         /* New check to avoid false timeout detection in case of preemption */
3655         if((hadc->Instance->CR & tmp_ADC_CR_ADSTART_JADSTART) != 0UL)
3656         {
3657           /* Update ADC state machine to error */
3658           SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
3659 
3660           /* Set ADC error code to ADC peripheral internal error */
3661           SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
3662 
3663           return HAL_ERROR;
3664         }
3665       }
3666     }
3667 
3668   }
3669 
3670   /* Return HAL status */
3671   return HAL_OK;
3672 }
3673 
3674 
3675 
3676 /**
3677   * @brief  Enable the selected ADC.
3678   * @note   Prerequisite condition to use this function: ADC must be disabled
3679   *         and voltage regulator must be enabled (done into HAL_ADC_Init()).
3680   * @param hadc ADC handle
3681   * @retval HAL status.
3682   */
3683 HAL_StatusTypeDef ADC_Enable(ADC_HandleTypeDef *hadc)
3684 {
3685   uint32_t tickstart;
3686 
3687   /* ADC enable and wait for ADC ready (in case of ADC is disabled or         */
3688   /* enabling phase not yet completed: flag ADC ready not yet set).           */
3689   /* Timeout implemented to not be stuck if ADC cannot be enabled (possible   */
3690   /* causes: ADC clock not running, ...).                                     */
3691   if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
3692   {
3693     /* Check if conditions to enable the ADC are fulfilled */
3694     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)
3695     {
3696       /* Update ADC state machine to error */
3697       SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
3698 
3699       /* Set ADC error code to ADC peripheral internal error */
3700       SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
3701 
3702       return HAL_ERROR;
3703     }
3704 
3705     /* Enable the ADC peripheral */
3706     LL_ADC_Enable(hadc->Instance);
3707 
3708     /* Wait for ADC effectively enabled */
3709     tickstart = HAL_GetTick();
3710 
3711     /* Poll for ADC ready flag raised except case of multimode enabled
3712        and ADC slave selected. */
3713     uint32_t tmp_multimode_config = LL_ADC_GetMultimode(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
3714     if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
3715         || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
3716        )
3717     {
3718       while (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_RDY) == 0UL)
3719       {
3720         /*  If ADEN bit is set less than 4 ADC clock cycles after the ADCAL bit
3721             has been cleared (after a calibration), ADEN bit is reset by the
3722             calibration logic.
3723             The workaround is to continue setting ADEN until ADRDY is becomes 1.
3724             Additionally, ADC_ENABLE_TIMEOUT is defined to encompass this
3725             4 ADC clock cycle duration */
3726         /* Note: Test of ADC enabled required due to hardware constraint to     */
3727         /*       not enable ADC if already enabled.                             */
3728         if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
3729         {
3730           LL_ADC_Enable(hadc->Instance);
3731         }
3732 
3733         if ((HAL_GetTick() - tickstart) > ADC_ENABLE_TIMEOUT)
3734         {
3735           /* New check to avoid false timeout detection in case of preemption */
3736           if (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_RDY) == 0UL)
3737           {
3738             /* Update ADC state machine to error */
3739             SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
3740 
3741             /* Set ADC error code to ADC peripheral internal error */
3742             SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
3743 
3744             return HAL_ERROR;
3745           }
3746         }
3747       }
3748     }
3749   }
3750 
3751   /* Return HAL status */
3752   return HAL_OK;
3753 }
3754 
3755 /**
3756   * @brief  Disable the selected ADC.
3757   * @note   Prerequisite condition to use this function: ADC conversions must be
3758   *         stopped.
3759   * @param hadc ADC handle
3760   * @retval HAL status.
3761   */
3762 HAL_StatusTypeDef ADC_Disable(ADC_HandleTypeDef *hadc)
3763 {
3764   uint32_t tickstart;
3765   const uint32_t tmp_adc_is_disable_on_going = LL_ADC_IsDisableOngoing(hadc->Instance);
3766 
3767   /* Verification if ADC is not already disabled:                             */
3768   /* Note: forbidden to disable ADC (set bit ADC_CR_ADDIS) if ADC is already  */
3769   /*       disabled.                                                          */
3770   if ((LL_ADC_IsEnabled(hadc->Instance) != 0UL)
3771       && (tmp_adc_is_disable_on_going == 0UL)
3772      )
3773   {
3774     /* Check if conditions to disable the ADC are fulfilled */
3775     if ((hadc->Instance->CR & (ADC_CR_JADSTART | ADC_CR_ADSTART | ADC_CR_ADEN)) == ADC_CR_ADEN)
3776     {
3777       /* Disable the ADC peripheral */
3778       LL_ADC_Disable(hadc->Instance);
3779       __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOSMP | ADC_FLAG_RDY));
3780     }
3781     else
3782     {
3783       /* Update ADC state machine to error */
3784       SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
3785 
3786       /* Set ADC error code to ADC peripheral internal error */
3787       SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
3788 
3789       return HAL_ERROR;
3790     }
3791 
3792     /* Wait for ADC effectively disabled */
3793     /* Get tick count */
3794     tickstart = HAL_GetTick();
3795 
3796     while ((hadc->Instance->CR & ADC_CR_ADEN) != 0UL)
3797     {
3798       if ((HAL_GetTick() - tickstart) > ADC_DISABLE_TIMEOUT)
3799       {
3800         /* New check to avoid false timeout detection in case of preemption */
3801         if ((hadc->Instance->CR & ADC_CR_ADEN) != 0UL)
3802         {
3803           /* Update ADC state machine to error */
3804           SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
3805 
3806           /* Set ADC error code to ADC peripheral internal error */
3807           SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
3808 
3809           return HAL_ERROR;
3810         }
3811       }
3812     }
3813   }
3814 
3815   /* Return HAL status */
3816   return HAL_OK;
3817 }
3818 
3819 /**
3820   * @brief  DMA transfer complete callback.
3821   * @param hdma pointer to DMA handle.
3822   * @retval None
3823   */
3824 void ADC_DMAConvCplt(DMA_HandleTypeDef *hdma)
3825 {
3826   /* Retrieve ADC handle corresponding to current DMA handle */
3827   ADC_HandleTypeDef *hadc = (ADC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
3828 
3829   /* Update state machine on conversion status if not in error state */
3830   if ((hadc->State & (HAL_ADC_STATE_ERROR_INTERNAL | HAL_ADC_STATE_ERROR_DMA)) == 0UL)
3831   {
3832     /* Set ADC state */
3833     SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
3834 
3835     /* Determine whether any further conversion upcoming on group regular     */
3836     /* by external trigger, continuous mode or scan sequence on going         */
3837     /* to disable interruption.                                               */
3838     /* Is it the end of the regular sequence ? */
3839     if ((hadc->Instance->ISR & ADC_FLAG_EOS) != 0UL)
3840     {
3841       /* Are conversions software-triggered ? */
3842       if (LL_ADC_REG_IsTriggerSourceSWStart(hadc->Instance) != 0UL)
3843       {
3844         /* Is CONT bit set ? */
3845         if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_CONT) == 0UL)
3846         {
3847           /* CONT bit is not set, no more conversions expected */
3848           CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
3849           if ((hadc->State & HAL_ADC_STATE_INJ_BUSY) == 0UL)
3850           {
3851             SET_BIT(hadc->State, HAL_ADC_STATE_READY);
3852           }
3853         }
3854       }
3855     }
3856     else
3857     {
3858       /* DMA End of Transfer interrupt was triggered but conversions sequence
3859          is not over. If DMACFG is set to 0, conversions are stopped. */
3860       if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_DMNGT) == 0UL)
3861       {
3862         /* DMACFG bit is not set, conversions are stopped. */
3863         CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
3864         if ((hadc->State & HAL_ADC_STATE_INJ_BUSY) == 0UL)
3865         {
3866           SET_BIT(hadc->State, HAL_ADC_STATE_READY);
3867         }
3868       }
3869     }
3870 
3871     /* Conversion complete callback */
3872 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
3873     hadc->ConvCpltCallback(hadc);
3874 #else
3875     HAL_ADC_ConvCpltCallback(hadc);
3876 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
3877   }
3878   else /* DMA and-or internal error occurred */
3879   {
3880     if ((hadc->State & HAL_ADC_STATE_ERROR_INTERNAL) != 0UL)
3881     {
3882       /* Call HAL ADC Error Callback function */
3883 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
3884       hadc->ErrorCallback(hadc);
3885 #else
3886       HAL_ADC_ErrorCallback(hadc);
3887 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
3888     }
3889     else
3890     {
3891       /* Call ADC DMA error callback */
3892       hadc->DMA_Handle->XferErrorCallback(hdma);
3893     }
3894   }
3895 }
3896 
3897 /**
3898   * @brief  DMA half transfer complete callback.
3899   * @param hdma pointer to DMA handle.
3900   * @retval None
3901   */
3902 void ADC_DMAHalfConvCplt(DMA_HandleTypeDef *hdma)
3903 {
3904   /* Retrieve ADC handle corresponding to current DMA handle */
3905   ADC_HandleTypeDef *hadc = (ADC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
3906 
3907   /* Half conversion callback */
3908 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
3909   hadc->ConvHalfCpltCallback(hadc);
3910 #else
3911   HAL_ADC_ConvHalfCpltCallback(hadc);
3912 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
3913 }
3914 
3915 /**
3916   * @brief  DMA error callback.
3917   * @param hdma pointer to DMA handle.
3918   * @retval None
3919   */
3920 void ADC_DMAError(DMA_HandleTypeDef *hdma)
3921 {
3922   /* Retrieve ADC handle corresponding to current DMA handle */
3923   ADC_HandleTypeDef *hadc = (ADC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
3924 
3925   /* Set ADC state */
3926   SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
3927 
3928   /* Set ADC error code to DMA error */
3929   SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_DMA);
3930 
3931   /* Error callback */
3932 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
3933   hadc->ErrorCallback(hadc);
3934 #else
3935   HAL_ADC_ErrorCallback(hadc);
3936 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
3937 }
3938 
3939 /**
3940   * @brief  Configure boost mode of selected ADC.
3941   * @note   Prerequisite condition to use this function: ADC conversions must be
3942   *         stopped.
3943   * @param  hadc ADC handle
3944   * @retval None.
3945   */
3946 void ADC_ConfigureBoostMode(ADC_HandleTypeDef *hadc)
3947 {
3948   uint32_t freq;
3949   if (ADC_IS_SYNCHRONOUS_CLOCK_MODE(hadc))
3950   {
3951     freq = HAL_RCC_GetHCLKFreq();
3952     switch (hadc->Init.ClockPrescaler)
3953     {
3954       case ADC_CLOCK_SYNC_PCLK_DIV1:
3955       case ADC_CLOCK_SYNC_PCLK_DIV2:
3956         freq /= (hadc->Init.ClockPrescaler >> ADC_CCR_CKMODE_Pos);
3957         break;
3958       case ADC_CLOCK_SYNC_PCLK_DIV4:
3959         freq /= 4UL;
3960         break;
3961       default:
3962         break;
3963     }
3964   }
3965   else
3966   {
3967     freq = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_ADC);
3968     switch (hadc->Init.ClockPrescaler)
3969     {
3970       case ADC_CLOCK_ASYNC_DIV2:
3971       case ADC_CLOCK_ASYNC_DIV4:
3972       case ADC_CLOCK_ASYNC_DIV6:
3973       case ADC_CLOCK_ASYNC_DIV8:
3974       case ADC_CLOCK_ASYNC_DIV10:
3975       case ADC_CLOCK_ASYNC_DIV12:
3976         freq /= ((hadc->Init.ClockPrescaler >> ADC_CCR_PRESC_Pos) << 1UL);
3977         break;
3978       case ADC_CLOCK_ASYNC_DIV16:
3979         freq /= 16UL;
3980         break;
3981       case ADC_CLOCK_ASYNC_DIV32:
3982         freq /= 32UL;
3983         break;
3984       case ADC_CLOCK_ASYNC_DIV64:
3985         freq /= 64UL;
3986         break;
3987       case ADC_CLOCK_ASYNC_DIV128:
3988         freq /= 128UL;
3989         break;
3990       case ADC_CLOCK_ASYNC_DIV256:
3991         freq /= 256UL;
3992         break;
3993       default:
3994         break;
3995     }
3996   }
3997 
3998 #if defined(ADC_VER_V5_3) || defined(ADC_VER_V5_V90)
3999   freq /= 2U;
4000   if (freq <= 6250000UL)
4001   {
4002     MODIFY_REG(hadc->Instance->CR, ADC_CR_BOOST, 0UL);
4003   }
4004   else if (freq <= 12500000UL)
4005   {
4006     MODIFY_REG(hadc->Instance->CR, ADC_CR_BOOST, ADC_CR_BOOST_0);
4007   }
4008   else if (freq <= 25000000UL)
4009   {
4010     MODIFY_REG(hadc->Instance->CR, ADC_CR_BOOST, ADC_CR_BOOST_1);
4011   }
4012   else /* if(freq > 25000000UL) */
4013   {
4014     MODIFY_REG(hadc->Instance->CR, ADC_CR_BOOST, ADC_CR_BOOST_1 | ADC_CR_BOOST_0);
4015   }
4016 #else
4017   if (HAL_GetREVID() <= REV_ID_Y) /* STM32H7 silicon Rev.Y */
4018   {
4019     if (freq > 20000000UL)
4020     {
4021       SET_BIT(hadc->Instance->CR, ADC_CR_BOOST_0);
4022     }
4023     else
4024     {
4025       CLEAR_BIT(hadc->Instance->CR, ADC_CR_BOOST_0);
4026     }
4027   }
4028   else /* STM32H7 silicon Rev.V */
4029   {
4030     freq /= 2U; /* divider by 2 for Rev.V */
4031 
4032     if (freq <= 6250000UL)
4033     {
4034       MODIFY_REG(hadc->Instance->CR, ADC_CR_BOOST, 0UL);
4035     }
4036     else if (freq <= 12500000UL)
4037     {
4038       MODIFY_REG(hadc->Instance->CR, ADC_CR_BOOST, ADC_CR_BOOST_0);
4039     }
4040     else if (freq <= 25000000UL)
4041     {
4042       MODIFY_REG(hadc->Instance->CR, ADC_CR_BOOST, ADC_CR_BOOST_1);
4043     }
4044     else /* if(freq > 25000000UL) */
4045     {
4046       MODIFY_REG(hadc->Instance->CR, ADC_CR_BOOST, ADC_CR_BOOST_1 | ADC_CR_BOOST_0);
4047     }
4048   }
4049 #endif /* ADC_VER_V5_3 */
4050 }
4051 
4052 /**
4053   * @}
4054   */
4055 
4056 #endif /* HAL_ADC_MODULE_ENABLED */
4057 /**
4058   * @}
4059   */
4060 
4061 /**
4062   * @}
4063   */
4064