mirror of https://github.com/ARMmbed/mbed-os.git
STM32F3 ADC: remove adc_inited flag
parent
f12391ad25
commit
7005bc21d7
|
@ -36,22 +36,9 @@
|
||||||
#include "mbed_error.h"
|
#include "mbed_error.h"
|
||||||
#include "PeripheralPins.h"
|
#include "PeripheralPins.h"
|
||||||
|
|
||||||
|
|
||||||
void analogin_init(analogin_t *obj, PinName pin)
|
void analogin_init(analogin_t *obj, PinName pin)
|
||||||
{
|
{
|
||||||
#if defined(ADC1)
|
static int adc_calibrated = 0;
|
||||||
static int adc1_inited = 0;
|
|
||||||
#endif
|
|
||||||
#if defined(ADC2)
|
|
||||||
static int adc2_inited = 0;
|
|
||||||
#endif
|
|
||||||
#if defined(ADC3)
|
|
||||||
static int adc3_inited = 0;
|
|
||||||
#endif
|
|
||||||
#if defined(ADC4)
|
|
||||||
static int adc4_inited = 0;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
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, ...)
|
||||||
|
@ -60,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
|
||||||
}
|
}
|
||||||
|
@ -79,41 +66,10 @@ 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;
|
||||||
|
|
||||||
// Check if ADC is already initialized
|
// Configure ADC object structures
|
||||||
// Enable ADC clock
|
|
||||||
#if defined(ADC1)
|
|
||||||
if (((ADCName)obj->handle.Instance == ADC_1) && adc1_inited) return;
|
|
||||||
if ((ADCName)obj->handle.Instance == ADC_1) {
|
|
||||||
__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) {
|
|
||||||
__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) {
|
|
||||||
__ADC34_CLK_ENABLE();
|
|
||||||
adc3_inited = 1;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
#if defined(ADC4)
|
|
||||||
if (((ADCName)obj->handle.Instance == ADC_4) && adc4_inited) return;
|
|
||||||
if ((ADCName)obj->handle.Instance == ADC_4) {
|
|
||||||
__ADC34_CLK_ENABLE();
|
|
||||||
adc4_inited = 1;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Configure ADC
|
|
||||||
obj->handle.State = HAL_ADC_STATE_RESET;
|
obj->handle.State = HAL_ADC_STATE_RESET;
|
||||||
obj->handle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2;
|
obj->handle.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
|
||||||
obj->handle.Init.Resolution = ADC_RESOLUTION12b;
|
obj->handle.Init.Resolution = ADC_RESOLUTION_12B;
|
||||||
obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
||||||
obj->handle.Init.ScanConvMode = DISABLE;
|
obj->handle.Init.ScanConvMode = DISABLE;
|
||||||
obj->handle.Init.EOCSelection = EOC_SINGLE_CONV;
|
obj->handle.Init.EOCSelection = EOC_SINGLE_CONV;
|
||||||
|
@ -127,12 +83,36 @@ void analogin_init(analogin_t *obj, PinName pin)
|
||||||
obj->handle.Init.DMAContinuousRequests = DISABLE;
|
obj->handle.Init.DMAContinuousRequests = DISABLE;
|
||||||
obj->handle.Init.Overrun = OVR_DATA_OVERWRITTEN;
|
obj->handle.Init.Overrun = OVR_DATA_OVERWRITTEN;
|
||||||
|
|
||||||
|
#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_ADC34_CLK_ENABLE();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#if defined(ADC4)
|
||||||
|
if ((ADCName)obj->handle.Instance == ADC_4) {
|
||||||
|
__HAL_RCC_ADC34_CLK_ENABLE();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
|
if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
|
||||||
error("Cannot initialize ADC");
|
error("Cannot initialize ADC");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calibrate ADC
|
// ADC calibration is done only once
|
||||||
|
if (adc_calibrated == 0) {
|
||||||
|
adc_calibrated = 1;
|
||||||
HAL_ADCEx_Calibration_Start(&obj->handle, ADC_SINGLE_ENDED);
|
HAL_ADCEx_Calibration_Start(&obj->handle, ADC_SINGLE_ENDED);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline uint16_t adc_read(analogin_t *obj)
|
static inline uint16_t adc_read(analogin_t *obj)
|
||||||
|
@ -211,7 +191,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;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue