summaryrefslogtreecommitdiff
path: root/gpio.c
blob: 64d79f3d55dbace054fdc7c20aa00e509dc7ce9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include "gpio.h"

#include <fcntl.h>
#include <poll.h>
#include <pthread.h>
#include <stdlib.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	"low"
#define GPIO_INIT_HIGH	"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);
	return c != '0';
}

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_multi(int export_fd, int port, const char *state)
{
	printf("enabling GPIO %d, state '%s'\n", port, state);

	dprintf(export_fd, "%d", port);
	int fd = gpio_open(port, "direction", O_WRONLY);
	write(fd, "direction", strlen(state));
	close(fd);
}

static void gpio_enable(int port, const char *state)
{
	int fd = open(GPIO_EXPORT, O_WRONLY);
	gpio_enable_multi(fd, port, state);
	close(fd);
}

void gpio_init(void)
{
	struct {
		uint8_t port;
		const char *initstate;
	} ports[] = {
		{ 108, GPIO_INIT_LOW },
		{ 94, GPIO_INIT_LOW },
		{ 101, GPIO_INIT_LOW },
		{ 5, GPIO_INIT_LOW },
		{ 4, GPIO_INIT_LOW },
		{ 98, GPIO_INIT_HIGH },
		{ 90, GPIO_INIT_HIGH },
		{ 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 },
		{ 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_multi(gpio_export, ports[i].port, ports[i].initstate);
	}
	close(gpio_export);
}

struct gpio_watcher {
	int port;
	void (*fn)(int);
};

void *gpio_watch_thread(void *arg)
{
	struct gpio_watcher *w = arg;
	int fd = gpio_open(w->port, "value", O_RDONLY);
	for (;;) {
		struct pollfd fds[] = { { fd, POLLPRI | POLLERR, 0 } };
		if (poll(fds, 1, -1) == 1) {
			if (gpio_get(w->port)) {
				w->fn(w->port);
			}
		}
	}
	close(fd);
	return NULL;
}

void gpio_watch(int port, void (*fn)(int))
{
	printf("starting watcher for %d\n", port);
	gpio_enable(port, "in");
	int edge = gpio_open(port, "edge", O_WRONLY);
	write(edge, "both", 4);
	close(edge);

	pthread_t thread;
	struct gpio_watcher *watcher = malloc(sizeof(*watcher));
	watcher->port = port;
	watcher->fn = fn;
	pthread_create(&thread, NULL, gpio_watch_thread, watcher);
}