Skip to content

Commit e4a41dc

Browse files
authored
Initialize ADC only once in Analogread
Initialize ADC only once in Analogread Signed-off-by: Candas1 <[email protected]>
1 parent 0c69f37 commit e4a41dc

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

Diff for: libraries/SrcWrapper/src/stm32/analog.cpp

+23-15
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,8 @@ void HAL_ADC_MspDeInit(ADC_HandleTypeDef *hadc)
759759
#endif
760760
}
761761

762+
int8_t first;
763+
762764
/**
763765
* @brief This function will set the ADC to the required value
764766
* @param pin : the pin to use
@@ -810,6 +812,9 @@ uint16_t adc_read_value(PinName pin, uint32_t resolution)
810812
return 0;
811813
}
812814

815+
if(!first){
816+
first = true;
817+
813818
#ifdef ADC_CLOCK_DIV
814819
AdcHandle.Init.ClockPrescaler = ADC_CLOCK_DIV; /* (A)synchronous clock mode, input ADC clock divided */
815820
#endif
@@ -975,12 +980,6 @@ uint16_t adc_read_value(PinName pin, uint32_t resolution)
975980
AdcChannelConf.OffsetSignedSaturation = DISABLE; /* Signed saturation feature is not used */
976981
#endif
977982

978-
/*##-2- Configure ADC regular channel ######################################*/
979-
if (HAL_ADC_ConfigChannel(&AdcHandle, &AdcChannelConf) != HAL_OK) {
980-
/* Channel Configuration Error */
981-
return 0;
982-
}
983-
984983
#if defined(ADC_CR_ADCAL) || defined(ADC_CR2_RSTCAL)
985984
/*##-2.1- Calibrate ADC then Start the conversion process ####################*/
986985
#if defined(ADC_CALIB_OFFSET)
@@ -995,6 +994,15 @@ uint16_t adc_read_value(PinName pin, uint32_t resolution)
995994
return 0;
996995
}
997996
#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+
}
9981006

9991007
/*##-3- Start the conversion process ####################*/
10001008
if (HAL_ADC_Start(&AdcHandle) != HAL_OK) {
@@ -1017,18 +1025,18 @@ uint16_t adc_read_value(PinName pin, uint32_t resolution)
10171025
uhADCxConvertedValue = HAL_ADC_GetValue(&AdcHandle);
10181026
}
10191027

1020-
if (HAL_ADC_Stop(&AdcHandle) != HAL_OK) {
1028+
//if (HAL_ADC_Stop(&AdcHandle) != HAL_OK) {
10211029
/* Stop Conversation Error */
1022-
return 0;
1023-
}
1030+
//return 0;
1031+
//}
10241032

1025-
if (HAL_ADC_DeInit(&AdcHandle) != HAL_OK) {
1026-
return 0;
1027-
}
1033+
//if (HAL_ADC_DeInit(&AdcHandle) != HAL_OK) {
1034+
//return 0;
1035+
//}
10281036

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+
//}
10321040
return uhADCxConvertedValue;
10331041
}
10341042
#endif /* HAL_ADC_MODULE_ENABLED && !HAL_ADC_MODULE_ONLY*/

0 commit comments

Comments
 (0)