STM32L4 ADC: remove adc_inited flag

pull/5513/head
bcostm 2017-11-14 15:27:41 +01:00
parent 52d942e2d2
commit 072ec2e765
1 changed files with 30 additions and 33 deletions

View File

@ -36,10 +36,9 @@
#include "mbed_error.h" #include "mbed_error.h"
#include "PeripheralPins.h" #include "PeripheralPins.h"
int adc_inited = 0;
void analogin_init(analogin_t *obj, PinName pin) void analogin_init(analogin_t *obj, PinName pin)
{ {
static int adc_calibrated = 0;
uint32_t function = (uint32_t)NC; uint32_t function = (uint32_t)NC;
// ADC Internal Channels "pins" (Temperature, Vref, Vbat, ...) // ADC Internal Channels "pins" (Temperature, Vref, Vbat, ...)
@ -48,14 +47,14 @@ void analogin_init(analogin_t *obj, PinName pin)
if ((pin < 0xF0) || (pin >= 0x100)) { if ((pin < 0xF0) || (pin >= 0x100)) {
// Normal channels // Normal channels
// Get the peripheral name from the pin and assign it to the object // Get the peripheral name from the pin and assign it to the object
obj->handle.Instance = (ADC_TypeDef *) pinmap_peripheral(pin, PinMap_ADC); obj->handle.Instance = (ADC_TypeDef *)pinmap_peripheral(pin, PinMap_ADC);
// Get the functions (adc channel) from the pin and assign it to the object // Get the functions (adc channel) from the pin and assign it to the object
function = pinmap_function(pin, PinMap_ADC); function = pinmap_function(pin, PinMap_ADC);
// Configure GPIO // Configure GPIO
pinmap_pinout(pin, PinMap_ADC); pinmap_pinout(pin, PinMap_ADC);
} else { } else {
// Internal channels // Internal channels
obj->handle.Instance = (ADC_TypeDef *) pinmap_peripheral(pin, PinMap_ADC_Internal); obj->handle.Instance = (ADC_TypeDef *)pinmap_peripheral(pin, PinMap_ADC_Internal);
function = pinmap_function(pin, PinMap_ADC_Internal); function = pinmap_function(pin, PinMap_ADC_Internal);
// No GPIO configuration for internal channels // No GPIO configuration for internal channels
} }
@ -67,37 +66,35 @@ void analogin_init(analogin_t *obj, PinName pin)
// Save pin number for the read function // Save pin number for the read function
obj->pin = pin; obj->pin = pin;
// The ADC initialization is done once // Configure ADC object structures
if (adc_inited == 0) { obj->handle.State = HAL_ADC_STATE_RESET;
adc_inited = 1; obj->handle.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV2; // Asynchronous clock mode, input ADC clock
obj->handle.Init.Resolution = ADC_RESOLUTION_12B;
obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
obj->handle.Init.ScanConvMode = DISABLE; // Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1)
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).
obj->handle.Init.LowPowerAutoWait = DISABLE;
obj->handle.Init.ContinuousConvMode = DISABLE; // Continuous mode disabled to have only 1 conversion at each conversion trig
obj->handle.Init.NbrOfConversion = 1; // Parameter discarded because sequencer is disabled
obj->handle.Init.DiscontinuousConvMode = DISABLE; // Parameter discarded because sequencer is disabled
obj->handle.Init.NbrOfDiscConversion = 1; // Parameter discarded because sequencer is disabled
obj->handle.Init.ExternalTrigConv = ADC_SOFTWARE_START; // Software start to trig the 1st conversion manually, without external event
obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
obj->handle.Init.DMAContinuousRequests = DISABLE;
obj->handle.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; // DR register is overwritten with the last conversion result in case of overrun
obj->handle.Init.OversamplingMode = DISABLE; // No oversampling
// Enable ADC clock // Enable ADC clock
__HAL_RCC_ADC_CLK_ENABLE(); __HAL_RCC_ADC_CLK_ENABLE();
__HAL_RCC_ADC_CONFIG(RCC_ADCCLKSOURCE_SYSCLK); __HAL_RCC_ADC_CONFIG(RCC_ADCCLKSOURCE_SYSCLK);
obj->handle.State = HAL_ADC_STATE_RESET; if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
// Configure ADC error("Cannot initialize ADC");
obj->handle.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV2; // Asynchronous clock mode, input ADC clock }
obj->handle.Init.Resolution = ADC_RESOLUTION_12B;
obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
obj->handle.Init.ScanConvMode = DISABLE; // Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1)
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).
obj->handle.Init.LowPowerAutoWait = DISABLE;
obj->handle.Init.ContinuousConvMode = DISABLE; // Continuous mode disabled to have only 1 conversion at each conversion trig
obj->handle.Init.NbrOfConversion = 1; // Parameter discarded because sequencer is disabled
obj->handle.Init.DiscontinuousConvMode = DISABLE; // Parameter discarded because sequencer is disabled
obj->handle.Init.NbrOfDiscConversion = 1; // Parameter discarded because sequencer is disabled
obj->handle.Init.ExternalTrigConv = ADC_SOFTWARE_START; // Software start to trig the 1st conversion manually, without external event
obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
obj->handle.Init.DMAContinuousRequests = DISABLE;
obj->handle.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; // DR register is overwritten with the last conversion result in case of overrun
obj->handle.Init.OversamplingMode = DISABLE; // No oversampling
if (HAL_ADC_Init(&obj->handle) != HAL_OK) { // ADC calibration is done only once
error("Cannot initialize ADC\n"); if (adc_calibrated == 0) {
} adc_calibrated = 1;
// Calibrate ADC
HAL_ADCEx_Calibration_Start(&obj->handle, ADC_SINGLE_ENDED); HAL_ADCEx_Calibration_Start(&obj->handle, ADC_SINGLE_ENDED);
} }
} }
@ -181,7 +178,7 @@ static inline uint16_t adc_read(analogin_t *obj)
// Wait end of conversion and get value // Wait end of conversion and get value
if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) { if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) {
return (HAL_ADC_GetValue(&obj->handle)); return (uint16_t)HAL_ADC_GetValue(&obj->handle);
} else { } else {
return 0; return 0;
} }