STM32L443xx HAL User Manual
stm32l4xx_hal_adc_ex.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32l4xx_hal_adc_ex.c
00004   * @author  MCD Application Team
00005   * @brief   This file provides firmware functions to manage the following
00006   *          functionalities of the Analog to Digital Converter (ADC)
00007   *          peripheral:
00008   *           + Peripheral Control functions
00009   *          Other functions (generic functions) are available in file
00010   *          "stm32l4xx_hal_adc.c".
00011   *
00012   ******************************************************************************
00013   * @attention
00014   *
00015   * Copyright (c) 2017 STMicroelectronics.
00016   * All rights reserved.
00017   *
00018   * This software is licensed under terms that can be found in the LICENSE file
00019   * in the root directory of this software component.
00020   * If no LICENSE file comes with this software, it is provided AS-IS.
00021   *
00022   ******************************************************************************
00023   @verbatim
00024   [..]
00025   (@) Sections "ADC peripheral features" and "How to use this driver" are
00026       available in file of generic functions "stm32l4xx_hal_adc.c".
00027   [..]
00028   @endverbatim
00029   ******************************************************************************
00030   */
00031 
00032 /* Includes ------------------------------------------------------------------*/
00033 #include "stm32l4xx_hal.h"
00034 
00035 /** @addtogroup STM32L4xx_HAL_Driver
00036   * @{
00037   */
00038 
00039 /** @defgroup ADCEx ADCEx
00040   * @brief ADC Extended HAL module driver
00041   * @{
00042   */
00043 
00044 #ifdef HAL_ADC_MODULE_ENABLED
00045 
00046 /* Private typedef -----------------------------------------------------------*/
00047 /* Private define ------------------------------------------------------------*/
00048 
00049 /** @defgroup ADCEx_Private_Constants ADC Extended Private Constants
00050   * @{
00051   */
00052 
00053 #define ADC_JSQR_FIELDS  ((ADC_JSQR_JL | ADC_JSQR_JEXTSEL | ADC_JSQR_JEXTEN |\
00054                            ADC_JSQR_JSQ1  | ADC_JSQR_JSQ2 |\
00055                            ADC_JSQR_JSQ3 | ADC_JSQR_JSQ4 ))  /*!< ADC_JSQR fields of parameters that can be updated anytime once the ADC is enabled */
00056 
00057 /* Fixed timeout value for ADC calibration.                                   */
00058 /* Values defined to be higher than worst cases: maximum ratio between ADC    */
00059 /* and CPU clock frequencies.                                                 */
00060 /* Example of profile low frequency : ADC frequency at 31.25kHz (ADC clock    */
00061 /* source PLL SAI 8MHz, ADC clock prescaler 256), CPU frequency 80MHz.        */
00062 /* Calibration time max = 116 / fADC (refer to datasheet)                     */
00063 /*                      = 296 960 CPU cycles                                  */
00064 #define ADC_CALIBRATION_TIMEOUT         (296960UL)   /*!< ADC calibration time-out value (unit: CPU cycles) */
00065 
00066 /**
00067   * @}
00068   */
00069 
00070 /* Private macro -------------------------------------------------------------*/
00071 /* Private variables ---------------------------------------------------------*/
00072 /* Private function prototypes -----------------------------------------------*/
00073 /* Exported functions --------------------------------------------------------*/
00074 
00075 /** @defgroup ADCEx_Exported_Functions ADC Extended Exported Functions
00076   * @{
00077   */
00078 
00079 /** @defgroup ADCEx_Exported_Functions_Group1 Extended Input and Output operation functions
00080   * @brief    Extended IO operation functions
00081   *
00082 @verbatim
00083  ===============================================================================
00084                       ##### IO operation functions #####
00085  ===============================================================================
00086     [..]  This section provides functions allowing to:
00087 
00088       (+) Perform the ADC self-calibration for single or differential ending.
00089       (+) Get calibration factors for single or differential ending.
00090       (+) Set calibration factors for single or differential ending.
00091 
00092       (+) Start conversion of ADC group injected.
00093       (+) Stop conversion of ADC group injected.
00094       (+) Poll for conversion complete on ADC group injected.
00095       (+) Get result of ADC group injected channel conversion.
00096       (+) Start conversion of ADC group injected and enable interruptions.
00097       (+) Stop conversion of ADC group injected and disable interruptions.
00098 
00099       (+) When multimode feature is available, start multimode and enable DMA transfer.
00100       (+) Stop multimode and disable ADC DMA transfer.
00101       (+) Get result of multimode conversion.
00102 
00103 @endverbatim
00104   * @{
00105   */
00106 
00107 /**
00108   * @brief  Perform an ADC automatic self-calibration
00109   *         Calibration prerequisite: ADC must be disabled (execute this
00110   *         function before HAL_ADC_Start() or after HAL_ADC_Stop() ).
00111   * @param  hadc       ADC handle
00112   * @param  SingleDiff Selection of single-ended or differential input
00113   *         This parameter can be one of the following values:
00114   *           @arg @ref ADC_SINGLE_ENDED       Channel in mode input single ended
00115   *           @arg @ref ADC_DIFFERENTIAL_ENDED Channel in mode input differential ended
00116   * @retval HAL status
00117   */
00118 HAL_StatusTypeDef HAL_ADCEx_Calibration_Start(ADC_HandleTypeDef *hadc, uint32_t SingleDiff)
00119 {
00120   HAL_StatusTypeDef tmp_hal_status;
00121   __IO uint32_t wait_loop_index = 0UL;
00122 
00123   /* Check the parameters */
00124   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
00125   assert_param(IS_ADC_SINGLE_DIFFERENTIAL(SingleDiff));
00126 
00127   /* Process locked */
00128   __HAL_LOCK(hadc);
00129 
00130   /* Calibration prerequisite: ADC must be disabled. */
00131 
00132   /* Disable the ADC (if not already disabled) */
00133   tmp_hal_status = ADC_Disable(hadc);
00134 
00135   /* Check if ADC is effectively disabled */
00136   if (tmp_hal_status == HAL_OK)
00137   {
00138     /* Set ADC state */
00139     ADC_STATE_CLR_SET(hadc->State,
00140                       HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
00141                       HAL_ADC_STATE_BUSY_INTERNAL);
00142 
00143     /* Start ADC calibration in mode single-ended or differential */
00144     LL_ADC_StartCalibration(hadc->Instance, SingleDiff);
00145 
00146     /* Wait for calibration completion */
00147     while (LL_ADC_IsCalibrationOnGoing(hadc->Instance) != 0UL)
00148     {
00149       wait_loop_index++;
00150       if (wait_loop_index >= ADC_CALIBRATION_TIMEOUT)
00151       {
00152         /* Update ADC state machine to error */
00153         ADC_STATE_CLR_SET(hadc->State,
00154                           HAL_ADC_STATE_BUSY_INTERNAL,
00155                           HAL_ADC_STATE_ERROR_INTERNAL);
00156 
00157         /* Process unlocked */
00158         __HAL_UNLOCK(hadc);
00159 
00160         return HAL_ERROR;
00161       }
00162     }
00163 
00164     /* Set ADC state */
00165     ADC_STATE_CLR_SET(hadc->State,
00166                       HAL_ADC_STATE_BUSY_INTERNAL,
00167                       HAL_ADC_STATE_READY);
00168   }
00169   else
00170   {
00171     SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
00172 
00173     /* Note: No need to update variable "tmp_hal_status" here: already set    */
00174     /*       to state "HAL_ERROR" by function disabling the ADC.              */
00175   }
00176 
00177   /* Process unlocked */
00178   __HAL_UNLOCK(hadc);
00179 
00180   /* Return function status */
00181   return tmp_hal_status;
00182 }
00183 
00184 /**
00185   * @brief  Get the calibration factor.
00186   * @param hadc ADC handle.
00187   * @param SingleDiff This parameter can be only:
00188   *           @arg @ref ADC_SINGLE_ENDED       Channel in mode input single ended
00189   *           @arg @ref ADC_DIFFERENTIAL_ENDED Channel in mode input differential ended
00190   * @retval Calibration value.
00191   */
00192 uint32_t HAL_ADCEx_Calibration_GetValue(ADC_HandleTypeDef *hadc, uint32_t SingleDiff)
00193 {
00194   /* Check the parameters */
00195   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
00196   assert_param(IS_ADC_SINGLE_DIFFERENTIAL(SingleDiff));
00197 
00198   /* Return the selected ADC calibration value */
00199   return LL_ADC_GetCalibrationFactor(hadc->Instance, SingleDiff);
00200 }
00201 
00202 /**
00203   * @brief  Set the calibration factor to overwrite automatic conversion result.
00204   *         ADC must be enabled and no conversion is ongoing.
00205   * @param hadc ADC handle
00206   * @param SingleDiff This parameter can be only:
00207   *           @arg @ref ADC_SINGLE_ENDED       Channel in mode input single ended
00208   *           @arg @ref ADC_DIFFERENTIAL_ENDED Channel in mode input differential ended
00209   * @param CalibrationFactor Calibration factor (coded on 7 bits maximum)
00210   * @retval HAL state
00211   */
00212 HAL_StatusTypeDef HAL_ADCEx_Calibration_SetValue(ADC_HandleTypeDef *hadc, uint32_t SingleDiff,
00213                                                  uint32_t CalibrationFactor)
00214 {
00215   HAL_StatusTypeDef tmp_hal_status = HAL_OK;
00216   uint32_t tmp_adc_is_conversion_on_going_regular;
00217   uint32_t tmp_adc_is_conversion_on_going_injected;
00218 
00219   /* Check the parameters */
00220   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
00221   assert_param(IS_ADC_SINGLE_DIFFERENTIAL(SingleDiff));
00222   assert_param(IS_ADC_CALFACT(CalibrationFactor));
00223 
00224   /* Process locked */
00225   __HAL_LOCK(hadc);
00226 
00227   /* Verification of hardware constraints before modifying the calibration    */
00228   /* factors register: ADC must be enabled, no conversion on going.           */
00229   tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
00230   tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
00231 
00232   if ((LL_ADC_IsEnabled(hadc->Instance) != 0UL)
00233       && (tmp_adc_is_conversion_on_going_regular == 0UL)
00234       && (tmp_adc_is_conversion_on_going_injected == 0UL)
00235      )
00236   {
00237     /* Set the selected ADC calibration value */
00238     LL_ADC_SetCalibrationFactor(hadc->Instance, SingleDiff, CalibrationFactor);
00239   }
00240   else
00241   {
00242     /* Update ADC state machine */
00243     SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
00244     /* Update ADC error code */
00245     SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
00246 
00247     /* Update ADC state machine to error */
00248     tmp_hal_status = HAL_ERROR;
00249   }
00250 
00251   /* Process unlocked */
00252   __HAL_UNLOCK(hadc);
00253 
00254   /* Return function status */
00255   return tmp_hal_status;
00256 }
00257 
00258 /**
00259   * @brief  Enable ADC, start conversion of injected group.
00260   * @note   Interruptions enabled in this function: None.
00261   * @note   Case of multimode enabled when multimode feature is available:
00262   *         HAL_ADCEx_InjectedStart() API must be called for ADC slave first,
00263   *         then for ADC master.
00264   *         For ADC slave, ADC is enabled only (conversion is not started).
00265   *         For ADC master, ADC is enabled and multimode conversion is started.
00266   * @param hadc ADC handle.
00267   * @retval HAL status
00268   */
00269 HAL_StatusTypeDef HAL_ADCEx_InjectedStart(ADC_HandleTypeDef *hadc)
00270 {
00271   HAL_StatusTypeDef tmp_hal_status;
00272   uint32_t tmp_config_injected_queue;
00273 #if defined(ADC_MULTIMODE_SUPPORT)
00274   uint32_t tmp_multimode_config = LL_ADC_GetMultimode(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
00275 #endif
00276 
00277   /* Check the parameters */
00278   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
00279 
00280   if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) != 0UL)
00281   {
00282     return HAL_BUSY;
00283   }
00284   else
00285   {
00286     /* In case of software trigger detection enabled, JQDIS must be set
00287       (which can be done only if ADSTART and JADSTART are both cleared).
00288        If JQDIS is not set at that point, returns an error
00289        - since software trigger detection is disabled. User needs to
00290        resort to HAL_ADCEx_DisableInjectedQueue() API to set JQDIS.
00291        - or (if JQDIS is intentionally reset) since JEXTEN = 0 which means
00292          the queue is empty */
00293     tmp_config_injected_queue = READ_BIT(hadc->Instance->CFGR, ADC_CFGR_JQDIS);
00294 
00295     if ((READ_BIT(hadc->Instance->JSQR, ADC_JSQR_JEXTEN) == 0UL)
00296         && (tmp_config_injected_queue == 0UL)
00297        )
00298     {
00299       SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
00300       return HAL_ERROR;
00301     }
00302 
00303     /* Process locked */
00304     __HAL_LOCK(hadc);
00305 
00306     /* Enable the ADC peripheral */
00307     tmp_hal_status = ADC_Enable(hadc);
00308 
00309     /* Start conversion if ADC is effectively enabled */
00310     if (tmp_hal_status == HAL_OK)
00311     {
00312       /* Check if a regular conversion is ongoing */
00313       if ((hadc->State & HAL_ADC_STATE_REG_BUSY) != 0UL)
00314       {
00315         /* Reset ADC error code field related to injected conversions only */
00316         CLEAR_BIT(hadc->ErrorCode, HAL_ADC_ERROR_JQOVF);
00317       }
00318       else
00319       {
00320         /* Set ADC error code to none */
00321         ADC_CLEAR_ERRORCODE(hadc);
00322       }
00323 
00324       /* Set ADC state                                                        */
00325       /* - Clear state bitfield related to injected group conversion results  */
00326       /* - Set state bitfield related to injected operation                   */
00327       ADC_STATE_CLR_SET(hadc->State,
00328                         HAL_ADC_STATE_READY | HAL_ADC_STATE_INJ_EOC,
00329                         HAL_ADC_STATE_INJ_BUSY);
00330 
00331 #if defined(ADC_MULTIMODE_SUPPORT)
00332       /* Reset HAL_ADC_STATE_MULTIMODE_SLAVE bit
00333         - if ADC instance is master or if multimode feature is not available
00334         - if multimode setting is disabled (ADC instance slave in independent mode) */
00335       if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
00336           || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
00337          )
00338       {
00339         CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
00340       }
00341 #endif
00342 
00343       /* Clear ADC group injected group conversion flag */
00344       /* (To ensure of no unknown state from potential previous ADC operations) */
00345       __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_JEOC | ADC_FLAG_JEOS));
00346 
00347       /* Process unlocked */
00348       /* Unlock before starting ADC conversions: in case of potential         */
00349       /* interruption, to let the process to ADC IRQ Handler.                 */
00350       __HAL_UNLOCK(hadc);
00351 
00352       /* Enable conversion of injected group, if automatic injected conversion  */
00353       /* is disabled.                                                           */
00354       /* If software start has been selected, conversion starts immediately.    */
00355       /* If external trigger has been selected, conversion will start at next   */
00356       /* trigger event.                                                         */
00357       /* Case of multimode enabled (when multimode feature is available):       */
00358       /* if ADC is slave,                                                       */
00359       /*    - ADC is enabled only (conversion is not started),                  */
00360       /*    - if multimode only concerns regular conversion, ADC is enabled     */
00361       /*     and conversion is started.                                         */
00362       /* If ADC is master or independent,                                       */
00363       /*    - ADC is enabled and conversion is started.                         */
00364 #if defined(ADC_MULTIMODE_SUPPORT)
00365       if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
00366           || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
00367           || (tmp_multimode_config == LL_ADC_MULTI_DUAL_REG_SIMULT)
00368           || (tmp_multimode_config == LL_ADC_MULTI_DUAL_REG_INTERL)
00369          )
00370       {
00371         /* ADC instance is not a multimode slave instance with multimode injected conversions enabled */
00372         if (LL_ADC_INJ_GetTrigAuto(hadc->Instance) == LL_ADC_INJ_TRIG_INDEPENDENT)
00373         {
00374           LL_ADC_INJ_StartConversion(hadc->Instance);
00375         }
00376       }
00377       else
00378       {
00379         /* ADC instance is not a multimode slave instance with multimode injected conversions enabled */
00380         SET_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
00381       }
00382 #else
00383       if (LL_ADC_INJ_GetTrigAuto(hadc->Instance) == LL_ADC_INJ_TRIG_INDEPENDENT)
00384       {
00385         /* Start ADC group injected conversion */
00386         LL_ADC_INJ_StartConversion(hadc->Instance);
00387       }
00388 #endif
00389 
00390     }
00391     else
00392     {
00393       /* Process unlocked */
00394       __HAL_UNLOCK(hadc);
00395     }
00396 
00397     /* Return function status */
00398     return tmp_hal_status;
00399   }
00400 }
00401 
00402 /**
00403   * @brief  Stop conversion of injected channels. Disable ADC peripheral if
00404   *         no regular conversion is on going.
00405   * @note   If ADC must be disabled and if conversion is on going on
00406   *         regular group, function HAL_ADC_Stop must be used to stop both
00407   *         injected and regular groups, and disable the ADC.
00408   * @note   If injected group mode auto-injection is enabled,
00409   *         function HAL_ADC_Stop must be used.
00410   * @note   In case of multimode enabled (when multimode feature is available),
00411   *         HAL_ADCEx_InjectedStop() must be called for ADC master first, then for ADC slave.
00412   *         For ADC master, conversion is stopped and ADC is disabled.
00413   *         For ADC slave, ADC is disabled only (conversion stop of ADC master
00414   *         has already stopped conversion of ADC slave).
00415   * @param hadc ADC handle.
00416   * @retval HAL status
00417   */
00418 HAL_StatusTypeDef HAL_ADCEx_InjectedStop(ADC_HandleTypeDef *hadc)
00419 {
00420   HAL_StatusTypeDef tmp_hal_status;
00421 
00422   /* Check the parameters */
00423   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
00424 
00425   /* Process locked */
00426   __HAL_LOCK(hadc);
00427 
00428   /* 1. Stop potential conversion on going on injected group only. */
00429   tmp_hal_status = ADC_ConversionStop(hadc, ADC_INJECTED_GROUP);
00430 
00431   /* Disable ADC peripheral if injected conversions are effectively stopped   */
00432   /* and if no conversion on regular group is on-going                       */
00433   if (tmp_hal_status == HAL_OK)
00434   {
00435     if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
00436     {
00437       /* 2. Disable the ADC peripheral */
00438       tmp_hal_status = ADC_Disable(hadc);
00439 
00440       /* Check if ADC is effectively disabled */
00441       if (tmp_hal_status == HAL_OK)
00442       {
00443         /* Set ADC state */
00444         ADC_STATE_CLR_SET(hadc->State,
00445                           HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
00446                           HAL_ADC_STATE_READY);
00447       }
00448     }
00449     /* Conversion on injected group is stopped, but ADC not disabled since    */
00450     /* conversion on regular group is still running.                          */
00451     else
00452     {
00453       /* Set ADC state */
00454       CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
00455     }
00456   }
00457 
00458   /* Process unlocked */
00459   __HAL_UNLOCK(hadc);
00460 
00461   /* Return function status */
00462   return tmp_hal_status;
00463 }
00464 
00465 /**
00466   * @brief  Wait for injected group conversion to be completed.
00467   * @param hadc ADC handle
00468   * @param Timeout Timeout value in millisecond.
00469   * @note   Depending on hadc->Init.EOCSelection, JEOS or JEOC is
00470   *         checked and cleared depending on AUTDLY bit status.
00471   * @retval HAL status
00472   */
00473 HAL_StatusTypeDef HAL_ADCEx_InjectedPollForConversion(ADC_HandleTypeDef *hadc, uint32_t Timeout)
00474 {
00475   uint32_t tickstart;
00476   uint32_t tmp_Flag_End;
00477   uint32_t tmp_adc_inj_is_trigger_source_sw_start;
00478   uint32_t tmp_adc_reg_is_trigger_source_sw_start;
00479   uint32_t tmp_cfgr;
00480 #if defined(ADC_MULTIMODE_SUPPORT)
00481   const ADC_TypeDef *tmpADC_Master;
00482   uint32_t tmp_multimode_config = LL_ADC_GetMultimode(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
00483 #endif
00484 
00485   /* Check the parameters */
00486   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
00487 
00488   /* If end of sequence selected */
00489   if (hadc->Init.EOCSelection == ADC_EOC_SEQ_CONV)
00490   {
00491     tmp_Flag_End = ADC_FLAG_JEOS;
00492   }
00493   else /* end of conversion selected */
00494   {
00495     tmp_Flag_End = ADC_FLAG_JEOC;
00496   }
00497 
00498   /* Get timeout */
00499   tickstart = HAL_GetTick();
00500 
00501   /* Wait until End of Conversion or Sequence flag is raised */
00502   while ((hadc->Instance->ISR & tmp_Flag_End) == 0UL)
00503   {
00504     /* Check if timeout is disabled (set to infinite wait) */
00505     if (Timeout != HAL_MAX_DELAY)
00506     {
00507       if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0UL))
00508       {
00509         /* New check to avoid false timeout detection in case of preemption */
00510         if ((hadc->Instance->ISR & tmp_Flag_End) == 0UL)
00511         {
00512           /* Update ADC state machine to timeout */
00513           SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
00514 
00515           /* Process unlocked */
00516           __HAL_UNLOCK(hadc);
00517 
00518           return HAL_TIMEOUT;
00519         }
00520       }
00521     }
00522   }
00523 
00524   /* Retrieve ADC configuration */
00525   tmp_adc_inj_is_trigger_source_sw_start = LL_ADC_INJ_IsTriggerSourceSWStart(hadc->Instance);
00526   tmp_adc_reg_is_trigger_source_sw_start = LL_ADC_REG_IsTriggerSourceSWStart(hadc->Instance);
00527   /* Get relevant register CFGR in ADC instance of ADC master or slave  */
00528   /* in function of multimode state (for devices with multimode         */
00529   /* available).                                                        */
00530 #if defined(ADC_MULTIMODE_SUPPORT)
00531   if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
00532       || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
00533       || (tmp_multimode_config == LL_ADC_MULTI_DUAL_REG_SIMULT)
00534       || (tmp_multimode_config == LL_ADC_MULTI_DUAL_REG_INTERL)
00535      )
00536   {
00537     tmp_cfgr = READ_REG(hadc->Instance->CFGR);
00538   }
00539   else
00540   {
00541     tmpADC_Master = __LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance);
00542     tmp_cfgr = READ_REG(tmpADC_Master->CFGR);
00543   }
00544 #else
00545   tmp_cfgr = READ_REG(hadc->Instance->CFGR);
00546 #endif
00547 
00548   /* Update ADC state machine */
00549   SET_BIT(hadc->State, HAL_ADC_STATE_INJ_EOC);
00550 
00551   /* Determine whether any further conversion upcoming on group injected      */
00552   /* by external trigger or by automatic injected conversion                  */
00553   /* from group regular.                                                      */
00554   if ((tmp_adc_inj_is_trigger_source_sw_start != 0UL)            ||
00555       ((READ_BIT(tmp_cfgr, ADC_CFGR_JAUTO) == 0UL)      &&
00556        ((tmp_adc_reg_is_trigger_source_sw_start != 0UL)  &&
00557         (READ_BIT(tmp_cfgr, ADC_CFGR_CONT) == 0UL))))
00558   {
00559     /* Check whether end of sequence is reached */
00560     if (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_JEOS))
00561     {
00562       /* Particular case if injected contexts queue is enabled:             */
00563       /* when the last context has been fully processed, JSQR is reset      */
00564       /* by the hardware. Even if no injected conversion is planned to come */
00565       /* (queue empty, triggers are ignored), it can start again            */
00566       /* immediately after setting a new context (JADSTART is still set).   */
00567       /* Therefore, state of HAL ADC injected group is kept to busy.        */
00568       if (READ_BIT(tmp_cfgr, ADC_CFGR_JQM) == 0UL)
00569       {
00570         /* Set ADC state */
00571         CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
00572 
00573         if ((hadc->State & HAL_ADC_STATE_REG_BUSY) == 0UL)
00574         {
00575           SET_BIT(hadc->State, HAL_ADC_STATE_READY);
00576         }
00577       }
00578     }
00579   }
00580 
00581   /* Clear polled flag */
00582   if (tmp_Flag_End == ADC_FLAG_JEOS)
00583   {
00584     /* Clear end of sequence JEOS flag of injected group if low power feature */
00585     /* "LowPowerAutoWait " is disabled, to not interfere with this feature.   */
00586     /* For injected groups, no new conversion will start before JEOS is       */
00587     /* cleared.                                                               */
00588     if (READ_BIT(tmp_cfgr, ADC_CFGR_AUTDLY) == 0UL)
00589     {
00590       __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_JEOC | ADC_FLAG_JEOS));
00591     }
00592   }
00593   else
00594   {
00595     __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOC);
00596   }
00597 
00598   /* Return API HAL status */
00599   return HAL_OK;
00600 }
00601 
00602 /**
00603   * @brief  Enable ADC, start conversion of injected group with interruption.
00604   * @note   Interruptions enabled in this function according to initialization
00605   *         setting : JEOC (end of conversion) or JEOS (end of sequence)
00606   * @note   Case of multimode enabled (when multimode feature is enabled):
00607   *         HAL_ADCEx_InjectedStart_IT() API must be called for ADC slave first,
00608   *         then for ADC master.
00609   *         For ADC slave, ADC is enabled only (conversion is not started).
00610   *         For ADC master, ADC is enabled and multimode conversion is started.
00611   * @param hadc ADC handle.
00612   * @retval HAL status.
00613   */
00614 HAL_StatusTypeDef HAL_ADCEx_InjectedStart_IT(ADC_HandleTypeDef *hadc)
00615 {
00616   HAL_StatusTypeDef tmp_hal_status;
00617   uint32_t tmp_config_injected_queue;
00618 #if defined(ADC_MULTIMODE_SUPPORT)
00619   uint32_t tmp_multimode_config = LL_ADC_GetMultimode(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
00620 #endif
00621 
00622   /* Check the parameters */
00623   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
00624 
00625   if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) != 0UL)
00626   {
00627     return HAL_BUSY;
00628   }
00629   else
00630   {
00631     /* In case of software trigger detection enabled, JQDIS must be set
00632       (which can be done only if ADSTART and JADSTART are both cleared).
00633        If JQDIS is not set at that point, returns an error
00634        - since software trigger detection is disabled. User needs to
00635        resort to HAL_ADCEx_DisableInjectedQueue() API to set JQDIS.
00636        - or (if JQDIS is intentionally reset) since JEXTEN = 0 which means
00637          the queue is empty */
00638     tmp_config_injected_queue = READ_BIT(hadc->Instance->CFGR, ADC_CFGR_JQDIS);
00639 
00640     if ((READ_BIT(hadc->Instance->JSQR, ADC_JSQR_JEXTEN) == 0UL)
00641         && (tmp_config_injected_queue == 0UL)
00642        )
00643     {
00644       SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
00645       return HAL_ERROR;
00646     }
00647 
00648     /* Process locked */
00649     __HAL_LOCK(hadc);
00650 
00651     /* Enable the ADC peripheral */
00652     tmp_hal_status = ADC_Enable(hadc);
00653 
00654     /* Start conversion if ADC is effectively enabled */
00655     if (tmp_hal_status == HAL_OK)
00656     {
00657       /* Check if a regular conversion is ongoing */
00658       if ((hadc->State & HAL_ADC_STATE_REG_BUSY) != 0UL)
00659       {
00660         /* Reset ADC error code field related to injected conversions only */
00661         CLEAR_BIT(hadc->ErrorCode, HAL_ADC_ERROR_JQOVF);
00662       }
00663       else
00664       {
00665         /* Set ADC error code to none */
00666         ADC_CLEAR_ERRORCODE(hadc);
00667       }
00668 
00669       /* Set ADC state                                                        */
00670       /* - Clear state bitfield related to injected group conversion results  */
00671       /* - Set state bitfield related to injected operation                   */
00672       ADC_STATE_CLR_SET(hadc->State,
00673                         HAL_ADC_STATE_READY | HAL_ADC_STATE_INJ_EOC,
00674                         HAL_ADC_STATE_INJ_BUSY);
00675 
00676 #if defined(ADC_MULTIMODE_SUPPORT)
00677       /* Reset HAL_ADC_STATE_MULTIMODE_SLAVE bit
00678         - if ADC instance is master or if multimode feature is not available
00679         - if multimode setting is disabled (ADC instance slave in independent mode) */
00680       if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
00681           || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
00682          )
00683       {
00684         CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
00685       }
00686 #endif
00687 
00688       /* Clear ADC group injected group conversion flag */
00689       /* (To ensure of no unknown state from potential previous ADC operations) */
00690       __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_JEOC | ADC_FLAG_JEOS));
00691 
00692       /* Process unlocked */
00693       /* Unlock before starting ADC conversions: in case of potential         */
00694       /* interruption, to let the process to ADC IRQ Handler.                 */
00695       __HAL_UNLOCK(hadc);
00696 
00697       /* Enable ADC Injected context queue overflow interrupt if this feature   */
00698       /* is enabled.                                                            */
00699       if ((hadc->Instance->CFGR & ADC_CFGR_JQM) != 0UL)
00700       {
00701         __HAL_ADC_ENABLE_IT(hadc, ADC_FLAG_JQOVF);
00702       }
00703 
00704       /* Enable ADC end of conversion interrupt */
00705       switch (hadc->Init.EOCSelection)
00706       {
00707         case ADC_EOC_SEQ_CONV:
00708           __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC);
00709           __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOS);
00710           break;
00711         /* case ADC_EOC_SINGLE_CONV */
00712         default:
00713           __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOS);
00714           __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOC);
00715           break;
00716       }
00717 
00718       /* Enable conversion of injected group, if automatic injected conversion  */
00719       /* is disabled.                                                           */
00720       /* If software start has been selected, conversion starts immediately.    */
00721       /* If external trigger has been selected, conversion will start at next   */
00722       /* trigger event.                                                         */
00723       /* Case of multimode enabled (when multimode feature is available):       */
00724       /* if ADC is slave,                                                       */
00725       /*    - ADC is enabled only (conversion is not started),                  */
00726       /*    - if multimode only concerns regular conversion, ADC is enabled     */
00727       /*     and conversion is started.                                         */
00728       /* If ADC is master or independent,                                       */
00729       /*    - ADC is enabled and conversion is started.                         */
00730 #if defined(ADC_MULTIMODE_SUPPORT)
00731       if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
00732           || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
00733           || (tmp_multimode_config == LL_ADC_MULTI_DUAL_REG_SIMULT)
00734           || (tmp_multimode_config == LL_ADC_MULTI_DUAL_REG_INTERL)
00735          )
00736       {
00737         /* ADC instance is not a multimode slave instance with multimode injected conversions enabled */
00738         if (LL_ADC_INJ_GetTrigAuto(hadc->Instance) == LL_ADC_INJ_TRIG_INDEPENDENT)
00739         {
00740           LL_ADC_INJ_StartConversion(hadc->Instance);
00741         }
00742       }
00743       else
00744       {
00745         /* ADC instance is not a multimode slave instance with multimode injected conversions enabled */
00746         SET_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
00747       }
00748 #else
00749       if (LL_ADC_INJ_GetTrigAuto(hadc->Instance) == LL_ADC_INJ_TRIG_INDEPENDENT)
00750       {
00751         /* Start ADC group injected conversion */
00752         LL_ADC_INJ_StartConversion(hadc->Instance);
00753       }
00754 #endif
00755 
00756     }
00757     else
00758     {
00759       /* Process unlocked */
00760       __HAL_UNLOCK(hadc);
00761     }
00762 
00763     /* Return function status */
00764     return tmp_hal_status;
00765   }
00766 }
00767 
00768 /**
00769   * @brief  Stop conversion of injected channels, disable interruption of
00770   *         end-of-conversion. Disable ADC peripheral if no regular conversion
00771   *         is on going.
00772   * @note   If ADC must be disabled and if conversion is on going on
00773   *         regular group, function HAL_ADC_Stop must be used to stop both
00774   *         injected and regular groups, and disable the ADC.
00775   * @note   If injected group mode auto-injection is enabled,
00776   *         function HAL_ADC_Stop must be used.
00777   * @note   Case of multimode enabled (when multimode feature is available):
00778   *         HAL_ADCEx_InjectedStop_IT() API must be called for ADC master first,
00779   *         then for ADC slave.
00780   *         For ADC master, conversion is stopped and ADC is disabled.
00781   *         For ADC slave, ADC is disabled only (conversion stop of ADC master
00782   *         has already stopped conversion of ADC slave).
00783   * @note   In case of auto-injection mode, HAL_ADC_Stop() must be used.
00784   * @param hadc ADC handle
00785   * @retval HAL status
00786   */
00787 HAL_StatusTypeDef HAL_ADCEx_InjectedStop_IT(ADC_HandleTypeDef *hadc)
00788 {
00789   HAL_StatusTypeDef tmp_hal_status;
00790 
00791   /* Check the parameters */
00792   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
00793 
00794   /* Process locked */
00795   __HAL_LOCK(hadc);
00796 
00797   /* 1. Stop potential conversion on going on injected group only. */
00798   tmp_hal_status = ADC_ConversionStop(hadc, ADC_INJECTED_GROUP);
00799 
00800   /* Disable ADC peripheral if injected conversions are effectively stopped   */
00801   /* and if no conversion on the other group (regular group) is intended to   */
00802   /* continue.                                                                */
00803   if (tmp_hal_status == HAL_OK)
00804   {
00805     /* Disable ADC end of conversion interrupt for injected channels */
00806     __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_JEOC | ADC_IT_JEOS | ADC_FLAG_JQOVF));
00807 
00808     if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
00809     {
00810       /* 2. Disable the ADC peripheral */
00811       tmp_hal_status = ADC_Disable(hadc);
00812 
00813       /* Check if ADC is effectively disabled */
00814       if (tmp_hal_status == HAL_OK)
00815       {
00816         /* Set ADC state */
00817         ADC_STATE_CLR_SET(hadc->State,
00818                           HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
00819                           HAL_ADC_STATE_READY);
00820       }
00821     }
00822     /* Conversion on injected group is stopped, but ADC not disabled since    */
00823     /* conversion on regular group is still running.                          */
00824     else
00825     {
00826       /* Set ADC state */
00827       CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
00828     }
00829   }
00830 
00831   /* Process unlocked */
00832   __HAL_UNLOCK(hadc);
00833 
00834   /* Return function status */
00835   return tmp_hal_status;
00836 }
00837 
00838 #if defined(ADC_MULTIMODE_SUPPORT)
00839 /**
00840   * @brief  Enable ADC, start MultiMode conversion and transfer regular results through DMA.
00841   * @note   Multimode must have been previously configured using
00842   *         HAL_ADCEx_MultiModeConfigChannel() function.
00843   *         Interruptions enabled in this function:
00844   *          overrun, DMA half transfer, DMA transfer complete.
00845   *         Each of these interruptions has its dedicated callback function.
00846   * @note   State field of Slave ADC handle is not updated in this configuration:
00847   *          user should not rely on it for information related to Slave regular
00848   *         conversions.
00849   * @param hadc ADC handle of ADC master (handle of ADC slave must not be used)
00850   * @param pData Destination Buffer address.
00851   * @param Length Length of data to be transferred from ADC peripheral to memory (in bytes).
00852   * @retval HAL status
00853   */
00854 HAL_StatusTypeDef HAL_ADCEx_MultiModeStart_DMA(ADC_HandleTypeDef *hadc, uint32_t *pData, uint32_t Length)
00855 {
00856   HAL_StatusTypeDef tmp_hal_status;
00857   ADC_HandleTypeDef tmphadcSlave;
00858   ADC_Common_TypeDef *tmpADC_Common;
00859 
00860   /* Check the parameters */
00861   assert_param(IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance));
00862   assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
00863   assert_param(IS_ADC_EXTTRIG_EDGE(hadc->Init.ExternalTrigConvEdge));
00864   assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DMAContinuousRequests));
00865 
00866   if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) != 0UL)
00867   {
00868     return HAL_BUSY;
00869   }
00870   else
00871   {
00872     /* Process locked */
00873     __HAL_LOCK(hadc);
00874 
00875     /* Temporary handle minimum initialization */
00876     __HAL_ADC_RESET_HANDLE_STATE(&tmphadcSlave);
00877     ADC_CLEAR_ERRORCODE(&tmphadcSlave);
00878 
00879     /* Set a temporary handle of the ADC slave associated to the ADC master   */
00880     ADC_MULTI_SLAVE(hadc, &tmphadcSlave);
00881 
00882     if (tmphadcSlave.Instance == NULL)
00883     {
00884       /* Set ADC state */
00885       SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
00886 
00887       /* Process unlocked */
00888       __HAL_UNLOCK(hadc);
00889 
00890       return HAL_ERROR;
00891     }
00892 
00893     /* Enable the ADC peripherals: master and slave (in case if not already   */
00894     /* enabled previously)                                                    */
00895     tmp_hal_status = ADC_Enable(hadc);
00896     if (tmp_hal_status == HAL_OK)
00897     {
00898       tmp_hal_status = ADC_Enable(&tmphadcSlave);
00899     }
00900 
00901     /* Start multimode conversion of ADCs pair */
00902     if (tmp_hal_status == HAL_OK)
00903     {
00904       /* Set ADC state */
00905       ADC_STATE_CLR_SET(hadc->State,
00906                         (HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP),
00907                         HAL_ADC_STATE_REG_BUSY);
00908 
00909       /* Set ADC error code to none */
00910       ADC_CLEAR_ERRORCODE(hadc);
00911 
00912       /* Set the DMA transfer complete callback */
00913       hadc->DMA_Handle->XferCpltCallback = ADC_DMAConvCplt;
00914 
00915       /* Set the DMA half transfer complete callback */
00916       hadc->DMA_Handle->XferHalfCpltCallback = ADC_DMAHalfConvCplt;
00917 
00918       /* Set the DMA error callback */
00919       hadc->DMA_Handle->XferErrorCallback = ADC_DMAError ;
00920 
00921       /* Pointer to the common control register  */
00922       tmpADC_Common = __LL_ADC_COMMON_INSTANCE(hadc->Instance);
00923 
00924       /* Manage ADC and DMA start: ADC overrun interruption, DMA start, ADC     */
00925       /* start (in case of SW start):                                           */
00926 
00927       /* Clear regular group conversion flag and overrun flag */
00928       /* (To ensure of no unknown state from potential previous ADC operations) */
00929       __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
00930 
00931       /* Process unlocked */
00932       /* Unlock before starting ADC conversions: in case of potential         */
00933       /* interruption, to let the process to ADC IRQ Handler.                 */
00934       __HAL_UNLOCK(hadc);
00935 
00936       /* Enable ADC overrun interrupt */
00937       __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);
00938 
00939       /* Start the DMA channel */
00940       tmp_hal_status = HAL_DMA_Start_IT(hadc->DMA_Handle, (uint32_t)&tmpADC_Common->CDR, (uint32_t)pData, Length);
00941 
00942       /* Enable conversion of regular group.                                    */
00943       /* If software start has been selected, conversion starts immediately.    */
00944       /* If external trigger has been selected, conversion will start at next   */
00945       /* trigger event.                                                         */
00946       /* Start ADC group regular conversion */
00947       LL_ADC_REG_StartConversion(hadc->Instance);
00948     }
00949     else
00950     {
00951       /* Process unlocked */
00952       __HAL_UNLOCK(hadc);
00953     }
00954 
00955     /* Return function status */
00956     return tmp_hal_status;
00957   }
00958 }
00959 
00960 /**
00961   * @brief  Stop multimode ADC conversion, disable ADC DMA transfer, disable ADC peripheral.
00962   * @note   Multimode is kept enabled after this function. MultiMode DMA bits
00963   *         (MDMA and DMACFG bits of common CCR register) are maintained. To disable
00964   *         Multimode (set with HAL_ADCEx_MultiModeConfigChannel()), ADC must be
00965   *         reinitialized using HAL_ADC_Init() or HAL_ADC_DeInit(), or the user can
00966   *         resort to HAL_ADCEx_DisableMultiMode() API.
00967   * @note   In case of DMA configured in circular mode, function
00968   *         HAL_ADC_Stop_DMA() must be called after this function with handle of
00969   *         ADC slave, to properly disable the DMA channel.
00970   * @param hadc ADC handle of ADC master (handle of ADC slave must not be used)
00971   * @retval HAL status
00972   */
00973 HAL_StatusTypeDef HAL_ADCEx_MultiModeStop_DMA(ADC_HandleTypeDef *hadc)
00974 {
00975   HAL_StatusTypeDef tmp_hal_status;
00976   uint32_t tickstart;
00977   ADC_HandleTypeDef tmphadcSlave;
00978   uint32_t tmphadcSlave_conversion_on_going;
00979   HAL_StatusTypeDef tmphadcSlave_disable_status;
00980 
00981   /* Check the parameters */
00982   assert_param(IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance));
00983 
00984   /* Process locked */
00985   __HAL_LOCK(hadc);
00986 
00987 
00988   /* 1. Stop potential multimode conversion on going, on regular and injected groups */
00989   tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_INJECTED_GROUP);
00990 
00991   /* Disable ADC peripheral if conversions are effectively stopped */
00992   if (tmp_hal_status == HAL_OK)
00993   {
00994     /* Temporary handle minimum initialization */
00995     __HAL_ADC_RESET_HANDLE_STATE(&tmphadcSlave);
00996     ADC_CLEAR_ERRORCODE(&tmphadcSlave);
00997 
00998     /* Set a temporary handle of the ADC slave associated to the ADC master   */
00999     ADC_MULTI_SLAVE(hadc, &tmphadcSlave);
01000 
01001     if (tmphadcSlave.Instance == NULL)
01002     {
01003       /* Update ADC state machine to error */
01004       SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
01005 
01006       /* Process unlocked */
01007       __HAL_UNLOCK(hadc);
01008 
01009       return HAL_ERROR;
01010     }
01011 
01012     /* Procedure to disable the ADC peripheral: wait for conversions          */
01013     /* effectively stopped (ADC master and ADC slave), then disable ADC       */
01014 
01015     /* 1. Wait for ADC conversion completion for ADC master and ADC slave */
01016     tickstart = HAL_GetTick();
01017 
01018     tmphadcSlave_conversion_on_going = LL_ADC_REG_IsConversionOngoing((&tmphadcSlave)->Instance);
01019     while ((LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 1UL)
01020            || (tmphadcSlave_conversion_on_going == 1UL)
01021           )
01022     {
01023       if ((HAL_GetTick() - tickstart) > ADC_STOP_CONVERSION_TIMEOUT)
01024       {
01025         /* New check to avoid false timeout detection in case of preemption */
01026         tmphadcSlave_conversion_on_going = LL_ADC_REG_IsConversionOngoing((&tmphadcSlave)->Instance);
01027         if ((LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 1UL)
01028             || (tmphadcSlave_conversion_on_going == 1UL)
01029            )
01030         {
01031           /* Update ADC state machine to error */
01032           SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
01033 
01034           /* Process unlocked */
01035           __HAL_UNLOCK(hadc);
01036 
01037           return HAL_ERROR;
01038         }
01039       }
01040 
01041       tmphadcSlave_conversion_on_going = LL_ADC_REG_IsConversionOngoing((&tmphadcSlave)->Instance);
01042     }
01043 
01044     /* Disable the DMA channel (in case of DMA in circular mode or stop       */
01045     /* while DMA transfer is on going)                                        */
01046     /* Note: DMA channel of ADC slave should be stopped after this function   */
01047     /*       with HAL_ADC_Stop_DMA() API.                                     */
01048     tmp_hal_status = HAL_DMA_Abort(hadc->DMA_Handle);
01049 
01050     /* Check if DMA channel effectively disabled */
01051     if (tmp_hal_status == HAL_ERROR)
01052     {
01053       /* Update ADC state machine to error */
01054       SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
01055     }
01056 
01057     /* Disable ADC overrun interrupt */
01058     __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);
01059 
01060     /* 2. Disable the ADC peripherals: master and slave */
01061     /* Update "tmp_hal_status" only if DMA channel disabling passed, to keep in */
01062     /* memory a potential failing status.                                     */
01063     if (tmp_hal_status == HAL_OK)
01064     {
01065       tmphadcSlave_disable_status = ADC_Disable(&tmphadcSlave);
01066       if ((ADC_Disable(hadc) == HAL_OK)           &&
01067           (tmphadcSlave_disable_status == HAL_OK))
01068       {
01069         tmp_hal_status = HAL_OK;
01070       }
01071     }
01072     else
01073     {
01074       /* In case of error, attempt to disable ADC master and slave without status assert */
01075       (void) ADC_Disable(hadc);
01076       (void) ADC_Disable(&tmphadcSlave);
01077     }
01078 
01079     /* Set ADC state (ADC master) */
01080     ADC_STATE_CLR_SET(hadc->State,
01081                       HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
01082                       HAL_ADC_STATE_READY);
01083   }
01084 
01085   /* Process unlocked */
01086   __HAL_UNLOCK(hadc);
01087 
01088   /* Return function status */
01089   return tmp_hal_status;
01090 }
01091 
01092 /**
01093   * @brief  Return the last ADC Master and Slave regular conversions results when in multimode configuration.
01094   * @param hadc ADC handle of ADC Master (handle of ADC Slave must not be used)
01095   * @retval The converted data values.
01096   */
01097 uint32_t HAL_ADCEx_MultiModeGetValue(ADC_HandleTypeDef *hadc)
01098 {
01099   const ADC_Common_TypeDef *tmpADC_Common;
01100 
01101   /* Check the parameters */
01102   assert_param(IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance));
01103 
01104   /* Prevent unused argument(s) compilation warning if no assert_param check */
01105   /* and possible no usage in __LL_ADC_COMMON_INSTANCE() below               */
01106   UNUSED(hadc);
01107 
01108   /* Pointer to the common control register  */
01109   tmpADC_Common = __LL_ADC_COMMON_INSTANCE(hadc->Instance);
01110 
01111   /* Return the multi mode conversion value */
01112   return tmpADC_Common->CDR;
01113 }
01114 #endif /* ADC_MULTIMODE_SUPPORT */
01115 
01116 /**
01117   * @brief  Get ADC injected group conversion result.
01118   * @note   Reading register JDRx automatically clears ADC flag JEOC
01119   *         (ADC group injected end of unitary conversion).
01120   * @note   This function does not clear ADC flag JEOS
01121   *         (ADC group injected end of sequence conversion)
01122   *         Occurrence of flag JEOS rising:
01123   *          - If sequencer is composed of 1 rank, flag JEOS is equivalent
01124   *            to flag JEOC.
01125   *          - If sequencer is composed of several ranks, during the scan
01126   *            sequence flag JEOC only is raised, at the end of the scan sequence
01127   *            both flags JEOC and EOS are raised.
01128   *         Flag JEOS must not be cleared by this function because
01129   *         it would not be compliant with low power features
01130   *         (feature low power auto-wait, not available on all STM32 families).
01131   *         To clear this flag, either use function:
01132   *         in programming model IT: @ref HAL_ADC_IRQHandler(), in programming
01133   *         model polling: @ref HAL_ADCEx_InjectedPollForConversion()
01134   *         or @ref __HAL_ADC_CLEAR_FLAG(&hadc, ADC_FLAG_JEOS).
01135   * @param hadc ADC handle
01136   * @param InjectedRank the converted ADC injected rank.
01137   *          This parameter can be one of the following values:
01138   *            @arg @ref ADC_INJECTED_RANK_1 ADC group injected rank 1
01139   *            @arg @ref ADC_INJECTED_RANK_2 ADC group injected rank 2
01140   *            @arg @ref ADC_INJECTED_RANK_3 ADC group injected rank 3
01141   *            @arg @ref ADC_INJECTED_RANK_4 ADC group injected rank 4
01142   * @retval ADC group injected conversion data
01143   */
01144 uint32_t HAL_ADCEx_InjectedGetValue(ADC_HandleTypeDef *hadc, uint32_t InjectedRank)
01145 {
01146   uint32_t tmp_jdr;
01147 
01148   /* Check the parameters */
01149   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
01150   assert_param(IS_ADC_INJECTED_RANK(InjectedRank));
01151 
01152   /* Get ADC converted value */
01153   switch (InjectedRank)
01154   {
01155     case ADC_INJECTED_RANK_4:
01156       tmp_jdr = hadc->Instance->JDR4;
01157       break;
01158     case ADC_INJECTED_RANK_3:
01159       tmp_jdr = hadc->Instance->JDR3;
01160       break;
01161     case ADC_INJECTED_RANK_2:
01162       tmp_jdr = hadc->Instance->JDR2;
01163       break;
01164     case ADC_INJECTED_RANK_1:
01165     default:
01166       tmp_jdr = hadc->Instance->JDR1;
01167       break;
01168   }
01169 
01170   /* Return ADC converted value */
01171   return tmp_jdr;
01172 }
01173 
01174 /**
01175   * @brief  Injected conversion complete callback in non-blocking mode.
01176   * @param hadc ADC handle
01177   * @retval None
01178   */
01179 __weak void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef *hadc)
01180 {
01181   /* Prevent unused argument(s) compilation warning */
01182   UNUSED(hadc);
01183 
01184   /* NOTE : This function should not be modified. When the callback is needed,
01185             function HAL_ADCEx_InjectedConvCpltCallback must be implemented in the user file.
01186   */
01187 }
01188 
01189 /**
01190   * @brief  Injected context queue overflow callback.
01191   * @note   This callback is called if injected context queue is enabled
01192             (parameter "QueueInjectedContext" in injected channel configuration)
01193             and if a new injected context is set when queue is full (maximum 2
01194             contexts).
01195   * @param hadc ADC handle
01196   * @retval None
01197   */
01198 __weak void HAL_ADCEx_InjectedQueueOverflowCallback(ADC_HandleTypeDef *hadc)
01199 {
01200   /* Prevent unused argument(s) compilation warning */
01201   UNUSED(hadc);
01202 
01203   /* NOTE : This function should not be modified. When the callback is needed,
01204             function HAL_ADCEx_InjectedQueueOverflowCallback must be implemented in the user file.
01205   */
01206 }
01207 
01208 /**
01209   * @brief  Analog watchdog 2 callback in non-blocking mode.
01210   * @param hadc ADC handle
01211   * @retval None
01212   */
01213 __weak void HAL_ADCEx_LevelOutOfWindow2Callback(ADC_HandleTypeDef *hadc)
01214 {
01215   /* Prevent unused argument(s) compilation warning */
01216   UNUSED(hadc);
01217 
01218   /* NOTE : This function should not be modified. When the callback is needed,
01219             function HAL_ADCEx_LevelOutOfWindow2Callback must be implemented in the user file.
01220   */
01221 }
01222 
01223 /**
01224   * @brief  Analog watchdog 3 callback in non-blocking mode.
01225   * @param hadc ADC handle
01226   * @retval None
01227   */
01228 __weak void HAL_ADCEx_LevelOutOfWindow3Callback(ADC_HandleTypeDef *hadc)
01229 {
01230   /* Prevent unused argument(s) compilation warning */
01231   UNUSED(hadc);
01232 
01233   /* NOTE : This function should not be modified. When the callback is needed,
01234             function HAL_ADCEx_LevelOutOfWindow3Callback must be implemented in the user file.
01235   */
01236 }
01237 
01238 
01239 /**
01240   * @brief  End Of Sampling callback in non-blocking mode.
01241   * @param hadc ADC handle
01242   * @retval None
01243   */
01244 __weak void HAL_ADCEx_EndOfSamplingCallback(ADC_HandleTypeDef *hadc)
01245 {
01246   /* Prevent unused argument(s) compilation warning */
01247   UNUSED(hadc);
01248 
01249   /* NOTE : This function should not be modified. When the callback is needed,
01250             function HAL_ADCEx_EndOfSamplingCallback must be implemented in the user file.
01251   */
01252 }
01253 
01254 /**
01255   * @brief  Stop ADC conversion of regular group (and injected channels in
01256   *         case of auto_injection mode), disable ADC peripheral if no
01257   *         conversion is on going on injected group.
01258   * @param hadc ADC handle
01259   * @retval HAL status.
01260   */
01261 HAL_StatusTypeDef HAL_ADCEx_RegularStop(ADC_HandleTypeDef *hadc)
01262 {
01263   HAL_StatusTypeDef tmp_hal_status;
01264 
01265   /* Check the parameters */
01266   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
01267 
01268   /* Process locked */
01269   __HAL_LOCK(hadc);
01270 
01271   /* 1. Stop potential regular conversion on going */
01272   tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_GROUP);
01273 
01274   /* Disable ADC peripheral if regular conversions are effectively stopped
01275      and if no injected conversions are on-going */
01276   if (tmp_hal_status == HAL_OK)
01277   {
01278     /* Clear HAL_ADC_STATE_REG_BUSY bit */
01279     CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
01280 
01281     if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) == 0UL)
01282     {
01283       /* 2. Disable the ADC peripheral */
01284       tmp_hal_status = ADC_Disable(hadc);
01285 
01286       /* Check if ADC is effectively disabled */
01287       if (tmp_hal_status == HAL_OK)
01288       {
01289         /* Set ADC state */
01290         ADC_STATE_CLR_SET(hadc->State,
01291                           HAL_ADC_STATE_INJ_BUSY,
01292                           HAL_ADC_STATE_READY);
01293       }
01294     }
01295     /* Conversion on injected group is stopped, but ADC not disabled since    */
01296     /* conversion on regular group is still running.                          */
01297     else
01298     {
01299       SET_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
01300     }
01301   }
01302 
01303   /* Process unlocked */
01304   __HAL_UNLOCK(hadc);
01305 
01306   /* Return function status */
01307   return tmp_hal_status;
01308 }
01309 
01310 
01311 /**
01312   * @brief  Stop ADC conversion of ADC groups regular and injected,
01313   *         disable interrution of end-of-conversion,
01314   *         disable ADC peripheral if no conversion is on going
01315   *         on injected group.
01316   * @param hadc ADC handle
01317   * @retval HAL status.
01318   */
01319 HAL_StatusTypeDef HAL_ADCEx_RegularStop_IT(ADC_HandleTypeDef *hadc)
01320 {
01321   HAL_StatusTypeDef tmp_hal_status;
01322 
01323   /* Check the parameters */
01324   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
01325 
01326   /* Process locked */
01327   __HAL_LOCK(hadc);
01328 
01329   /* 1. Stop potential regular conversion on going */
01330   tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_GROUP);
01331 
01332   /* Disable ADC peripheral if conversions are effectively stopped
01333     and if no injected conversion is on-going */
01334   if (tmp_hal_status == HAL_OK)
01335   {
01336     /* Clear HAL_ADC_STATE_REG_BUSY bit */
01337     CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
01338 
01339     /* Disable all regular-related interrupts */
01340     __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_EOS | ADC_IT_OVR));
01341 
01342     /* 2. Disable ADC peripheral if no injected conversions are on-going */
01343     if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) == 0UL)
01344     {
01345       tmp_hal_status = ADC_Disable(hadc);
01346       /* if no issue reported */
01347       if (tmp_hal_status == HAL_OK)
01348       {
01349         /* Set ADC state */
01350         ADC_STATE_CLR_SET(hadc->State,
01351                           HAL_ADC_STATE_INJ_BUSY,
01352                           HAL_ADC_STATE_READY);
01353       }
01354     }
01355     else
01356     {
01357       SET_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
01358     }
01359   }
01360 
01361   /* Process unlocked */
01362   __HAL_UNLOCK(hadc);
01363 
01364   /* Return function status */
01365   return tmp_hal_status;
01366 }
01367 
01368 /**
01369   * @brief  Stop ADC conversion of regular group (and injected group in
01370   *         case of auto_injection mode), disable ADC DMA transfer, disable
01371   *         ADC peripheral if no conversion is on going
01372   *         on injected group.
01373   * @note   HAL_ADCEx_RegularStop_DMA() function is dedicated to single-ADC mode only.
01374   *         For multimode (when multimode feature is available),
01375   *         HAL_ADCEx_RegularMultiModeStop_DMA() API must be used.
01376   * @param hadc ADC handle
01377   * @retval HAL status.
01378   */
01379 HAL_StatusTypeDef HAL_ADCEx_RegularStop_DMA(ADC_HandleTypeDef *hadc)
01380 {
01381   HAL_StatusTypeDef tmp_hal_status;
01382 
01383   /* Check the parameters */
01384   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
01385 
01386   /* Process locked */
01387   __HAL_LOCK(hadc);
01388 
01389   /* 1. Stop potential regular conversion on going */
01390   tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_GROUP);
01391 
01392   /* Disable ADC peripheral if conversions are effectively stopped
01393      and if no injected conversion is on-going */
01394   if (tmp_hal_status == HAL_OK)
01395   {
01396     /* Clear HAL_ADC_STATE_REG_BUSY bit */
01397     CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
01398 
01399     /* Disable ADC DMA (ADC DMA configuration ADC_CFGR_DMACFG is kept) */
01400     CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_DMAEN);
01401 
01402     /* Disable the DMA channel (in case of DMA in circular mode or stop while */
01403     /* while DMA transfer is on going)                                        */
01404     tmp_hal_status = HAL_DMA_Abort(hadc->DMA_Handle);
01405 
01406     /* Check if DMA channel effectively disabled */
01407     if (tmp_hal_status != HAL_OK)
01408     {
01409       /* Update ADC state machine to error */
01410       SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
01411     }
01412 
01413     /* Disable ADC overrun interrupt */
01414     __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);
01415 
01416     /* 2. Disable the ADC peripheral */
01417     /* Update "tmp_hal_status" only if DMA channel disabling passed,          */
01418     /* to keep in memory a potential failing status.                          */
01419     if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) == 0UL)
01420     {
01421       if (tmp_hal_status == HAL_OK)
01422       {
01423         tmp_hal_status = ADC_Disable(hadc);
01424       }
01425       else
01426       {
01427         (void)ADC_Disable(hadc);
01428       }
01429 
01430       /* Check if ADC is effectively disabled */
01431       if (tmp_hal_status == HAL_OK)
01432       {
01433         /* Set ADC state */
01434         ADC_STATE_CLR_SET(hadc->State,
01435                           HAL_ADC_STATE_INJ_BUSY,
01436                           HAL_ADC_STATE_READY);
01437       }
01438     }
01439     else
01440     {
01441       SET_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
01442     }
01443   }
01444 
01445   /* Process unlocked */
01446   __HAL_UNLOCK(hadc);
01447 
01448   /* Return function status */
01449   return tmp_hal_status;
01450 }
01451 
01452 #if defined(ADC_MULTIMODE_SUPPORT)
01453 /**
01454   * @brief  Stop DMA-based multimode ADC conversion, disable ADC DMA transfer, disable ADC peripheral if no injected conversion is on-going.
01455   * @note   Multimode is kept enabled after this function. Multimode DMA bits
01456   *         (MDMA and DMACFG bits of common CCR register) are maintained. To disable
01457   *         multimode (set with HAL_ADCEx_MultiModeConfigChannel()), ADC must be
01458   *         reinitialized using HAL_ADC_Init() or HAL_ADC_DeInit(), or the user can
01459   *         resort to HAL_ADCEx_DisableMultiMode() API.
01460   * @note   In case of DMA configured in circular mode, function
01461   *         HAL_ADCEx_RegularStop_DMA() must be called after this function with handle of
01462   *         ADC slave, to properly disable the DMA channel.
01463   * @param hadc ADC handle of ADC master (handle of ADC slave must not be used)
01464   * @retval HAL status
01465   */
01466 HAL_StatusTypeDef HAL_ADCEx_RegularMultiModeStop_DMA(ADC_HandleTypeDef *hadc)
01467 {
01468   HAL_StatusTypeDef tmp_hal_status;
01469   uint32_t tickstart;
01470   ADC_HandleTypeDef tmphadcSlave;
01471   uint32_t tmphadcSlave_conversion_on_going;
01472 
01473   /* Check the parameters */
01474   assert_param(IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance));
01475 
01476   /* Process locked */
01477   __HAL_LOCK(hadc);
01478 
01479 
01480   /* 1. Stop potential multimode conversion on going, on regular groups */
01481   tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_GROUP);
01482 
01483   /* Disable ADC peripheral if conversions are effectively stopped */
01484   if (tmp_hal_status == HAL_OK)
01485   {
01486     /* Clear HAL_ADC_STATE_REG_BUSY bit */
01487     CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
01488 
01489     /* Temporary handle minimum initialization */
01490     __HAL_ADC_RESET_HANDLE_STATE(&tmphadcSlave);
01491     ADC_CLEAR_ERRORCODE(&tmphadcSlave);
01492 
01493     /* Set a temporary handle of the ADC slave associated to the ADC master   */
01494     ADC_MULTI_SLAVE(hadc, &tmphadcSlave);
01495 
01496     if (tmphadcSlave.Instance == NULL)
01497     {
01498       /* Update ADC state machine to error */
01499       SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
01500 
01501       /* Process unlocked */
01502       __HAL_UNLOCK(hadc);
01503 
01504       return HAL_ERROR;
01505     }
01506 
01507     /* Procedure to disable the ADC peripheral: wait for conversions          */
01508     /* effectively stopped (ADC master and ADC slave), then disable ADC       */
01509 
01510     /* 1. Wait for ADC conversion completion for ADC master and ADC slave */
01511     tickstart = HAL_GetTick();
01512 
01513     tmphadcSlave_conversion_on_going = LL_ADC_REG_IsConversionOngoing((&tmphadcSlave)->Instance);
01514     while ((LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 1UL)
01515            || (tmphadcSlave_conversion_on_going == 1UL)
01516           )
01517     {
01518       if ((HAL_GetTick() - tickstart) > ADC_STOP_CONVERSION_TIMEOUT)
01519       {
01520         /* New check to avoid false timeout detection in case of preemption */
01521         tmphadcSlave_conversion_on_going = LL_ADC_REG_IsConversionOngoing((&tmphadcSlave)->Instance);
01522         if ((LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 1UL)
01523             || (tmphadcSlave_conversion_on_going == 1UL)
01524            )
01525         {
01526           /* Update ADC state machine to error */
01527           SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
01528 
01529           /* Process unlocked */
01530           __HAL_UNLOCK(hadc);
01531 
01532           return HAL_ERROR;
01533         }
01534       }
01535 
01536       tmphadcSlave_conversion_on_going = LL_ADC_REG_IsConversionOngoing((&tmphadcSlave)->Instance);
01537     }
01538 
01539     /* Disable the DMA channel (in case of DMA in circular mode or stop       */
01540     /* while DMA transfer is on going)                                        */
01541     /* Note: DMA channel of ADC slave should be stopped after this function   */
01542     /* with HAL_ADCEx_RegularStop_DMA() API.                                  */
01543     tmp_hal_status = HAL_DMA_Abort(hadc->DMA_Handle);
01544 
01545     /* Check if DMA channel effectively disabled */
01546     if (tmp_hal_status != HAL_OK)
01547     {
01548       /* Update ADC state machine to error */
01549       SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
01550     }
01551 
01552     /* Disable ADC overrun interrupt */
01553     __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);
01554 
01555     /* 2. Disable the ADC peripherals: master and slave if no injected        */
01556     /*   conversion is on-going.                                              */
01557     /* Update "tmp_hal_status" only if DMA channel disabling passed, to keep in */
01558     /* memory a potential failing status.                                     */
01559     if (tmp_hal_status == HAL_OK)
01560     {
01561       if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) == 0UL)
01562       {
01563         tmp_hal_status =  ADC_Disable(hadc);
01564         if (tmp_hal_status == HAL_OK)
01565         {
01566           if (LL_ADC_INJ_IsConversionOngoing((&tmphadcSlave)->Instance) == 0UL)
01567           {
01568             tmp_hal_status =  ADC_Disable(&tmphadcSlave);
01569           }
01570         }
01571       }
01572 
01573       if (tmp_hal_status == HAL_OK)
01574       {
01575         /* Both Master and Slave ADC's could be disabled. Update Master State */
01576         /* Clear HAL_ADC_STATE_INJ_BUSY bit, set HAL_ADC_STATE_READY bit */
01577         ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_BUSY, HAL_ADC_STATE_READY);
01578       }
01579       else
01580       {
01581         /* injected (Master or Slave) conversions are still on-going,
01582            no Master State change */
01583       }
01584     }
01585   }
01586 
01587   /* Process unlocked */
01588   __HAL_UNLOCK(hadc);
01589 
01590   /* Return function status */
01591   return tmp_hal_status;
01592 }
01593 #endif /* ADC_MULTIMODE_SUPPORT */
01594 
01595 /**
01596   * @}
01597   */
01598 
01599 /** @defgroup ADCEx_Exported_Functions_Group2 ADC Extended Peripheral Control functions
01600   * @brief    ADC Extended Peripheral Control functions
01601   *
01602 @verbatim
01603  ===============================================================================
01604              ##### Peripheral Control functions #####
01605  ===============================================================================
01606     [..]  This section provides functions allowing to:
01607       (+) Configure channels on injected group
01608       (+) Configure multimode when multimode feature is available
01609       (+) Enable or Disable Injected Queue
01610       (+) Disable ADC voltage regulator
01611       (+) Enter ADC deep-power-down mode
01612 
01613 @endverbatim
01614   * @{
01615   */
01616 
01617 /**
01618   * @brief  Configure a channel to be assigned to ADC group injected.
01619   * @note   Possibility to update parameters on the fly:
01620   *         This function initializes injected group, following calls to this
01621   *         function can be used to reconfigure some parameters of structure
01622   *         "ADC_InjectionConfTypeDef" on the fly, without resetting the ADC.
01623   *         The setting of these parameters is conditioned to ADC state:
01624   *         Refer to comments of structure "ADC_InjectionConfTypeDef".
01625   * @note   In case of usage of internal measurement channels:
01626   *         Vbat/VrefInt/TempSensor.
01627   *         These internal paths can be disabled using function
01628   *         HAL_ADC_DeInit().
01629   * @note   Caution: For Injected Context Queue use, a context must be fully
01630   *         defined before start of injected conversion. All channels are configured
01631   *         consecutively for the same ADC instance. Therefore, the number of calls to
01632   *         HAL_ADCEx_InjectedConfigChannel() must be equal to the value of parameter
01633   *         InjectedNbrOfConversion for each context.
01634   *  - Example 1: If 1 context is intended to be used (or if there is no use of the
01635   *    Injected Queue Context feature) and if the context contains 3 injected ranks
01636   *    (InjectedNbrOfConversion = 3), HAL_ADCEx_InjectedConfigChannel() must be
01637   *    called once for each channel (i.e. 3 times) before starting a conversion.
01638   *    This function must not be called to configure a 4th injected channel:
01639   *    it would start a new context into context queue.
01640   *  - Example 2: If 2 contexts are intended to be used and each of them contains
01641   *    3 injected ranks (InjectedNbrOfConversion = 3),
01642   *    HAL_ADCEx_InjectedConfigChannel() must be called once for each channel and
01643   *    for each context (3 channels x 2 contexts = 6 calls). Conversion can
01644   *    start once the 1st context is set, that is after the first three
01645   *    HAL_ADCEx_InjectedConfigChannel() calls. The 2nd context can be set on the fly.
01646   * @param hadc ADC handle
01647   * @param sConfigInjected Structure of ADC injected group and ADC channel for
01648   *         injected group.
01649   * @retval HAL status
01650   */
01651 HAL_StatusTypeDef HAL_ADCEx_InjectedConfigChannel(ADC_HandleTypeDef *hadc, ADC_InjectionConfTypeDef *sConfigInjected)
01652 {
01653   HAL_StatusTypeDef tmp_hal_status = HAL_OK;
01654   uint32_t tmpOffsetShifted;
01655   uint32_t tmp_config_internal_channel;
01656   uint32_t tmp_adc_is_conversion_on_going_regular;
01657   uint32_t tmp_adc_is_conversion_on_going_injected;
01658   __IO uint32_t wait_loop_index = 0;
01659 
01660   uint32_t tmp_JSQR_ContextQueueBeingBuilt = 0U;
01661 
01662   /* Check the parameters */
01663   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
01664   assert_param(IS_ADC_SAMPLE_TIME(sConfigInjected->InjectedSamplingTime));
01665   assert_param(IS_ADC_SINGLE_DIFFERENTIAL(sConfigInjected->InjectedSingleDiff));
01666   assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->AutoInjectedConv));
01667   assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->QueueInjectedContext));
01668   assert_param(IS_ADC_EXTTRIGINJEC_EDGE(sConfigInjected->ExternalTrigInjecConvEdge));
01669   assert_param(IS_ADC_EXTTRIGINJEC(hadc, sConfigInjected->ExternalTrigInjecConv));
01670   assert_param(IS_ADC_OFFSET_NUMBER(sConfigInjected->InjectedOffsetNumber));
01671   assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), sConfigInjected->InjectedOffset));
01672   assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->InjecOversamplingMode));
01673 
01674   if (hadc->Init.ScanConvMode != ADC_SCAN_DISABLE)
01675   {
01676     assert_param(IS_ADC_INJECTED_RANK(sConfigInjected->InjectedRank));
01677     assert_param(IS_ADC_INJECTED_NB_CONV(sConfigInjected->InjectedNbrOfConversion));
01678     assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->InjectedDiscontinuousConvMode));
01679   }
01680 
01681 
01682   /* if JOVSE is set, the value of the OFFSETy_EN bit in ADCx_OFRy register is
01683      ignored (considered as reset) */
01684   assert_param(!((sConfigInjected->InjectedOffsetNumber != ADC_OFFSET_NONE) && (sConfigInjected->InjecOversamplingMode == ENABLE)));
01685 
01686   /* JDISCEN and JAUTO bits can't be set at the same time  */
01687   assert_param(!((sConfigInjected->InjectedDiscontinuousConvMode == ENABLE) && (sConfigInjected->AutoInjectedConv == ENABLE)));
01688 
01689   /*  DISCEN and JAUTO bits can't be set at the same time */
01690   assert_param(!((hadc->Init.DiscontinuousConvMode == ENABLE) && (sConfigInjected->AutoInjectedConv == ENABLE)));
01691 
01692   /* Verification of channel number */
01693   if (sConfigInjected->InjectedSingleDiff != ADC_DIFFERENTIAL_ENDED)
01694   {
01695     assert_param(IS_ADC_CHANNEL(hadc, sConfigInjected->InjectedChannel));
01696   }
01697   else
01698   {
01699     assert_param(IS_ADC_DIFF_CHANNEL(hadc, sConfigInjected->InjectedChannel));
01700   }
01701 
01702   /* Process locked */
01703   __HAL_LOCK(hadc);
01704 
01705   /* Configuration of injected group sequencer:                               */
01706   /* Hardware constraint: Must fully define injected context register JSQR    */
01707   /* before make it entering into injected sequencer queue.                   */
01708   /*                                                                          */
01709   /* - if scan mode is disabled:                                              */
01710   /*    * Injected channels sequence length is set to 0x00: 1 channel         */
01711   /*      converted (channel on injected rank 1)                              */
01712   /*      Parameter "InjectedNbrOfConversion" is discarded.                   */
01713   /*    * Injected context register JSQR setting is simple: register is fully */
01714   /*      defined on one call of this function (for injected rank 1) and can  */
01715   /*      be entered into queue directly.                                     */
01716   /* - if scan mode is enabled:                                               */
01717   /*    * Injected channels sequence length is set to parameter               */
01718   /*      "InjectedNbrOfConversion".                                          */
01719   /*    * Injected context register JSQR setting more complex: register is    */
01720   /*      fully defined over successive calls of this function, for each      */
01721   /*      injected channel rank. It is entered into queue only when all       */
01722   /*      injected ranks have been set.                                       */
01723   /*   Note: Scan mode is not present by hardware on this device, but used    */
01724   /*   by software for alignment over all STM32 devices.                      */
01725 
01726   if ((hadc->Init.ScanConvMode == ADC_SCAN_DISABLE)  ||
01727       (sConfigInjected->InjectedNbrOfConversion == 1U))
01728   {
01729     /* Configuration of context register JSQR:                                */
01730     /*  - number of ranks in injected group sequencer: fixed to 1st rank      */
01731     /*    (scan mode disabled, only rank 1 used)                              */
01732     /*  - external trigger to start conversion                                */
01733     /*  - external trigger polarity                                           */
01734     /*  - channel set to rank 1 (scan mode disabled, only rank 1 can be used) */
01735 
01736     if (sConfigInjected->InjectedRank == ADC_INJECTED_RANK_1)
01737     {
01738       /* Enable external trigger if trigger selection is different of         */
01739       /* software start.                                                      */
01740       /* Note: This configuration keeps the hardware feature of parameter     */
01741       /*       ExternalTrigInjecConvEdge "trigger edge none" equivalent to    */
01742       /*       software start.                                                */
01743       if (sConfigInjected->ExternalTrigInjecConv != ADC_INJECTED_SOFTWARE_START)
01744       {
01745         tmp_JSQR_ContextQueueBeingBuilt = (ADC_JSQR_RK(sConfigInjected->InjectedChannel, ADC_INJECTED_RANK_1)
01746                                            | (sConfigInjected->ExternalTrigInjecConv & ADC_JSQR_JEXTSEL)
01747                                            | sConfigInjected->ExternalTrigInjecConvEdge
01748                                           );
01749       }
01750       else
01751       {
01752         tmp_JSQR_ContextQueueBeingBuilt = (ADC_JSQR_RK(sConfigInjected->InjectedChannel, ADC_INJECTED_RANK_1));
01753       }
01754 
01755       MODIFY_REG(hadc->Instance->JSQR, ADC_JSQR_FIELDS, tmp_JSQR_ContextQueueBeingBuilt);
01756       /* For debug and informative reasons, hadc handle saves JSQR setting */
01757       hadc->InjectionConfig.ContextQueue = tmp_JSQR_ContextQueueBeingBuilt;
01758 
01759     }
01760   }
01761   else
01762   {
01763     /* Case of scan mode enabled, several channels to set into injected group */
01764     /* sequencer.                                                             */
01765     /*                                                                        */
01766     /* Procedure to define injected context register JSQR over successive     */
01767     /* calls of this function, for each injected channel rank:                */
01768     /* 1. Start new context and set parameters related to all injected        */
01769     /*    channels: injected sequence length and trigger.                     */
01770 
01771     /* if hadc->InjectionConfig.ChannelCount is equal to 0, this is the first */
01772     /*   call of the context under setting                                    */
01773     if (hadc->InjectionConfig.ChannelCount == 0U)
01774     {
01775       /* Initialize number of channels that will be configured on the context */
01776       /*  being built                                                         */
01777       hadc->InjectionConfig.ChannelCount = sConfigInjected->InjectedNbrOfConversion;
01778       /* Handle hadc saves the context under build up over each HAL_ADCEx_InjectedConfigChannel()
01779          call, this context will be written in JSQR register at the last call.
01780          At this point, the context is merely reset  */
01781       hadc->InjectionConfig.ContextQueue = 0x00000000U;
01782 
01783       /* Configuration of context register JSQR:                              */
01784       /*  - number of ranks in injected group sequencer                       */
01785       /*  - external trigger to start conversion                              */
01786       /*  - external trigger polarity                                         */
01787 
01788       /* Enable external trigger if trigger selection is different of         */
01789       /* software start.                                                      */
01790       /* Note: This configuration keeps the hardware feature of parameter     */
01791       /*       ExternalTrigInjecConvEdge "trigger edge none" equivalent to    */
01792       /*       software start.                                                */
01793       if (sConfigInjected->ExternalTrigInjecConv != ADC_INJECTED_SOFTWARE_START)
01794       {
01795         tmp_JSQR_ContextQueueBeingBuilt = ((sConfigInjected->InjectedNbrOfConversion - 1U)
01796                                            | (sConfigInjected->ExternalTrigInjecConv & ADC_JSQR_JEXTSEL)
01797                                            | sConfigInjected->ExternalTrigInjecConvEdge
01798                                           );
01799       }
01800       else
01801       {
01802         tmp_JSQR_ContextQueueBeingBuilt = ((sConfigInjected->InjectedNbrOfConversion - 1U));
01803       }
01804 
01805     }
01806 
01807     /* 2. Continue setting of context under definition with parameter       */
01808     /*    related to each channel: channel rank sequence                    */
01809     /* Clear the old JSQx bits for the selected rank */
01810     tmp_JSQR_ContextQueueBeingBuilt &= ~ADC_JSQR_RK(ADC_SQR3_SQ10, sConfigInjected->InjectedRank);
01811 
01812     /* Set the JSQx bits for the selected rank */
01813     tmp_JSQR_ContextQueueBeingBuilt |= ADC_JSQR_RK(sConfigInjected->InjectedChannel, sConfigInjected->InjectedRank);
01814 
01815     /* Decrease channel count  */
01816     hadc->InjectionConfig.ChannelCount--;
01817 
01818     /* 3. tmp_JSQR_ContextQueueBeingBuilt is fully built for this HAL_ADCEx_InjectedConfigChannel()
01819           call, aggregate the setting to those already built during the previous
01820           HAL_ADCEx_InjectedConfigChannel() calls (for the same context of course)  */
01821     hadc->InjectionConfig.ContextQueue |= tmp_JSQR_ContextQueueBeingBuilt;
01822 
01823     /* 4. End of context setting: if this is the last channel set, then write context
01824         into register JSQR and make it enter into queue                   */
01825     if (hadc->InjectionConfig.ChannelCount == 0U)
01826     {
01827       MODIFY_REG(hadc->Instance->JSQR, ADC_JSQR_FIELDS, hadc->InjectionConfig.ContextQueue);
01828     }
01829   }
01830 
01831   /* Parameters update conditioned to ADC state:                              */
01832   /* Parameters that can be updated when ADC is disabled or enabled without   */
01833   /* conversion on going on injected group:                                   */
01834   /*  - Injected context queue: Queue disable (active context is kept) or     */
01835   /*    enable (context decremented, up to 2 contexts queued)                 */
01836   /*  - Injected discontinuous mode: can be enabled only if auto-injected     */
01837   /*    mode is disabled.                                                     */
01838   if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) == 0UL)
01839   {
01840     /* If auto-injected mode is disabled: no constraint                       */
01841     if (sConfigInjected->AutoInjectedConv == DISABLE)
01842     {
01843       MODIFY_REG(hadc->Instance->CFGR,
01844                  ADC_CFGR_JQM | ADC_CFGR_JDISCEN,
01845                  ADC_CFGR_INJECT_CONTEXT_QUEUE((uint32_t)sConfigInjected->QueueInjectedContext)           |
01846                  ADC_CFGR_INJECT_DISCCONTINUOUS((uint32_t)sConfigInjected->InjectedDiscontinuousConvMode));
01847     }
01848     /* If auto-injected mode is enabled: Injected discontinuous setting is    */
01849     /* discarded.                                                             */
01850     else
01851     {
01852       MODIFY_REG(hadc->Instance->CFGR,
01853                  ADC_CFGR_JQM | ADC_CFGR_JDISCEN,
01854                  ADC_CFGR_INJECT_CONTEXT_QUEUE((uint32_t)sConfigInjected->QueueInjectedContext));
01855     }
01856 
01857   }
01858 
01859   /* Parameters update conditioned to ADC state:                              */
01860   /* Parameters that can be updated when ADC is disabled or enabled without   */
01861   /* conversion on going on regular and injected groups:                      */
01862   /*  - Automatic injected conversion: can be enabled if injected group       */
01863   /*    external triggers are disabled.                                       */
01864   /*  - Channel sampling time                                                 */
01865   /*  - Channel offset                                                        */
01866   tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
01867   tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
01868 
01869   if ((tmp_adc_is_conversion_on_going_regular == 0UL)
01870       && (tmp_adc_is_conversion_on_going_injected == 0UL)
01871      )
01872   {
01873     /* If injected group external triggers are disabled (set to injected      */
01874     /* software start): no constraint                                         */
01875     if ((sConfigInjected->ExternalTrigInjecConv == ADC_INJECTED_SOFTWARE_START)
01876         || (sConfigInjected->ExternalTrigInjecConvEdge == ADC_EXTERNALTRIGINJECCONV_EDGE_NONE))
01877     {
01878       if (sConfigInjected->AutoInjectedConv == ENABLE)
01879       {
01880         SET_BIT(hadc->Instance->CFGR, ADC_CFGR_JAUTO);
01881       }
01882       else
01883       {
01884         CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_JAUTO);
01885       }
01886     }
01887     /* If Automatic injected conversion was intended to be set and could not  */
01888     /* due to injected group external triggers enabled, error is reported.    */
01889     else
01890     {
01891       if (sConfigInjected->AutoInjectedConv == ENABLE)
01892       {
01893         /* Update ADC state machine to error */
01894         SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
01895 
01896         tmp_hal_status = HAL_ERROR;
01897       }
01898       else
01899       {
01900         CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_JAUTO);
01901       }
01902     }
01903 
01904     if (sConfigInjected->InjecOversamplingMode == ENABLE)
01905     {
01906       assert_param(IS_ADC_OVERSAMPLING_RATIO(sConfigInjected->InjecOversampling.Ratio));
01907       assert_param(IS_ADC_RIGHT_BIT_SHIFT(sConfigInjected->InjecOversampling.RightBitShift));
01908 
01909       /*  JOVSE must be reset in case of triggered regular mode  */
01910       assert_param(!(READ_BIT(hadc->Instance->CFGR2, ADC_CFGR2_ROVSE | ADC_CFGR2_TROVS) == (ADC_CFGR2_ROVSE | ADC_CFGR2_TROVS)));
01911 
01912       /* Configuration of Injected Oversampler:                                 */
01913       /*  - Oversampling Ratio                                                  */
01914       /*  - Right bit shift                                                     */
01915 
01916       /* Enable OverSampling mode */
01917       MODIFY_REG(hadc->Instance->CFGR2,
01918                  ADC_CFGR2_JOVSE |
01919                  ADC_CFGR2_OVSR  |
01920                  ADC_CFGR2_OVSS,
01921                  ADC_CFGR2_JOVSE                                  |
01922                  sConfigInjected->InjecOversampling.Ratio         |
01923                  sConfigInjected->InjecOversampling.RightBitShift
01924                 );
01925     }
01926     else
01927     {
01928       /* Disable Regular OverSampling */
01929       CLEAR_BIT(hadc->Instance->CFGR2, ADC_CFGR2_JOVSE);
01930     }
01931 
01932 #if defined(ADC_SMPR1_SMPPLUS)
01933     /* Manage specific case of sampling time 3.5 cycles replacing 2.5 cyles */
01934     if (sConfigInjected->InjectedSamplingTime == ADC_SAMPLETIME_3CYCLES_5)
01935     {
01936       /* Set sampling time of the selected ADC channel */
01937       LL_ADC_SetChannelSamplingTime(hadc->Instance, sConfigInjected->InjectedChannel, LL_ADC_SAMPLINGTIME_2CYCLES_5);
01938 
01939       /* Set ADC sampling time common configuration */
01940       LL_ADC_SetSamplingTimeCommonConfig(hadc->Instance, LL_ADC_SAMPLINGTIME_COMMON_3C5_REPL_2C5);
01941     }
01942     else
01943     {
01944       /* Set sampling time of the selected ADC channel */
01945       LL_ADC_SetChannelSamplingTime(hadc->Instance, sConfigInjected->InjectedChannel, sConfigInjected->InjectedSamplingTime);
01946 
01947       /* Set ADC sampling time common configuration */
01948       LL_ADC_SetSamplingTimeCommonConfig(hadc->Instance, LL_ADC_SAMPLINGTIME_COMMON_DEFAULT);
01949     }
01950 #else
01951     /* Set sampling time of the selected ADC channel */
01952     LL_ADC_SetChannelSamplingTime(hadc->Instance, sConfigInjected->InjectedChannel, sConfigInjected->InjectedSamplingTime);
01953 #endif
01954 
01955     /* Configure the offset: offset enable/disable, channel, offset value */
01956 
01957     /* Shift the offset with respect to the selected ADC resolution. */
01958     /* Offset has to be left-aligned on bit 11, the LSB (right bits) are set to 0 */
01959     tmpOffsetShifted = ADC_OFFSET_SHIFT_RESOLUTION(hadc, sConfigInjected->InjectedOffset);
01960 
01961     if (sConfigInjected->InjectedOffsetNumber != ADC_OFFSET_NONE)
01962     {
01963       /* Set ADC selected offset number */
01964       LL_ADC_SetOffset(hadc->Instance, sConfigInjected->InjectedOffsetNumber, sConfigInjected->InjectedChannel,
01965                        tmpOffsetShifted);
01966 
01967     }
01968     else
01969     {
01970       /* Scan each offset register to check if the selected channel is targeted. */
01971       /* If this is the case, the corresponding offset number is disabled.       */
01972       if (__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_1))
01973           == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfigInjected->InjectedChannel))
01974       {
01975         LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_1, LL_ADC_OFFSET_DISABLE);
01976       }
01977       if (__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_2))
01978           == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfigInjected->InjectedChannel))
01979       {
01980         LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_2, LL_ADC_OFFSET_DISABLE);
01981       }
01982       if (__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_3))
01983           == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfigInjected->InjectedChannel))
01984       {
01985         LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_3, LL_ADC_OFFSET_DISABLE);
01986       }
01987       if (__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_4))
01988           == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfigInjected->InjectedChannel))
01989       {
01990         LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_4, LL_ADC_OFFSET_DISABLE);
01991       }
01992     }
01993 
01994   }
01995 
01996   /* Parameters update conditioned to ADC state:                              */
01997   /* Parameters that can be updated only when ADC is disabled:                */
01998   /*  - Single or differential mode                                           */
01999   if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
02000   {
02001     /* Set mode single-ended or differential input of the selected ADC channel */
02002     LL_ADC_SetChannelSingleDiff(hadc->Instance, sConfigInjected->InjectedChannel, sConfigInjected->InjectedSingleDiff);
02003 
02004     /* Configuration of differential mode */
02005     /* Note: ADC channel number masked with value "0x1F" to ensure shift value within 32 bits range */
02006     if (sConfigInjected->InjectedSingleDiff == ADC_DIFFERENTIAL_ENDED)
02007     {
02008       /* Set sampling time of the selected ADC channel */
02009       LL_ADC_SetChannelSamplingTime(hadc->Instance,
02010                                     (uint32_t)(__LL_ADC_DECIMAL_NB_TO_CHANNEL((__LL_ADC_CHANNEL_TO_DECIMAL_NB((uint32_t)sConfigInjected->InjectedChannel)
02011                                                                                + 1UL) & 0x1FUL)), sConfigInjected->InjectedSamplingTime);
02012     }
02013 
02014   }
02015 
02016   /* Management of internal measurement channels: Vbat/VrefInt/TempSensor   */
02017   /* internal measurement paths enable: If internal channel selected,       */
02018   /* enable dedicated internal buffers and path.                            */
02019   /* Note: these internal measurement paths can be disabled using           */
02020   /* HAL_ADC_DeInit().                                                      */
02021 
02022   if (__LL_ADC_IS_CHANNEL_INTERNAL(sConfigInjected->InjectedChannel))
02023   {
02024     tmp_config_internal_channel = LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
02025 
02026     /* If the requested internal measurement path has already been enabled,   */
02027     /* bypass the configuration processing.                                   */
02028     if ((sConfigInjected->InjectedChannel == ADC_CHANNEL_TEMPSENSOR)
02029         && ((tmp_config_internal_channel & LL_ADC_PATH_INTERNAL_TEMPSENSOR) == 0UL))
02030     {
02031       if (ADC_TEMPERATURE_SENSOR_INSTANCE(hadc))
02032       {
02033         LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance),
02034                                        LL_ADC_PATH_INTERNAL_TEMPSENSOR | tmp_config_internal_channel);
02035 
02036         /* Delay for temperature sensor stabilization time */
02037         /* Wait loop initialization and execution */
02038         /* Note: Variable divided by 2 to compensate partially              */
02039         /*       CPU processing cycles, scaling in us split to not          */
02040         /*       exceed 32 bits register capacity and handle low frequency. */
02041         wait_loop_index = ((LL_ADC_DELAY_TEMPSENSOR_STAB_US / 10UL) * (((SystemCoreClock / (100000UL * 2UL)) + 1UL) + 1UL));
02042         while (wait_loop_index != 0UL)
02043         {
02044           wait_loop_index--;
02045         }
02046       }
02047     }
02048     else if ((sConfigInjected->InjectedChannel == ADC_CHANNEL_VBAT)
02049              && ((tmp_config_internal_channel & LL_ADC_PATH_INTERNAL_VBAT) == 0UL))
02050     {
02051       if (ADC_BATTERY_VOLTAGE_INSTANCE(hadc))
02052       {
02053         LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance),
02054                                        LL_ADC_PATH_INTERNAL_VBAT | tmp_config_internal_channel);
02055       }
02056     }
02057     else if ((sConfigInjected->InjectedChannel == ADC_CHANNEL_VREFINT)
02058              && ((tmp_config_internal_channel & LL_ADC_PATH_INTERNAL_VREFINT) == 0UL))
02059     {
02060       if (ADC_VREFINT_INSTANCE(hadc))
02061       {
02062         LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance),
02063                                        LL_ADC_PATH_INTERNAL_VREFINT | tmp_config_internal_channel);
02064       }
02065     }
02066     else
02067     {
02068       /* nothing to do */
02069     }
02070   }
02071 
02072   /* Process unlocked */
02073   __HAL_UNLOCK(hadc);
02074 
02075   /* Return function status */
02076   return tmp_hal_status;
02077 }
02078 
02079 #if defined(ADC_MULTIMODE_SUPPORT)
02080 /**
02081   * @brief  Enable ADC multimode and configure multimode parameters
02082   * @note   Possibility to update parameters on the fly:
02083   *         This function initializes multimode parameters, following
02084   *         calls to this function can be used to reconfigure some parameters
02085   *         of structure "ADC_MultiModeTypeDef" on the fly, without resetting
02086   *         the ADCs.
02087   *         The setting of these parameters is conditioned to ADC state.
02088   *         For parameters constraints, see comments of structure
02089   *         "ADC_MultiModeTypeDef".
02090   * @note   To move back configuration from multimode to single mode, ADC must
02091   *         be reset (using function HAL_ADC_Init() ).
02092   * @param hadc Master ADC handle
02093   * @param multimode Structure of ADC multimode configuration
02094   * @retval HAL status
02095   */
02096 HAL_StatusTypeDef HAL_ADCEx_MultiModeConfigChannel(ADC_HandleTypeDef *hadc, ADC_MultiModeTypeDef *multimode)
02097 {
02098   HAL_StatusTypeDef tmp_hal_status = HAL_OK;
02099   ADC_Common_TypeDef *tmpADC_Common;
02100   ADC_HandleTypeDef tmphadcSlave;
02101   uint32_t tmphadcSlave_conversion_on_going;
02102 
02103   /* Check the parameters */
02104   assert_param(IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance));
02105   assert_param(IS_ADC_MULTIMODE(multimode->Mode));
02106   if (multimode->Mode != ADC_MODE_INDEPENDENT)
02107   {
02108     assert_param(IS_ADC_DMA_ACCESS_MULTIMODE(multimode->DMAAccessMode));
02109     assert_param(IS_ADC_SAMPLING_DELAY(multimode->TwoSamplingDelay));
02110   }
02111 
02112   /* Process locked */
02113   __HAL_LOCK(hadc);
02114 
02115   /* Temporary handle minimum initialization */
02116   __HAL_ADC_RESET_HANDLE_STATE(&tmphadcSlave);
02117   ADC_CLEAR_ERRORCODE(&tmphadcSlave);
02118 
02119   ADC_MULTI_SLAVE(hadc, &tmphadcSlave);
02120 
02121   if (tmphadcSlave.Instance == NULL)
02122   {
02123     /* Update ADC state machine to error */
02124     SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
02125 
02126     /* Process unlocked */
02127     __HAL_UNLOCK(hadc);
02128 
02129     return HAL_ERROR;
02130   }
02131 
02132   /* Parameters update conditioned to ADC state:                              */
02133   /* Parameters that can be updated when ADC is disabled or enabled without   */
02134   /* conversion on going on regular group:                                    */
02135   /*  - Multimode DMA configuration                                           */
02136   /*  - Multimode DMA mode                                                    */
02137   tmphadcSlave_conversion_on_going = LL_ADC_REG_IsConversionOngoing((&tmphadcSlave)->Instance);
02138   if ((LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
02139       && (tmphadcSlave_conversion_on_going == 0UL))
02140   {
02141     /* Pointer to the common control register */
02142     tmpADC_Common = __LL_ADC_COMMON_INSTANCE(hadc->Instance);
02143 
02144     /* If multimode is selected, configure all multimode parameters.          */
02145     /* Otherwise, reset multimode parameters (can be used in case of          */
02146     /* transition from multimode to independent mode).                        */
02147     if (multimode->Mode != ADC_MODE_INDEPENDENT)
02148     {
02149       MODIFY_REG(tmpADC_Common->CCR, ADC_CCR_MDMA | ADC_CCR_DMACFG,
02150                  multimode->DMAAccessMode |
02151                  ADC_CCR_MULTI_DMACONTREQ((uint32_t)hadc->Init.DMAContinuousRequests));
02152 
02153       /* Parameters that can be updated only when ADC is disabled:                */
02154       /*  - Multimode mode selection                                              */
02155       /*  - Multimode delay                                                       */
02156       /*    Note: Delay range depends on selected resolution:                     */
02157       /*      from 1 to 12 clock cycles for 12 bits                               */
02158       /*      from 1 to 10 clock cycles for 10 bits,                              */
02159       /*      from 1 to 8 clock cycles for 8 bits                                 */
02160       /*      from 1 to 6 clock cycles for 6 bits                                 */
02161       /*    If a higher delay is selected, it will be clipped to maximum delay    */
02162       /*    range                                                                 */
02163       if (__LL_ADC_IS_ENABLED_ALL_COMMON_INSTANCE(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) == 0UL)
02164       {
02165         MODIFY_REG(tmpADC_Common->CCR,
02166                    ADC_CCR_DUAL |
02167                    ADC_CCR_DELAY,
02168                    multimode->Mode |
02169                    multimode->TwoSamplingDelay
02170                   );
02171       }
02172     }
02173     else /* ADC_MODE_INDEPENDENT */
02174     {
02175       CLEAR_BIT(tmpADC_Common->CCR, ADC_CCR_MDMA | ADC_CCR_DMACFG);
02176 
02177       /* Parameters that can be updated only when ADC is disabled:                */
02178       /*  - Multimode mode selection                                              */
02179       /*  - Multimode delay                                                       */
02180       if (__LL_ADC_IS_ENABLED_ALL_COMMON_INSTANCE(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) == 0UL)
02181       {
02182         CLEAR_BIT(tmpADC_Common->CCR, ADC_CCR_DUAL | ADC_CCR_DELAY);
02183       }
02184     }
02185   }
02186   /* If one of the ADC sharing the same common group is enabled, no update    */
02187   /* could be done on neither of the multimode structure parameters.          */
02188   else
02189   {
02190     /* Update ADC state machine to error */
02191     SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
02192 
02193     tmp_hal_status = HAL_ERROR;
02194   }
02195 
02196   /* Process unlocked */
02197   __HAL_UNLOCK(hadc);
02198 
02199   /* Return function status */
02200   return tmp_hal_status;
02201 }
02202 #endif /* ADC_MULTIMODE_SUPPORT */
02203 
02204 /**
02205   * @brief  Enable Injected Queue
02206   * @note   This function resets CFGR register JQDIS bit in order to enable the
02207   *         Injected Queue. JQDIS can be written only when ADSTART and JDSTART
02208   *         are both equal to 0 to ensure that no regular nor injected
02209   *         conversion is ongoing.
02210   * @param hadc ADC handle
02211   * @retval HAL status
02212   */
02213 HAL_StatusTypeDef HAL_ADCEx_EnableInjectedQueue(ADC_HandleTypeDef *hadc)
02214 {
02215   HAL_StatusTypeDef tmp_hal_status;
02216   uint32_t tmp_adc_is_conversion_on_going_regular;
02217   uint32_t tmp_adc_is_conversion_on_going_injected;
02218 
02219   /* Check the parameters */
02220   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
02221 
02222   tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
02223   tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
02224 
02225   /* Parameter can be set only if no conversion is on-going */
02226   if ((tmp_adc_is_conversion_on_going_regular == 0UL)
02227       && (tmp_adc_is_conversion_on_going_injected == 0UL)
02228      )
02229   {
02230     CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_JQDIS);
02231 
02232     /* Update state, clear previous result related to injected queue overflow */
02233     CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_JQOVF);
02234 
02235     tmp_hal_status = HAL_OK;
02236   }
02237   else
02238   {
02239     tmp_hal_status = HAL_ERROR;
02240   }
02241 
02242   return tmp_hal_status;
02243 }
02244 
02245 /**
02246   * @brief  Disable Injected Queue
02247   * @note   This function sets CFGR register JQDIS bit in order to disable the
02248   *         Injected Queue. JQDIS can be written only when ADSTART and JDSTART
02249   *         are both equal to 0 to ensure that no regular nor injected
02250   *         conversion is ongoing.
02251   * @param hadc ADC handle
02252   * @retval HAL status
02253   */
02254 HAL_StatusTypeDef HAL_ADCEx_DisableInjectedQueue(ADC_HandleTypeDef *hadc)
02255 {
02256   HAL_StatusTypeDef tmp_hal_status;
02257   uint32_t tmp_adc_is_conversion_on_going_regular;
02258   uint32_t tmp_adc_is_conversion_on_going_injected;
02259 
02260   /* Check the parameters */
02261   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
02262 
02263   tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
02264   tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
02265 
02266   /* Parameter can be set only if no conversion is on-going */
02267   if ((tmp_adc_is_conversion_on_going_regular == 0UL)
02268       && (tmp_adc_is_conversion_on_going_injected == 0UL)
02269      )
02270   {
02271     LL_ADC_INJ_SetQueueMode(hadc->Instance, LL_ADC_INJ_QUEUE_DISABLE);
02272     tmp_hal_status = HAL_OK;
02273   }
02274   else
02275   {
02276     tmp_hal_status = HAL_ERROR;
02277   }
02278 
02279   return tmp_hal_status;
02280 }
02281 
02282 /**
02283   * @brief  Disable ADC voltage regulator.
02284   * @note   Disabling voltage regulator allows to save power. This operation can
02285   *         be carried out only when ADC is disabled.
02286   * @note   To enable again the voltage regulator, the user is expected to
02287   *         resort to HAL_ADC_Init() API.
02288   * @param hadc ADC handle
02289   * @retval HAL status
02290   */
02291 HAL_StatusTypeDef HAL_ADCEx_DisableVoltageRegulator(ADC_HandleTypeDef *hadc)
02292 {
02293   HAL_StatusTypeDef tmp_hal_status;
02294 
02295   /* Check the parameters */
02296   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
02297 
02298   /* Setting of this feature is conditioned to ADC state: ADC must be ADC disabled */
02299   if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
02300   {
02301     LL_ADC_DisableInternalRegulator(hadc->Instance);
02302     tmp_hal_status = HAL_OK;
02303   }
02304   else
02305   {
02306     tmp_hal_status = HAL_ERROR;
02307   }
02308 
02309   return tmp_hal_status;
02310 }
02311 
02312 /**
02313   * @brief  Enter ADC deep-power-down mode
02314   * @note   This mode is achieved in setting DEEPPWD bit and allows to save power
02315   *         in reducing leakage currents. It is particularly interesting before
02316   *         entering stop modes.
02317   * @note   Setting DEEPPWD automatically clears ADVREGEN bit and disables the
02318   *         ADC voltage regulator. This means that this API encompasses
02319   *         HAL_ADCEx_DisableVoltageRegulator(). Additionally, the internal
02320   *         calibration is lost.
02321   * @note   To exit the ADC deep-power-down mode, the user is expected to
02322   *         resort to HAL_ADC_Init() API as well as to relaunch a calibration
02323   *         with HAL_ADCEx_Calibration_Start() API or to re-apply a previously
02324   *         saved calibration factor.
02325   * @param hadc ADC handle
02326   * @retval HAL status
02327   */
02328 HAL_StatusTypeDef HAL_ADCEx_EnterADCDeepPowerDownMode(ADC_HandleTypeDef *hadc)
02329 {
02330   HAL_StatusTypeDef tmp_hal_status;
02331 
02332   /* Check the parameters */
02333   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
02334 
02335   /* Setting of this feature is conditioned to ADC state: ADC must be ADC disabled */
02336   if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
02337   {
02338     LL_ADC_EnableDeepPowerDown(hadc->Instance);
02339     tmp_hal_status = HAL_OK;
02340   }
02341   else
02342   {
02343     tmp_hal_status = HAL_ERROR;
02344   }
02345 
02346   return tmp_hal_status;
02347 }
02348 
02349 /**
02350   * @}
02351   */
02352 
02353 /**
02354   * @}
02355   */
02356 
02357 #endif /* HAL_ADC_MODULE_ENABLED */
02358 /**
02359   * @}
02360   */
02361 
02362 /**
02363   * @}
02364   */