aboutsummaryrefslogtreecommitdiffstats
path: root/src/stm32/adc.c
diff options
context:
space:
mode:
authorKevin O'Connor <kevin@koconnor.net>2020-10-07 11:15:02 -0400
committerKevin O'Connor <kevin@koconnor.net>2020-10-29 11:55:06 -0400
commitd985b53ccb0194a21c705c6e629ede9412b7e337 (patch)
tree9b3f48c37bb508b09f7c7b4fbb656804ec02c7d1 /src/stm32/adc.c
parentd9b789113322780a125d1594a9e723e5990f2744 (diff)
downloadkutter-d985b53ccb0194a21c705c6e629ede9412b7e337.tar.gz
kutter-d985b53ccb0194a21c705c6e629ede9412b7e337.tar.xz
kutter-d985b53ccb0194a21c705c6e629ede9412b7e337.zip
stm32: Support reading chip based temperature sensor on stm32f1/2/4
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Diffstat (limited to 'src/stm32/adc.c')
-rw-r--r--src/stm32/adc.c29
1 files changed, 24 insertions, 5 deletions
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 <kevin@koconnor.net>
+// Copyright (C) 2019-2020 Kevin O'Connor <kevin@koconnor.net>
//
// 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 };
}