Skip to content

Commit 072ec2e

Browse files
committed
STM32L4 ADC: remove adc_inited flag
1 parent 52d942e commit 072ec2e

File tree

1 file changed

+33
-36
lines changed

1 file changed

+33
-36
lines changed

targets/TARGET_STM/TARGET_STM32L4/analogin_api.c

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,9 @@
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;
4443

4544
// ADC Internal Channels "pins" (Temperature, Vref, Vbat, ...)
@@ -48,14 +47,14 @@ void analogin_init(analogin_t *obj, PinName pin)
4847
if ((pin < 0xF0) || (pin >= 0x100)) {
4948
// Normal channels
5049
// Get the peripheral name from the pin and assign it to the object
51-
obj->handle.Instance = (ADC_TypeDef *) pinmap_peripheral(pin, PinMap_ADC);
50+
obj->handle.Instance = (ADC_TypeDef *)pinmap_peripheral(pin, PinMap_ADC);
5251
// Get the functions (adc channel) from the pin and assign it to the object
5352
function = pinmap_function(pin, PinMap_ADC);
5453
// Configure GPIO
5554
pinmap_pinout(pin, PinMap_ADC);
5655
} else {
5756
// Internal channels
58-
obj->handle.Instance = (ADC_TypeDef *) pinmap_peripheral(pin, PinMap_ADC_Internal);
57+
obj->handle.Instance = (ADC_TypeDef *)pinmap_peripheral(pin, PinMap_ADC_Internal);
5958
function = pinmap_function(pin, PinMap_ADC_Internal);
6059
// No GPIO configuration for internal channels
6160
}
@@ -67,37 +66,35 @@ void analogin_init(analogin_t *obj, PinName pin)
6766
// Save pin number for the read function
6867
obj->pin = pin;
6968

70-
// The ADC initialization is done once
71-
if (adc_inited == 0) {
72-
adc_inited = 1;
73-
74-
// Enable ADC clock
75-
__HAL_RCC_ADC_CLK_ENABLE();
76-
__HAL_RCC_ADC_CONFIG(RCC_ADCCLKSOURCE_SYSCLK);
77-
78-
obj->handle.State = HAL_ADC_STATE_RESET;
79-
// Configure ADC
80-
obj->handle.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV2; // Asynchronous clock mode, input ADC clock
81-
obj->handle.Init.Resolution = ADC_RESOLUTION_12B;
82-
obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
83-
obj->handle.Init.ScanConvMode = DISABLE; // Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1)
84-
obj->handle.Init.EOCSelection = ADC_EOC_SINGLE_CONV; // On STM32L1xx ADC, overrun detection is enabled only if EOC selection is set to each conversion (or transfer by DMA enabled, this is not the case in this example).
85-
obj->handle.Init.LowPowerAutoWait = DISABLE;
86-
obj->handle.Init.ContinuousConvMode = DISABLE; // Continuous mode disabled to have only 1 conversion at each conversion trig
87-
obj->handle.Init.NbrOfConversion = 1; // Parameter discarded because sequencer is disabled
88-
obj->handle.Init.DiscontinuousConvMode = DISABLE; // Parameter discarded because sequencer is disabled
89-
obj->handle.Init.NbrOfDiscConversion = 1; // Parameter discarded because sequencer is disabled
90-
obj->handle.Init.ExternalTrigConv = ADC_SOFTWARE_START; // Software start to trig the 1st conversion manually, without external event
91-
obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
92-
obj->handle.Init.DMAContinuousRequests = DISABLE;
93-
obj->handle.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; // DR register is overwritten with the last conversion result in case of overrun
94-
obj->handle.Init.OversamplingMode = DISABLE; // No oversampling
95-
96-
if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
97-
error("Cannot initialize ADC\n");
98-
}
99-
100-
// Calibrate ADC
69+
// Configure ADC object structures
70+
obj->handle.State = HAL_ADC_STATE_RESET;
71+
obj->handle.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV2; // Asynchronous clock mode, input ADC clock
72+
obj->handle.Init.Resolution = ADC_RESOLUTION_12B;
73+
obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
74+
obj->handle.Init.ScanConvMode = DISABLE; // Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1)
75+
obj->handle.Init.EOCSelection = ADC_EOC_SINGLE_CONV; // On STM32L1xx ADC, overrun detection is enabled only if EOC selection is set to each conversion (or transfer by DMA enabled, this is not the case in this example).
76+
obj->handle.Init.LowPowerAutoWait = DISABLE;
77+
obj->handle.Init.ContinuousConvMode = DISABLE; // Continuous mode disabled to have only 1 conversion at each conversion trig
78+
obj->handle.Init.NbrOfConversion = 1; // Parameter discarded because sequencer is disabled
79+
obj->handle.Init.DiscontinuousConvMode = DISABLE; // Parameter discarded because sequencer is disabled
80+
obj->handle.Init.NbrOfDiscConversion = 1; // Parameter discarded because sequencer is disabled
81+
obj->handle.Init.ExternalTrigConv = ADC_SOFTWARE_START; // Software start to trig the 1st conversion manually, without external event
82+
obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
83+
obj->handle.Init.DMAContinuousRequests = DISABLE;
84+
obj->handle.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; // DR register is overwritten with the last conversion result in case of overrun
85+
obj->handle.Init.OversamplingMode = DISABLE; // No oversampling
86+
87+
// Enable ADC clock
88+
__HAL_RCC_ADC_CLK_ENABLE();
89+
__HAL_RCC_ADC_CONFIG(RCC_ADCCLKSOURCE_SYSCLK);
90+
91+
if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
92+
error("Cannot initialize ADC");
93+
}
94+
95+
// ADC calibration is done only once
96+
if (adc_calibrated == 0) {
97+
adc_calibrated = 1;
10198
HAL_ADCEx_Calibration_Start(&obj->handle, ADC_SINGLE_ENDED);
10299
}
103100
}
@@ -181,7 +178,7 @@ static inline uint16_t adc_read(analogin_t *obj)
181178

182179
// Wait end of conversion and get value
183180
if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) {
184-
return (HAL_ADC_GetValue(&obj->handle));
181+
return (uint16_t)HAL_ADC_GetValue(&obj->handle);
185182
} else {
186183
return 0;
187184
}

0 commit comments

Comments
 (0)