aboutsummaryrefslogtreecommitdiffstats
path: root/src/gpio/gpio.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/gpio/gpio.c')
-rw-r--r--src/gpio/gpio.c172
1 files changed, 172 insertions, 0 deletions
diff --git a/src/gpio/gpio.c b/src/gpio/gpio.c
new file mode 100644
index 0000000..45639a3
--- /dev/null
+++ b/src/gpio/gpio.c
@@ -0,0 +1,172 @@
+/*
+ * The Rin Library – gpio module
+ *
+ * Copyright (C) 2016 Gediminas Jakutis
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; version 2.1
+ * of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+ */
+
+#include "rin/gpio.h"
+#include "gpio_private.h"
+
+unsigned int rin_gpio_open(const unsigned int num, const enum rin_gpiotype type)
+{
+ struct stat st;
+ struct timespec grace = { 0, 1 * 1000 * 1000 }; /* 1 ms */
+ char buf[48] = { 0 };
+ int fd;
+ unsigned int timeout;
+
+ init();
+
+ if (type == rin_gpio_closed || !rin_gpio_pin_exists(num) || gpio[num].type != rin_gpio_closed) {
+ return 1;
+ }
+
+ fd = open("/sys/class/gpio/export", O_WRONLY | O_DSYNC);
+ sprintf(buf, "%u", num);
+ write(fd, buf, strlen(buf));
+ close(fd);
+ sprintf(buf, "/sys/class/gpio/gpio%u/direction", num);
+ timeout = 500; /* 500 ms, if it takes longer, something ain't right. */
+ while (stat(buf, &st)) {
+ nanosleep(&grace, NULL);
+ if (!(--timeout)) {
+ return 1;
+ }
+ }
+ fd = open(buf, O_WRONLY | O_DSYNC);
+ if (type == rin_gpio_in) {
+ write(fd, "in", 2);
+ } else {
+ write(fd, "out", 3);
+ }
+ close(fd);
+ gpio[num].type = type;
+ sprintf(buf, "/sys/class/gpio/gpio%u/value", num);
+ if (type == rin_gpio_in) {
+ gpio[num].fd = open(buf, O_RDONLY);
+ } else {
+ gpio[num].fd = open(buf, O_WRONLY);
+ }
+
+ return 0;
+}
+
+unsigned int rin_gpio_close(const unsigned int num)
+{
+ char buf[32] = { 0 };
+ struct stat st;
+ int fd;
+
+ init();
+ sprintf(buf, "/sys/class/gpio/gpio%u", num);
+ if (stat(buf, &st)) {
+ return 0;
+ }
+ close(gpio[num].fd);
+ gpio[num].fd = -1;
+ fd = open("/sys/class/gpio/unexport", O_WRONLY);
+ sprintf(buf, "%u", num);
+ write(fd, buf, strlen(buf));
+ close(fd);
+ gpio[num].type = rin_gpio_closed;
+ return 0;
+}
+
+unsigned int rin_gpio_setval(const unsigned int num, const unsigned int val)
+{
+ char buf[8] = { 0 };
+
+ init();
+ if (!rin_gpio_pin_exists(num) || gpio[num].type != rin_gpio_out) {
+ return 1;
+ }
+ sprintf(buf, "%u", val);
+ lseek(gpio[num].fd, 0, SEEK_SET);
+ write(gpio[num].fd, buf, strlen(buf));
+ return 0;
+}
+
+unsigned int rin_gpio_readval(const unsigned int num, unsigned int * const val)
+{
+ init();
+
+ if (!rin_gpio_pin_exists(num) || gpio[num].type != rin_gpio_in) {
+ exit(EXIT_FAILURE);
+ }
+
+ lseek(gpio[num].fd, 0, SEEK_SET);
+ read(gpio[num].fd, val, 1);
+
+ if (*val >= '0') {
+ *val -= '0';
+ }
+
+ return 0;
+}
+
+/*
+ * right now is rpi V1 specific, should be rewritten when/if we support
+ * other platforms.
+ */
+unsigned int rin_gpio_pin_exists(const unsigned int pin)
+{
+ switch (pin) {
+ case 2:
+ case 3:
+ case 4:
+ case 7:
+ case 8:
+ case 9:
+ case 10:
+ case 11:
+ case 14:
+ case 15:
+ case 17:
+ case 18:
+ case 22:
+ case 23:
+ case 24:
+ case 25:
+ return 1;
+ default:
+ return 0;
+ }
+}
+
+/*
+ * in case we ever support hardware other than the V1 Raspberry,
+ * we need this to get the pin usable pin count
+ */
+unsigned int rin_gpio_get_pincount(void)
+{
+ return 16;
+}
+
+static void init(void)
+{
+ size_t i;
+ static unsigned int firstrun = 1;
+
+ if (firstrun) {
+ firstrun = 0;
+ for (i = 0; i < gpio_count; ++i) {
+ gpio[i].num = i;
+ gpio[i].type = rin_gpio_closed;
+ gpio[i].fd = -1;
+ }
+ }
+}