aboutsummaryrefslogtreecommitdiffstats
path: root/src/stm32f4/adc.c
diff options
context:
space:
mode:
authorKevin O'Connor <kevin@koconnor.net>2019-07-28 14:40:41 -0400
committerKevinOConnor <kevin@koconnor.net>2019-08-05 11:25:40 -0400
commit7efc53ff59111061908405dae889a38cc6e60dbb (patch)
treed9034e66fb34629203fb278b381af8af8ebf980c /src/stm32f4/adc.c
parent485164b8b3fcb973a8766264e1c1e9e190ea80af (diff)
downloadkutter-7efc53ff59111061908405dae889a38cc6e60dbb.tar.gz
kutter-7efc53ff59111061908405dae889a38cc6e60dbb.tar.xz
kutter-7efc53ff59111061908405dae889a38cc6e60dbb.zip
stm32f4: Add support for STM32F103
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Diffstat (limited to 'src/stm32f4/adc.c')
-rw-r--r--src/stm32f4/adc.c24
1 files changed, 20 insertions, 4 deletions
diff --git a/src/stm32f4/adc.c b/src/stm32f4/adc.c
index 07d03003..8cd50b48 100644
--- a/src/stm32f4/adc.c
+++ b/src/stm32f4/adc.c
@@ -8,6 +8,7 @@
#include "board/misc.h" // timer_from_us
#include "command.h" // shutdown
#include "compiler.h" // ARRAY_SIZE
+#include "generic/armcm_timer.h" // udelay
#include "gpio.h" // gpio_adc_setup
#include "internal.h" // GPIO
#include "sched.h" // sched_shutdown
@@ -21,6 +22,12 @@ static const uint8_t adc_pins[] = {
GPIO('C', 2), GPIO('C', 3), GPIO('C', 4), GPIO('C', 5)
};
+#if CONFIG_MACH_STM32F1xx
+#define CR2_FLAGS (ADC_CR2_ADON | (7 << ADC_CR2_EXTSEL_Pos) | ADC_CR2_EXTTRIG)
+#else
+#define CR2_FLAGS ADC_CR2_ADON
+#endif
+
struct gpio_adc
gpio_adc_setup(uint32_t pin)
{
@@ -36,14 +43,22 @@ gpio_adc_setup(uint32_t pin)
// Enable the ADC
if (!is_enabled_pclock(ADC1_BASE)) {
enable_pclock(ADC1_BASE);
- uint32_t aticks = 3; // 56 adc cycles
+ uint32_t aticks = 3; // 2.5-3.2us (depending on stm32 chip)
ADC1->SMPR1 = (aticks | (aticks << 3) | (aticks << 6) | (aticks << 9)
| (aticks << 12) | (aticks << 15) | (aticks << 18)
| (aticks << 21) | (aticks << 24));
ADC1->SMPR2 = (aticks | (aticks << 3) | (aticks << 6) | (aticks << 9)
| (aticks << 12) | (aticks << 15) | (aticks << 18)
| (aticks << 21) | (aticks << 24) | (aticks << 27));
- ADC1->CR2 = ADC_CR2_ADON;
+ ADC1->CR2 = CR2_FLAGS;
+
+#if CONFIG_MACH_STM32F1xx
+ // Perform calibration
+ udelay(timer_from_us(1));
+ ADC1->CR2 = ADC_CR2_CAL | CR2_FLAGS;
+ while (ADC1->CR2 & ADC_CR2_CAL)
+ ;
+#endif
}
gpio_peripheral(pin, GPIO_ANALOG, 0);
@@ -67,9 +82,10 @@ gpio_adc_sample(struct gpio_adc g)
}
// Start sample
ADC1->SQR3 = g.chan;
- ADC1->CR2 = ADC_CR2_SWSTART | ADC_CR2_ADON;
+ ADC1->CR2 = ADC_CR2_SWSTART | CR2_FLAGS;
+
need_delay:
- return timer_from_us(4);
+ return timer_from_us(10);
}
// Read a value; use only after gpio_adc_sample() returns zero