Skip to content

Commit 52d942e

Browse files
committed
STM32L1 ADC: remove adc_inited flag
1 parent 4b82479 commit 52d942e

File tree

1 file changed

+30
-33
lines changed

1 file changed

+30
-33
lines changed

targets/TARGET_STM/TARGET_STM32L1/analogin_api.c

Lines changed: 30 additions & 33 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_hsi_inited = 0;
4342
RCC_OscInitTypeDef RCC_OscInitStruct;
4443
uint32_t function = (uint32_t)NC;
4544

@@ -49,14 +48,14 @@ void analogin_init(analogin_t *obj, PinName pin)
4948
if ((pin < 0xF0) || (pin >= 0x100)) {
5049
// Normal channels
5150
// Get the peripheral name from the pin and assign it to the object
52-
obj->handle.Instance = (ADC_TypeDef *) pinmap_peripheral(pin, PinMap_ADC);
51+
obj->handle.Instance = (ADC_TypeDef *)pinmap_peripheral(pin, PinMap_ADC);
5352
// Get the functions (adc channel) from the pin and assign it to the object
5453
function = pinmap_function(pin, PinMap_ADC);
5554
// Configure GPIO
5655
pinmap_pinout(pin, PinMap_ADC);
5756
} else {
5857
// Internal channels
59-
obj->handle.Instance = (ADC_TypeDef *) pinmap_peripheral(pin, PinMap_ADC_Internal);
58+
obj->handle.Instance = (ADC_TypeDef *)pinmap_peripheral(pin, PinMap_ADC_Internal);
6059
function = pinmap_function(pin, PinMap_ADC_Internal);
6160
// No GPIO configuration for internal channels
6261
}
@@ -68,41 +67,39 @@ void analogin_init(analogin_t *obj, PinName pin)
6867
// Save pin number for the read function
6968
obj->pin = pin;
7069

71-
// The ADC initialization is done once
72-
if (adc_inited == 0) {
73-
adc_inited = 1;
70+
// Configure ADC object structures
71+
obj->handle.State = HAL_ADC_STATE_RESET;
72+
obj->handle.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV4;
73+
obj->handle.Init.Resolution = ADC_RESOLUTION_12B;
74+
obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
75+
obj->handle.Init.ScanConvMode = DISABLE; // Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1)
76+
obj->handle.Init.EOCSelection = 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).
77+
obj->handle.Init.LowPowerAutoWait = ADC_AUTOWAIT_UNTIL_DATA_READ; // Enable the dynamic low power Auto Delay: new conversion start only when the previous conversion (for regular group) or previous sequence (for injected group) has been treated by user software.
78+
obj->handle.Init.LowPowerAutoPowerOff = ADC_AUTOPOWEROFF_IDLE_PHASE; // Enable the auto-off mode: the ADC automatically powers-off after a conversion and automatically wakes-up when a new conversion is triggered (with startup time between trigger and start of sampling).
79+
obj->handle.Init.ChannelsBank = ADC_CHANNELS_BANK_A;
80+
obj->handle.Init.ContinuousConvMode = DISABLE; // Continuous mode disabled to have only 1 conversion at each conversion trig
81+
obj->handle.Init.NbrOfConversion = 1; // Parameter discarded because sequencer is disabled
82+
obj->handle.Init.DiscontinuousConvMode = DISABLE; // Parameter discarded because sequencer is disabled
83+
obj->handle.Init.NbrOfDiscConversion = 1; // Parameter discarded because sequencer is disabled
84+
obj->handle.Init.ExternalTrigConv = 0; // Not used
85+
obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
86+
obj->handle.Init.DMAContinuousRequests = DISABLE;
87+
88+
__HAL_RCC_ADC1_CLK_ENABLE();
89+
90+
if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
91+
error("Cannot initialize ADC");
92+
}
7493

94+
// This section is done only once
95+
if (adc_hsi_inited == 0) {
96+
adc_hsi_inited = 1;
7597
// Enable the HSI (to clock the ADC)
7698
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
7799
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
78100
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
79101
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
80102
HAL_RCC_OscConfig(&RCC_OscInitStruct);
81-
82-
obj->handle.State = HAL_ADC_STATE_RESET;
83-
// Enable ADC clock
84-
__ADC1_CLK_ENABLE();
85-
86-
// Configure ADC
87-
obj->handle.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV4;
88-
obj->handle.Init.Resolution = ADC_RESOLUTION12b;
89-
obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
90-
obj->handle.Init.ScanConvMode = DISABLE; // Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1)
91-
obj->handle.Init.EOCSelection = 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).
92-
obj->handle.Init.LowPowerAutoWait = ADC_AUTOWAIT_UNTIL_DATA_READ; // Enable the dynamic low power Auto Delay: new conversion start only when the previous conversion (for regular group) or previous sequence (for injected group) has been treated by user software.
93-
obj->handle.Init.LowPowerAutoPowerOff = ADC_AUTOPOWEROFF_IDLE_PHASE; // Enable the auto-off mode: the ADC automatically powers-off after a conversion and automatically wakes-up when a new conversion is triggered (with startup time between trigger and start of sampling).
94-
obj->handle.Init.ChannelsBank = ADC_CHANNELS_BANK_A;
95-
obj->handle.Init.ContinuousConvMode = DISABLE; // Continuous mode disabled to have only 1 conversion at each conversion trig
96-
obj->handle.Init.NbrOfConversion = 1; // Parameter discarded because sequencer is disabled
97-
obj->handle.Init.DiscontinuousConvMode = DISABLE; // Parameter discarded because sequencer is disabled
98-
obj->handle.Init.NbrOfDiscConversion = 1; // Parameter discarded because sequencer is disabled
99-
obj->handle.Init.ExternalTrigConv = 0; // Not used
100-
obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
101-
obj->handle.Init.DMAContinuousRequests = DISABLE;
102-
103-
if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
104-
error("Cannot initialize ADC");
105-
}
106103
}
107104
}
108105

@@ -231,7 +228,7 @@ static inline uint16_t adc_read(analogin_t *obj)
231228

232229
// Wait end of conversion and get value
233230
if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) {
234-
return (HAL_ADC_GetValue(&obj->handle));
231+
return (uint16_t)HAL_ADC_GetValue(&obj->handle);
235232
} else {
236233
return 0;
237234
}

0 commit comments

Comments
 (0)