STM32F0 ADC: remove adc_inited flag

pull/5513/head
bcostm 2017-11-14 13:07:35 +01:00
parent b025ea16d6
commit 2e2744ccbe
1 changed files with 38 additions and 38 deletions

View File

@ -33,12 +33,12 @@
#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"
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, ...)
@ -47,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
} }
@ -66,40 +66,38 @@ 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_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 __HAL_RCC_ADC1_CLK_ENABLE();
__ADC1_CLK_ENABLE();
// Configure ADC if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
obj->handle.State = HAL_ADC_STATE_RESET; error("Cannot initialize ADC");
obj->handle.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4; }
obj->handle.Init.Resolution = ADC_RESOLUTION12b;
obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT; // ADC calibration is done only once
obj->handle.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD; if (adc_calibrated == 0) {
obj->handle.Init.EOCSelection = EOC_SINGLE_CONV; adc_calibrated = 1;
obj->handle.Init.LowPowerAutoWait = DISABLE; HAL_ADCEx_Calibration_Start(&obj->handle);
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");
}
} }
} }
static inline uint16_t adc_read(analogin_t *obj) { static inline uint16_t adc_read(analogin_t *obj)
ADC_ChannelConfTypeDef sConfig; {
ADC_ChannelConfTypeDef sConfig = {0};
// Configure ADC channel // Configure ADC channel
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER; 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 // 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;
} }
} }
uint16_t analogin_read_u16(analogin_t *obj) { uint16_t analogin_read_u16(analogin_t *obj)
{
uint16_t value = adc_read(obj); uint16_t value = adc_read(obj);
// 12-bit to 16-bit conversion // 12-bit to 16-bit conversion
value = ((value << 4) & (uint16_t)0xFFF0) | ((value >> 8) & (uint16_t)0x000F); value = ((value << 4) & (uint16_t)0xFFF0) | ((value >> 8) & (uint16_t)0x000F);
return value; return value;
} }
float analogin_read(analogin_t *obj) { float analogin_read(analogin_t *obj)
{
uint16_t value = adc_read(obj); uint16_t value = adc_read(obj);
return (float)value * (1.0f / (float)0xFFF); // 12 bits range return (float)value * (1.0f / (float)0xFFF); // 12 bits range
} }