Skip to content

Commit 36110e1

Browse files
committed
STM32F1 ADC: remove adc_inited flag
1 parent 2e2744c commit 36110e1

File tree

1 file changed

+25
-28
lines changed

1 file changed

+25
-28
lines changed

targets/TARGET_STM/TARGET_STM32F1/analogin_api.c

Lines changed: 25 additions & 28 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
RCC_PeriphCLKInitTypeDef PeriphClkInit;
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,13 +67,26 @@ 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;
74-
75-
// Enable ADC clock
76-
__HAL_RCC_ADC1_CLK_ENABLE();
70+
// Enable ADC clock
71+
__HAL_RCC_ADC1_CLK_ENABLE();
72+
73+
// Configure ADC object structures
74+
obj->handle.State = HAL_ADC_STATE_RESET;
75+
obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
76+
obj->handle.Init.ScanConvMode = DISABLE;
77+
obj->handle.Init.ContinuousConvMode = DISABLE;
78+
obj->handle.Init.NbrOfConversion = 1;
79+
obj->handle.Init.DiscontinuousConvMode = DISABLE;
80+
obj->handle.Init.NbrOfDiscConversion = 0;
81+
obj->handle.Init.ExternalTrigConv = ADC_SOFTWARE_START;
82+
83+
if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
84+
error("Cannot initialize ADC");
85+
}
7786

87+
// This section is done only once
88+
if (adc_calibrated == 0) {
89+
adc_calibrated = 1;
7890
// Configure ADC clock prescaler
7991
// Caution: On STM32F1, ADC clock frequency max is 14 MHz (refer to device datasheet).
8092
// Therefore, ADC clock prescaler must be configured in function
@@ -84,29 +96,14 @@ void analogin_init(analogin_t *obj, PinName pin)
8496
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
8597
PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
8698
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
87-
88-
// Configure ADC
89-
obj->handle.State = HAL_ADC_STATE_RESET;
90-
obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
91-
obj->handle.Init.ScanConvMode = DISABLE;
92-
obj->handle.Init.ContinuousConvMode = DISABLE;
93-
obj->handle.Init.NbrOfConversion = 1;
94-
obj->handle.Init.DiscontinuousConvMode = DISABLE;
95-
obj->handle.Init.NbrOfDiscConversion = 0;
96-
obj->handle.Init.ExternalTrigConv = ADC_SOFTWARE_START;
97-
98-
if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
99-
error("Cannot initialize ADC\n");
100-
}
101-
102-
// Calibrate ADC
99+
// Calibration
103100
HAL_ADCEx_Calibration_Start(&obj->handle);
104101
}
105102
}
106103

107104
static inline uint16_t adc_read(analogin_t *obj)
108105
{
109-
ADC_ChannelConfTypeDef sConfig;
106+
ADC_ChannelConfTypeDef sConfig = {0};
110107

111108
// Configure ADC channel
112109
sConfig.Rank = 1;
@@ -177,7 +174,7 @@ static inline uint16_t adc_read(analogin_t *obj)
177174

178175
// Wait end of conversion and get value
179176
if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) {
180-
return (HAL_ADC_GetValue(&obj->handle));
177+
return (uint16_t)HAL_ADC_GetValue(&obj->handle);
181178
} else {
182179
return 0;
183180
}

0 commit comments

Comments
 (0)