diff --git a/boards/common/blxxxpill/include/periph_conf.h b/boards/common/blxxxpill/include/periph_conf.h index 143b69a82a..7cf572a8a2 100644 --- a/boards/common/blxxxpill/include/periph_conf.h +++ b/boards/common/blxxxpill/include/periph_conf.h @@ -74,9 +74,13 @@ extern "C" { { .dev = 0, .pin = GPIO_PIN(PORT_A, 7), .chan = 7 }, \ { .dev = 0, .pin = GPIO_PIN(PORT_B, 0), .chan = 8 }, \ { .dev = 0, .pin = GPIO_PIN(PORT_B, 1), .chan = 9 }, \ + /* ADC Temperature channel */ \ + { .dev = 0, .pin = GPIO_UNDEF, .chan = 16 }, \ + /* ADC VREF channel */ \ + { .dev = 0, .pin = GPIO_UNDEF, .chan = 17 }, \ } -#define ADC_NUMOF 10 +#define ADC_NUMOF 12 /** @} */ /** diff --git a/cpu/stm32f1/periph/adc.c b/cpu/stm32f1/periph/adc.c index 2c68df9430..270547bdf9 100644 --- a/cpu/stm32f1/periph/adc.c +++ b/cpu/stm32f1/periph/adc.c @@ -57,10 +57,22 @@ static inline void prep(adc_t line) { mutex_lock(&locks[adc_config[line].dev]); periph_clk_en(APB2, (RCC_APB2ENR_ADC1EN << adc_config[line].dev)); + + /* check if this channel is an internal ADC channel, if so + * enable the internal temperature and Vref */ + if (adc_config[line].chan == 16 || adc_config[line].chan == 17) { + dev(line)->CR2 |= ADC_CR2_TSVREFE; + } } static inline void done(adc_t line) { + /* check if this channel is an internal ADC channel, if so + * disable the internal temperature and Vref */ + if (adc_config[line].chan == 16 || adc_config[line].chan == 17) { + dev(line)->CR2 &= ~ADC_CR2_TSVREFE; + } + periph_clk_dis(APB2, (RCC_APB2ENR_ADC1EN << adc_config[line].dev)); mutex_unlock(&locks[adc_config[line].dev]); } @@ -78,7 +90,9 @@ int adc_init(adc_t line) prep(line); /* configure the pin */ - gpio_init_analog(adc_config[line].pin); + if (adc_config[line].pin != GPIO_UNDEF) { + gpio_init_analog(adc_config[line].pin); + } /* set clock prescaler to get the maximal possible ADC clock value */ for (clk_div = 2; clk_div < 8; clk_div += 2) { if ((CLOCK_CORECLOCK / clk_div) <= MAX_ADC_SPEED) { @@ -109,15 +123,11 @@ int adc_init(adc_t line) /* start sampling from software */ dev(line)->CR2 |= ADC_CR2_EXTTRIG | ADC_CR2_EXTSEL; - /* check if this channel is an internal ADC channel, if so - * enable the internal temperature and Vref */ + /* check if the internal channels are configured to use ADC1 */ if (adc_config[line].chan == 16 || adc_config[line].chan == 17) { - /* check if the internal channels are configured to use ADC1 */ if (dev(line) != ADC1) { return -3; } - - dev(line)->CR2 |= ADC_CR2_TSVREFE; } /* free the device again */