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);
21 static int mappin(int num)
23 if((num < 0) || (num >= mapn))
28 static void setport(int p, int v)
30 char path[256], line[256];
34 if((rp = mappin(p)) < 0) {
35 fprintf(stderr, "gpio: %i: no such port\n", p);
38 sprintf(path, "/sys/class/gpio/gpio%i/direction", rp);
39 if((fp = fopen(path, "r")) == NULL)
41 rv = !!fgets(line, sizeof(line), fp);
43 if(!rv || strcmp(line, "out\n")) {
44 if((fp = fopen(path, "w")) == NULL)
46 fprintf(fp, "out\n"); fflush(fp);
48 errx(1, "gpio%i: could not set to output", rp);
51 sprintf(path, "/sys/class/gpio/gpio%i/value", rp);
52 if((fp = fopen(path, "w")) == NULL)
54 fprintf(fp, "%i\n", v); fflush(fp);
56 errx(1, "gpio%i: could not set value", rp);
60 static void usage(FILE *out)
62 fprintf(out, "usage: gpio [-hi] PORT=VAL...\n");
65 int main(int argc, char **argv)
70 while((c = getopt(argc, argv, "hi")) >= 0) {
77 mapn = sizeof(imap) / sizeof(*imap);
88 for(i = optind; i < argc; i++) {
89 if((p = strchr(argv[i], '=')) == NULL) {
90 fprintf(stderr, "gpio: %s: not of the form PORT=VAL\n", argv[i]);
93 port = strtol(argv[i], &e, 10);
95 fprintf(stderr, "gpio: %s: not of the form PORT=VAL\n", argv[i]);
98 val = strtol(p + 1, &e, 10);
100 fprintf(stderr, "gpio: %s: not of the form PORT=VAL\n", argv[i]);
110 * compile-command: "gcc -Wall -g -O2 -march=native -o gpio gpio.c"