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;
@ -68,24 +67,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;
// The ADC initialization is done once
if (adc_inited == 0) {
adc_inited = 1;
// Enable ADC clock // Enable ADC clock
__HAL_RCC_ADC1_CLK_ENABLE(); __HAL_RCC_ADC1_CLK_ENABLE();
// Configure ADC clock prescaler // Configure ADC object structures
// Caution: On STM32F1, ADC clock frequency max is 14 MHz (refer to device datasheet).
// Therefore, ADC clock prescaler must be configured in function
// of ADC clock source frequency to remain below this maximum frequency.
// with 8 MHz external xtal: PCLK2 = 72 MHz --> ADC clock = 72/6 = 12 MHz
// with internal clock : PCLK2 = 64 MHz --> ADC clock = 64/6 = 10.67 MHz
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
// Configure ADC
obj->handle.State = HAL_ADC_STATE_RESET; obj->handle.State = HAL_ADC_STATE_RESET;
obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT; obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
obj->handle.Init.ScanConvMode = DISABLE; obj->handle.Init.ScanConvMode = DISABLE;
@ -96,17 +81,29 @@ void analogin_init(analogin_t *obj, PinName pin)
obj->handle.Init.ExternalTrigConv = ADC_SOFTWARE_START; obj->handle.Init.ExternalTrigConv = ADC_SOFTWARE_START;
if (HAL_ADC_Init(&obj->handle) != HAL_OK) { if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
error("Cannot initialize ADC\n"); error("Cannot initialize ADC");
} }
// Calibrate ADC // This section is done only once
if (adc_calibrated == 0) {
adc_calibrated = 1;
// Configure ADC clock prescaler
// Caution: On STM32F1, ADC clock frequency max is 14 MHz (refer to device datasheet).
// Therefore, ADC clock prescaler must be configured in function
// of ADC clock source frequency to remain below this maximum frequency.
// with 8 MHz external xtal: PCLK2 = 72 MHz --> ADC clock = 72/6 = 12 MHz
// with internal clock : PCLK2 = 64 MHz --> ADC clock = 64/6 = 10.67 MHz
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
// Calibration
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;
} }