Skip to content

Commit 2e2744c

Browse files
committed
STM32F0 ADC: remove adc_inited flag
1 parent b025ea1 commit 2e2744c

File tree

1 file changed

+40
-40
lines changed

1 file changed

+40
-40
lines changed

targets/TARGET_STM/TARGET_STM32F0/analogin_api.c

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@
3333
#include "mbed_wait_api.h"
3434
#include "cmsis.h"
3535
#include "pinmap.h"
36-
#include "PeripheralPins.h"
3736
#include "mbed_error.h"
37+
#include "PeripheralPins.h"
3838

39-
int adc_inited = 0;
40-
41-
void analogin_init(analogin_t *obj, PinName pin) {
39+
void analogin_init(analogin_t *obj, PinName pin)
40+
{
41+
static int adc_calibrated = 0;
4242
uint32_t function = (uint32_t)NC;
4343

4444
// ADC Internal Channels "pins" (Temperature, Vref, Vbat, ...)
@@ -47,14 +47,14 @@ void analogin_init(analogin_t *obj, PinName pin) {
4747
if ((pin < 0xF0) || (pin >= 0x100)) {
4848
// Normal channels
4949
// Get the peripheral name from the pin and assign it to the object
50-
obj->handle.Instance = (ADC_TypeDef *) pinmap_peripheral(pin, PinMap_ADC);
50+
obj->handle.Instance = (ADC_TypeDef *)pinmap_peripheral(pin, PinMap_ADC);
5151
// Get the functions (adc channel) from the pin and assign it to the object
5252
function = pinmap_function(pin, PinMap_ADC);
5353
// Configure GPIO
5454
pinmap_pinout(pin, PinMap_ADC);
5555
} else {
5656
// Internal channels
57-
obj->handle.Instance = (ADC_TypeDef *) pinmap_peripheral(pin, PinMap_ADC_Internal);
57+
obj->handle.Instance = (ADC_TypeDef *)pinmap_peripheral(pin, PinMap_ADC_Internal);
5858
function = pinmap_function(pin, PinMap_ADC_Internal);
5959
// No GPIO configuration for internal channels
6060
}
@@ -66,40 +66,38 @@ void analogin_init(analogin_t *obj, PinName pin) {
6666
// Save pin number for the read function
6767
obj->pin = pin;
6868

69-
// The ADC initialization is done once
70-
if (adc_inited == 0) {
71-
adc_inited = 1;
72-
73-
// Enable ADC clock
74-
__ADC1_CLK_ENABLE();
75-
76-
// Configure ADC
77-
obj->handle.State = HAL_ADC_STATE_RESET;
78-
obj->handle.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
79-
obj->handle.Init.Resolution = ADC_RESOLUTION12b;
80-
obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
81-
obj->handle.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
82-
obj->handle.Init.EOCSelection = EOC_SINGLE_CONV;
83-
obj->handle.Init.LowPowerAutoWait = DISABLE;
84-
obj->handle.Init.LowPowerAutoPowerOff = DISABLE;
85-
obj->handle.Init.ContinuousConvMode = DISABLE;
86-
obj->handle.Init.DiscontinuousConvMode = DISABLE;
87-
obj->handle.Init.ExternalTrigConv = ADC_SOFTWARE_START;
88-
obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
89-
obj->handle.Init.DMAContinuousRequests = DISABLE;
90-
obj->handle.Init.Overrun = OVR_DATA_OVERWRITTEN;
91-
if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
92-
error("Cannot initialize ADC");
93-
}
94-
// Run the ADC calibration
95-
if (HAL_ADCEx_Calibration_Start(&obj->handle) != HAL_OK) {
96-
error("Cannot Start ADC_Calibration");
97-
}
69+
// Configure ADC object structures
70+
obj->handle.State = HAL_ADC_STATE_RESET;
71+
obj->handle.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
72+
obj->handle.Init.Resolution = ADC_RESOLUTION_12B;
73+
obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
74+
obj->handle.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
75+
obj->handle.Init.EOCSelection = EOC_SINGLE_CONV;
76+
obj->handle.Init.LowPowerAutoWait = DISABLE;
77+
obj->handle.Init.LowPowerAutoPowerOff = DISABLE;
78+
obj->handle.Init.ContinuousConvMode = DISABLE;
79+
obj->handle.Init.DiscontinuousConvMode = DISABLE;
80+
obj->handle.Init.ExternalTrigConv = ADC_SOFTWARE_START;
81+
obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
82+
obj->handle.Init.DMAContinuousRequests = DISABLE;
83+
obj->handle.Init.Overrun = OVR_DATA_OVERWRITTEN;
84+
85+
__HAL_RCC_ADC1_CLK_ENABLE();
86+
87+
if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
88+
error("Cannot initialize ADC");
89+
}
90+
91+
// ADC calibration is done only once
92+
if (adc_calibrated == 0) {
93+
adc_calibrated = 1;
94+
HAL_ADCEx_Calibration_Start(&obj->handle);
9895
}
9996
}
10097

101-
static inline uint16_t adc_read(analogin_t *obj) {
102-
ADC_ChannelConfTypeDef sConfig;
98+
static inline uint16_t adc_read(analogin_t *obj)
99+
{
100+
ADC_ChannelConfTypeDef sConfig = {0};
103101

104102
// Configure ADC channel
105103
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
@@ -182,20 +180,22 @@ static inline uint16_t adc_read(analogin_t *obj) {
182180

183181
// Wait end of conversion and get value
184182
if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) {
185-
return (HAL_ADC_GetValue(&obj->handle));
183+
return (uint16_t)HAL_ADC_GetValue(&obj->handle);
186184
} else {
187185
return 0;
188186
}
189187
}
190188

191-
uint16_t analogin_read_u16(analogin_t *obj) {
189+
uint16_t analogin_read_u16(analogin_t *obj)
190+
{
192191
uint16_t value = adc_read(obj);
193192
// 12-bit to 16-bit conversion
194193
value = ((value << 4) & (uint16_t)0xFFF0) | ((value >> 8) & (uint16_t)0x000F);
195194
return value;
196195
}
197196

198-
float analogin_read(analogin_t *obj) {
197+
float analogin_read(analogin_t *obj)
198+
{
199199
uint16_t value = adc_read(obj);
200200
return (float)value * (1.0f / (float)0xFFF); // 12 bits range
201201
}

0 commit comments

Comments
 (0)