STM32L4xx_HAL_Driver  1.14.0
ADC Private Functions

Functions

HAL_StatusTypeDef ADC_ConversionStop (ADC_HandleTypeDef *hadc, uint32_t ConversionGroup)
 Stop ADC conversion. More...
 
HAL_StatusTypeDef ADC_Enable (ADC_HandleTypeDef *hadc)
 Enable the selected ADC. More...
 
HAL_StatusTypeDef ADC_Disable (ADC_HandleTypeDef *hadc)
 Disable the selected ADC. More...
 
void ADC_DMAConvCplt (DMA_HandleTypeDef *hdma)
 DMA transfer complete callback. More...
 
void ADC_DMAHalfConvCplt (DMA_HandleTypeDef *hdma)
 DMA half transfer complete callback. More...
 
void ADC_DMAError (DMA_HandleTypeDef *hdma)
 DMA error callback. More...
 

Detailed Description

Function Documentation

◆ ADC_ConversionStop()

HAL_StatusTypeDef ADC_ConversionStop ( ADC_HandleTypeDef hadc,
uint32_t  ConversionGroup 
)

Stop ADC conversion.

Parameters
hadcADC handle
ConversionGroupADC group regular and/or injected. This parameter can be one of the following values:
  • ADC_REGULAR_GROUP ADC regular conversion type.
  • ADC_INJECTED_GROUP ADC injected conversion type.
  • ADC_REGULAR_INJECTED_GROUP ADC regular and injected conversion type.
Return values
HALstatus.

Definition at line 3237 of file stm32l4xx_hal_adc.c.

3238 {
3239  uint32_t tickstart;
3240  uint32_t Conversion_Timeout_CPU_cycles = 0UL;
3241  uint32_t conversion_group_reassigned = ConversionGroup;
3242  uint32_t tmp_ADC_CR_ADSTART_JADSTART;
3243  uint32_t tmp_adc_is_conversion_on_going_regular;
3244  uint32_t tmp_adc_is_conversion_on_going_injected;
3245 
3246  /* Check the parameters */
3247  assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
3248  assert_param(IS_ADC_CONVERSION_GROUP(ConversionGroup));
3249 
3250  /* Verification if ADC is not already stopped (on regular and injected */
3251  /* groups) to bypass this function if not needed. */
3252  tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
3253  tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
3254  if ((tmp_adc_is_conversion_on_going_regular != 0UL)
3255  || (tmp_adc_is_conversion_on_going_injected != 0UL)
3256  )
3257  {
3258  /* Particular case of continuous auto-injection mode combined with */
3259  /* auto-delay mode. */
3260  /* In auto-injection mode, regular group stop ADC_CR_ADSTP is used (not */
3261  /* injected group stop ADC_CR_JADSTP). */
3262  /* Procedure to be followed: Wait until JEOS=1, clear JEOS, set ADSTP=1 */
3263  /* (see reference manual). */
3264  if (((hadc->Instance->CFGR & ADC_CFGR_JAUTO) != 0UL)
3265  && (hadc->Init.ContinuousConvMode == ENABLE)
3266  && (hadc->Init.LowPowerAutoWait == ENABLE)
3267  )
3268  {
3269  /* Use stop of regular group */
3270  conversion_group_reassigned = ADC_REGULAR_GROUP;
3271 
3272  /* Wait until JEOS=1 (maximum Timeout: 4 injected conversions) */
3273  while (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_JEOS) == 0UL)
3274  {
3275  if (Conversion_Timeout_CPU_cycles >= (ADC_CONVERSION_TIME_MAX_CPU_CYCLES * 4UL))
3276  {
3277  /* Update ADC state machine to error */
3278  SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
3279 
3280  /* Set ADC error code to ADC peripheral internal error */
3281  SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
3282 
3283  return HAL_ERROR;
3284  }
3285  Conversion_Timeout_CPU_cycles ++;
3286  }
3287 
3288  /* Clear JEOS */
3289  __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOS);
3290  }
3291 
3292  /* Stop potential conversion on going on ADC group regular */
3293  if (conversion_group_reassigned != ADC_INJECTED_GROUP)
3294  {
3295  /* Software is allowed to set ADSTP only when ADSTART=1 and ADDIS=0 */
3296  if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) != 0UL)
3297  {
3298  if (LL_ADC_IsDisableOngoing(hadc->Instance) == 0UL)
3299  {
3300  /* Stop ADC group regular conversion */
3301  LL_ADC_REG_StopConversion(hadc->Instance);
3302  }
3303  }
3304  }
3305 
3306  /* Stop potential conversion on going on ADC group injected */
3307  if (conversion_group_reassigned != ADC_REGULAR_GROUP)
3308  {
3309  /* Software is allowed to set JADSTP only when JADSTART=1 and ADDIS=0 */
3310  if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) != 0UL)
3311  {
3312  if (LL_ADC_IsDisableOngoing(hadc->Instance) == 0UL)
3313  {
3314  /* Stop ADC group injected conversion */
3315  LL_ADC_INJ_StopConversion(hadc->Instance);
3316  }
3317  }
3318  }
3319 
3320  /* Selection of start and stop bits with respect to the regular or injected group */
3321  switch (conversion_group_reassigned)
3322  {
3323  case ADC_REGULAR_INJECTED_GROUP:
3324  tmp_ADC_CR_ADSTART_JADSTART = (ADC_CR_ADSTART | ADC_CR_JADSTART);
3325  break;
3326  case ADC_INJECTED_GROUP:
3327  tmp_ADC_CR_ADSTART_JADSTART = ADC_CR_JADSTART;
3328  break;
3329  /* Case ADC_REGULAR_GROUP only*/
3330  default:
3331  tmp_ADC_CR_ADSTART_JADSTART = ADC_CR_ADSTART;
3332  break;
3333  }
3334 
3335  /* Wait for conversion effectively stopped */
3336  tickstart = HAL_GetTick();
3337 
3338  while ((hadc->Instance->CR & tmp_ADC_CR_ADSTART_JADSTART) != 0UL)
3339  {
3340  if ((HAL_GetTick() - tickstart) > ADC_STOP_CONVERSION_TIMEOUT)
3341  {
3342  /* Update ADC state machine to error */
3343  SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
3344 
3345  /* Set ADC error code to ADC peripheral internal error */
3346  SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
3347 
3348  return HAL_ERROR;
3349  }
3350  }
3351 
3352  }
3353 
3354  /* Return HAL status */
3355  return HAL_OK;
3356 }
__STATIC_INLINE void LL_ADC_INJ_StopConversion(ADC_TypeDef *ADCx)
Stop ADC group injected conversion.
__STATIC_INLINE uint32_t LL_ADC_IsDisableOngoing(ADC_TypeDef *ADCx)
Get the selected ADC instance disable state. CR ADDIS LL_ADC_IsDisableOngoing.
uint32_t HAL_GetTick(void)
Provide a tick value in millisecond.
__STATIC_INLINE void LL_ADC_REG_StopConversion(ADC_TypeDef *ADCx)
Stop ADC group regular conversion.
return HAL_OK
__STATIC_INLINE uint32_t LL_ADC_INJ_IsConversionOngoing(ADC_TypeDef *ADCx)
Get ADC group injected conversion state. CR JADSTART LL_ADC_INJ_IsConversionOngoing.
__STATIC_INLINE uint32_t LL_ADC_REG_IsConversionOngoing(ADC_TypeDef *ADCx)
Get ADC group regular conversion state. CR ADSTART LL_ADC_REG_IsConversionOngoing.
assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock))

◆ ADC_Disable()

HAL_StatusTypeDef ADC_Disable ( ADC_HandleTypeDef hadc)

Disable the selected ADC.

Note
Prerequisite condition to use this function: ADC conversions must be stopped.
Parameters
hadcADC handle
Return values
HALstatus.

Definition at line 3434 of file stm32l4xx_hal_adc.c.

3435 {
3436  uint32_t tickstart;
3437  const uint32_t tmp_adc_is_disable_on_going = LL_ADC_IsDisableOngoing(hadc->Instance);
3438 
3439  /* Verification if ADC is not already disabled: */
3440  /* Note: forbidden to disable ADC (set bit ADC_CR_ADDIS) if ADC is already */
3441  /* disabled. */
3442  if ((LL_ADC_IsEnabled(hadc->Instance) != 0UL)
3443  && (tmp_adc_is_disable_on_going == 0UL)
3444  )
3445  {
3446  /* Check if conditions to disable the ADC are fulfilled */
3447  if ((hadc->Instance->CR & (ADC_CR_JADSTART | ADC_CR_ADSTART | ADC_CR_ADEN)) == ADC_CR_ADEN)
3448  {
3449  /* Disable the ADC peripheral */
3450  LL_ADC_Disable(hadc->Instance);
3451  __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOSMP | ADC_FLAG_RDY));
3452  }
3453  else
3454  {
3455  /* Update ADC state machine to error */
3456  SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
3457 
3458  /* Set ADC error code to ADC peripheral internal error */
3459  SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
3460 
3461  return HAL_ERROR;
3462  }
3463 
3464  /* Wait for ADC effectively disabled */
3465  /* Get tick count */
3466  tickstart = HAL_GetTick();
3467 
3468  while ((hadc->Instance->CR & ADC_CR_ADEN) != 0UL)
3469  {
3470  if ((HAL_GetTick() - tickstart) > ADC_DISABLE_TIMEOUT)
3471  {
3472  /* Update ADC state machine to error */
3473  SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
3474 
3475  /* Set ADC error code to ADC peripheral internal error */
3476  SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
3477 
3478  return HAL_ERROR;
3479  }
3480  }
3481  }
3482 
3483  /* Return HAL status */
3484  return HAL_OK;
3485 }
__STATIC_INLINE uint32_t LL_ADC_IsEnabled(ADC_TypeDef *ADCx)
Get the selected ADC instance enable state.
__STATIC_INLINE uint32_t LL_ADC_IsDisableOngoing(ADC_TypeDef *ADCx)
Get the selected ADC instance disable state. CR ADDIS LL_ADC_IsDisableOngoing.
uint32_t HAL_GetTick(void)
Provide a tick value in millisecond.
__STATIC_INLINE void LL_ADC_Disable(ADC_TypeDef *ADCx)
Disable the selected ADC instance.
return HAL_OK

◆ ADC_DMAConvCplt()

void ADC_DMAConvCplt ( DMA_HandleTypeDef hdma)

DMA transfer complete callback.

Parameters
hdmapointer to DMA handle.
Return values
None

Definition at line 3492 of file stm32l4xx_hal_adc.c.

3493 {
3494  /* Retrieve ADC handle corresponding to current DMA handle */
3495  ADC_HandleTypeDef *hadc = (ADC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
3496 
3497  /* Update state machine on conversion status if not in error state */
3498  if ((hadc->State & (HAL_ADC_STATE_ERROR_INTERNAL | HAL_ADC_STATE_ERROR_DMA)) == 0UL)
3499  {
3500  /* Set ADC state */
3501  SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
3502 
3503  /* Determine whether any further conversion upcoming on group regular */
3504  /* by external trigger, continuous mode or scan sequence on going */
3505  /* to disable interruption. */
3506  /* Is it the end of the regular sequence ? */
3507  if ((hadc->Instance->ISR & ADC_FLAG_EOS) != 0UL)
3508  {
3509  /* Are conversions software-triggered ? */
3510  if (LL_ADC_REG_IsTriggerSourceSWStart(hadc->Instance) != 0UL)
3511  {
3512  /* Is CONT bit set ? */
3513  if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_CONT) == 0UL)
3514  {
3515  /* CONT bit is not set, no more conversions expected */
3516  CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
3517  if ((hadc->State & HAL_ADC_STATE_INJ_BUSY) == 0UL)
3518  {
3519  SET_BIT(hadc->State, HAL_ADC_STATE_READY);
3520  }
3521  }
3522  }
3523  }
3524  else
3525  {
3526  /* DMA End of Transfer interrupt was triggered but conversions sequence
3527  is not over. If DMACFG is set to 0, conversions are stopped. */
3528  if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_DMACFG) == 0UL)
3529  {
3530  /* DMACFG bit is not set, conversions are stopped. */
3531  CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
3532  if ((hadc->State & HAL_ADC_STATE_INJ_BUSY) == 0UL)
3533  {
3534  SET_BIT(hadc->State, HAL_ADC_STATE_READY);
3535  }
3536  }
3537  }
3538 
3539  /* Conversion complete callback */
3540 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
3541  hadc->ConvCpltCallback(hadc);
3542 #else
3544 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
3545  }
3546  else /* DMA and-or internal error occurred */
3547  {
3548  if ((hadc->State & HAL_ADC_STATE_ERROR_INTERNAL) != 0UL)
3549  {
3550  /* Call HAL ADC Error Callback function */
3551 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
3552  hadc->ErrorCallback(hadc);
3553 #else
3554  HAL_ADC_ErrorCallback(hadc);
3555 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
3556  }
3557  else
3558  {
3559  /* Call ADC DMA error callback */
3560  hadc->DMA_Handle->XferErrorCallback(hdma);
3561  }
3562  }
3563 }
__STATIC_INLINE uint32_t LL_ADC_REG_IsTriggerSourceSWStart(ADC_TypeDef *ADCx)
Get ADC group regular conversion trigger source internal (SW start) or external.
DMA handle Structure definition.
void HAL_ADC_ErrorCallback(ADC_HandleTypeDef *hadc)
ADC error callback in non-blocking mode (ADC conversion with interruption or transfer by DMA)...
CLEAR_BIT(hrtc->Instance->CR, RTC_CR_WUTE)
struct __ADC_HandleTypeDef else typedef struct endif ADC_HandleTypeDef
ADC handle Structure definition.
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
Conversion complete callback in non-blocking mode.

◆ ADC_DMAError()

void ADC_DMAError ( DMA_HandleTypeDef hdma)

DMA error callback.

Parameters
hdmapointer to DMA handle.
Return values
None

Definition at line 3588 of file stm32l4xx_hal_adc.c.

3589 {
3590  /* Retrieve ADC handle corresponding to current DMA handle */
3591  ADC_HandleTypeDef *hadc = (ADC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
3592 
3593  /* Set ADC state */
3594  SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
3595 
3596  /* Set ADC error code to DMA error */
3597  SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_DMA);
3598 
3599  /* Error callback */
3600 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
3601  hadc->ErrorCallback(hadc);
3602 #else
3603  HAL_ADC_ErrorCallback(hadc);
3604 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
3605 }
DMA handle Structure definition.
void HAL_ADC_ErrorCallback(ADC_HandleTypeDef *hadc)
ADC error callback in non-blocking mode (ADC conversion with interruption or transfer by DMA)...
struct __ADC_HandleTypeDef else typedef struct endif ADC_HandleTypeDef
ADC handle Structure definition.

◆ ADC_DMAHalfConvCplt()

void ADC_DMAHalfConvCplt ( DMA_HandleTypeDef hdma)

DMA half transfer complete callback.

Parameters
hdmapointer to DMA handle.
Return values
None

Definition at line 3570 of file stm32l4xx_hal_adc.c.

3571 {
3572  /* Retrieve ADC handle corresponding to current DMA handle */
3573  ADC_HandleTypeDef *hadc = (ADC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
3574 
3575  /* Half conversion callback */
3576 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
3577  hadc->ConvHalfCpltCallback(hadc);
3578 #else
3580 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
3581 }
DMA handle Structure definition.
struct __ADC_HandleTypeDef else typedef struct endif ADC_HandleTypeDef
ADC handle Structure definition.
void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef *hadc)
Conversion DMA half-transfer callback in non-blocking mode.

◆ ADC_Enable()

HAL_StatusTypeDef ADC_Enable ( ADC_HandleTypeDef hadc)

Enable the selected ADC.

Note
Prerequisite condition to use this function: ADC must be disabled and voltage regulator must be enabled (done into HAL_ADC_Init()).
Parameters
hadcADC handle
Return values
HALstatus.

Definition at line 3367 of file stm32l4xx_hal_adc.c.

3368 {
3369  uint32_t tickstart;
3370 
3371  /* ADC enable and wait for ADC ready (in case of ADC is disabled or */
3372  /* enabling phase not yet completed: flag ADC ready not yet set). */
3373  /* Timeout implemented to not be stuck if ADC cannot be enabled (possible */
3374  /* causes: ADC clock not running, ...). */
3375  if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
3376  {
3377  /* Check if conditions to enable the ADC are fulfilled */
3378  if ((hadc->Instance->CR & (ADC_CR_ADCAL | ADC_CR_JADSTP | ADC_CR_ADSTP | ADC_CR_JADSTART | ADC_CR_ADSTART | ADC_CR_ADDIS | ADC_CR_ADEN)) != 0UL)
3379  {
3380  /* Update ADC state machine to error */
3381  SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
3382 
3383  /* Set ADC error code to ADC peripheral internal error */
3384  SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
3385 
3386  return HAL_ERROR;
3387  }
3388 
3389  /* Enable the ADC peripheral */
3390  LL_ADC_Enable(hadc->Instance);
3391 
3392  /* Wait for ADC effectively enabled */
3393  tickstart = HAL_GetTick();
3394 
3395  while (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_RDY) == 0UL)
3396  {
3397  /* If ADEN bit is set less than 4 ADC clock cycles after the ADCAL bit
3398  has been cleared (after a calibration), ADEN bit is reset by the
3399  calibration logic.
3400  The workaround is to continue setting ADEN until ADRDY is becomes 1.
3401  Additionally, ADC_ENABLE_TIMEOUT is defined to encompass this
3402  4 ADC clock cycle duration */
3403  /* Note: Test of ADC enabled required due to hardware constraint to */
3404  /* not enable ADC if already enabled. */
3405  if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
3406  {
3407  LL_ADC_Enable(hadc->Instance);
3408  }
3409 
3410  if ((HAL_GetTick() - tickstart) > ADC_ENABLE_TIMEOUT)
3411  {
3412  /* Update ADC state machine to error */
3413  SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
3414 
3415  /* Set ADC error code to ADC peripheral internal error */
3416  SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
3417 
3418  return HAL_ERROR;
3419  }
3420  }
3421  }
3422 
3423  /* Return HAL status */
3424  return HAL_OK;
3425 }
__STATIC_INLINE uint32_t LL_ADC_IsEnabled(ADC_TypeDef *ADCx)
Get the selected ADC instance enable state.
uint32_t HAL_GetTick(void)
Provide a tick value in millisecond.
__STATIC_INLINE void LL_ADC_Enable(ADC_TypeDef *ADCx)
Enable the selected ADC instance.
return HAL_OK