STM32L4xx_HAL_Driver  1.14.0
Initialization and de-initialization functions

ADC Initialization and Configuration functions. More...

Functions

HAL_StatusTypeDef HAL_ADC_Init (ADC_HandleTypeDef *hadc)
 Initialize the ADC peripheral and regular group according to parameters specified in structure "ADC_InitTypeDef". More...
 
HAL_StatusTypeDef HAL_ADC_DeInit (ADC_HandleTypeDef *hadc)
 Deinitialize the ADC peripheral registers to their default reset values, with deinitialization of the ADC MSP. More...
 
void HAL_ADC_MspInit (ADC_HandleTypeDef *hadc)
 Initialize the ADC MSP. More...
 
void HAL_ADC_MspDeInit (ADC_HandleTypeDef *hadc)
 DeInitialize the ADC MSP. More...
 
HAL_StatusTypeDef HAL_ADC_RegisterCallback (ADC_HandleTypeDef *hadc, HAL_ADC_CallbackIDTypeDef CallbackID, pADC_CallbackTypeDef pCallback)
 Register a User ADC Callback To be used instead of the weak predefined callback. More...
 
HAL_StatusTypeDef HAL_ADC_UnRegisterCallback (ADC_HandleTypeDef *hadc, HAL_ADC_CallbackIDTypeDef CallbackID)
 Unregister a ADC Callback ADC callback is redirected to the weak predefined callback. More...
 

Detailed Description

ADC Initialization and Configuration functions.

Initialization and Configuration functions.

 ===============================================================================
              ##### Initialization and de-initialization functions #####
 ===============================================================================
    [..]  This section provides functions allowing to:
      (+) Initialize and configure the ADC.
      (+) De-initialize the ADC.

Function Documentation

◆ HAL_ADC_DeInit()

HAL_StatusTypeDef HAL_ADC_DeInit ( ADC_HandleTypeDef hadc)

Deinitialize the ADC peripheral registers to their default reset values, with deinitialization of the ADC MSP.

Note
For devices with several ADCs: reset of ADC common registers is done only if all ADCs sharing the same common group are disabled. (function "HAL_ADC_MspDeInit()" is also called under the same conditions: all ADC instances use the same core clock at RCC level, disabling the core clock reset all ADC instances). If this is not the case, reset of these common parameters reset is bypassed without error reporting: it can be the intended behavior in case of reset of a single ADC while the other ADCs sharing the same common group is still running.
By default, HAL_ADC_DeInit() set ADC in mode deep power-down: this saves more power by reducing leakage currents and is particularly interesting before entering MCU low-power modes.
Parameters
hadcADC handle
Return values
HALstatus

Definition at line 707 of file stm32l4xx_hal_adc.c.

708 {
709  HAL_StatusTypeDef tmp_hal_status;
710 
711  /* Check ADC handle */
712  if (hadc == NULL)
713  {
714  return HAL_ERROR;
715  }
716 
717  /* Check the parameters */
718  assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
719 
720  /* Set ADC state */
721  SET_BIT(hadc->State, HAL_ADC_STATE_BUSY_INTERNAL);
722 
723  /* Stop potential conversion on going */
724  tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_INJECTED_GROUP);
725 
726  /* Disable ADC peripheral if conversions are effectively stopped */
727  /* Flush register JSQR: reset the queue sequencer when injected */
728  /* queue sequencer is enabled and ADC disabled. */
729  /* The software and hardware triggers of the injected sequence are both */
730  /* internally disabled just after the completion of the last valid */
731  /* injected sequence. */
732  SET_BIT(hadc->Instance->CFGR, ADC_CFGR_JQM);
733 
734  /* Disable ADC peripheral if conversions are effectively stopped */
735  if (tmp_hal_status == HAL_OK)
736  {
737  /* Disable the ADC peripheral */
738  tmp_hal_status = ADC_Disable(hadc);
739 
740  /* Check if ADC is effectively disabled */
741  if (tmp_hal_status == HAL_OK)
742  {
743  /* Change ADC state */
744  hadc->State = HAL_ADC_STATE_READY;
745  }
746  }
747 
748  /* Note: HAL ADC deInit is done independently of ADC conversion stop */
749  /* and disable return status. In case of status fail, attempt to */
750  /* perform deinitialization anyway and it is up user code in */
751  /* in HAL_ADC_MspDeInit() to reset the ADC peripheral using */
752  /* system RCC hard reset. */
753 
754  /* ========== Reset ADC registers ========== */
755  /* Reset register IER */
756  __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_AWD3 | ADC_IT_AWD2 | ADC_IT_AWD1 |
757  ADC_IT_JQOVF | ADC_IT_OVR |
758  ADC_IT_JEOS | ADC_IT_JEOC |
759  ADC_IT_EOS | ADC_IT_EOC |
760  ADC_IT_EOSMP | ADC_IT_RDY));
761 
762  /* Reset register ISR */
763  __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_AWD3 | ADC_FLAG_AWD2 | ADC_FLAG_AWD1 |
764  ADC_FLAG_JQOVF | ADC_FLAG_OVR |
765  ADC_FLAG_JEOS | ADC_FLAG_JEOC |
766  ADC_FLAG_EOS | ADC_FLAG_EOC |
767  ADC_FLAG_EOSMP | ADC_FLAG_RDY));
768 
769  /* Reset register CR */
770  /* Bits ADC_CR_JADSTP, ADC_CR_ADSTP, ADC_CR_JADSTART, ADC_CR_ADSTART,
771  ADC_CR_ADCAL, ADC_CR_ADDIS and ADC_CR_ADEN are in access mode "read-set":
772  no direct reset applicable.
773  Update CR register to reset value where doable by software */
774  CLEAR_BIT(hadc->Instance->CR, ADC_CR_ADVREGEN | ADC_CR_ADCALDIF);
775  SET_BIT(hadc->Instance->CR, ADC_CR_DEEPPWD);
776 
777  /* Reset register CFGR */
778  CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_FIELDS);
779  SET_BIT(hadc->Instance->CFGR, ADC_CFGR_JQDIS);
780 
781  /* Reset register CFGR2 */
782  CLEAR_BIT(hadc->Instance->CFGR2, ADC_CFGR2_ROVSM | ADC_CFGR2_TROVS | ADC_CFGR2_OVSS |
783  ADC_CFGR2_OVSR | ADC_CFGR2_JOVSE | ADC_CFGR2_ROVSE);
784 
785  /* Reset register SMPR1 */
786  CLEAR_BIT(hadc->Instance->SMPR1, ADC_SMPR1_FIELDS);
787 
788  /* Reset register SMPR2 */
789  CLEAR_BIT(hadc->Instance->SMPR2, ADC_SMPR2_SMP18 | ADC_SMPR2_SMP17 | ADC_SMPR2_SMP16 |
790  ADC_SMPR2_SMP15 | ADC_SMPR2_SMP14 | ADC_SMPR2_SMP13 |
791  ADC_SMPR2_SMP12 | ADC_SMPR2_SMP11 | ADC_SMPR2_SMP10);
792 
793  /* Reset register TR1 */
794  CLEAR_BIT(hadc->Instance->TR1, ADC_TR1_HT1 | ADC_TR1_LT1);
795 
796  /* Reset register TR2 */
797  CLEAR_BIT(hadc->Instance->TR2, ADC_TR2_HT2 | ADC_TR2_LT2);
798 
799  /* Reset register TR3 */
800  CLEAR_BIT(hadc->Instance->TR3, ADC_TR3_HT3 | ADC_TR3_LT3);
801 
802  /* Reset register SQR1 */
803  CLEAR_BIT(hadc->Instance->SQR1, ADC_SQR1_SQ4 | ADC_SQR1_SQ3 | ADC_SQR1_SQ2 |
804  ADC_SQR1_SQ1 | ADC_SQR1_L);
805 
806  /* Reset register SQR2 */
807  CLEAR_BIT(hadc->Instance->SQR2, ADC_SQR2_SQ9 | ADC_SQR2_SQ8 | ADC_SQR2_SQ7 |
808  ADC_SQR2_SQ6 | ADC_SQR2_SQ5);
809 
810  /* Reset register SQR3 */
811  CLEAR_BIT(hadc->Instance->SQR3, ADC_SQR3_SQ14 | ADC_SQR3_SQ13 | ADC_SQR3_SQ12 |
812  ADC_SQR3_SQ11 | ADC_SQR3_SQ10);
813 
814  /* Reset register SQR4 */
815  CLEAR_BIT(hadc->Instance->SQR4, ADC_SQR4_SQ16 | ADC_SQR4_SQ15);
816 
817  /* Register JSQR was reset when the ADC was disabled */
818 
819  /* Reset register DR */
820  /* bits in access mode read only, no direct reset applicable*/
821 
822  /* Reset register OFR1 */
823  CLEAR_BIT(hadc->Instance->OFR1, ADC_OFR1_OFFSET1_EN | ADC_OFR1_OFFSET1_CH | ADC_OFR1_OFFSET1);
824  /* Reset register OFR2 */
825  CLEAR_BIT(hadc->Instance->OFR2, ADC_OFR2_OFFSET2_EN | ADC_OFR2_OFFSET2_CH | ADC_OFR2_OFFSET2);
826  /* Reset register OFR3 */
827  CLEAR_BIT(hadc->Instance->OFR3, ADC_OFR3_OFFSET3_EN | ADC_OFR3_OFFSET3_CH | ADC_OFR3_OFFSET3);
828  /* Reset register OFR4 */
829  CLEAR_BIT(hadc->Instance->OFR4, ADC_OFR4_OFFSET4_EN | ADC_OFR4_OFFSET4_CH | ADC_OFR4_OFFSET4);
830 
831  /* Reset registers JDR1, JDR2, JDR3, JDR4 */
832  /* bits in access mode read only, no direct reset applicable*/
833 
834  /* Reset register AWD2CR */
835  CLEAR_BIT(hadc->Instance->AWD2CR, ADC_AWD2CR_AWD2CH);
836 
837  /* Reset register AWD3CR */
838  CLEAR_BIT(hadc->Instance->AWD3CR, ADC_AWD3CR_AWD3CH);
839 
840  /* Reset register DIFSEL */
841  CLEAR_BIT(hadc->Instance->DIFSEL, ADC_DIFSEL_DIFSEL);
842 
843  /* Reset register CALFACT */
844  CLEAR_BIT(hadc->Instance->CALFACT, ADC_CALFACT_CALFACT_D | ADC_CALFACT_CALFACT_S);
845 
846 
847  /* ========== Reset common ADC registers ========== */
848 
849  /* Software is allowed to change common parameters only when all the other
850  ADCs are disabled. */
851  if (__LL_ADC_IS_ENABLED_ALL_COMMON_INSTANCE(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) == 0UL)
852  {
853  /* Reset configuration of ADC common register CCR:
854  - clock mode: CKMODE, PRESCEN
855  - multimode related parameters (when this feature is available): MDMA,
856  DMACFG, DELAY, DUAL (set by HAL_ADCEx_MultiModeConfigChannel() API)
857  - internal measurement paths: Vbat, temperature sensor, Vref (set into
858  HAL_ADC_ConfigChannel() or HAL_ADCEx_InjectedConfigChannel() )
859  */
860  ADC_CLEAR_COMMON_CONTROL_REGISTER(hadc);
861  }
862 
863  /* DeInit the low level hardware.
864 
865  For example:
866  __HAL_RCC_ADC_FORCE_RESET();
867  __HAL_RCC_ADC_RELEASE_RESET();
868  __HAL_RCC_ADC_CLK_DISABLE();
869 
870  Keep in mind that all ADCs use the same clock: disabling
871  the clock will reset all ADCs.
872 
873  */
874 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
875  if (hadc->MspDeInitCallback == NULL)
876  {
877  hadc->MspDeInitCallback = HAL_ADC_MspDeInit; /* Legacy weak MspDeInit */
878  }
879 
880  /* DeInit the low level hardware: RCC clock, NVIC */
881  hadc->MspDeInitCallback(hadc);
882 #else
883  /* DeInit the low level hardware: RCC clock, NVIC */
884  HAL_ADC_MspDeInit(hadc);
885 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
886 
887  /* Set ADC error code to none */
888  ADC_CLEAR_ERRORCODE(hadc);
889 
890  /* Reset injected channel configuration parameters */
891  hadc->InjectionConfig.ContextQueue = 0;
892  hadc->InjectionConfig.ChannelCount = 0;
893 
894  /* Set ADC state */
895  hadc->State = HAL_ADC_STATE_RESET;
896 
897  /* Process unlocked */
898  __HAL_UNLOCK(hadc);
899 
900  /* Return function status */
901  return tmp_hal_status;
902 }
__HAL_UNLOCK(hrtc)
CLEAR_BIT(hrtc->Instance->CR, RTC_CR_WUTE)
return HAL_OK
HAL_StatusTypeDef ADC_ConversionStop(ADC_HandleTypeDef *hadc, uint32_t ConversionGroup)
Stop ADC conversion.
HAL_StatusTypeDef ADC_Disable(ADC_HandleTypeDef *hadc)
Disable the selected ADC.
void HAL_ADC_MspDeInit(ADC_HandleTypeDef *hadc)
DeInitialize the ADC MSP.
assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock))

◆ HAL_ADC_Init()

HAL_StatusTypeDef HAL_ADC_Init ( ADC_HandleTypeDef hadc)

Initialize the ADC peripheral and regular group according to parameters specified in structure "ADC_InitTypeDef".

Note
As prerequisite, ADC clock must be configured at RCC top level (refer to description of RCC configuration for ADC in header of this file).
Possibility to update parameters on the fly: This function initializes the ADC MSP (HAL_ADC_MspInit()) only when coming from ADC state reset. Following calls to this function can be used to reconfigure some parameters of ADC_InitTypeDef structure on the fly, without modifying MSP configuration. If ADC MSP has to be modified again, HAL_ADC_DeInit() must be called before HAL_ADC_Init(). The setting of these parameters is conditioned to ADC state. For parameters constraints, see comments of structure "ADC_InitTypeDef".
This function configures the ADC within 2 scopes: scope of entire ADC and scope of regular group. For parameters details, see comments of structure "ADC_InitTypeDef".
Parameters related to common ADC registers (ADC clock mode) are set only if all ADCs are disabled. If this is not the case, these common parameters setting are bypassed without error reporting: it can be the intended behaviour in case of update of a parameter of ADC_InitTypeDef on the fly, without disabling the other ADCs.
Parameters
hadcADC handle
Return values
HALstatus

Definition at line 402 of file stm32l4xx_hal_adc.c.

403 {
404  HAL_StatusTypeDef tmp_hal_status = HAL_OK;
405  uint32_t tmpCFGR;
406  uint32_t tmp_adc_reg_is_conversion_on_going;
407  __IO uint32_t wait_loop_index = 0UL;
408  uint32_t tmp_adc_is_conversion_on_going_regular;
409  uint32_t tmp_adc_is_conversion_on_going_injected;
410 
411  /* Check ADC handle */
412  if (hadc == NULL)
413  {
414  return HAL_ERROR;
415  }
416 
417  /* Check the parameters */
418  assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
419  assert_param(IS_ADC_CLOCKPRESCALER(hadc->Init.ClockPrescaler));
420  assert_param(IS_ADC_RESOLUTION(hadc->Init.Resolution));
421 #if defined(ADC_CFGR_DFSDMCFG) &&defined(DFSDM1_Channel0)
422  assert_param(IS_ADC_DFSDMCFG_MODE(hadc));
423 #endif
424  assert_param(IS_ADC_DATA_ALIGN(hadc->Init.DataAlign));
425  assert_param(IS_ADC_SCAN_MODE(hadc->Init.ScanConvMode));
426  assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
427  assert_param(IS_ADC_EXTTRIG_EDGE(hadc->Init.ExternalTrigConvEdge));
428  assert_param(IS_ADC_EXTTRIG(hadc, hadc->Init.ExternalTrigConv));
429  assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DMAContinuousRequests));
430  assert_param(IS_ADC_EOC_SELECTION(hadc->Init.EOCSelection));
431  assert_param(IS_ADC_OVERRUN(hadc->Init.Overrun));
432  assert_param(IS_FUNCTIONAL_STATE(hadc->Init.LowPowerAutoWait));
433  assert_param(IS_FUNCTIONAL_STATE(hadc->Init.OversamplingMode));
434 
435  if (hadc->Init.ScanConvMode != ADC_SCAN_DISABLE)
436  {
437  assert_param(IS_ADC_REGULAR_NB_CONV(hadc->Init.NbrOfConversion));
438  assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DiscontinuousConvMode));
439 
440  if (hadc->Init.DiscontinuousConvMode == ENABLE)
441  {
442  assert_param(IS_ADC_REGULAR_DISCONT_NUMBER(hadc->Init.NbrOfDiscConversion));
443  }
444  }
445 
446  /* DISCEN and CONT bits cannot be set at the same time */
447  assert_param(!((hadc->Init.DiscontinuousConvMode == ENABLE) && (hadc->Init.ContinuousConvMode == ENABLE)));
448 
449  /* Actions performed only if ADC is coming from state reset: */
450  /* - Initialization of ADC MSP */
451  if (hadc->State == HAL_ADC_STATE_RESET)
452  {
453 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
454  /* Init the ADC Callback settings */
455  hadc->ConvCpltCallback = HAL_ADC_ConvCpltCallback; /* Legacy weak callback */
456  hadc->ConvHalfCpltCallback = HAL_ADC_ConvHalfCpltCallback; /* Legacy weak callback */
457  hadc->LevelOutOfWindowCallback = HAL_ADC_LevelOutOfWindowCallback; /* Legacy weak callback */
458  hadc->ErrorCallback = HAL_ADC_ErrorCallback; /* Legacy weak callback */
459  hadc->InjectedConvCpltCallback = HAL_ADCEx_InjectedConvCpltCallback; /* Legacy weak callback */
460  hadc->InjectedQueueOverflowCallback = HAL_ADCEx_InjectedQueueOverflowCallback; /* Legacy weak callback */
461  hadc->LevelOutOfWindow2Callback = HAL_ADCEx_LevelOutOfWindow2Callback; /* Legacy weak callback */
462  hadc->LevelOutOfWindow3Callback = HAL_ADCEx_LevelOutOfWindow3Callback; /* Legacy weak callback */
463  hadc->EndOfSamplingCallback = HAL_ADCEx_EndOfSamplingCallback; /* Legacy weak callback */
464 
465  if (hadc->MspInitCallback == NULL)
466  {
467  hadc->MspInitCallback = HAL_ADC_MspInit; /* Legacy weak MspInit */
468  }
469 
470  /* Init the low level hardware */
471  hadc->MspInitCallback(hadc);
472 #else
473  /* Init the low level hardware */
474  HAL_ADC_MspInit(hadc);
475 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
476 
477  /* Set ADC error code to none */
478  ADC_CLEAR_ERRORCODE(hadc);
479 
480  /* Initialize Lock */
481  hadc->Lock = HAL_UNLOCKED;
482  }
483 
484  /* - Exit from deep-power-down mode and ADC voltage regulator enable */
485  if (LL_ADC_IsDeepPowerDownEnabled(hadc->Instance) != 0UL)
486  {
487  /* Disable ADC deep power down mode */
488  LL_ADC_DisableDeepPowerDown(hadc->Instance);
489 
490  /* System was in deep power down mode, calibration must
491  be relaunched or a previously saved calibration factor
492  re-applied once the ADC voltage regulator is enabled */
493  }
494 
495  if (LL_ADC_IsInternalRegulatorEnabled(hadc->Instance) == 0UL)
496  {
497  /* Enable ADC internal voltage regulator */
498  LL_ADC_EnableInternalRegulator(hadc->Instance);
499 
500  /* Note: Variable divided by 2 to compensate partially */
501  /* CPU processing cycles, scaling in us split to not */
502  /* exceed 32 bits register capacity and handle low frequency. */
503  wait_loop_index = ((LL_ADC_DELAY_INTERNAL_REGUL_STAB_US / 10UL) * (SystemCoreClock / (100000UL * 2UL)));
504  while (wait_loop_index != 0UL)
505  {
506  wait_loop_index--;
507  }
508  }
509 
510  /* Verification that ADC voltage regulator is correctly enabled, whether */
511  /* or not ADC is coming from state reset (if any potential problem of */
512  /* clocking, voltage regulator would not be enabled). */
513  if (LL_ADC_IsInternalRegulatorEnabled(hadc->Instance) == 0UL)
514  {
515  /* Update ADC state machine to error */
516  SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
517 
518  /* Set ADC error code to ADC peripheral internal error */
519  SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
520 
521  tmp_hal_status = HAL_ERROR;
522  }
523 
524  /* Configuration of ADC parameters if previous preliminary actions are */
525  /* correctly completed and if there is no conversion on going on regular */
526  /* group (ADC may already be enabled at this point if HAL_ADC_Init() is */
527  /* called to update a parameter on the fly). */
528  tmp_adc_reg_is_conversion_on_going = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
529 
530  if (((hadc->State & HAL_ADC_STATE_ERROR_INTERNAL) == 0UL)
531  && (tmp_adc_reg_is_conversion_on_going == 0UL)
532  )
533  {
534  /* Set ADC state */
535  ADC_STATE_CLR_SET(hadc->State,
536  HAL_ADC_STATE_REG_BUSY,
537  HAL_ADC_STATE_BUSY_INTERNAL);
538 
539  /* Configuration of common ADC parameters */
540 
541  /* Parameters update conditioned to ADC state: */
542  /* Parameters that can be updated only when ADC is disabled: */
543  /* - clock configuration */
544  if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
545  {
546  if (__LL_ADC_IS_ENABLED_ALL_COMMON_INSTANCE(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) == 0UL)
547  {
548  /* Reset configuration of ADC common register CCR: */
549  /* */
550  /* - ADC clock mode and ACC prescaler (CKMODE and PRESC bits)are set */
551  /* according to adc->Init.ClockPrescaler. It selects the clock */
552  /* source and sets the clock division factor. */
553  /* */
554  /* Some parameters of this register are not reset, since they are set */
555  /* by other functions and must be kept in case of usage of this */
556  /* function on the fly (update of a parameter of ADC_InitTypeDef */
557  /* without needing to reconfigure all other ADC groups/channels */
558  /* parameters): */
559  /* - when multimode feature is available, multimode-related */
560  /* parameters: MDMA, DMACFG, DELAY, DUAL (set by API */
561  /* HAL_ADCEx_MultiModeConfigChannel() ) */
562  /* - internal measurement paths: Vbat, temperature sensor, Vref */
563  /* (set into HAL_ADC_ConfigChannel() or */
564  /* HAL_ADCEx_InjectedConfigChannel() ) */
565  LL_ADC_SetCommonClock(__LL_ADC_COMMON_INSTANCE(hadc->Instance), hadc->Init.ClockPrescaler);
566  }
567  }
568 
569  /* Configuration of ADC: */
570  /* - resolution Init.Resolution */
571  /* - data alignment Init.DataAlign */
572  /* - external trigger to start conversion Init.ExternalTrigConv */
573  /* - external trigger polarity Init.ExternalTrigConvEdge */
574  /* - continuous conversion mode Init.ContinuousConvMode */
575  /* - overrun Init.Overrun */
576  /* - discontinuous mode Init.DiscontinuousConvMode */
577  /* - discontinuous mode channel count Init.NbrOfDiscConversion */
578  tmpCFGR = (ADC_CFGR_CONTINUOUS((uint32_t)hadc->Init.ContinuousConvMode) |
579  hadc->Init.Overrun |
580  hadc->Init.DataAlign |
581  hadc->Init.Resolution |
582  ADC_CFGR_REG_DISCONTINUOUS((uint32_t)hadc->Init.DiscontinuousConvMode));
583 
584  if (hadc->Init.DiscontinuousConvMode == ENABLE)
585  {
586  tmpCFGR |= ADC_CFGR_DISCONTINUOUS_NUM(hadc->Init.NbrOfDiscConversion);
587  }
588 
589  /* Enable external trigger if trigger selection is different of software */
590  /* start. */
591  /* Note: This configuration keeps the hardware feature of parameter */
592  /* ExternalTrigConvEdge "trigger edge none" equivalent to */
593  /* software start. */
594  if (hadc->Init.ExternalTrigConv != ADC_SOFTWARE_START)
595  {
596  tmpCFGR |= ((hadc->Init.ExternalTrigConv & ADC_CFGR_EXTSEL)
597  | hadc->Init.ExternalTrigConvEdge
598  );
599  }
600 
601  /* Update Configuration Register CFGR */
602  MODIFY_REG(hadc->Instance->CFGR, ADC_CFGR_FIELDS_1, tmpCFGR);
603 
604  /* Parameters update conditioned to ADC state: */
605  /* Parameters that can be updated when ADC is disabled or enabled without */
606  /* conversion on going on regular and injected groups: */
607  /* - DMA continuous request Init.DMAContinuousRequests */
608  /* - LowPowerAutoWait feature Init.LowPowerAutoWait */
609  /* - Oversampling parameters Init.Oversampling */
610  tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
611  tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
612  if ((tmp_adc_is_conversion_on_going_regular == 0UL)
613  && (tmp_adc_is_conversion_on_going_injected == 0UL)
614  )
615  {
616  tmpCFGR = (ADC_CFGR_DFSDM(hadc) |
617  ADC_CFGR_AUTOWAIT((uint32_t)hadc->Init.LowPowerAutoWait) |
618  ADC_CFGR_DMACONTREQ((uint32_t)hadc->Init.DMAContinuousRequests));
619 
620  MODIFY_REG(hadc->Instance->CFGR, ADC_CFGR_FIELDS_2, tmpCFGR);
621 
622  if (hadc->Init.OversamplingMode == ENABLE)
623  {
624  assert_param(IS_ADC_OVERSAMPLING_RATIO(hadc->Init.Oversampling.Ratio));
625  assert_param(IS_ADC_RIGHT_BIT_SHIFT(hadc->Init.Oversampling.RightBitShift));
626  assert_param(IS_ADC_TRIGGERED_OVERSAMPLING_MODE(hadc->Init.Oversampling.TriggeredMode));
627  assert_param(IS_ADC_REGOVERSAMPLING_MODE(hadc->Init.Oversampling.OversamplingStopReset));
628 
629  /* Configuration of Oversampler: */
630  /* - Oversampling Ratio */
631  /* - Right bit shift */
632  /* - Triggered mode */
633  /* - Oversampling mode (continued/resumed) */
634  MODIFY_REG(hadc->Instance->CFGR2,
635  ADC_CFGR2_OVSR |
636  ADC_CFGR2_OVSS |
637  ADC_CFGR2_TROVS |
638  ADC_CFGR2_ROVSM,
639  ADC_CFGR2_ROVSE |
640  hadc->Init.Oversampling.Ratio |
641  hadc->Init.Oversampling.RightBitShift |
642  hadc->Init.Oversampling.TriggeredMode |
643  hadc->Init.Oversampling.OversamplingStopReset
644  );
645  }
646  else
647  {
648  /* Disable ADC oversampling scope on ADC group regular */
649  CLEAR_BIT(hadc->Instance->CFGR2, ADC_CFGR2_ROVSE);
650  }
651 
652  }
653 
654  /* Configuration of regular group sequencer: */
655  /* - if scan mode is disabled, regular channels sequence length is set to */
656  /* 0x00: 1 channel converted (channel on regular rank 1) */
657  /* Parameter "NbrOfConversion" is discarded. */
658  /* Note: Scan mode is not present by hardware on this device, but */
659  /* emulated by software for alignment over all STM32 devices. */
660  /* - if scan mode is enabled, regular channels sequence length is set to */
661  /* parameter "NbrOfConversion". */
662 
663  if (hadc->Init.ScanConvMode == ADC_SCAN_ENABLE)
664  {
665  /* Set number of ranks in regular group sequencer */
666  MODIFY_REG(hadc->Instance->SQR1, ADC_SQR1_L, (hadc->Init.NbrOfConversion - (uint8_t)1));
667  }
668  else
669  {
670  CLEAR_BIT(hadc->Instance->SQR1, ADC_SQR1_L);
671  }
672 
673  /* Initialize the ADC state */
674  /* Clear HAL_ADC_STATE_BUSY_INTERNAL bit, set HAL_ADC_STATE_READY bit */
675  ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_BUSY_INTERNAL, HAL_ADC_STATE_READY);
676  }
677  else
678  {
679  /* Update ADC state machine to error */
680  SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
681 
682  tmp_hal_status = HAL_ERROR;
683  }
684 
685  /* Return function status */
686  return tmp_hal_status;
687 }
__STATIC_INLINE void LL_ADC_DisableDeepPowerDown(ADC_TypeDef *ADCx)
Disable ADC deep power down mode.
__STATIC_INLINE uint32_t LL_ADC_IsEnabled(ADC_TypeDef *ADCx)
Get the selected ADC instance enable state.
void HAL_ADCEx_LevelOutOfWindow2Callback(ADC_HandleTypeDef *hadc)
Analog watchdog 2 callback in non-blocking mode.
__STATIC_INLINE uint32_t LL_ADC_IsInternalRegulatorEnabled(ADC_TypeDef *ADCx)
Get the selected ADC instance internal voltage regulator state. CR ADVREGEN LL_ADC_IsInternalRegulat...
void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef *hadc)
Injected conversion complete callback in non-blocking mode.
__STATIC_INLINE void LL_ADC_EnableInternalRegulator(ADC_TypeDef *ADCx)
Enable ADC instance internal voltage regulator.
void HAL_ADC_ErrorCallback(ADC_HandleTypeDef *hadc)
ADC error callback in non-blocking mode (ADC conversion with interruption or transfer by DMA)...
void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc)
Initialize the ADC MSP.
void HAL_ADCEx_InjectedQueueOverflowCallback(ADC_HandleTypeDef *hadc)
Injected context queue overflow callback.
CLEAR_BIT(hrtc->Instance->CR, RTC_CR_WUTE)
__STATIC_INLINE void LL_ADC_SetCommonClock(ADC_Common_TypeDef *ADCxy_COMMON, uint32_t CommonClock)
Set parameter common to several ADC: Clock source and prescaler.
return HAL_OK
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
Conversion complete callback in non-blocking mode.
__STATIC_INLINE uint32_t LL_ADC_INJ_IsConversionOngoing(ADC_TypeDef *ADCx)
Get ADC group injected conversion state. CR JADSTART LL_ADC_INJ_IsConversionOngoing.
void HAL_ADCEx_EndOfSamplingCallback(ADC_HandleTypeDef *hadc)
End Of Sampling callback in non-blocking mode.
void HAL_ADCEx_LevelOutOfWindow3Callback(ADC_HandleTypeDef *hadc)
Analog watchdog 3 callback in non-blocking mode.
void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef *hadc)
Conversion DMA half-transfer callback in non-blocking mode.
__STATIC_INLINE uint32_t LL_ADC_IsDeepPowerDownEnabled(ADC_TypeDef *ADCx)
Get the selected ADC instance deep power down state. CR DEEPPWD LL_ADC_IsDeepPowerDownEnabled.
__STATIC_INLINE uint32_t LL_ADC_REG_IsConversionOngoing(ADC_TypeDef *ADCx)
Get ADC group regular conversion state. CR ADSTART LL_ADC_REG_IsConversionOngoing.
MODIFY_REG(hrtc->Instance->CR, RTC_CR_WUCKSEL,(uint32_t) WakeUpClock)
void HAL_ADC_LevelOutOfWindowCallback(ADC_HandleTypeDef *hadc)
Analog watchdog 1 callback in non-blocking mode.
assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock))

◆ HAL_ADC_MspDeInit()

__weak void HAL_ADC_MspDeInit ( ADC_HandleTypeDef hadc)

DeInitialize the ADC MSP.

Parameters
hadcADC handle
Note
All ADC instances use the same core clock at RCC level, disabling the core clock reset all ADC instances).
Return values
None

Definition at line 926 of file stm32l4xx_hal_adc.c.

927 {
928  /* Prevent unused argument(s) compilation warning */
929  UNUSED(hadc);
930 
931  /* NOTE : This function should not be modified. When the callback is needed,
932  function HAL_ADC_MspDeInit must be implemented in the user file.
933  */
934 }

◆ HAL_ADC_MspInit()

__weak void HAL_ADC_MspInit ( ADC_HandleTypeDef hadc)

Initialize the ADC MSP.

Parameters
hadcADC handle
Return values
None

Definition at line 909 of file stm32l4xx_hal_adc.c.

910 {
911  /* Prevent unused argument(s) compilation warning */
912  UNUSED(hadc);
913 
914  /* NOTE : This function should not be modified. When the callback is needed,
915  function HAL_ADC_MspInit must be implemented in the user file.
916  */
917 }

◆ HAL_ADC_RegisterCallback()

HAL_StatusTypeDef HAL_ADC_RegisterCallback ( ADC_HandleTypeDef hadc,
HAL_ADC_CallbackIDTypeDef  CallbackID,
pADC_CallbackTypeDef  pCallback 
)

Register a User ADC Callback To be used instead of the weak predefined callback.

Parameters
hadcPointer to a ADC_HandleTypeDef structure that contains the configuration information for the specified ADC.
CallbackIDID of the callback to be registered This parameter can be one of the following values:
pCallbackpointer to the Callback function
Return values
HALstatus

Definition at line 960 of file stm32l4xx_hal_adc.c.

961 {
962  HAL_StatusTypeDef status = HAL_OK;
963 
964  if (pCallback == NULL)
965  {
966  /* Update the error code */
967  hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
968 
969  return HAL_ERROR;
970  }
971 
972  if ((hadc->State & HAL_ADC_STATE_READY) != 0UL)
973  {
974  switch (CallbackID)
975  {
977  hadc->ConvCpltCallback = pCallback;
978  break;
979 
981  hadc->ConvHalfCpltCallback = pCallback;
982  break;
983 
985  hadc->LevelOutOfWindowCallback = pCallback;
986  break;
987 
988  case HAL_ADC_ERROR_CB_ID :
989  hadc->ErrorCallback = pCallback;
990  break;
991 
993  hadc->InjectedConvCpltCallback = pCallback;
994  break;
995 
997  hadc->InjectedQueueOverflowCallback = pCallback;
998  break;
999 
1001  hadc->LevelOutOfWindow2Callback = pCallback;
1002  break;
1003 
1005  hadc->LevelOutOfWindow3Callback = pCallback;
1006  break;
1007 
1009  hadc->EndOfSamplingCallback = pCallback;
1010  break;
1011 
1012  case HAL_ADC_MSPINIT_CB_ID :
1013  hadc->MspInitCallback = pCallback;
1014  break;
1015 
1017  hadc->MspDeInitCallback = pCallback;
1018  break;
1019 
1020  default :
1021  /* Update the error code */
1022  hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
1023 
1024  /* Return error status */
1025  status = HAL_ERROR;
1026  break;
1027  }
1028  }
1029  else if (HAL_ADC_STATE_RESET == hadc->State)
1030  {
1031  switch (CallbackID)
1032  {
1033  case HAL_ADC_MSPINIT_CB_ID :
1034  hadc->MspInitCallback = pCallback;
1035  break;
1036 
1038  hadc->MspDeInitCallback = pCallback;
1039  break;
1040 
1041  default :
1042  /* Update the error code */
1043  hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
1044 
1045  /* Return error status */
1046  status = HAL_ERROR;
1047  break;
1048  }
1049  }
1050  else
1051  {
1052  /* Update the error code */
1053  hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
1054 
1055  /* Return error status */
1056  status = HAL_ERROR;
1057  }
1058 
1059  return status;
1060 }
return HAL_OK

◆ HAL_ADC_UnRegisterCallback()

HAL_StatusTypeDef HAL_ADC_UnRegisterCallback ( ADC_HandleTypeDef hadc,
HAL_ADC_CallbackIDTypeDef  CallbackID 
)

Unregister a ADC Callback ADC callback is redirected to the weak predefined callback.

Parameters
hadcPointer to a ADC_HandleTypeDef structure that contains the configuration information for the specified ADC.
CallbackIDID of the callback to be unregistered This parameter can be one of the following values:
Return values
HALstatus

Definition at line 1084 of file stm32l4xx_hal_adc.c.

1085 {
1086  HAL_StatusTypeDef status = HAL_OK;
1087 
1088  if ((hadc->State & HAL_ADC_STATE_READY) != 0UL)
1089  {
1090  switch (CallbackID)
1091  {
1093  hadc->ConvCpltCallback = HAL_ADC_ConvCpltCallback;
1094  break;
1095 
1097  hadc->ConvHalfCpltCallback = HAL_ADC_ConvHalfCpltCallback;
1098  break;
1099 
1101  hadc->LevelOutOfWindowCallback = HAL_ADC_LevelOutOfWindowCallback;
1102  break;
1103 
1104  case HAL_ADC_ERROR_CB_ID :
1105  hadc->ErrorCallback = HAL_ADC_ErrorCallback;
1106  break;
1107 
1109  hadc->InjectedConvCpltCallback = HAL_ADCEx_InjectedConvCpltCallback;
1110  break;
1111 
1113  hadc->InjectedQueueOverflowCallback = HAL_ADCEx_InjectedQueueOverflowCallback;
1114  break;
1115 
1117  hadc->LevelOutOfWindow2Callback = HAL_ADCEx_LevelOutOfWindow2Callback;
1118  break;
1119 
1121  hadc->LevelOutOfWindow3Callback = HAL_ADCEx_LevelOutOfWindow3Callback;
1122  break;
1123 
1125  hadc->EndOfSamplingCallback = HAL_ADCEx_EndOfSamplingCallback;
1126  break;
1127 
1128  case HAL_ADC_MSPINIT_CB_ID :
1129  hadc->MspInitCallback = HAL_ADC_MspInit; /* Legacy weak MspInit */
1130  break;
1131 
1133  hadc->MspDeInitCallback = HAL_ADC_MspDeInit; /* Legacy weak MspDeInit */
1134  break;
1135 
1136  default :
1137  /* Update the error code */
1138  hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
1139 
1140  /* Return error status */
1141  status = HAL_ERROR;
1142  break;
1143  }
1144  }
1145  else if (HAL_ADC_STATE_RESET == hadc->State)
1146  {
1147  switch (CallbackID)
1148  {
1149  case HAL_ADC_MSPINIT_CB_ID :
1150  hadc->MspInitCallback = HAL_ADC_MspInit; /* Legacy weak MspInit */
1151  break;
1152 
1154  hadc->MspDeInitCallback = HAL_ADC_MspDeInit; /* Legacy weak MspDeInit */
1155  break;
1156 
1157  default :
1158  /* Update the error code */
1159  hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
1160 
1161  /* Return error status */
1162  status = HAL_ERROR;
1163  break;
1164  }
1165  }
1166  else
1167  {
1168  /* Update the error code */
1169  hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
1170 
1171  /* Return error status */
1172  status = HAL_ERROR;
1173  }
1174 
1175  return status;
1176 }
void HAL_ADCEx_LevelOutOfWindow2Callback(ADC_HandleTypeDef *hadc)
Analog watchdog 2 callback in non-blocking mode.
void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef *hadc)
Injected conversion complete callback in non-blocking mode.
void HAL_ADC_ErrorCallback(ADC_HandleTypeDef *hadc)
ADC error callback in non-blocking mode (ADC conversion with interruption or transfer by DMA)...
void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc)
Initialize the ADC MSP.
void HAL_ADCEx_InjectedQueueOverflowCallback(ADC_HandleTypeDef *hadc)
Injected context queue overflow callback.
return HAL_OK
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
Conversion complete callback in non-blocking mode.
void HAL_ADCEx_EndOfSamplingCallback(ADC_HandleTypeDef *hadc)
End Of Sampling callback in non-blocking mode.
void HAL_ADCEx_LevelOutOfWindow3Callback(ADC_HandleTypeDef *hadc)
Analog watchdog 3 callback in non-blocking mode.
void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef *hadc)
Conversion DMA half-transfer callback in non-blocking mode.
void HAL_ADC_LevelOutOfWindowCallback(ADC_HandleTypeDef *hadc)
Analog watchdog 1 callback in non-blocking mode.
void HAL_ADC_MspDeInit(ADC_HandleTypeDef *hadc)
DeInitialize the ADC MSP.