mirror of https://github.com/ARMmbed/mbed-os.git
STM32L0 ADC: remove adc_inited flag
parent
75f80abddc
commit
4b82479175
|
|
@ -36,12 +36,10 @@
|
|||
#include "mbed_error.h"
|
||||
#include "PeripheralPins.h"
|
||||
|
||||
int adc_inited = 0;
|
||||
|
||||
void analogin_init(analogin_t *obj, PinName pin)
|
||||
{
|
||||
static int adc_calibrated = 0;
|
||||
uint32_t function = (uint32_t)NC;
|
||||
obj->handle.Instance = (ADC_TypeDef *)NC;
|
||||
|
||||
// ADC Internal Channels "pins" (Temperature, Vref, Vbat, ...)
|
||||
// are described in PinNames.h and PeripheralPins.c
|
||||
|
|
@ -68,41 +66,38 @@ void analogin_init(analogin_t *obj, PinName pin)
|
|||
// Save pin number for the read function
|
||||
obj->pin = pin;
|
||||
|
||||
// The ADC initialization is done once
|
||||
if (adc_inited == 0) {
|
||||
adc_inited = 1;
|
||||
// Configure ADC object structures
|
||||
obj->handle.State = HAL_ADC_STATE_RESET;
|
||||
obj->handle.Init.OversamplingMode = DISABLE;
|
||||
obj->handle.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV1;
|
||||
obj->handle.Init.Resolution = ADC_RESOLUTION_12B;
|
||||
obj->handle.Init.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
|
||||
obj->handle.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
|
||||
obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
||||
obj->handle.Init.ContinuousConvMode = DISABLE;
|
||||
obj->handle.Init.DiscontinuousConvMode = DISABLE;
|
||||
obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIG_EDGE_NONE;
|
||||
obj->handle.Init.ExternalTrigConv = ADC_EXTERNALTRIG0_T6_TRGO; // Not used here
|
||||
obj->handle.Init.DMAContinuousRequests = DISABLE;
|
||||
obj->handle.Init.EOCSelection = EOC_SINGLE_CONV;
|
||||
obj->handle.Init.Overrun = OVR_DATA_OVERWRITTEN;
|
||||
obj->handle.Init.LowPowerAutoWait = ENABLE;
|
||||
obj->handle.Init.LowPowerFrequencyMode = DISABLE; // To be enabled only if ADC clock < 2.8 MHz
|
||||
obj->handle.Init.LowPowerAutoPowerOff = DISABLE;
|
||||
|
||||
obj->handle.State = HAL_ADC_STATE_RESET;
|
||||
// Enable ADC clock
|
||||
__ADC1_CLK_ENABLE();
|
||||
__HAL_RCC_ADC1_CLK_ENABLE();
|
||||
|
||||
// Configure ADC
|
||||
obj->handle.Init.OversamplingMode = DISABLE;
|
||||
obj->handle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV1;
|
||||
obj->handle.Init.Resolution = ADC_RESOLUTION12b;
|
||||
obj->handle.Init.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
|
||||
obj->handle.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
|
||||
obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
||||
obj->handle.Init.ContinuousConvMode = DISABLE;
|
||||
obj->handle.Init.DiscontinuousConvMode = DISABLE;
|
||||
obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIG_EDGE_NONE;
|
||||
obj->handle.Init.ExternalTrigConv = ADC_EXTERNALTRIG0_T6_TRGO; // Not used here
|
||||
obj->handle.Init.DMAContinuousRequests = DISABLE;
|
||||
obj->handle.Init.EOCSelection = EOC_SINGLE_CONV;
|
||||
obj->handle.Init.Overrun = OVR_DATA_OVERWRITTEN;
|
||||
obj->handle.Init.LowPowerAutoWait = ENABLE;
|
||||
obj->handle.Init.LowPowerFrequencyMode = DISABLE; // To be enabled only if ADC clock < 2.8 MHz
|
||||
obj->handle.Init.LowPowerAutoPowerOff = DISABLE;
|
||||
|
||||
if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
|
||||
error("Cannot initialize ADC");
|
||||
}
|
||||
|
||||
// Calibration
|
||||
HAL_ADCEx_Calibration_Start(&obj->handle, ADC_SINGLE_ENDED);
|
||||
|
||||
__HAL_ADC_ENABLE(&obj->handle);
|
||||
if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
|
||||
error("Cannot initialize ADC");
|
||||
}
|
||||
|
||||
// ADC calibration is done only once
|
||||
if (adc_calibrated == 0) {
|
||||
adc_calibrated = 1;
|
||||
HAL_ADCEx_Calibration_Start(&obj->handle, ADC_SINGLE_ENDED);
|
||||
}
|
||||
|
||||
__HAL_ADC_ENABLE(&obj->handle);
|
||||
}
|
||||
|
||||
static inline uint16_t adc_read(analogin_t *obj)
|
||||
|
|
@ -182,7 +177,7 @@ static inline uint16_t adc_read(analogin_t *obj)
|
|||
|
||||
// Wait end of conversion and get value
|
||||
if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) {
|
||||
return (HAL_ADC_GetValue(&obj->handle));
|
||||
return (uint16_t)HAL_ADC_GetValue(&obj->handle);
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue