From 2e2744ccbe284fa95aa03e2a4eec30e238d3dc8c Mon Sep 17 00:00:00 2001 From: bcostm Date: Tue, 14 Nov 2017 13:07:35 +0100 Subject: [PATCH] STM32F0 ADC: remove adc_inited flag --- .../TARGET_STM/TARGET_STM32F0/analogin_api.c | 76 +++++++++---------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/targets/TARGET_STM/TARGET_STM32F0/analogin_api.c b/targets/TARGET_STM/TARGET_STM32F0/analogin_api.c index bfa8adc443..3ebefb4830 100644 --- a/targets/TARGET_STM/TARGET_STM32F0/analogin_api.c +++ b/targets/TARGET_STM/TARGET_STM32F0/analogin_api.c @@ -33,12 +33,12 @@ #include "mbed_wait_api.h" #include "cmsis.h" #include "pinmap.h" -#include "PeripheralPins.h" #include "mbed_error.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; // ADC Internal Channels "pins" (Temperature, Vref, Vbat, ...) @@ -47,14 +47,14 @@ void analogin_init(analogin_t *obj, PinName pin) { if ((pin < 0xF0) || (pin >= 0x100)) { // Normal channels // 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 function = pinmap_function(pin, PinMap_ADC); // Configure GPIO pinmap_pinout(pin, PinMap_ADC); } else { // 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); // No GPIO configuration for internal channels } @@ -66,40 +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.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4; + obj->handle.Init.Resolution = ADC_RESOLUTION_12B; + obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT; + obj->handle.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD; + obj->handle.Init.EOCSelection = EOC_SINGLE_CONV; + obj->handle.Init.LowPowerAutoWait = DISABLE; + obj->handle.Init.LowPowerAutoPowerOff = DISABLE; + obj->handle.Init.ContinuousConvMode = DISABLE; + obj->handle.Init.DiscontinuousConvMode = DISABLE; + obj->handle.Init.ExternalTrigConv = ADC_SOFTWARE_START; + obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; + obj->handle.Init.DMAContinuousRequests = DISABLE; + obj->handle.Init.Overrun = OVR_DATA_OVERWRITTEN; - // Enable ADC clock - __ADC1_CLK_ENABLE(); + __HAL_RCC_ADC1_CLK_ENABLE(); - // Configure ADC - obj->handle.State = HAL_ADC_STATE_RESET; - obj->handle.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4; - obj->handle.Init.Resolution = ADC_RESOLUTION12b; - obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT; - obj->handle.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD; - obj->handle.Init.EOCSelection = EOC_SINGLE_CONV; - obj->handle.Init.LowPowerAutoWait = DISABLE; - obj->handle.Init.LowPowerAutoPowerOff = DISABLE; - obj->handle.Init.ContinuousConvMode = DISABLE; - obj->handle.Init.DiscontinuousConvMode = DISABLE; - obj->handle.Init.ExternalTrigConv = ADC_SOFTWARE_START; - obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; - obj->handle.Init.DMAContinuousRequests = DISABLE; - obj->handle.Init.Overrun = OVR_DATA_OVERWRITTEN; - if (HAL_ADC_Init(&obj->handle) != HAL_OK) { - error("Cannot initialize ADC"); - } - // Run the ADC calibration - if (HAL_ADCEx_Calibration_Start(&obj->handle) != HAL_OK) { - error("Cannot Start ADC_Calibration"); - } + 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); } } -static inline uint16_t adc_read(analogin_t *obj) { - ADC_ChannelConfTypeDef sConfig; +static inline uint16_t adc_read(analogin_t *obj) +{ + ADC_ChannelConfTypeDef sConfig = {0}; // Configure ADC channel sConfig.Rank = ADC_RANK_CHANNEL_NUMBER; @@ -182,20 +180,22 @@ 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; } } -uint16_t analogin_read_u16(analogin_t *obj) { +uint16_t analogin_read_u16(analogin_t *obj) +{ uint16_t value = adc_read(obj); // 12-bit to 16-bit conversion value = ((value << 4) & (uint16_t)0xFFF0) | ((value >> 8) & (uint16_t)0x000F); return value; } -float analogin_read(analogin_t *obj) { +float analogin_read(analogin_t *obj) +{ uint16_t value = adc_read(obj); return (float)value * (1.0f / (float)0xFFF); // 12 bits range }