summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2021-11-13 23:59:55 +0100
committerDaniel Friesel <derf@finalrewind.org>2021-11-13 23:59:55 +0100
commit560b35559fb810ba3c0a85c24de59735c74e784e (patch)
treed92bd44978b5e8f79d83756e947d0ad68b6343d2
parent2042fd5305117b5bad604803e641cdc883b50ddc (diff)
add adctest app
-rw-r--r--src/app/adctest/Kconfig6
-rw-r--r--src/app/adctest/Makefile.inc9
-rw-r--r--src/app/adctest/main.cc79
3 files changed, 94 insertions, 0 deletions
diff --git a/src/app/adctest/Kconfig b/src/app/adctest/Kconfig
new file mode 100644
index 0000000..4739855
--- /dev/null
+++ b/src/app/adctest/Kconfig
@@ -0,0 +1,6 @@
+# Copyright 2021 Daniel Friesel
+#
+# SPDX-License-Identifier: CC0-1.0
+
+prompt "ADC Test"
+depends on meta_driver_adc && arch_arduino_nano && loop && !wakeup
diff --git a/src/app/adctest/Makefile.inc b/src/app/adctest/Makefile.inc
new file mode 100644
index 0000000..c201be2
--- /dev/null
+++ b/src/app/adctest/Makefile.inc
@@ -0,0 +1,9 @@
+# vim:ft=make
+#
+# Copyright 2021 Daniel Friesel
+#
+# SPDX-License-Identifier: CC0-1.0
+
+ifdef app
+ loop = 1
+endif
diff --git a/src/app/adctest/main.cc b/src/app/adctest/main.cc
new file mode 100644
index 0000000..7a87ede
--- /dev/null
+++ b/src/app/adctest/main.cc
@@ -0,0 +1,79 @@
+/*
+ * Copyright 2021 Daniel Friesel
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+#include "arch.h"
+#include "driver/adc.h"
+#include "driver/gpio.h"
+#include "driver/stdout.h"
+
+void loop(void)
+{
+ uint16_t vcc = adc.getVCC_mV();
+ uint16_t temp = adc.getTemp_mdegC();
+
+ kout << "Voltage " << vcc << " mv" << endl;
+ kout << "Temperature " << temp << " m°C" << endl;
+
+ for (uint8_t admux_sel = 0; admux_sel < 8; admux_sel++) {
+ // measure with avcc reference
+ ADMUX = _BV(REFS0) | admux_sel;
+
+ // Enable ADC with /64 prescaler
+ ADCSRA = _BV(ADEN) | _BV(ADPS2);
+
+ // Start conversion
+ ADCSRA |= _BV(ADSC);
+
+ // wait until conversion is complete
+ while (ADCSRA & _BV(ADSC)) ;
+
+ uint8_t adcr_l = ADCL;
+ uint8_t adcr_h = ADCH;
+ uint16_t adcr = adcr_l + (adcr_h << 8);
+ uint16_t vadc = (uint32_t)vcc * adcr / 1023L;
+
+ kout << "ADC" << admux_sel << " " << vadc << " mV vs AVCC" << endl;
+ }
+
+ // enable bandgap; wait for it to stabilise
+ ADMUX = _BV(REFS1) | _BV(REFS0);
+ ADCSRA = _BV(ADEN) | _BV(ADPS2);
+ arch.delay_ms(1);
+
+ for (uint8_t admux_sel = 0; admux_sel < 8; admux_sel++) {
+ // measure with bandgap reference
+ ADMUX = _BV(REFS1) | _BV(REFS0) | admux_sel;
+
+ // Enable ADC with /64 prescaler
+ ADCSRA = _BV(ADEN) | _BV(ADPS2);
+
+ // Start conversion
+ ADCSRA |= _BV(ADSC);
+
+ // wait until conversion is complete
+ while (ADCSRA & _BV(ADSC)) ;
+
+ uint8_t adcr_l = ADCL;
+ uint8_t adcr_h = ADCH;
+ uint16_t adcr = adcr_l + (adcr_h << 8);
+ uint16_t vadc = 1100L * adcr / 1023L;
+
+ kout << "ADC" << admux_sel << " " << vadc << " mV vs 1.1V bandgap" << endl;
+ }
+
+ // disable ADC
+ ADCSRA &= ~_BV(ADEN);
+}
+
+int main(void)
+{
+ arch.setup();
+ gpio.setup();
+ kout.setup();
+
+ arch.idle_loop();
+
+ return 0;
+}