9 static const int bmap[] = {
11 -1, -1, 2, -1, 3, -1, 4, 14, -1, 15, 17, 18, 27, -1, 22, 23, -1, 24, 10, -1,
12 9, 25, 11, 8, -1, 7, -1, -1, 5, -1, 6, 12, 13, -1, 19, 16, 21, 20, -1, 21,
14 static const int imap[] = {
15 -1, -1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
16 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, 27,
18 static const int *umap = bmap;
19 static int mapn = sizeof(bmap) / sizeof(*bmap);
20 static int preserve = 0;
22 static int mappin(int num)
24 if((num < 0) || (num >= mapn))
29 static void export(int p)
35 if((rp = mappin(p)) < 0)
36 errx(1, "%i: no such port", p);
37 sprintf(path, "/sys/class/gpio/gpio%i", rp);
38 if(!access(path, R_OK | X_OK))
41 errx(2, "gpio%i: not exported", rp);
42 sprintf(path, "/sys/class/gpio/export");
43 if((fp = fopen(path, "w")) == NULL)
45 fprintf(fp, "%i\n", rp); fflush(fp);
47 errx(1, "gpio%i: could not export", rp);
49 sprintf(path, "/sys/class/gpio/gpio%i", rp);
50 if(access(path, R_OK | X_OK))
51 errx(1, "gpio%i: still not available after export", rp);
54 static void checkdir(int rp, char *dir)
56 char path[256], line[256];
61 sprintf(path, "/sys/class/gpio/gpio%i/direction", rp);
62 if((fp = fopen(path, "r")) == NULL)
64 rv = !!fgets(line, sizeof(line), fp);
66 if(!rv || ((ln = strlen(line)) < 1) || (line[ln - 1] != '\n'))
67 errx(1, "gpio%i: could not read direction", rp);
69 if(strcmp(line, dir)) {
71 errx(2, "gpio%i: direction not set to %s", rp, dir);
72 if((fp = fopen(path, "w")) == NULL)
74 fprintf(fp, "%s\n", dir); fflush(fp);
76 errx(1, "gpio%i: could not set to direction to %s", rp, dir);
81 static void setport(int p, int v)
87 if((rp = mappin(p)) < 0)
88 errx(1, "%i: no such port", p);
90 sprintf(path, "/sys/class/gpio/gpio%i/value", rp);
91 if((fp = fopen(path, "w")) == NULL)
93 fprintf(fp, "%i\n", v); fflush(fp);
95 errx(1, "gpio%i: could not set value", rp);
99 static int getport(int p)
101 char path[256], line[256], *ep;
106 if((rp = mappin(p)) < 0)
107 errx(1, "%i: no such port", p);
109 sprintf(path, "/sys/class/gpio/gpio%i/value", rp);
110 if((fp = fopen(path, "r")) == NULL)
112 rv = !!fgets(line, sizeof(line), fp);
114 if(!rv || ((ln = strlen(line)) < 1) || (line[ln - 1] != '\n'))
115 errx(1, "gpio%i: could not read direction", rp);
117 rv = (int)strtol(line, &ep, 10);
119 errx(1, "gpio%i: unexpected contents: %s", rp, line);
123 static void usage(FILE *out)
125 fprintf(out, "usage: gpio [-hip] {PORT=VAL|PORT==VAL|PORT?}...\n");
128 int main(int argc, char **argv)
133 while((c = getopt(argc, argv, "hip")) >= 0) {
140 mapn = sizeof(imap) / sizeof(*imap);
154 for(i = optind; i < argc; i++) {
155 port = strtol(argv[i], &e, 10);
156 if((e > argv[i]) && (e[0] == '=') && (e[1] == '=')) {
157 val = strtol(e + 2, &e, 10);
159 errx(1, "gpio: %s: not of the form PORT==VAL", argv[i]);
161 return(getport(port) != val);
162 } else if((e > argv[i]) && (e[0] == '=')) {
163 val = strtol(e + 1, &e, 10);
165 errx(1, "gpio: %s: not of the form PORT=VAL", argv[i]);
168 } else if((e > argv[i]) && (e[0] == '?') && !e[1]) {
170 printf("%i\n", getport(port));
172 errx(1, "gpio: %s: not a valid argument", argv[i]);
180 * compile-command: "gcc -Wall -g -O2 -march=native -c -o gpio.o gpio.c && gcc -o gpio gpio.o"