@@ -759,6 +759,8 @@ void HAL_ADC_MspDeInit(ADC_HandleTypeDef *hadc)
759
759
#endif
760
760
}
761
761
762
+ int8_t first;
763
+
762
764
/* *
763
765
* @brief This function will set the ADC to the required value
764
766
* @param pin : the pin to use
@@ -810,6 +812,9 @@ uint16_t adc_read_value(PinName pin, uint32_t resolution)
810
812
return 0 ;
811
813
}
812
814
815
+ if (!first){
816
+ first = true ;
817
+
813
818
#ifdef ADC_CLOCK_DIV
814
819
AdcHandle.Init .ClockPrescaler = ADC_CLOCK_DIV; /* (A)synchronous clock mode, input ADC clock divided */
815
820
#endif
@@ -975,12 +980,6 @@ uint16_t adc_read_value(PinName pin, uint32_t resolution)
975
980
AdcChannelConf.OffsetSignedSaturation = DISABLE; /* Signed saturation feature is not used */
976
981
#endif
977
982
978
- /* ##-2- Configure ADC regular channel ######################################*/
979
- if (HAL_ADC_ConfigChannel (&AdcHandle, &AdcChannelConf) != HAL_OK) {
980
- /* Channel Configuration Error */
981
- return 0 ;
982
- }
983
-
984
983
#if defined(ADC_CR_ADCAL) || defined(ADC_CR2_RSTCAL)
985
984
/* ##-2.1- Calibrate ADC then Start the conversion process ####################*/
986
985
#if defined(ADC_CALIB_OFFSET)
@@ -995,6 +994,15 @@ uint16_t adc_read_value(PinName pin, uint32_t resolution)
995
994
return 0 ;
996
995
}
997
996
#endif
997
+
998
+ }
999
+ // Initializer ADC only once
1000
+
1001
+ /* ##-2- Configure ADC regular channel ######################################*/
1002
+ if (HAL_ADC_ConfigChannel (&AdcHandle, &AdcChannelConf) != HAL_OK) {
1003
+ /* Channel Configuration Error */
1004
+ return 0 ;
1005
+ }
998
1006
999
1007
/* ##-3- Start the conversion process ####################*/
1000
1008
if (HAL_ADC_Start (&AdcHandle) != HAL_OK) {
@@ -1017,18 +1025,18 @@ uint16_t adc_read_value(PinName pin, uint32_t resolution)
1017
1025
uhADCxConvertedValue = HAL_ADC_GetValue (&AdcHandle);
1018
1026
}
1019
1027
1020
- if (HAL_ADC_Stop (&AdcHandle) != HAL_OK) {
1028
+ // if (HAL_ADC_Stop(&AdcHandle) != HAL_OK) {
1021
1029
/* Stop Conversation Error */
1022
- return 0 ;
1023
- }
1030
+ // return 0;
1031
+ // }
1024
1032
1025
- if (HAL_ADC_DeInit (&AdcHandle) != HAL_OK) {
1026
- return 0 ;
1027
- }
1033
+ // if (HAL_ADC_DeInit(&AdcHandle) != HAL_OK) {
1034
+ // return 0;
1035
+ // }
1028
1036
1029
- if (__LL_ADC_COMMON_INSTANCE (AdcHandle.Instance ) != 0U ) {
1030
- LL_ADC_SetCommonPathInternalCh (__LL_ADC_COMMON_INSTANCE (AdcHandle.Instance ), LL_ADC_PATH_INTERNAL_NONE);
1031
- }
1037
+ // if (__LL_ADC_COMMON_INSTANCE(AdcHandle.Instance) != 0U) {
1038
+ // LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(AdcHandle.Instance), LL_ADC_PATH_INTERNAL_NONE);
1039
+ // }
1032
1040
return uhADCxConvertedValue;
1033
1041
}
1034
1042
#endif /* HAL_ADC_MODULE_ENABLED && !HAL_ADC_MODULE_ONLY*/
0 commit comments