diff options
author | Jakob Kaivo <jakob@kaivo.net> | 2018-11-10 16:03:37 -0500 |
---|---|---|
committer | Jakob Kaivo <jakob@kaivo.net> | 2018-11-10 16:03:37 -0500 |
commit | 9356468e9675a044eab70095b25869e92cf30f47 (patch) | |
tree | 9b691cff8c19198487913ccc15fb8a23af820b96 | |
parent | 166f001c73e0709e915cb0292a95f3ddf6006389 (diff) |
split GPIO stuff into separate file
-rw-r--r-- | Makefile | 6 | ||||
-rw-r--r-- | freeonebook.c | 135 | ||||
-rw-r--r-- | gpio.c | 159 | ||||
-rw-r--r-- | gpio.h | 9 |
4 files changed, 177 insertions, 132 deletions
@@ -1,7 +1,11 @@ CC=arm-linux-gnueabihf-gcc CFLAGS=-Wall -Wextra -freeonebook: freeonebook.o +freeonebook: freeonebook.o gpio.o + +freeonebook.o: freeonebook.c gpio.h + +gpio.o: gpio.c gpio.h clean: rm -f freeonebook *.o diff --git a/freeonebook.c b/freeonebook.c index 0dba0cf..86cf470 100644 --- a/freeonebook.c +++ b/freeonebook.c @@ -4,6 +4,8 @@ #include <fcntl.h> #include <unistd.h> +#include "gpio.h" + #define BUTTON_SPECIAL 89 #define BUTTON_PREVBOOK 91 #define BUTTON_PREVPAGE 92 @@ -11,142 +13,13 @@ #define BUTTON_NEXTCHAPTER 96 #define BUTTON_NEXTBOOK 100 -#define GPIO_BASEDIR "/sys/class/gpio" -#define GPIO_EXPORT GPIO_BASEDIR "/export" - #define ENABLE_LEFT_DISPLAY 5 #define ENABLE_RIGHT_DISPLAY 4 -void gpio_write(int port, const char *field, const char *value) -{ - char path[256]; - sprintf(path, "%s/gpio%d/%s", GPIO_BASEDIR, port, field); - int fd = open(path, O_WRONLY); - ssize_t n = write(fd, value, strlen(value)); - close(fd); - printf("write '%s' to '%s' => %d\n", value, path, n); -} - void enable_displays(void) { - gpio_write(ENABLE_LEFT_DISPLAY, "value", "1"); - gpio_write(ENABLE_RIGHT_DISPLAY, "value", "1"); -} - -void gpio_init(void) -{ - struct { - uint8_t port; - enum { in, out } direction; - uint8_t value; - } ports[] = { - { 100, in, 0 }, - { 91, in, 0 }, - { 96, in, 0 }, - { 89, in, 0 }, - { 95, in, 0 }, - { 92, in, 0 }, - { 108, out, 0 }, - { 94, out, 0 }, - { 101, out, 0 }, /* 95, 92 */ - { 5, out, 0 }, - { 4, out, 0 }, - { 93, in, 0 }, - { 98, out, 1 }, - { 90, out, 1 }, /* 108, 94, 101 */ - { 88, in, 0 }, - { 0, out, 0 }, - { 1, out, 0 }, - { 2, out, 0 }, - { 3, out, 0 }, - { 6, out, 0 }, - { 28, out, 0 }, - { 29, out, 0 }, - { 30, out, 0 }, - { 33, out, 0 }, - { 37, out, 0 }, - { 38, out, 0 }, - { 47, out, 0 }, - { 48, out, 0 }, - { 49, out, 0 }, - { 50, out, 0 }, - { 51, out, 0 }, - { 52, out, 0 }, - { 53, out, 0 }, - { 54, out, 0 }, - { 55, out, 0 }, - { 56, out, 0 }, - { 57, out, 0 }, - { 58, out, 0 }, - { 59, out, 0 }, - { 60, out, 0 }, - { 61, out, 0 }, - { 62, out, 0 }, - { 63, out, 0 }, - { 64, out, 0 }, - { 65, out, 0 }, - { 66, out, 0 }, - { 67, out, 0 }, - { 68, out, 0 }, - { 69, out, 0 }, - { 70, out, 0 }, - { 71, out, 0 }, - { 72, out, 0 }, - { 73, out, 0 }, - { 74, out, 0 }, - { 75, out, 0 }, /* not enabled */ - { 78, out, 0 }, - { 79, out, 0 }, - { 86, out, 0 }, - { 87, out, 0 }, - { 97, out, 0 }, - { 99, out, 0 }, - { 104, out, 0 }, - { 105, out, 0 }, - { 106, out, 0 }, - { 107, out, 0 }, - { 109, out, 0 }, - { 110, out, 0 }, - { 111, out, 0 }, - { 112, out, 0 }, - { 113, out, 0 }, - { 114, out, 0 }, - { 115, out, 0 }, - { 116, out, 0 }, - { 117, out, 0 }, - { 118, out, 0 }, - { 119, out, 0 }, - { 120, out, 0 }, - { 121, out, 0 }, - { 122, out, 0 }, - { 135, out, 0 }, - { 137, out, 0 }, - { 138, out, 0 }, - { 140, out, 0 }, - { 144, out, 0 }, - { 145, out, 0 }, - { 146, out, 0 }, - { 147, out, 0 }, - { 148, out, 0 }, - { 149, out, 0 }, - }; - - int gpio_export = open(GPIO_EXPORT, O_WRONLY); - for (size_t i = 0; i < sizeof(ports) / sizeof(ports[0]); i++) { - printf("enabling port %d\n", ports[i].port); - dprintf(gpio_export, "%d", ports[i].port); - } - close(gpio_export); - - for (size_t i = 0; i < sizeof(ports) / sizeof(ports[0]); i++) { - gpio_write(ports[i].port, "direction", ports[i].direction == in ? "in" : "out"); - } - - for (size_t i = 0; i < sizeof(ports) / sizeof(ports[0]); i++) { - if (ports[i].direction == out) { - gpio_write(ports[i].port, "value", ports[i].value == 1 ? "1" : "0"); - } - } + gpio_set(ENABLE_LEFT_DISPLAY); + gpio_set(ENABLE_RIGHT_DISPLAY); } int main(int argc, char *argv[]) @@ -0,0 +1,159 @@ +#include "gpio.h" + +#include <fcntl.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +#define GPIO_BASEDIR "/sys/class/gpio" +#define GPIO_EXPORT GPIO_BASEDIR "/export" + +#define GPIO_IN "in" +#define GPIO_INIT_LOW "GPIO_INIT_LOW" +#define GPIO_INIT_HIGH "GPIO_INIT_HIGH" + +static int gpio_open(int port, const char *field, int flags) +{ + char path[256]; + sprintf(path, "%s/gpio%d/%s", GPIO_BASEDIR, port, field); + return open(path, flags); +} + +int gpio_get(int port) +{ + char c; + int fd = gpio_open(port, "value", O_RDONLY); + read(fd, &c, 1); + close(fd); + if (c == '0') { + return 0; + } + return 1; +} + +void gpio_set(int port) +{ + int fd = gpio_open(port, "value", O_WRONLY); + write(fd, "1", 1); + close(fd); +} + +void gpio_clear(int port) +{ + int fd = gpio_open(port, "value", O_WRONLY); + write(fd, "0", 1); + close(fd); +} + +static void gpio_enable(int export_fd, int port, const char *state) +{ + dprintf(export_fd, "%d", port); + int fd = gpio_open(port, "direction", O_WRONLY); + write(fd, "state", strlen(state)); + close(fd); +} + +void gpio_init(void) +{ + struct { + uint8_t port; + const char *initstate; + } ports[] = { + { 100, GPIO_IN }, + { 91, GPIO_IN }, + { 96, GPIO_IN }, + { 89, GPIO_IN }, + { 95, GPIO_IN }, + { 92, GPIO_IN }, + { 108, GPIO_INIT_LOW }, + { 94, GPIO_INIT_LOW }, + { 101, GPIO_INIT_LOW }, /* 95, 92 */ + { 5, GPIO_INIT_LOW }, + { 4, GPIO_INIT_LOW }, + { 93, GPIO_IN }, + { 98, GPIO_INIT_HIGH }, + { 90, GPIO_INIT_HIGH }, /* 108, 94, 101 */ + { 88, GPIO_IN }, + { 0, GPIO_INIT_LOW }, + { 1, GPIO_INIT_LOW }, + { 2, GPIO_INIT_LOW }, + { 3, GPIO_INIT_LOW }, + { 6, GPIO_INIT_LOW }, + { 28, GPIO_INIT_LOW }, + { 29, GPIO_INIT_LOW }, + { 30, GPIO_INIT_LOW }, + { 33, GPIO_INIT_LOW }, + { 37, GPIO_INIT_LOW }, + { 38, GPIO_INIT_LOW }, + { 47, GPIO_INIT_LOW }, + { 48, GPIO_INIT_LOW }, + { 49, GPIO_INIT_LOW }, + { 50, GPIO_INIT_LOW }, + { 51, GPIO_INIT_LOW }, + { 52, GPIO_INIT_LOW }, + { 53, GPIO_INIT_LOW }, + { 54, GPIO_INIT_LOW }, + { 55, GPIO_INIT_LOW }, + { 56, GPIO_INIT_LOW }, + { 57, GPIO_INIT_LOW }, + { 58, GPIO_INIT_LOW }, + { 59, GPIO_INIT_LOW }, + { 60, GPIO_INIT_LOW }, + { 61, GPIO_INIT_LOW }, + { 62, GPIO_INIT_LOW }, + { 63, GPIO_INIT_LOW }, + { 64, GPIO_INIT_LOW }, + { 65, GPIO_INIT_LOW }, + { 66, GPIO_INIT_LOW }, + { 67, GPIO_INIT_LOW }, + { 68, GPIO_INIT_LOW }, + { 69, GPIO_INIT_LOW }, + { 70, GPIO_INIT_LOW }, + { 71, GPIO_INIT_LOW }, + { 72, GPIO_INIT_LOW }, + { 73, GPIO_INIT_LOW }, + { 74, GPIO_INIT_LOW }, + { 75, GPIO_INIT_LOW }, /* not enabled */ + { 78, GPIO_INIT_LOW }, + { 79, GPIO_INIT_LOW }, + { 86, GPIO_INIT_LOW }, + { 87, GPIO_INIT_LOW }, + { 97, GPIO_INIT_LOW }, + { 99, GPIO_INIT_LOW }, + { 104, GPIO_INIT_LOW }, + { 105, GPIO_INIT_LOW }, + { 106, GPIO_INIT_LOW }, + { 107, GPIO_INIT_LOW }, + { 109, GPIO_INIT_LOW }, + { 110, GPIO_INIT_LOW }, + { 111, GPIO_INIT_LOW }, + { 112, GPIO_INIT_LOW }, + { 113, GPIO_INIT_LOW }, + { 114, GPIO_INIT_LOW }, + { 115, GPIO_INIT_LOW }, + { 116, GPIO_INIT_LOW }, + { 117, GPIO_INIT_LOW }, + { 118, GPIO_INIT_LOW }, + { 119, GPIO_INIT_LOW }, + { 120, GPIO_INIT_LOW }, + { 121, GPIO_INIT_LOW }, + { 122, GPIO_INIT_LOW }, + { 135, GPIO_INIT_LOW }, + { 137, GPIO_INIT_LOW }, + { 138, GPIO_INIT_LOW }, + { 140, GPIO_INIT_LOW }, + { 144, GPIO_INIT_LOW }, + { 145, GPIO_INIT_LOW }, + { 146, GPIO_INIT_LOW }, + { 147, GPIO_INIT_LOW }, + { 148, GPIO_INIT_LOW }, + { 149, GPIO_INIT_LOW }, + }; + + int gpio_export = open(GPIO_EXPORT, O_WRONLY); + for (size_t i = 0; i < sizeof(ports) / sizeof(ports[0]); i++) { + gpio_enable(gpio_export, ports[i].port, ports[i].initstate); + } + close(gpio_export); +} @@ -0,0 +1,9 @@ +#ifndef GPIO_H +#define GPIO_H + +int gpio_get(int port); +void gpio_set(int port); +void gpio_clear(int port); +void gpio_init(void); + +#endif |