STM32F1 ADC: remove adc_inited flag

pull/5513/head
bcostm 2017-11-14 13:20:21 +01:00
parent 2e2744ccbe
commit 36110e10cf
1 changed files with 24 additions and 27 deletions

View File

@ -36,10 +36,9 @@
#include "mbed_error.h" #include "mbed_error.h"
#include "PeripheralPins.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;
RCC_PeriphCLKInitTypeDef PeriphClkInit; RCC_PeriphCLKInitTypeDef PeriphClkInit;
uint32_t function = (uint32_t)NC; uint32_t function = (uint32_t)NC;
@ -49,14 +48,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
} }
@ -68,13 +67,26 @@ 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 // Enable ADC clock
if (adc_inited == 0) { __HAL_RCC_ADC1_CLK_ENABLE();
adc_inited = 1;
// Enable ADC clock // Configure ADC object structures
__HAL_RCC_ADC1_CLK_ENABLE(); obj->handle.State = HAL_ADC_STATE_RESET;
obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
obj->handle.Init.ScanConvMode = DISABLE;
obj->handle.Init.ContinuousConvMode = DISABLE;
obj->handle.Init.NbrOfConversion = 1;
obj->handle.Init.DiscontinuousConvMode = DISABLE;
obj->handle.Init.NbrOfDiscConversion = 0;
obj->handle.Init.ExternalTrigConv = ADC_SOFTWARE_START;
if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
error("Cannot initialize ADC");
}
// This section is done only once
if (adc_calibrated == 0) {
adc_calibrated = 1;
// Configure ADC clock prescaler // Configure ADC clock prescaler
// Caution: On STM32F1, ADC clock frequency max is 14 MHz (refer to device datasheet). // Caution: On STM32F1, ADC clock frequency max is 14 MHz (refer to device datasheet).
// Therefore, ADC clock prescaler must be configured in function // Therefore, ADC clock prescaler must be configured in function
@ -84,29 +96,14 @@ void analogin_init(analogin_t *obj, PinName pin)
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC; PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6; PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit); HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
// Calibration
// Configure ADC
obj->handle.State = HAL_ADC_STATE_RESET;
obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
obj->handle.Init.ScanConvMode = DISABLE;
obj->handle.Init.ContinuousConvMode = DISABLE;
obj->handle.Init.NbrOfConversion = 1;
obj->handle.Init.DiscontinuousConvMode = DISABLE;
obj->handle.Init.NbrOfDiscConversion = 0;
obj->handle.Init.ExternalTrigConv = ADC_SOFTWARE_START;
if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
error("Cannot initialize ADC\n");
}
// Calibrate ADC
HAL_ADCEx_Calibration_Start(&obj->handle); HAL_ADCEx_Calibration_Start(&obj->handle);
} }
} }
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 = 1; sConfig.Rank = 1;
@ -177,7 +174,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;
} }