mirror of https://github.com/ARMmbed/mbed-os.git
STM32F7 ADC: remove adc_inited flag
parent
d5ff05a03b
commit
75f80abddc
|
@ -33,35 +33,27 @@
|
||||||
#include "mbed_wait_api.h"
|
#include "mbed_wait_api.h"
|
||||||
#include "cmsis.h"
|
#include "cmsis.h"
|
||||||
#include "pinmap.h"
|
#include "pinmap.h"
|
||||||
#include "PeripheralPins.h"
|
|
||||||
#include "mbed_error.h"
|
#include "mbed_error.h"
|
||||||
|
#include "PeripheralPins.h"
|
||||||
|
|
||||||
void analogin_init(analogin_t *obj, PinName pin)
|
void analogin_init(analogin_t *obj, PinName pin)
|
||||||
{
|
{
|
||||||
uint32_t function = (uint32_t)NC;
|
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, ...)
|
// ADC Internal Channels "pins" (Temperature, Vref, Vbat, ...)
|
||||||
// are described in PinNames.h and PeripheralPins.c
|
// are described in PinNames.h and PeripheralPins.c
|
||||||
// Pin value must be between 0xF0 and 0xFF
|
// Pin value must be between 0xF0 and 0xFF
|
||||||
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
|
||||||
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
|
||||||
}
|
}
|
||||||
|
@ -73,33 +65,9 @@ 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) {
|
|
||||||
__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
|
|
||||||
obj->handle.State = HAL_ADC_STATE_RESET;
|
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.Resolution = ADC_RESOLUTION_12B;
|
||||||
obj->handle.Init.ScanConvMode = DISABLE;
|
obj->handle.Init.ScanConvMode = DISABLE;
|
||||||
obj->handle.Init.ContinuousConvMode = 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.DMAContinuousRequests = DISABLE;
|
||||||
obj->handle.Init.EOCSelection = 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) {
|
if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
|
||||||
error("Cannot initialize ADC");
|
error("Cannot initialize ADC");
|
||||||
}
|
}
|
||||||
|
@ -188,16 +172,13 @@ static inline uint16_t adc_read(analogin_t *obj)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (HAL_ADC_ConfigChannel(&obj->handle, &sConfig) != HAL_OK) {
|
HAL_ADC_ConfigChannel(&obj->handle, &sConfig);
|
||||||
error("Cannot configure ADC channel");
|
|
||||||
}
|
|
||||||
|
|
||||||
HAL_ADC_Start(&obj->handle); // Start conversion
|
HAL_ADC_Start(&obj->handle); // Start conversion
|
||||||
|
|
||||||
// Wait end of conversion and get value
|
// Wait end of conversion and get value
|
||||||
HAL_ADC_PollForConversion(&obj->handle, 10);
|
if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) {
|
||||||
if (HAL_ADC_GetState(&obj->handle) & HAL_ADC_STATE_EOC_REG) {
|
return (uint16_t)HAL_ADC_GetValue(&obj->handle);
|
||||||
return (HAL_ADC_GetValue(&obj->handle));
|
|
||||||
} else {
|
} else {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue