STM32F4xx_HAL_Driver  1.8.3
stm32f4xx_hal_adc.c
Go to the documentation of this file.
1 
247 /* Includes ------------------------------------------------------------------*/
248 #include "stm32f4xx_hal.h"
249 
259 #ifdef HAL_ADC_MODULE_ENABLED
260 
261 /* Private typedef -----------------------------------------------------------*/
262 /* Private define ------------------------------------------------------------*/
263 /* Private macro -------------------------------------------------------------*/
264 /* Private variables ---------------------------------------------------------*/
268 /* Private function prototypes -----------------------------------------------*/
269 static void ADC_Init(ADC_HandleTypeDef *hadc);
270 static void ADC_DMAConvCplt(DMA_HandleTypeDef *hdma);
271 static void ADC_DMAError(DMA_HandleTypeDef *hdma);
272 static void ADC_DMAHalfConvCplt(DMA_HandleTypeDef *hdma);
276 /* Exported functions --------------------------------------------------------*/
311 HAL_StatusTypeDef HAL_ADC_Init(ADC_HandleTypeDef *hadc)
312 {
313  HAL_StatusTypeDef tmp_hal_status = HAL_OK;
314 
315  /* Check ADC handle */
316  if (hadc == NULL)
317  {
318  return HAL_ERROR;
319  }
320 
321  /* Check the parameters */
322  assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
323  assert_param(IS_ADC_CLOCKPRESCALER(hadc->Init.ClockPrescaler));
324  assert_param(IS_ADC_RESOLUTION(hadc->Init.Resolution));
325  assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ScanConvMode));
326  assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
327  assert_param(IS_ADC_EXT_TRIG(hadc->Init.ExternalTrigConv));
328  assert_param(IS_ADC_DATA_ALIGN(hadc->Init.DataAlign));
329  assert_param(IS_ADC_REGULAR_LENGTH(hadc->Init.NbrOfConversion));
330  assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DMAContinuousRequests));
331  assert_param(IS_ADC_EOCSelection(hadc->Init.EOCSelection));
332  assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DiscontinuousConvMode));
333 
334  if (hadc->Init.ExternalTrigConv != ADC_SOFTWARE_START)
335  {
336  assert_param(IS_ADC_EXT_TRIG_EDGE(hadc->Init.ExternalTrigConvEdge));
337  }
338 
339  if (hadc->State == HAL_ADC_STATE_RESET)
340  {
341 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
342  /* Init the ADC Callback settings */
343  hadc->ConvCpltCallback = HAL_ADC_ConvCpltCallback; /* Legacy weak callback */
344  hadc->ConvHalfCpltCallback = HAL_ADC_ConvHalfCpltCallback; /* Legacy weak callback */
345  hadc->LevelOutOfWindowCallback = HAL_ADC_LevelOutOfWindowCallback; /* Legacy weak callback */
346  hadc->ErrorCallback = HAL_ADC_ErrorCallback; /* Legacy weak callback */
347  hadc->InjectedConvCpltCallback = HAL_ADCEx_InjectedConvCpltCallback; /* Legacy weak callback */
348  if (hadc->MspInitCallback == NULL)
349  {
350  hadc->MspInitCallback = HAL_ADC_MspInit; /* Legacy weak MspInit */
351  }
352 
353  /* Init the low level hardware */
354  hadc->MspInitCallback(hadc);
355 #else
356  /* Init the low level hardware */
357  HAL_ADC_MspInit(hadc);
358 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
359 
360  /* Initialize ADC error code */
361  ADC_CLEAR_ERRORCODE(hadc);
362 
363  /* Allocate lock resource and initialize it */
364  hadc->Lock = HAL_UNLOCKED;
365  }
366 
367  /* Configuration of ADC parameters if previous preliminary actions are */
368  /* correctly completed. */
369  if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL))
370  {
371  /* Set ADC state */
372  ADC_STATE_CLR_SET(hadc->State,
373  HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
374  HAL_ADC_STATE_BUSY_INTERNAL);
375 
376  /* Set ADC parameters */
377  ADC_Init(hadc);
378 
379  /* Set ADC error code to none */
380  ADC_CLEAR_ERRORCODE(hadc);
381 
382  /* Set the ADC state */
383  ADC_STATE_CLR_SET(hadc->State,
384  HAL_ADC_STATE_BUSY_INTERNAL,
385  HAL_ADC_STATE_READY);
386  }
387  else
388  {
389  tmp_hal_status = HAL_ERROR;
390  }
391 
392  /* Release Lock */
393  __HAL_UNLOCK(hadc);
394 
395  /* Return function status */
396  return tmp_hal_status;
397 }
398 
405 HAL_StatusTypeDef HAL_ADC_DeInit(ADC_HandleTypeDef *hadc)
406 {
407  HAL_StatusTypeDef tmp_hal_status = HAL_OK;
408 
409  /* Check ADC handle */
410  if (hadc == NULL)
411  {
412  return HAL_ERROR;
413  }
414 
415  /* Check the parameters */
416  assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
417 
418  /* Set ADC state */
419  SET_BIT(hadc->State, HAL_ADC_STATE_BUSY_INTERNAL);
420 
421  /* Stop potential conversion on going, on regular and injected groups */
422  /* Disable ADC peripheral */
423  __HAL_ADC_DISABLE(hadc);
424 
425  /* Configuration of ADC parameters if previous preliminary actions are */
426  /* correctly completed. */
427  if (HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_ADON))
428  {
429 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
430  if (hadc->MspDeInitCallback == NULL)
431  {
432  hadc->MspDeInitCallback = HAL_ADC_MspDeInit; /* Legacy weak MspDeInit */
433  }
434 
435  /* DeInit the low level hardware: RCC clock, NVIC */
436  hadc->MspDeInitCallback(hadc);
437 #else
438  /* DeInit the low level hardware: RCC clock, NVIC */
439  HAL_ADC_MspDeInit(hadc);
440 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
441 
442  /* Set ADC error code to none */
443  ADC_CLEAR_ERRORCODE(hadc);
444 
445  /* Set ADC state */
446  hadc->State = HAL_ADC_STATE_RESET;
447  }
448 
449  /* Process unlocked */
450  __HAL_UNLOCK(hadc);
451 
452  /* Return function status */
453  return tmp_hal_status;
454 }
455 
456 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
475 {
476  HAL_StatusTypeDef status = HAL_OK;
477 
478  if (pCallback == NULL)
479  {
480  /* Update the error code */
481  hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
482 
483  return HAL_ERROR;
484  }
485 
486  if ((hadc->State & HAL_ADC_STATE_READY) != 0UL)
487  {
488  switch (CallbackID)
489  {
491  hadc->ConvCpltCallback = pCallback;
492  break;
493 
495  hadc->ConvHalfCpltCallback = pCallback;
496  break;
497 
499  hadc->LevelOutOfWindowCallback = pCallback;
500  break;
501 
502  case HAL_ADC_ERROR_CB_ID :
503  hadc->ErrorCallback = pCallback;
504  break;
505 
507  hadc->InjectedConvCpltCallback = pCallback;
508  break;
509 
510  case HAL_ADC_MSPINIT_CB_ID :
511  hadc->MspInitCallback = pCallback;
512  break;
513 
515  hadc->MspDeInitCallback = pCallback;
516  break;
517 
518  default :
519  /* Update the error code */
520  hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
521 
522  /* Return error status */
523  status = HAL_ERROR;
524  break;
525  }
526  }
527  else if (HAL_ADC_STATE_RESET == hadc->State)
528  {
529  switch (CallbackID)
530  {
531  case HAL_ADC_MSPINIT_CB_ID :
532  hadc->MspInitCallback = pCallback;
533  break;
534 
536  hadc->MspDeInitCallback = pCallback;
537  break;
538 
539  default :
540  /* Update the error code */
541  hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
542 
543  /* Return error status */
544  status = HAL_ERROR;
545  break;
546  }
547  }
548  else
549  {
550  /* Update the error code */
551  hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
552 
553  /* Return error status */
554  status = HAL_ERROR;
555  }
556 
557  return status;
558 }
559 
577 {
578  HAL_StatusTypeDef status = HAL_OK;
579 
580  if ((hadc->State & HAL_ADC_STATE_READY) != 0UL)
581  {
582  switch (CallbackID)
583  {
585  hadc->ConvCpltCallback = HAL_ADC_ConvCpltCallback;
586  break;
587 
589  hadc->ConvHalfCpltCallback = HAL_ADC_ConvHalfCpltCallback;
590  break;
591 
593  hadc->LevelOutOfWindowCallback = HAL_ADC_LevelOutOfWindowCallback;
594  break;
595 
596  case HAL_ADC_ERROR_CB_ID :
597  hadc->ErrorCallback = HAL_ADC_ErrorCallback;
598  break;
599 
601  hadc->InjectedConvCpltCallback = HAL_ADCEx_InjectedConvCpltCallback;
602  break;
603 
604  case HAL_ADC_MSPINIT_CB_ID :
605  hadc->MspInitCallback = HAL_ADC_MspInit; /* Legacy weak MspInit */
606  break;
607 
609  hadc->MspDeInitCallback = HAL_ADC_MspDeInit; /* Legacy weak MspDeInit */
610  break;
611 
612  default :
613  /* Update the error code */
614  hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
615 
616  /* Return error status */
617  status = HAL_ERROR;
618  break;
619  }
620  }
621  else if (HAL_ADC_STATE_RESET == hadc->State)
622  {
623  switch (CallbackID)
624  {
625  case HAL_ADC_MSPINIT_CB_ID :
626  hadc->MspInitCallback = HAL_ADC_MspInit; /* Legacy weak MspInit */
627  break;
628 
630  hadc->MspDeInitCallback = HAL_ADC_MspDeInit; /* Legacy weak MspDeInit */
631  break;
632 
633  default :
634  /* Update the error code */
635  hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
636 
637  /* Return error status */
638  status = HAL_ERROR;
639  break;
640  }
641  }
642  else
643  {
644  /* Update the error code */
645  hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
646 
647  /* Return error status */
648  status = HAL_ERROR;
649  }
650 
651  return status;
652 }
653 
654 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
655 
663 {
664  /* Prevent unused argument(s) compilation warning */
665  UNUSED(hadc);
666  /* NOTE : This function Should not be modified, when the callback is needed,
667  the HAL_ADC_MspInit could be implemented in the user file
668  */
669 }
670 
678 {
679  /* Prevent unused argument(s) compilation warning */
680  UNUSED(hadc);
681  /* NOTE : This function Should not be modified, when the callback is needed,
682  the HAL_ADC_MspDeInit could be implemented in the user file
683  */
684 }
685 
716 HAL_StatusTypeDef HAL_ADC_Start(ADC_HandleTypeDef *hadc)
717 {
718  __IO uint32_t counter = 0U;
719  ADC_Common_TypeDef *tmpADC_Common;
720 
721  /* Check the parameters */
722  assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
723  assert_param(IS_ADC_EXT_TRIG_EDGE(hadc->Init.ExternalTrigConvEdge));
724 
725  /* Process locked */
726  __HAL_LOCK(hadc);
727 
728  /* Enable the ADC peripheral */
729  /* Check if ADC peripheral is disabled in order to enable it and wait during
730  Tstab time the ADC's stabilization */
731  if ((hadc->Instance->CR2 & ADC_CR2_ADON) != ADC_CR2_ADON)
732  {
733  /* Enable the Peripheral */
734  __HAL_ADC_ENABLE(hadc);
735 
736  /* Delay for ADC stabilization time */
737  /* Compute number of CPU cycles to wait for */
738  counter = (ADC_STAB_DELAY_US * (SystemCoreClock / 1000000U));
739  while (counter != 0U)
740  {
741  counter--;
742  }
743  }
744 
745  /* Start conversion if ADC is effectively enabled */
746  if (HAL_IS_BIT_SET(hadc->Instance->CR2, ADC_CR2_ADON))
747  {
748  /* Set ADC state */
749  /* - Clear state bitfield related to regular group conversion results */
750  /* - Set state bitfield related to regular group operation */
751  ADC_STATE_CLR_SET(hadc->State,
752  HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR,
753  HAL_ADC_STATE_REG_BUSY);
754 
755  /* If conversions on group regular are also triggering group injected, */
756  /* update ADC state. */
757  if (READ_BIT(hadc->Instance->CR1, ADC_CR1_JAUTO) != RESET)
758  {
759  ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
760  }
761 
762  /* State machine update: Check if an injected conversion is ongoing */
763  if (HAL_IS_BIT_SET(hadc->State, HAL_ADC_STATE_INJ_BUSY))
764  {
765  /* Reset ADC error code fields related to conversions on group regular */
766  CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR | HAL_ADC_ERROR_DMA));
767  }
768  else
769  {
770  /* Reset ADC all error code fields */
771  ADC_CLEAR_ERRORCODE(hadc);
772  }
773 
774  /* Process unlocked */
775  /* Unlock before starting ADC conversions: in case of potential */
776  /* interruption, to let the process to ADC IRQ Handler. */
777  __HAL_UNLOCK(hadc);
778 
779  /* Pointer to the common control register to which is belonging hadc */
780  /* (Depending on STM32F4 product, there may be up to 3 ADCs and 1 common */
781  /* control register) */
782  tmpADC_Common = ADC_COMMON_REGISTER(hadc);
783 
784  /* Clear regular group conversion flag and overrun flag */
785  /* (To ensure of no unknown state from potential previous ADC operations) */
786  __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOC | ADC_FLAG_OVR);
787 
788  /* Check if Multimode enabled */
789  if (HAL_IS_BIT_CLR(tmpADC_Common->CCR, ADC_CCR_MULTI))
790  {
791 #if defined(ADC2) && defined(ADC3)
792  if ((hadc->Instance == ADC1) || ((hadc->Instance == ADC2) && ((ADC->CCR & ADC_CCR_MULTI_Msk) < ADC_CCR_MULTI_0)) \
793  || ((hadc->Instance == ADC3) && ((ADC->CCR & ADC_CCR_MULTI_Msk) < ADC_CCR_MULTI_4)))
794  {
795 #endif /* ADC2 || ADC3 */
796  /* if no external trigger present enable software conversion of regular channels */
797  if ((hadc->Instance->CR2 & ADC_CR2_EXTEN) == RESET)
798  {
799  /* Enable the selected ADC software conversion for regular group */
800  hadc->Instance->CR2 |= (uint32_t)ADC_CR2_SWSTART;
801  }
802 #if defined(ADC2) && defined(ADC3)
803  }
804 #endif /* ADC2 || ADC3 */
805  }
806  else
807  {
808  /* if instance of handle correspond to ADC1 and no external trigger present enable software conversion of regular channels */
809  if ((hadc->Instance == ADC1) && ((hadc->Instance->CR2 & ADC_CR2_EXTEN) == RESET))
810  {
811  /* Enable the selected ADC software conversion for regular group */
812  hadc->Instance->CR2 |= (uint32_t)ADC_CR2_SWSTART;
813  }
814  }
815  }
816  else
817  {
818  /* Update ADC state machine to error */
819  SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
820 
821  /* Set ADC error code to ADC IP internal error */
822  SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
823  }
824 
825  /* Return function status */
826  return HAL_OK;
827 }
828 
839 HAL_StatusTypeDef HAL_ADC_Stop(ADC_HandleTypeDef *hadc)
840 {
841  /* Check the parameters */
842  assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
843 
844  /* Process locked */
845  __HAL_LOCK(hadc);
846 
847  /* Stop potential conversion on going, on regular and injected groups */
848  /* Disable ADC peripheral */
849  __HAL_ADC_DISABLE(hadc);
850 
851  /* Check if ADC is effectively disabled */
852  if (HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_ADON))
853  {
854  /* Set ADC state */
855  ADC_STATE_CLR_SET(hadc->State,
856  HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
857  HAL_ADC_STATE_READY);
858  }
859 
860  /* Process unlocked */
861  __HAL_UNLOCK(hadc);
862 
863  /* Return function status */
864  return HAL_OK;
865 }
866 
882 HAL_StatusTypeDef HAL_ADC_PollForConversion(ADC_HandleTypeDef *hadc, uint32_t Timeout)
883 {
884  uint32_t tickstart = 0U;
885 
886  /* Verification that ADC configuration is compliant with polling for */
887  /* each conversion: */
888  /* Particular case is ADC configured in DMA mode and ADC sequencer with */
889  /* several ranks and polling for end of each conversion. */
890  /* For code simplicity sake, this particular case is generalized to */
891  /* ADC configured in DMA mode and polling for end of each conversion. */
892  if (HAL_IS_BIT_SET(hadc->Instance->CR2, ADC_CR2_EOCS) &&
893  HAL_IS_BIT_SET(hadc->Instance->CR2, ADC_CR2_DMA))
894  {
895  /* Update ADC state machine to error */
896  SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
897 
898  /* Process unlocked */
899  __HAL_UNLOCK(hadc);
900 
901  return HAL_ERROR;
902  }
903 
904  /* Get tick */
905  tickstart = HAL_GetTick();
906 
907  /* Check End of conversion flag */
908  while (!(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOC)))
909  {
910  /* Check if timeout is disabled (set to infinite wait) */
911  if (Timeout != HAL_MAX_DELAY)
912  {
913  if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
914  {
915  /* New check to avoid false timeout detection in case of preemption */
916  if (!(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOC)))
917  {
918  /* Update ADC state machine to timeout */
919  SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
920 
921  /* Process unlocked */
922  __HAL_UNLOCK(hadc);
923 
924  return HAL_TIMEOUT;
925  }
926  }
927  }
928  }
929 
930  /* Clear regular group conversion flag */
931  __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_STRT | ADC_FLAG_EOC);
932 
933  /* Update ADC state machine */
934  SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
935 
936  /* Determine whether any further conversion upcoming on group regular */
937  /* by external trigger, continuous mode or scan sequence on going. */
938  /* Note: On STM32F4, there is no independent flag of end of sequence. */
939  /* The test of scan sequence on going is done either with scan */
940  /* sequence disabled or with end of conversion flag set to */
941  /* of end of sequence. */
942  if (ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
943  (hadc->Init.ContinuousConvMode == DISABLE) &&
944  (HAL_IS_BIT_CLR(hadc->Instance->SQR1, ADC_SQR1_L) ||
945  HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_EOCS)))
946  {
947  /* Set ADC state */
948  CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
949 
950  if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_INJ_BUSY))
951  {
952  SET_BIT(hadc->State, HAL_ADC_STATE_READY);
953  }
954  }
955 
956  /* Return ADC state */
957  return HAL_OK;
958 }
959 
971 HAL_StatusTypeDef HAL_ADC_PollForEvent(ADC_HandleTypeDef *hadc, uint32_t EventType, uint32_t Timeout)
972 {
973  uint32_t tickstart = 0U;
974 
975  /* Check the parameters */
976  assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
977  assert_param(IS_ADC_EVENT_TYPE(EventType));
978 
979  /* Get tick */
980  tickstart = HAL_GetTick();
981 
982  /* Check selected event flag */
983  while (!(__HAL_ADC_GET_FLAG(hadc, EventType)))
984  {
985  /* Check for the Timeout */
986  if (Timeout != HAL_MAX_DELAY)
987  {
988  if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
989  {
990  /* New check to avoid false timeout detection in case of preemption */
991  if (!(__HAL_ADC_GET_FLAG(hadc, EventType)))
992  {
993  /* Update ADC state machine to timeout */
994  SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
995 
996  /* Process unlocked */
997  __HAL_UNLOCK(hadc);
998 
999  return HAL_TIMEOUT;
1000  }
1001  }
1002  }
1003  }
1004 
1005  /* Analog watchdog (level out of window) event */
1006  if (EventType == ADC_AWD_EVENT)
1007  {
1008  /* Set ADC state */
1009  SET_BIT(hadc->State, HAL_ADC_STATE_AWD1);
1010 
1011  /* Clear ADC analog watchdog flag */
1012  __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD);
1013  }
1014  /* Overrun event */
1015  else
1016  {
1017  /* Set ADC state */
1018  SET_BIT(hadc->State, HAL_ADC_STATE_REG_OVR);
1019  /* Set ADC error code to overrun */
1020  SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_OVR);
1021 
1022  /* Clear ADC overrun flag */
1023  __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
1024  }
1025 
1026  /* Return ADC state */
1027  return HAL_OK;
1028 }
1029 
1030 
1037 HAL_StatusTypeDef HAL_ADC_Start_IT(ADC_HandleTypeDef *hadc)
1038 {
1039  __IO uint32_t counter = 0U;
1040  ADC_Common_TypeDef *tmpADC_Common;
1041 
1042  /* Check the parameters */
1043  assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
1044  assert_param(IS_ADC_EXT_TRIG_EDGE(hadc->Init.ExternalTrigConvEdge));
1045 
1046  /* Process locked */
1047  __HAL_LOCK(hadc);
1048 
1049  /* Enable the ADC peripheral */
1050  /* Check if ADC peripheral is disabled in order to enable it and wait during
1051  Tstab time the ADC's stabilization */
1052  if ((hadc->Instance->CR2 & ADC_CR2_ADON) != ADC_CR2_ADON)
1053  {
1054  /* Enable the Peripheral */
1055  __HAL_ADC_ENABLE(hadc);
1056 
1057  /* Delay for ADC stabilization time */
1058  /* Compute number of CPU cycles to wait for */
1059  counter = (ADC_STAB_DELAY_US * (SystemCoreClock / 1000000U));
1060  while (counter != 0U)
1061  {
1062  counter--;
1063  }
1064  }
1065 
1066  /* Start conversion if ADC is effectively enabled */
1067  if (HAL_IS_BIT_SET(hadc->Instance->CR2, ADC_CR2_ADON))
1068  {
1069  /* Set ADC state */
1070  /* - Clear state bitfield related to regular group conversion results */
1071  /* - Set state bitfield related to regular group operation */
1072  ADC_STATE_CLR_SET(hadc->State,
1073  HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR,
1074  HAL_ADC_STATE_REG_BUSY);
1075 
1076  /* If conversions on group regular are also triggering group injected, */
1077  /* update ADC state. */
1078  if (READ_BIT(hadc->Instance->CR1, ADC_CR1_JAUTO) != RESET)
1079  {
1080  ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
1081  }
1082 
1083  /* State machine update: Check if an injected conversion is ongoing */
1084  if (HAL_IS_BIT_SET(hadc->State, HAL_ADC_STATE_INJ_BUSY))
1085  {
1086  /* Reset ADC error code fields related to conversions on group regular */
1087  CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR | HAL_ADC_ERROR_DMA));
1088  }
1089  else
1090  {
1091  /* Reset ADC all error code fields */
1092  ADC_CLEAR_ERRORCODE(hadc);
1093  }
1094 
1095  /* Process unlocked */
1096  /* Unlock before starting ADC conversions: in case of potential */
1097  /* interruption, to let the process to ADC IRQ Handler. */
1098  __HAL_UNLOCK(hadc);
1099 
1100  /* Pointer to the common control register to which is belonging hadc */
1101  /* (Depending on STM32F4 product, there may be up to 3 ADCs and 1 common */
1102  /* control register) */
1103  tmpADC_Common = ADC_COMMON_REGISTER(hadc);
1104 
1105  /* Clear regular group conversion flag and overrun flag */
1106  /* (To ensure of no unknown state from potential previous ADC operations) */
1107  __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOC | ADC_FLAG_OVR);
1108 
1109  /* Enable end of conversion interrupt for regular group */
1110  __HAL_ADC_ENABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_OVR));
1111 
1112  /* Check if Multimode enabled */
1113  if (HAL_IS_BIT_CLR(tmpADC_Common->CCR, ADC_CCR_MULTI))
1114  {
1115 #if defined(ADC2) && defined(ADC3)
1116  if ((hadc->Instance == ADC1) || ((hadc->Instance == ADC2) && ((ADC->CCR & ADC_CCR_MULTI_Msk) < ADC_CCR_MULTI_0)) \
1117  || ((hadc->Instance == ADC3) && ((ADC->CCR & ADC_CCR_MULTI_Msk) < ADC_CCR_MULTI_4)))
1118  {
1119 #endif /* ADC2 || ADC3 */
1120  /* if no external trigger present enable software conversion of regular channels */
1121  if ((hadc->Instance->CR2 & ADC_CR2_EXTEN) == RESET)
1122  {
1123  /* Enable the selected ADC software conversion for regular group */
1124  hadc->Instance->CR2 |= (uint32_t)ADC_CR2_SWSTART;
1125  }
1126 #if defined(ADC2) && defined(ADC3)
1127  }
1128 #endif /* ADC2 || ADC3 */
1129  }
1130  else
1131  {
1132  /* if instance of handle correspond to ADC1 and no external trigger present enable software conversion of regular channels */
1133  if ((hadc->Instance == ADC1) && ((hadc->Instance->CR2 & ADC_CR2_EXTEN) == RESET))
1134  {
1135  /* Enable the selected ADC software conversion for regular group */
1136  hadc->Instance->CR2 |= (uint32_t)ADC_CR2_SWSTART;
1137  }
1138  }
1139  }
1140  else
1141  {
1142  /* Update ADC state machine to error */
1143  SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
1144 
1145  /* Set ADC error code to ADC IP internal error */
1146  SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
1147  }
1148 
1149  /* Return function status */
1150  return HAL_OK;
1151 }
1152 
1162 HAL_StatusTypeDef HAL_ADC_Stop_IT(ADC_HandleTypeDef *hadc)
1163 {
1164  /* Check the parameters */
1165  assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
1166 
1167  /* Process locked */
1168  __HAL_LOCK(hadc);
1169 
1170  /* Stop potential conversion on going, on regular and injected groups */
1171  /* Disable ADC peripheral */
1172  __HAL_ADC_DISABLE(hadc);
1173 
1174  /* Check if ADC is effectively disabled */
1175  if (HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_ADON))
1176  {
1177  /* Disable ADC end of conversion interrupt for regular group */
1178  __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_OVR));
1179 
1180  /* Set ADC state */
1181  ADC_STATE_CLR_SET(hadc->State,
1182  HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
1183  HAL_ADC_STATE_READY);
1184  }
1185 
1186  /* Process unlocked */
1187  __HAL_UNLOCK(hadc);
1188 
1189  /* Return function status */
1190  return HAL_OK;
1191 }
1192 
1200 {
1201  uint32_t tmp1 = 0U, tmp2 = 0U;
1202 
1203  uint32_t tmp_sr = hadc->Instance->SR;
1204  uint32_t tmp_cr1 = hadc->Instance->CR1;
1205 
1206  /* Check the parameters */
1207  assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
1208  assert_param(IS_ADC_REGULAR_LENGTH(hadc->Init.NbrOfConversion));
1209  assert_param(IS_ADC_EOCSelection(hadc->Init.EOCSelection));
1210 
1211  tmp1 = tmp_sr & ADC_FLAG_EOC;
1212  tmp2 = tmp_cr1 & ADC_IT_EOC;
1213  /* Check End of conversion flag for regular channels */
1214  if (tmp1 && tmp2)
1215  {
1216  /* Update state machine on conversion status if not in error state */
1217  if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL))
1218  {
1219  /* Set ADC state */
1220  SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
1221  }
1222 
1223  /* Determine whether any further conversion upcoming on group regular */
1224  /* by external trigger, continuous mode or scan sequence on going. */
1225  /* Note: On STM32F4, there is no independent flag of end of sequence. */
1226  /* The test of scan sequence on going is done either with scan */
1227  /* sequence disabled or with end of conversion flag set to */
1228  /* of end of sequence. */
1229  if (ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
1230  (hadc->Init.ContinuousConvMode == DISABLE) &&
1231  (HAL_IS_BIT_CLR(hadc->Instance->SQR1, ADC_SQR1_L) ||
1232  HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_EOCS)))
1233  {
1234  /* Disable ADC end of single conversion interrupt on group regular */
1235  /* Note: Overrun interrupt was enabled with EOC interrupt in */
1236  /* HAL_ADC_Start_IT(), but is not disabled here because can be used */
1237  /* by overrun IRQ process below. */
1238  __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC);
1239 
1240  /* Set ADC state */
1241  CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
1242 
1243  if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_INJ_BUSY))
1244  {
1245  SET_BIT(hadc->State, HAL_ADC_STATE_READY);
1246  }
1247  }
1248 
1249  /* Conversion complete callback */
1250 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
1251  hadc->ConvCpltCallback(hadc);
1252 #else
1254 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
1255 
1256  /* Clear regular group conversion flag */
1257  __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_STRT | ADC_FLAG_EOC);
1258  }
1259 
1260  tmp1 = tmp_sr & ADC_FLAG_JEOC;
1261  tmp2 = tmp_cr1 & ADC_IT_JEOC;
1262  /* Check End of conversion flag for injected channels */
1263  if (tmp1 && tmp2)
1264  {
1265  /* Update state machine on conversion status if not in error state */
1266  if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL))
1267  {
1268  /* Set ADC state */
1269  SET_BIT(hadc->State, HAL_ADC_STATE_INJ_EOC);
1270  }
1271 
1272  /* Determine whether any further conversion upcoming on group injected */
1273  /* by external trigger, scan sequence on going or by automatic injected */
1274  /* conversion from group regular (same conditions as group regular */
1275  /* interruption disabling above). */
1276  if (ADC_IS_SOFTWARE_START_INJECTED(hadc) &&
1277  (HAL_IS_BIT_CLR(hadc->Instance->JSQR, ADC_JSQR_JL) ||
1278  HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_EOCS)) &&
1279  (HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO) &&
1280  (ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
1281  (hadc->Init.ContinuousConvMode == DISABLE))))
1282  {
1283  /* Disable ADC end of single conversion interrupt on group injected */
1284  __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC);
1285 
1286  /* Set ADC state */
1287  CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
1288 
1289  if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_REG_BUSY))
1290  {
1291  SET_BIT(hadc->State, HAL_ADC_STATE_READY);
1292  }
1293  }
1294 
1295  /* Conversion complete callback */
1296  /* Conversion complete callback */
1297 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
1298  hadc->InjectedConvCpltCallback(hadc);
1299 #else
1301 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
1302 
1303  /* Clear injected group conversion flag */
1304  __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_JSTRT | ADC_FLAG_JEOC));
1305  }
1306 
1307  tmp1 = tmp_sr & ADC_FLAG_AWD;
1308  tmp2 = tmp_cr1 & ADC_IT_AWD;
1309  /* Check Analog watchdog flag */
1310  if (tmp1 && tmp2)
1311  {
1312  if (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_AWD))
1313  {
1314  /* Set ADC state */
1315  SET_BIT(hadc->State, HAL_ADC_STATE_AWD1);
1316 
1317  /* Level out of window callback */
1318 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
1319  hadc->LevelOutOfWindowCallback(hadc);
1320 #else
1322 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
1323 
1324  /* Clear the ADC analog watchdog flag */
1325  __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD);
1326  }
1327  }
1328 
1329  tmp1 = tmp_sr & ADC_FLAG_OVR;
1330  tmp2 = tmp_cr1 & ADC_IT_OVR;
1331  /* Check Overrun flag */
1332  if (tmp1 && tmp2)
1333  {
1334  /* Note: On STM32F4, ADC overrun can be set through other parameters */
1335  /* refer to description of parameter "EOCSelection" for more */
1336  /* details. */
1337 
1338  /* Set ADC error code to overrun */
1339  SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_OVR);
1340 
1341  /* Clear ADC overrun flag */
1342  __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
1343 
1344  /* Error callback */
1345 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
1346  hadc->ErrorCallback(hadc);
1347 #else
1348  HAL_ADC_ErrorCallback(hadc);
1349 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
1350 
1351  /* Clear the Overrun flag */
1352  __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
1353  }
1354 }
1355 
1364 HAL_StatusTypeDef HAL_ADC_Start_DMA(ADC_HandleTypeDef *hadc, uint32_t *pData, uint32_t Length)
1365 {
1366  __IO uint32_t counter = 0U;
1367  ADC_Common_TypeDef *tmpADC_Common;
1368 
1369  /* Check the parameters */
1370  assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
1371  assert_param(IS_ADC_EXT_TRIG_EDGE(hadc->Init.ExternalTrigConvEdge));
1372 
1373  /* Process locked */
1374  __HAL_LOCK(hadc);
1375 
1376  /* Enable the ADC peripheral */
1377  /* Check if ADC peripheral is disabled in order to enable it and wait during
1378  Tstab time the ADC's stabilization */
1379  if ((hadc->Instance->CR2 & ADC_CR2_ADON) != ADC_CR2_ADON)
1380  {
1381  /* Enable the Peripheral */
1382  __HAL_ADC_ENABLE(hadc);
1383 
1384  /* Delay for ADC stabilization time */
1385  /* Compute number of CPU cycles to wait for */
1386  counter = (ADC_STAB_DELAY_US * (SystemCoreClock / 1000000U));
1387  while (counter != 0U)
1388  {
1389  counter--;
1390  }
1391  }
1392 
1393  /* Check ADC DMA Mode */
1394  /* - disable the DMA Mode if it is already enabled */
1395  if ((hadc->Instance->CR2 & ADC_CR2_DMA) == ADC_CR2_DMA)
1396  {
1397  CLEAR_BIT(hadc->Instance->CR2, ADC_CR2_DMA);
1398  }
1399 
1400  /* Start conversion if ADC is effectively enabled */
1401  if (HAL_IS_BIT_SET(hadc->Instance->CR2, ADC_CR2_ADON))
1402  {
1403  /* Set ADC state */
1404  /* - Clear state bitfield related to regular group conversion results */
1405  /* - Set state bitfield related to regular group operation */
1406  ADC_STATE_CLR_SET(hadc->State,
1407  HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR,
1408  HAL_ADC_STATE_REG_BUSY);
1409 
1410  /* If conversions on group regular are also triggering group injected, */
1411  /* update ADC state. */
1412  if (READ_BIT(hadc->Instance->CR1, ADC_CR1_JAUTO) != RESET)
1413  {
1414  ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
1415  }
1416 
1417  /* State machine update: Check if an injected conversion is ongoing */
1418  if (HAL_IS_BIT_SET(hadc->State, HAL_ADC_STATE_INJ_BUSY))
1419  {
1420  /* Reset ADC error code fields related to conversions on group regular */
1421  CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR | HAL_ADC_ERROR_DMA));
1422  }
1423  else
1424  {
1425  /* Reset ADC all error code fields */
1426  ADC_CLEAR_ERRORCODE(hadc);
1427  }
1428 
1429  /* Process unlocked */
1430  /* Unlock before starting ADC conversions: in case of potential */
1431  /* interruption, to let the process to ADC IRQ Handler. */
1432  __HAL_UNLOCK(hadc);
1433 
1434  /* Pointer to the common control register to which is belonging hadc */
1435  /* (Depending on STM32F4 product, there may be up to 3 ADCs and 1 common */
1436  /* control register) */
1437  tmpADC_Common = ADC_COMMON_REGISTER(hadc);
1438 
1439  /* Set the DMA transfer complete callback */
1440  hadc->DMA_Handle->XferCpltCallback = ADC_DMAConvCplt;
1441 
1442  /* Set the DMA half transfer complete callback */
1443  hadc->DMA_Handle->XferHalfCpltCallback = ADC_DMAHalfConvCplt;
1444 
1445  /* Set the DMA error callback */
1446  hadc->DMA_Handle->XferErrorCallback = ADC_DMAError;
1447 
1448 
1449  /* Manage ADC and DMA start: ADC overrun interruption, DMA start, ADC */
1450  /* start (in case of SW start): */
1451 
1452  /* Clear regular group conversion flag and overrun flag */
1453  /* (To ensure of no unknown state from potential previous ADC operations) */
1454  __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOC | ADC_FLAG_OVR);
1455 
1456  /* Enable ADC overrun interrupt */
1457  __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);
1458 
1459  /* Enable ADC DMA mode */
1460  hadc->Instance->CR2 |= ADC_CR2_DMA;
1461 
1462  /* Start the DMA channel */
1463  HAL_DMA_Start_IT(hadc->DMA_Handle, (uint32_t)&hadc->Instance->DR, (uint32_t)pData, Length);
1464 
1465  /* Check if Multimode enabled */
1466  if (HAL_IS_BIT_CLR(tmpADC_Common->CCR, ADC_CCR_MULTI))
1467  {
1468 #if defined(ADC2) && defined(ADC3)
1469  if ((hadc->Instance == ADC1) || ((hadc->Instance == ADC2) && ((ADC->CCR & ADC_CCR_MULTI_Msk) < ADC_CCR_MULTI_0)) \
1470  || ((hadc->Instance == ADC3) && ((ADC->CCR & ADC_CCR_MULTI_Msk) < ADC_CCR_MULTI_4)))
1471  {
1472 #endif /* ADC2 || ADC3 */
1473  /* if no external trigger present enable software conversion of regular channels */
1474  if ((hadc->Instance->CR2 & ADC_CR2_EXTEN) == RESET)
1475  {
1476  /* Enable the selected ADC software conversion for regular group */
1477  hadc->Instance->CR2 |= (uint32_t)ADC_CR2_SWSTART;
1478  }
1479 #if defined(ADC2) && defined(ADC3)
1480  }
1481 #endif /* ADC2 || ADC3 */
1482  }
1483  else
1484  {
1485  /* if instance of handle correspond to ADC1 and no external trigger present enable software conversion of regular channels */
1486  if ((hadc->Instance == ADC1) && ((hadc->Instance->CR2 & ADC_CR2_EXTEN) == RESET))
1487  {
1488  /* Enable the selected ADC software conversion for regular group */
1489  hadc->Instance->CR2 |= (uint32_t)ADC_CR2_SWSTART;
1490  }
1491  }
1492  }
1493  else
1494  {
1495  /* Update ADC state machine to error */
1496  SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
1497 
1498  /* Set ADC error code to ADC IP internal error */
1499  SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
1500  }
1501 
1502  /* Return function status */
1503  return HAL_OK;
1504 }
1505 
1512 HAL_StatusTypeDef HAL_ADC_Stop_DMA(ADC_HandleTypeDef *hadc)
1513 {
1514  HAL_StatusTypeDef tmp_hal_status = HAL_OK;
1515 
1516  /* Check the parameters */
1517  assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
1518 
1519  /* Process locked */
1520  __HAL_LOCK(hadc);
1521 
1522  /* Stop potential conversion on going, on regular and injected groups */
1523  /* Disable ADC peripheral */
1524  __HAL_ADC_DISABLE(hadc);
1525 
1526  /* Check if ADC is effectively disabled */
1527  if (HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_ADON))
1528  {
1529  /* Disable the selected ADC DMA mode */
1530  hadc->Instance->CR2 &= ~ADC_CR2_DMA;
1531 
1532  /* Disable the DMA channel (in case of DMA in circular mode or stop while */
1533  /* DMA transfer is on going) */
1534  if (hadc->DMA_Handle->State == HAL_DMA_STATE_BUSY)
1535  {
1536  tmp_hal_status = HAL_DMA_Abort(hadc->DMA_Handle);
1537 
1538  /* Check if DMA channel effectively disabled */
1539  if (tmp_hal_status != HAL_OK)
1540  {
1541  /* Update ADC state machine to error */
1542  SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
1543  }
1544  }
1545 
1546  /* Disable ADC overrun interrupt */
1547  __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);
1548 
1549  /* Set ADC state */
1550  ADC_STATE_CLR_SET(hadc->State,
1551  HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
1552  HAL_ADC_STATE_READY);
1553  }
1554 
1555  /* Process unlocked */
1556  __HAL_UNLOCK(hadc);
1557 
1558  /* Return function status */
1559  return tmp_hal_status;
1560 }
1561 
1569 {
1570  /* Return the selected ADC converted value */
1571  return hadc->Instance->DR;
1572 }
1573 
1581 {
1582  /* Prevent unused argument(s) compilation warning */
1583  UNUSED(hadc);
1584  /* NOTE : This function Should not be modified, when the callback is needed,
1585  the HAL_ADC_ConvCpltCallback could be implemented in the user file
1586  */
1587 }
1588 
1596 {
1597  /* Prevent unused argument(s) compilation warning */
1598  UNUSED(hadc);
1599  /* NOTE : This function Should not be modified, when the callback is needed,
1600  the HAL_ADC_ConvHalfCpltCallback could be implemented in the user file
1601  */
1602 }
1603 
1611 {
1612  /* Prevent unused argument(s) compilation warning */
1613  UNUSED(hadc);
1614  /* NOTE : This function Should not be modified, when the callback is needed,
1615  the HAL_ADC_LevelOoutOfWindowCallback could be implemented in the user file
1616  */
1617 }
1618 
1632 {
1633  /* Prevent unused argument(s) compilation warning */
1634  UNUSED(hadc);
1635  /* NOTE : This function Should not be modified, when the callback is needed,
1636  the HAL_ADC_ErrorCallback could be implemented in the user file
1637  */
1638 }
1639 
1670 {
1671  __IO uint32_t counter = 0U;
1672  ADC_Common_TypeDef *tmpADC_Common;
1673 
1674  /* Check the parameters */
1675  assert_param(IS_ADC_CHANNEL(sConfig->Channel));
1676  assert_param(IS_ADC_REGULAR_RANK(sConfig->Rank));
1677  assert_param(IS_ADC_SAMPLE_TIME(sConfig->SamplingTime));
1678 
1679  /* Process locked */
1680  __HAL_LOCK(hadc);
1681 
1682  /* if ADC_Channel_10 ... ADC_Channel_18 is selected */
1683  if (sConfig->Channel > ADC_CHANNEL_9)
1684  {
1685  /* Clear the old sample time */
1686  hadc->Instance->SMPR1 &= ~ADC_SMPR1(ADC_SMPR1_SMP10, sConfig->Channel);
1687 
1688  /* Set the new sample time */
1689  hadc->Instance->SMPR1 |= ADC_SMPR1(sConfig->SamplingTime, sConfig->Channel);
1690  }
1691  else /* ADC_Channel include in ADC_Channel_[0..9] */
1692  {
1693  /* Clear the old sample time */
1694  hadc->Instance->SMPR2 &= ~ADC_SMPR2(ADC_SMPR2_SMP0, sConfig->Channel);
1695 
1696  /* Set the new sample time */
1697  hadc->Instance->SMPR2 |= ADC_SMPR2(sConfig->SamplingTime, sConfig->Channel);
1698  }
1699 
1700  /* For Rank 1 to 6 */
1701  if (sConfig->Rank < 7U)
1702  {
1703  /* Clear the old SQx bits for the selected rank */
1704  hadc->Instance->SQR3 &= ~ADC_SQR3_RK(ADC_SQR3_SQ1, sConfig->Rank);
1705 
1706  /* Set the SQx bits for the selected rank */
1707  hadc->Instance->SQR3 |= ADC_SQR3_RK(sConfig->Channel, sConfig->Rank);
1708  }
1709  /* For Rank 7 to 12 */
1710  else if (sConfig->Rank < 13U)
1711  {
1712  /* Clear the old SQx bits for the selected rank */
1713  hadc->Instance->SQR2 &= ~ADC_SQR2_RK(ADC_SQR2_SQ7, sConfig->Rank);
1714 
1715  /* Set the SQx bits for the selected rank */
1716  hadc->Instance->SQR2 |= ADC_SQR2_RK(sConfig->Channel, sConfig->Rank);
1717  }
1718  /* For Rank 13 to 16 */
1719  else
1720  {
1721  /* Clear the old SQx bits for the selected rank */
1722  hadc->Instance->SQR1 &= ~ADC_SQR1_RK(ADC_SQR1_SQ13, sConfig->Rank);
1723 
1724  /* Set the SQx bits for the selected rank */
1725  hadc->Instance->SQR1 |= ADC_SQR1_RK(sConfig->Channel, sConfig->Rank);
1726  }
1727 
1728  /* Pointer to the common control register to which is belonging hadc */
1729  /* (Depending on STM32F4 product, there may be up to 3 ADCs and 1 common */
1730  /* control register) */
1731  tmpADC_Common = ADC_COMMON_REGISTER(hadc);
1732 
1733  /* if ADC1 Channel_18 is selected for VBAT Channel ennable VBATE */
1734  if ((hadc->Instance == ADC1) && (sConfig->Channel == ADC_CHANNEL_VBAT))
1735  {
1736  /* Disable the TEMPSENSOR channel in case of using board with multiplixed ADC_CHANNEL_VBAT & ADC_CHANNEL_TEMPSENSOR*/
1737  if ((uint16_t)ADC_CHANNEL_TEMPSENSOR == (uint16_t)ADC_CHANNEL_VBAT)
1738  {
1739  tmpADC_Common->CCR &= ~ADC_CCR_TSVREFE;
1740  }
1741  /* Enable the VBAT channel*/
1742  tmpADC_Common->CCR |= ADC_CCR_VBATE;
1743  }
1744 
1745  /* if ADC1 Channel_16 or Channel_18 is selected for Temperature sensor or
1746  Channel_17 is selected for VREFINT enable TSVREFE */
1747  if ((hadc->Instance == ADC1) && ((sConfig->Channel == ADC_CHANNEL_TEMPSENSOR) || (sConfig->Channel == ADC_CHANNEL_VREFINT)))
1748  {
1749  /* Disable the VBAT channel in case of using board with multiplixed ADC_CHANNEL_VBAT & ADC_CHANNEL_TEMPSENSOR*/
1750  if ((uint16_t)ADC_CHANNEL_TEMPSENSOR == (uint16_t)ADC_CHANNEL_VBAT)
1751  {
1752  tmpADC_Common->CCR &= ~ADC_CCR_VBATE;
1753  }
1754  /* Enable the Temperature sensor and VREFINT channel*/
1755  tmpADC_Common->CCR |= ADC_CCR_TSVREFE;
1756 
1757  if (sConfig->Channel == ADC_CHANNEL_TEMPSENSOR)
1758  {
1759  /* Delay for temperature sensor stabilization time */
1760  /* Compute number of CPU cycles to wait for */
1761  counter = (ADC_TEMPSENSOR_DELAY_US * (SystemCoreClock / 1000000U));
1762  while (counter != 0U)
1763  {
1764  counter--;
1765  }
1766  }
1767  }
1768 
1769  /* Process unlocked */
1770  __HAL_UNLOCK(hadc);
1771 
1772  /* Return function status */
1773  return HAL_OK;
1774 }
1775 
1792 HAL_StatusTypeDef HAL_ADC_AnalogWDGConfig(ADC_HandleTypeDef *hadc, ADC_AnalogWDGConfTypeDef *AnalogWDGConfig)
1793 {
1794 #ifdef USE_FULL_ASSERT
1795  uint32_t tmp = 0U;
1796 #endif /* USE_FULL_ASSERT */
1797 
1798  /* Check the parameters */
1799  assert_param(IS_ADC_ANALOG_WATCHDOG(AnalogWDGConfig->WatchdogMode));
1800  assert_param(IS_ADC_CHANNEL(AnalogWDGConfig->Channel));
1801  assert_param(IS_FUNCTIONAL_STATE(AnalogWDGConfig->ITMode));
1802 
1803 #ifdef USE_FULL_ASSERT
1804  tmp = ADC_GET_RESOLUTION(hadc);
1805  assert_param(IS_ADC_RANGE(tmp, AnalogWDGConfig->HighThreshold));
1806  assert_param(IS_ADC_RANGE(tmp, AnalogWDGConfig->LowThreshold));
1807 #endif /* USE_FULL_ASSERT */
1808 
1809  /* Process locked */
1810  __HAL_LOCK(hadc);
1811 
1812  if (AnalogWDGConfig->ITMode == ENABLE)
1813  {
1814  /* Enable the ADC Analog watchdog interrupt */
1815  __HAL_ADC_ENABLE_IT(hadc, ADC_IT_AWD);
1816  }
1817  else
1818  {
1819  /* Disable the ADC Analog watchdog interrupt */
1820  __HAL_ADC_DISABLE_IT(hadc, ADC_IT_AWD);
1821  }
1822 
1823  /* Clear AWDEN, JAWDEN and AWDSGL bits */
1824  hadc->Instance->CR1 &= ~(ADC_CR1_AWDSGL | ADC_CR1_JAWDEN | ADC_CR1_AWDEN);
1825 
1826  /* Set the analog watchdog enable mode */
1827  hadc->Instance->CR1 |= AnalogWDGConfig->WatchdogMode;
1828 
1829  /* Set the high threshold */
1830  hadc->Instance->HTR = AnalogWDGConfig->HighThreshold;
1831 
1832  /* Set the low threshold */
1833  hadc->Instance->LTR = AnalogWDGConfig->LowThreshold;
1834 
1835  /* Clear the Analog watchdog channel select bits */
1836  hadc->Instance->CR1 &= ~ADC_CR1_AWDCH;
1837 
1838  /* Set the Analog watchdog channel */
1839  hadc->Instance->CR1 |= (uint32_t)((uint16_t)(AnalogWDGConfig->Channel));
1840 
1841  /* Process unlocked */
1842  __HAL_UNLOCK(hadc);
1843 
1844  /* Return function status */
1845  return HAL_OK;
1846 }
1847 
1875 {
1876  /* Return ADC state */
1877  return hadc->State;
1878 }
1879 
1887 {
1888  return hadc->ErrorCode;
1889 }
1890 
1906 static void ADC_Init(ADC_HandleTypeDef *hadc)
1907 {
1908  ADC_Common_TypeDef *tmpADC_Common;
1909 
1910  /* Set ADC parameters */
1911  /* Pointer to the common control register to which is belonging hadc */
1912  /* (Depending on STM32F4 product, there may be up to 3 ADCs and 1 common */
1913  /* control register) */
1914  tmpADC_Common = ADC_COMMON_REGISTER(hadc);
1915 
1916  /* Set the ADC clock prescaler */
1917  tmpADC_Common->CCR &= ~(ADC_CCR_ADCPRE);
1918  tmpADC_Common->CCR |= hadc->Init.ClockPrescaler;
1919 
1920  /* Set ADC scan mode */
1921  hadc->Instance->CR1 &= ~(ADC_CR1_SCAN);
1922  hadc->Instance->CR1 |= ADC_CR1_SCANCONV(hadc->Init.ScanConvMode);
1923 
1924  /* Set ADC resolution */
1925  hadc->Instance->CR1 &= ~(ADC_CR1_RES);
1926  hadc->Instance->CR1 |= hadc->Init.Resolution;
1927 
1928  /* Set ADC data alignment */
1929  hadc->Instance->CR2 &= ~(ADC_CR2_ALIGN);
1930  hadc->Instance->CR2 |= hadc->Init.DataAlign;
1931 
1932  /* Enable external trigger if trigger selection is different of software */
1933  /* start. */
1934  /* Note: This configuration keeps the hardware feature of parameter */
1935  /* ExternalTrigConvEdge "trigger edge none" equivalent to */
1936  /* software start. */
1937  if (hadc->Init.ExternalTrigConv != ADC_SOFTWARE_START)
1938  {
1939  /* Select external trigger to start conversion */
1940  hadc->Instance->CR2 &= ~(ADC_CR2_EXTSEL);
1941  hadc->Instance->CR2 |= hadc->Init.ExternalTrigConv;
1942 
1943  /* Select external trigger polarity */
1944  hadc->Instance->CR2 &= ~(ADC_CR2_EXTEN);
1945  hadc->Instance->CR2 |= hadc->Init.ExternalTrigConvEdge;
1946  }
1947  else
1948  {
1949  /* Reset the external trigger */
1950  hadc->Instance->CR2 &= ~(ADC_CR2_EXTSEL);
1951  hadc->Instance->CR2 &= ~(ADC_CR2_EXTEN);
1952  }
1953 
1954  /* Enable or disable ADC continuous conversion mode */
1955  hadc->Instance->CR2 &= ~(ADC_CR2_CONT);
1956  hadc->Instance->CR2 |= ADC_CR2_CONTINUOUS((uint32_t)hadc->Init.ContinuousConvMode);
1957 
1958  if (hadc->Init.DiscontinuousConvMode != DISABLE)
1959  {
1960  assert_param(IS_ADC_REGULAR_DISC_NUMBER(hadc->Init.NbrOfDiscConversion));
1961 
1962  /* Enable the selected ADC regular discontinuous mode */
1963  hadc->Instance->CR1 |= (uint32_t)ADC_CR1_DISCEN;
1964 
1965  /* Set the number of channels to be converted in discontinuous mode */
1966  hadc->Instance->CR1 &= ~(ADC_CR1_DISCNUM);
1967  hadc->Instance->CR1 |= ADC_CR1_DISCONTINUOUS(hadc->Init.NbrOfDiscConversion);
1968  }
1969  else
1970  {
1971  /* Disable the selected ADC regular discontinuous mode */
1972  hadc->Instance->CR1 &= ~(ADC_CR1_DISCEN);
1973  }
1974 
1975  /* Set ADC number of conversion */
1976  hadc->Instance->SQR1 &= ~(ADC_SQR1_L);
1977  hadc->Instance->SQR1 |= ADC_SQR1(hadc->Init.NbrOfConversion);
1978 
1979  /* Enable or disable ADC DMA continuous request */
1980  hadc->Instance->CR2 &= ~(ADC_CR2_DDS);
1981  hadc->Instance->CR2 |= ADC_CR2_DMAContReq((uint32_t)hadc->Init.DMAContinuousRequests);
1982 
1983  /* Enable or disable ADC end of conversion selection */
1984  hadc->Instance->CR2 &= ~(ADC_CR2_EOCS);
1985  hadc->Instance->CR2 |= ADC_CR2_EOCSelection(hadc->Init.EOCSelection);
1986 }
1987 
1994 static void ADC_DMAConvCplt(DMA_HandleTypeDef *hdma)
1995 {
1996  /* Retrieve ADC handle corresponding to current DMA handle */
1997  ADC_HandleTypeDef *hadc = (ADC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
1998 
1999  /* Update state machine on conversion status if not in error state */
2000  if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL | HAL_ADC_STATE_ERROR_DMA))
2001  {
2002  /* Update ADC state machine */
2003  SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
2004 
2005  /* Determine whether any further conversion upcoming on group regular */
2006  /* by external trigger, continuous mode or scan sequence on going. */
2007  /* Note: On STM32F4, there is no independent flag of end of sequence. */
2008  /* The test of scan sequence on going is done either with scan */
2009  /* sequence disabled or with end of conversion flag set to */
2010  /* of end of sequence. */
2011  if (ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
2012  (hadc->Init.ContinuousConvMode == DISABLE) &&
2013  (HAL_IS_BIT_CLR(hadc->Instance->SQR1, ADC_SQR1_L) ||
2014  HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_EOCS)))
2015  {
2016  /* Disable ADC end of single conversion interrupt on group regular */
2017  /* Note: Overrun interrupt was enabled with EOC interrupt in */
2018  /* HAL_ADC_Start_IT(), but is not disabled here because can be used */
2019  /* by overrun IRQ process below. */
2020  __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC);
2021 
2022  /* Set ADC state */
2023  CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
2024 
2025  if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_INJ_BUSY))
2026  {
2027  SET_BIT(hadc->State, HAL_ADC_STATE_READY);
2028  }
2029  }
2030 
2031  /* Conversion complete callback */
2032 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
2033  hadc->ConvCpltCallback(hadc);
2034 #else
2036 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
2037  }
2038  else /* DMA and-or internal error occurred */
2039  {
2040  if ((hadc->State & HAL_ADC_STATE_ERROR_INTERNAL) != 0UL)
2041  {
2042  /* Call HAL ADC Error Callback function */
2043 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
2044  hadc->ErrorCallback(hadc);
2045 #else
2046  HAL_ADC_ErrorCallback(hadc);
2047 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
2048  }
2049  else
2050  {
2051  /* Call DMA error callback */
2052  hadc->DMA_Handle->XferErrorCallback(hdma);
2053  }
2054  }
2055 }
2056 
2063 static void ADC_DMAHalfConvCplt(DMA_HandleTypeDef *hdma)
2064 {
2065  ADC_HandleTypeDef *hadc = (ADC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
2066  /* Half conversion callback */
2067 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
2068  hadc->ConvHalfCpltCallback(hadc);
2069 #else
2071 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
2072 }
2073 
2080 static void ADC_DMAError(DMA_HandleTypeDef *hdma)
2081 {
2082  ADC_HandleTypeDef *hadc = (ADC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
2083  hadc->State = HAL_ADC_STATE_ERROR_DMA;
2084  /* Set ADC error code to DMA error */
2085  hadc->ErrorCode |= HAL_ADC_ERROR_DMA;
2086  /* Error callback */
2087 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
2088  hadc->ErrorCallback(hadc);
2089 #else
2090  HAL_ADC_ErrorCallback(hadc);
2091 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
2092 }
2093 
2102 #endif /* HAL_ADC_MODULE_ENABLED */
void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef *hadc)
Injected conversion complete callback in non blocking mode.
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.
HAL_StatusTypeDef HAL_ADC_DeInit(ADC_HandleTypeDef *hadc)
Deinitializes the ADCx peripheral registers to their default reset values.
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.
HAL_StatusTypeDef HAL_ADC_Init(ADC_HandleTypeDef *hadc)
Initializes the ADCx peripheral according to the specified parameters in the ADC_InitStruct and initi...
void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc)
Initializes the ADC MSP.
void HAL_ADC_MspDeInit(ADC_HandleTypeDef *hadc)
DeInitializes the ADC MSP.
HAL_StatusTypeDef HAL_ADC_PollForEvent(ADC_HandleTypeDef *hadc, uint32_t EventType, uint32_t Timeout)
Poll for conversion event.
HAL_StatusTypeDef HAL_ADC_Start(ADC_HandleTypeDef *hadc)
Enables ADC and starts conversion of the regular channels.
HAL_StatusTypeDef HAL_ADC_Stop(ADC_HandleTypeDef *hadc)
Disables ADC and stop conversion of regular channels.
void HAL_ADC_IRQHandler(ADC_HandleTypeDef *hadc)
Handles ADC interrupt request.
uint32_t HAL_ADC_GetValue(ADC_HandleTypeDef *hadc)
Gets the converted value from data register of regular channel.
void HAL_ADC_LevelOutOfWindowCallback(ADC_HandleTypeDef *hadc)
Analog watchdog callback in non blocking mode.
HAL_StatusTypeDef HAL_ADC_Start_DMA(ADC_HandleTypeDef *hadc, uint32_t *pData, uint32_t Length)
Enables ADC DMA request after last transfer (Single-ADC mode) and enables ADC peripheral.
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
Regular conversion complete callback in non blocking mode.
void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef *hadc)
Regular conversion half DMA transfer callback in non blocking mode.
HAL_StatusTypeDef HAL_ADC_PollForConversion(ADC_HandleTypeDef *hadc, uint32_t Timeout)
Poll for regular conversion complete.
void HAL_ADC_ErrorCallback(ADC_HandleTypeDef *hadc)
Error ADC callback.
HAL_StatusTypeDef HAL_ADC_Stop_DMA(ADC_HandleTypeDef *hadc)
Disables ADC DMA (Single-ADC mode) and disables ADC peripheral.
HAL_StatusTypeDef HAL_ADC_Stop_IT(ADC_HandleTypeDef *hadc)
Disables the interrupt and stop ADC conversion of regular channels.
HAL_StatusTypeDef HAL_ADC_Start_IT(ADC_HandleTypeDef *hadc)
Enables the interrupt and starts ADC conversion of regular channels.
HAL_StatusTypeDef HAL_ADC_ConfigChannel(ADC_HandleTypeDef *hadc, ADC_ChannelConfTypeDef *sConfig)
Configures for the selected ADC regular channel its corresponding rank in the sequencer and its sampl...
HAL_StatusTypeDef HAL_ADC_AnalogWDGConfig(ADC_HandleTypeDef *hadc, ADC_AnalogWDGConfTypeDef *AnalogWDGConfig)
Configures the analog watchdog.
uint32_t HAL_ADC_GetError(ADC_HandleTypeDef *hadc)
Return the ADC error code.
uint32_t HAL_ADC_GetState(ADC_HandleTypeDef *hadc)
return the ADC state
HAL_ADC_CallbackIDTypeDef
HAL ADC Callback ID enumeration definition.
struct __ADC_HandleTypeDef else typedef struct endif ADC_HandleTypeDef
ADC handle Structure definition.
void(* pADC_CallbackTypeDef)(ADC_HandleTypeDef *hadc)
HAL ADC Callback pointer definition.
@ HAL_ADC_INJ_CONVERSION_COMPLETE_CB_ID
@ HAL_ADC_CONVERSION_COMPLETE_CB_ID
@ HAL_ADC_CONVERSION_HALF_CB_ID
@ HAL_ADC_MSPDEINIT_CB_ID
@ HAL_ADC_ERROR_CB_ID
@ HAL_ADC_MSPINIT_CB_ID
@ HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID
ADC Configuration multi-mode structure definition.
Structure definition of ADC channel for regular group.
HAL_StatusTypeDef HAL_DMA_Abort(DMA_HandleTypeDef *hdma)
Aborts the DMA Transfer.
HAL_StatusTypeDef HAL_DMA_Start_IT(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
Start the DMA Transfer with interrupt enabled.
uint32_t HAL_GetTick(void)
Provides a tick value in millisecond.
This file contains all the functions prototypes for the HAL module driver.
@ HAL_DMA_STATE_BUSY
DMA handle Structure definition.