Skip to content

Commit 4b82479

Browse files
committed
STM32L0 ADC: remove adc_inited flag
1 parent 75f80ab commit 4b82479

File tree

1 file changed

+32
-37
lines changed

1 file changed

+32
-37
lines changed

targets/TARGET_STM/TARGET_STM32L0/analogin_api.c

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,10 @@
3636
#include "mbed_error.h"
3737
#include "PeripheralPins.h"
3838

39-
int adc_inited = 0;
40-
4139
void analogin_init(analogin_t *obj, PinName pin)
4240
{
41+
static int adc_calibrated = 0;
4342
uint32_t function = (uint32_t)NC;
44-
obj->handle.Instance = (ADC_TypeDef *)NC;
4543

4644
// ADC Internal Channels "pins" (Temperature, Vref, Vbat, ...)
4745
// are described in PinNames.h and PeripheralPins.c
@@ -68,41 +66,38 @@ void analogin_init(analogin_t *obj, PinName pin)
6866
// Save pin number for the read function
6967
obj->pin = pin;
7068

71-
// The ADC initialization is done once
72-
if (adc_inited == 0) {
73-
adc_inited = 1;
74-
75-
obj->handle.State = HAL_ADC_STATE_RESET;
76-
// Enable ADC clock
77-
__ADC1_CLK_ENABLE();
78-
79-
// Configure ADC
80-
obj->handle.Init.OversamplingMode = DISABLE;
81-
obj->handle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV1;
82-
obj->handle.Init.Resolution = ADC_RESOLUTION12b;
83-
obj->handle.Init.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
84-
obj->handle.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
85-
obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
86-
obj->handle.Init.ContinuousConvMode = DISABLE;
87-
obj->handle.Init.DiscontinuousConvMode = DISABLE;
88-
obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIG_EDGE_NONE;
89-
obj->handle.Init.ExternalTrigConv = ADC_EXTERNALTRIG0_T6_TRGO; // Not used here
90-
obj->handle.Init.DMAContinuousRequests = DISABLE;
91-
obj->handle.Init.EOCSelection = EOC_SINGLE_CONV;
92-
obj->handle.Init.Overrun = OVR_DATA_OVERWRITTEN;
93-
obj->handle.Init.LowPowerAutoWait = ENABLE;
94-
obj->handle.Init.LowPowerFrequencyMode = DISABLE; // To be enabled only if ADC clock < 2.8 MHz
95-
obj->handle.Init.LowPowerAutoPowerOff = DISABLE;
96-
97-
if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
98-
error("Cannot initialize ADC");
99-
}
100-
101-
// Calibration
102-
HAL_ADCEx_Calibration_Start(&obj->handle, ADC_SINGLE_ENDED);
69+
// Configure ADC object structures
70+
obj->handle.State = HAL_ADC_STATE_RESET;
71+
obj->handle.Init.OversamplingMode = DISABLE;
72+
obj->handle.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV1;
73+
obj->handle.Init.Resolution = ADC_RESOLUTION_12B;
74+
obj->handle.Init.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
75+
obj->handle.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
76+
obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
77+
obj->handle.Init.ContinuousConvMode = DISABLE;
78+
obj->handle.Init.DiscontinuousConvMode = DISABLE;
79+
obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIG_EDGE_NONE;
80+
obj->handle.Init.ExternalTrigConv = ADC_EXTERNALTRIG0_T6_TRGO; // Not used here
81+
obj->handle.Init.DMAContinuousRequests = DISABLE;
82+
obj->handle.Init.EOCSelection = EOC_SINGLE_CONV;
83+
obj->handle.Init.Overrun = OVR_DATA_OVERWRITTEN;
84+
obj->handle.Init.LowPowerAutoWait = ENABLE;
85+
obj->handle.Init.LowPowerFrequencyMode = DISABLE; // To be enabled only if ADC clock < 2.8 MHz
86+
obj->handle.Init.LowPowerAutoPowerOff = DISABLE;
87+
88+
__HAL_RCC_ADC1_CLK_ENABLE();
89+
90+
if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
91+
error("Cannot initialize ADC");
92+
}
10393

104-
__HAL_ADC_ENABLE(&obj->handle);
94+
// ADC calibration is done only once
95+
if (adc_calibrated == 0) {
96+
adc_calibrated = 1;
97+
HAL_ADCEx_Calibration_Start(&obj->handle, ADC_SINGLE_ENDED);
10598
}
99+
100+
__HAL_ADC_ENABLE(&obj->handle);
106101
}
107102

108103
static inline uint16_t adc_read(analogin_t *obj)
@@ -182,7 +177,7 @@ static inline uint16_t adc_read(analogin_t *obj)
182177

183178
// Wait end of conversion and get value
184179
if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) {
185-
return (HAL_ADC_GetValue(&obj->handle));
180+
return (uint16_t)HAL_ADC_GetValue(&obj->handle);
186181
} else {
187182
return 0;
188183
}

0 commit comments

Comments
 (0)