aboutsummaryrefslogtreecommitdiffstats
path: root/src/pru
diff options
context:
space:
mode:
authorKevin O'Connor <kevin@koconnor.net>2017-07-12 19:56:06 -0400
committerKevin O'Connor <kevin@koconnor.net>2017-07-12 23:03:14 -0400
commitc105ff1c51136d43c054ccedd81ed8a557e60804 (patch)
tree35486344d88885dc26909c32166232f73527294d /src/pru
parentae9bc93cccf8ed1f33f45555ceb7a26fcc8e4990 (diff)
downloadkutter-c105ff1c51136d43c054ccedd81ed8a557e60804.tar.gz
kutter-c105ff1c51136d43c054ccedd81ed8a557e60804.tar.xz
kutter-c105ff1c51136d43c054ccedd81ed8a557e60804.zip
pru: Move ADC code from gpio.c to new file adc.c
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Diffstat (limited to 'src/pru')
-rw-r--r--src/pru/Makefile1
-rw-r--r--src/pru/adc.c77
-rw-r--r--src/pru/gpio.c66
3 files changed, 78 insertions, 66 deletions
diff --git a/src/pru/Makefile b/src/pru/Makefile
index 323d1aa6..ae70a7da 100644
--- a/src/pru/Makefile
+++ b/src/pru/Makefile
@@ -16,6 +16,7 @@ CFLAGS_pru1.elf := $(CFLAGS)
# Add source files
src-y += pru/main.c pru/gpio.c generic/timer_irq.c
+src-$(CONFIG_HAVE_GPIO_ADC) += pru/adc.c
pru0-y := pru/pru0.c generic/crc16_ccitt.c command.c
pru0-y += ../lib/pru_rpmsg/pru_rpmsg.c ../lib/pru_rpmsg/pru_virtqueue.c
diff --git a/src/pru/adc.c b/src/pru/adc.c
new file mode 100644
index 00000000..64d4fcb4
--- /dev/null
+++ b/src/pru/adc.c
@@ -0,0 +1,77 @@
+// Analog to digital conversion (ADC) code on PRU
+//
+// Copyright (C) 2017 Kevin O'Connor <kevin@koconnor.net>
+//
+// This file may be distributed under the terms of the GNU GPLv3 license.
+
+#include "board/io.h" // readl
+#include "command.h" // shutdown
+#include "compiler.h" // ARRAY_SIZE
+#include "gpio.h" // gpio_adc_setup
+#include "internal.h" // ADC
+#include "sched.h" // sched_shutdown
+
+
+/****************************************************************
+ * Analog to Digital Converter (ADC) pins
+ ****************************************************************/
+
+DECL_CONSTANT(ADC_MAX, 4095);
+
+struct gpio_adc
+gpio_adc_setup(uint8_t pin)
+{
+ uint8_t chan = pin - 4 * 32;
+ if (chan >= 8)
+ shutdown("Not an adc channel");
+ if (!readl(&ADC->ctrl))
+ shutdown("ADC module not enabled");
+ return (struct gpio_adc){ .chan = chan };
+}
+
+enum { ADC_DUMMY=0xff };
+static uint8_t last_analog_read = ADC_DUMMY;
+static uint16_t last_analog_sample;
+
+// Try to sample a value. Returns zero if sample ready, otherwise
+// returns the number of clock ticks the caller should wait before
+// retrying this function.
+uint32_t
+gpio_adc_sample(struct gpio_adc g)
+{
+ uint8_t last = last_analog_read;
+ if (last == ADC_DUMMY) {
+ // Start sample
+ last_analog_read = g.chan;
+ writel(&ADC->stepenable, 1 << (g.chan + 1));
+ goto need_delay;
+ }
+ if (last == g.chan) {
+ // Check if sample ready
+ while (readl(&ADC->fifo0count)) {
+ uint32_t sample = readl(&ADC->fifo0data);
+ if (sample >> 16 == g.chan) {
+ last_analog_read = ADC_DUMMY;
+ last_analog_sample = sample;
+ return 0;
+ }
+ }
+ }
+need_delay:
+ return 160;
+}
+
+// Read a value; use only after gpio_adc_sample() returns zero
+uint16_t
+gpio_adc_read(struct gpio_adc g)
+{
+ return last_analog_sample;
+}
+
+// Cancel a sample that may have been started with gpio_adc_sample()
+void
+gpio_adc_cancel_sample(struct gpio_adc g)
+{
+ if (last_analog_read == g.chan)
+ last_analog_read = ADC_DUMMY;
+}
diff --git a/src/pru/gpio.c b/src/pru/gpio.c
index d0d0268b..ba4ba935 100644
--- a/src/pru/gpio.c
+++ b/src/pru/gpio.c
@@ -8,7 +8,6 @@
#include "command.h" // shutdown
#include "compiler.h" // ARRAY_SIZE
#include "gpio.h" // gpio_out_setup
-#include "internal.h" // ADC
#include "sched.h" // sched_shutdown
@@ -142,68 +141,3 @@ gpio_in_read(struct gpio_in g)
{
return !!(*g.reg & g.bit);
}
-
-
-/****************************************************************
- * Analog to Digital Converter (ADC) pins
- ****************************************************************/
-
-DECL_CONSTANT(ADC_MAX, 4095);
-
-struct gpio_adc
-gpio_adc_setup(uint8_t pin)
-{
- uint8_t chan = pin - ARRAY_SIZE(digital_regs) * 32;
- if (chan >= 8)
- shutdown("Not an adc channel");
- if (!readl(&ADC->ctrl))
- shutdown("ADC module not enabled");
- return (struct gpio_adc){ .chan = chan };
-}
-
-enum { ADC_DUMMY=0xff };
-static uint8_t last_analog_read = ADC_DUMMY;
-static uint16_t last_analog_sample;
-
-// Try to sample a value. Returns zero if sample ready, otherwise
-// returns the number of clock ticks the caller should wait before
-// retrying this function.
-uint32_t
-gpio_adc_sample(struct gpio_adc g)
-{
- uint8_t last = last_analog_read;
- if (last == ADC_DUMMY) {
- // Start sample
- last_analog_read = g.chan;
- writel(&ADC->stepenable, 1 << (g.chan + 1));
- goto need_delay;
- }
- if (last == g.chan) {
- // Check if sample ready
- while (readl(&ADC->fifo0count)) {
- uint32_t sample = readl(&ADC->fifo0data);
- if (sample >> 16 == g.chan) {
- last_analog_read = ADC_DUMMY;
- last_analog_sample = sample;
- return 0;
- }
- }
- }
-need_delay:
- return 160;
-}
-
-// Read a value; use only after gpio_adc_sample() returns zero
-uint16_t
-gpio_adc_read(struct gpio_adc g)
-{
- return last_analog_sample;
-}
-
-// Cancel a sample that may have been started with gpio_adc_sample()
-void
-gpio_adc_cancel_sample(struct gpio_adc g)
-{
- if (last_analog_read == g.chan)
- last_analog_read = ADC_DUMMY;
-}