Skip to content

Commit

Permalink
Initialize each ADC only one in Analogread
Browse files Browse the repository at this point in the history
  • Loading branch information
Candas1 committed Sep 18, 2023
1 parent 6c48d73 commit cc785a9
Showing 1 changed file with 40 additions and 21 deletions.
61 changes: 40 additions & 21 deletions libraries/SrcWrapper/src/stm32/analog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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)
Expand All @@ -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) {
Expand All @@ -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*/
Expand Down

0 comments on commit cc785a9

Please sign in to comment.