From cc785a9505b66320b8e80d555135e9d212fc24b1 Mon Sep 17 00:00:00 2001 From: candas Date: Mon, 18 Sep 2023 23:52:28 +0200 Subject: [PATCH] Initialize each ADC only one in Analogread --- libraries/SrcWrapper/src/stm32/analog.cpp | 61 +++++++++++++++-------- 1 file changed, 40 insertions(+), 21 deletions(-) diff --git a/libraries/SrcWrapper/src/stm32/analog.cpp b/libraries/SrcWrapper/src/stm32/analog.cpp index 63ff690c47..7e215d1024 100644 --- a/libraries/SrcWrapper/src/stm32/analog.cpp +++ b/libraries/SrcWrapper/src/stm32/analog.cpp @@ -101,6 +101,20 @@ static PinName g_current_pin = NC; #define ADC_REGULAR_RANK_1 1 #endif +#if defined(ADC5) +#define ADC_COUNT 5 +#elif defined(ADC4) +#define ADC_COUNT 4 +#elif defined(ADC3) +#define ADC_COUNT 3 +#elif defined(ADC2) +#define ADC_COUNT 2 +#elif defined(ADC1) +#define ADC_COUNT 1 +#endif + +uint8_t adc_is_active[ADC_COUNT] = {0}; + /* Exported Functions */ /** * @brief Return ADC HAL channel linked to a PinName @@ -773,6 +787,7 @@ uint16_t adc_read_value(PinName pin, uint32_t resolution) uint32_t samplingTime = ADC_SAMPLINGTIME; uint32_t channel = 0; uint32_t bank = 0; + uint8_t adc_index = 0; if ((pin & PADC_BASE) && (pin < ANA_START)) { #if defined(STM32H7xx) || defined(STM32MP1xx) @@ -806,9 +821,23 @@ uint16_t adc_read_value(PinName pin, uint32_t resolution) #endif } - if (AdcHandle.Instance == NP) { - return 0; - } + if (AdcHandle.Instance == NP) return 0; + else if(AdcHandle.Instance == ADC1) adc_index = 0; + #ifdef ADC2 + else if(AdcHandle.Instance == ADC2) adc_index = 1; + #endif + #ifdef ADC3 + else if(AdcHandle.Instance == ADC3) adc_index = 2; + #endif + #ifdef ADC4 + else if(AdcHandle.Instance == ADC4) adc_index = 3; + #endif + #ifdef ADC5 + else if(AdcHandle.Instance == ADC5) adc_index = 4; + #endif + + if(!adc_is_active[adc_index]){ + adc_is_active[adc_index] = true; #ifdef ADC_CLOCK_DIV AdcHandle.Init.ClockPrescaler = ADC_CLOCK_DIV; /* (A)synchronous clock mode, input ADC clock divided */ @@ -975,12 +1004,6 @@ uint16_t adc_read_value(PinName pin, uint32_t resolution) AdcChannelConf.OffsetSignedSaturation = DISABLE; /* Signed saturation feature is not used */ #endif - /*##-2- Configure ADC regular channel ######################################*/ - if (HAL_ADC_ConfigChannel(&AdcHandle, &AdcChannelConf) != HAL_OK) { - /* Channel Configuration Error */ - return 0; - } - #if defined(ADC_CR_ADCAL) || defined(ADC_CR2_RSTCAL) /*##-2.1- Calibrate ADC then Start the conversion process ####################*/ #if defined(ADC_CALIB_OFFSET) @@ -995,6 +1018,14 @@ uint16_t adc_read_value(PinName pin, uint32_t resolution) return 0; } #endif + + } // Initialize ADC only once + + /*##-2- Configure ADC regular channel ######################################*/ + if (HAL_ADC_ConfigChannel(&AdcHandle, &AdcChannelConf) != HAL_OK) { + /* Channel Configuration Error */ + return 0; + } /*##-3- Start the conversion process ####################*/ if (HAL_ADC_Start(&AdcHandle) != HAL_OK) { @@ -1017,18 +1048,6 @@ uint16_t adc_read_value(PinName pin, uint32_t resolution) uhADCxConvertedValue = HAL_ADC_GetValue(&AdcHandle); } - if (HAL_ADC_Stop(&AdcHandle) != HAL_OK) { - /* Stop Conversation Error */ - return 0; - } - - if (HAL_ADC_DeInit(&AdcHandle) != HAL_OK) { - return 0; - } - - if (__LL_ADC_COMMON_INSTANCE(AdcHandle.Instance) != 0U) { - LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(AdcHandle.Instance), LL_ADC_PATH_INTERNAL_NONE); - } return uhADCxConvertedValue; } #endif /* HAL_ADC_MODULE_ENABLED && !HAL_ADC_MODULE_ONLY*/