diff --git a/src/stm32/adc.c b/src/stm32/adc.c index 9f1a6d53..0d3f33ce 100644 --- a/src/stm32/adc.c +++ b/src/stm32/adc.c @@ -1,6 +1,6 @@ // ADC functions on STM32 // -// Copyright (C) 2019 Kevin O'Connor +// Copyright (C) 2019-2020 Kevin O'Connor // // This file may be distributed under the terms of the GNU GPLv3 license. @@ -15,11 +15,23 @@ DECL_CONSTANT("ADC_MAX", 4095); +#define ADC_TEMPERATURE_PIN 0xfe +DECL_ENUMERATION("pin", "ADC_TEMPERATURE", ADC_TEMPERATURE_PIN); + static const uint8_t adc_pins[] = { GPIO('A', 0), GPIO('A', 1), GPIO('A', 2), GPIO('A', 3), GPIO('A', 4), GPIO('A', 5), GPIO('A', 6), GPIO('A', 7), GPIO('B', 0), GPIO('B', 1), GPIO('C', 0), GPIO('C', 1), GPIO('C', 2), GPIO('C', 3), GPIO('C', 4), GPIO('C', 5), + +#if CONFIG_MACH_STM32F1 + ADC_TEMPERATURE_PIN, +#elif CONFIG_MACH_STM32F2 || CONFIG_MACH_STM32F407 + ADC_TEMPERATURE_PIN, 0x00, 0x00, +#elif CONFIG_MACH_STM32F446 + 0x00, 0x00, ADC_TEMPERATURE_PIN, +#endif + #if CONFIG_MACH_STM32F4 0x00, 0x00, 0x00, 0x00, GPIO('F', 6), GPIO('F', 7), GPIO('F', 8), GPIO('F', 9), @@ -29,7 +41,8 @@ static const uint8_t adc_pins[] = { }; #if CONFIG_MACH_STM32F1 -#define CR2_FLAGS (ADC_CR2_ADON | (7 << ADC_CR2_EXTSEL_Pos) | ADC_CR2_EXTTRIG) +#define CR2_FLAGS (ADC_CR2_ADON | (7 << ADC_CR2_EXTSEL_Pos) | ADC_CR2_EXTTRIG \ + | ADC_CR2_TSVREFE) #else #define CR2_FLAGS ADC_CR2_ADON #endif @@ -71,11 +84,11 @@ gpio_adc_setup(uint32_t pin) ADC_TypeDef *adc = ADC1; uint32_t adc_base = ADC1_BASE; #if CONFIG_MACH_STM32F4 - if (chan >= 16) { + if (chan >= 19) { // On the STM32F4, some ADC channels are only available from ADC3 adc = ADC3; adc_base += 0x800; - chan -= 16; + chan -= 19; } #endif @@ -94,7 +107,13 @@ gpio_adc_setup(uint32_t pin) adc->CR2 = CR2_FLAGS; } - gpio_peripheral(pin, GPIO_ANALOG, 0); + if (pin == ADC_TEMPERATURE_PIN) { +#if !CONFIG_MACH_STM32F1 + ADC123_COMMON->CCR = ADC_CCR_TSVREFE; +#endif + } else { + gpio_peripheral(pin, GPIO_ANALOG, 0); + } return (struct gpio_adc){ .adc = adc, .chan = chan }; }