aboutsummaryrefslogtreecommitdiffstats
path: root/src/linux/analog.c
diff options
context:
space:
mode:
authorKevin O'Connor <kevin@koconnor.net>2017-08-27 13:58:19 -0400
committerKevin O'Connor <kevin@koconnor.net>2017-09-20 12:55:28 -0400
commit16d2ec3a905204dd804831611aff37a4b508e0fa (patch)
tree9f2b4f64b575fae07f24ea599b8796d95d5ec86e /src/linux/analog.c
parent4d60567bc66417c48e7c3095ceded6c24ec1888f (diff)
downloadkutter-16d2ec3a905204dd804831611aff37a4b508e0fa.tar.gz
kutter-16d2ec3a905204dd804831611aff37a4b508e0fa.tar.xz
kutter-16d2ec3a905204dd804831611aff37a4b508e0fa.zip
linux: Add support for analog IIO devices
Add support for reading analog values via the standard Linux IIO interface. This can be used on Replicape boards to sample analog input pins. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Diffstat (limited to 'src/linux/analog.c')
-rw-r--r--src/linux/analog.c61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/linux/analog.c b/src/linux/analog.c
new file mode 100644
index 00000000..88adc9dd
--- /dev/null
+++ b/src/linux/analog.c
@@ -0,0 +1,61 @@
+// Read analog values from Linux IIO device
+//
+// Copyright (C) 2017 Kevin O'Connor <kevin@koconnor.net>
+//
+// This file may be distributed under the terms of the GNU GPLv3 license.
+
+#include <fcntl.h> // open
+#include <stdio.h> // snprintf
+#include <stdlib.h> // atoi
+#include <unistd.h> // read
+#include "command.h" // shutdown
+#include "gpio.h" // gpio_adc_setup
+#include "internal.h" // report_errno
+#include "sched.h" // sched_shutdown
+
+DECL_CONSTANT(ADC_MAX, 4095); // Assume 12bit adc
+
+#define IIO_PATH "/sys/bus/iio/devices/iio:device0/in_voltage%d_raw"
+
+struct gpio_adc
+gpio_adc_setup(uint8_t pin)
+{
+ char fname[256];
+ snprintf(fname, sizeof(fname), IIO_PATH, pin);
+
+ int fd = open(fname, O_RDONLY|O_CLOEXEC);
+ if (fd < 0) {
+ report_errno("analog open", fd);
+ shutdown("Unable to open adc path");
+ }
+ int ret = set_non_blocking(fd);
+ if (ret < 0) {
+ report_errno("analog set_non_blocking", ret);
+ shutdown("Unable to set non blocking on adc path");
+ }
+ return (struct gpio_adc){ .fd = fd };
+}
+
+uint32_t
+gpio_adc_sample(struct gpio_adc g)
+{
+ return 0;
+}
+
+uint16_t
+gpio_adc_read(struct gpio_adc g)
+{
+ char buf[64];
+ int ret = pread(g.fd, buf, sizeof(buf)-1, 0);
+ if (ret <= 0) {
+ report_errno("analog read", ret);
+ shutdown("Error on analog read");
+ }
+ buf[ret] = '\0';
+ return atoi(buf);
+}
+
+void
+gpio_adc_cancel_sample(struct gpio_adc g)
+{
+}