STM32F7 ADC: remove adc_inited flag

pull/5513/head
bcostm 2017-11-14 14:31:49 +01:00
parent d5ff05a03b
commit 75f80abddc
1 changed files with 25 additions and 44 deletions

View File

@ -33,35 +33,27 @@
#include "mbed_wait_api.h"
#include "cmsis.h"
#include "pinmap.h"
#include "PeripheralPins.h"
#include "mbed_error.h"
#include "PeripheralPins.h"
void analogin_init(analogin_t *obj, PinName pin)
{
uint32_t function = (uint32_t)NC;
#if defined(ADC1)
static int adc1_inited = 0;
#endif
#if defined(ADC2)
static int adc2_inited = 0;
#endif
#if defined(ADC3)
static int adc3_inited = 0;
#endif
// ADC Internal Channels "pins" (Temperature, Vref, Vbat, ...)
// are described in PinNames.h and PeripheralPins.c
// Pin value must be between 0xF0 and 0xFF
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
}
@ -73,33 +65,9 @@ void analogin_init(analogin_t *obj, PinName pin)
// Save pin number for the read function
obj->pin = pin;
// Check if ADC is already initialized
// Enable ADC clock
#if defined(ADC1)
if (((ADCName)obj->handle.Instance == ADC_1) && adc1_inited) return;
if ((ADCName)obj->handle.Instance == ADC_1) {
__HAL_RCC_ADC1_CLK_ENABLE();
adc1_inited = 1;
}
#endif
#if defined(ADC2)
if (((ADCName)obj->handle.Instance == ADC_2) && adc2_inited) return;
if ((ADCName)obj->handle.Instance == ADC_2) {
__HAL_RCC_ADC2_CLK_ENABLE();
adc2_inited = 1;
}
#endif
#if defined(ADC3)
if (((ADCName)obj->handle.Instance == ADC_3) && adc3_inited) return;
if ((ADCName)obj->handle.Instance == ADC_3) {
__HAL_RCC_ADC3_CLK_ENABLE();
adc3_inited = 1;
}
#endif
// Configure ADC
// Configure ADC object structures
obj->handle.State = HAL_ADC_STATE_RESET;
obj->handle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV4;
obj->handle.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
obj->handle.Init.Resolution = ADC_RESOLUTION_12B;
obj->handle.Init.ScanConvMode = DISABLE;
obj->handle.Init.ContinuousConvMode = DISABLE;
@ -112,6 +80,22 @@ void analogin_init(analogin_t *obj, PinName pin)
obj->handle.Init.DMAContinuousRequests = DISABLE;
obj->handle.Init.EOCSelection = DISABLE;
#if defined(ADC1)
if ((ADCName)obj->handle.Instance == ADC_1) {
__HAL_RCC_ADC1_CLK_ENABLE();
}
#endif
#if defined(ADC2)
if ((ADCName)obj->handle.Instance == ADC_2) {
__HAL_RCC_ADC2_CLK_ENABLE();
}
#endif
#if defined(ADC3)
if ((ADCName)obj->handle.Instance == ADC_3) {
__HAL_RCC_ADC3_CLK_ENABLE();
}
#endif
if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
error("Cannot initialize ADC");
}
@ -188,16 +172,13 @@ static inline uint16_t adc_read(analogin_t *obj)
return 0;
}
if (HAL_ADC_ConfigChannel(&obj->handle, &sConfig) != HAL_OK) {
error("Cannot configure ADC channel");
}
HAL_ADC_ConfigChannel(&obj->handle, &sConfig);
HAL_ADC_Start(&obj->handle); // Start conversion
// Wait end of conversion and get value
HAL_ADC_PollForConversion(&obj->handle, 10);
if (HAL_ADC_GetState(&obj->handle) & HAL_ADC_STATE_EOC_REG) {
return (HAL_ADC_GetValue(&obj->handle));
if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) {
return (uint16_t)HAL_ADC_GetValue(&obj->handle);
} else {
return 0;
}