From: Fredrik Tolf Date: Fri, 14 Sep 2018 18:44:29 +0000 (+0200) Subject: gpio: Added port-number mapping. X-Git-Url: http://git.dolda2000.com/gitweb/?a=commitdiff_plain;h=cc450aa9fb0792ca0c5ae05f648e04dba0fc19d1;hp=3564716794ca62ecc5f8368f8e78f6eb0374ad5d;p=utils.git gpio: Added port-number mapping. --- diff --git a/gpio.c b/gpio.c index babc061..1d8b322 100644 --- a/gpio.c +++ b/gpio.c @@ -6,13 +6,36 @@ #include #include +static const int bmap[] = { + -1, + -1, -1, 2, -1, 3, -1, 4, 14, -1, 15, 17, 18, 27, -1, 22, 23, -1, 24, 10, -1, + 9, 25, 11, 8, -1, 7, -1, -1, 5, -1, 6, 12, 13, -1, 19, 16, 21, 20, -1, 21, +}; +static const int imap[] = { + -1, -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, -1, 27, +}; +static const int *umap = bmap; +static int mapn = sizeof(bmap) / sizeof(*bmap); + +static int mappin(int num) +{ + if((num < 0) || (num >= mapn)) + return(-1); + return(umap[num]); +} + static void setport(int p, int v) { char path[256], line[256]; FILE *fp; - int rv; + int rv, rp; - sprintf(path, "/sys/class/gpio/gpio%i/direction", p); + if((rp = mappin(p)) < 0) { + fprintf(stderr, "gpio: %i: no such port\n", p); + exit(1); + } + sprintf(path, "/sys/class/gpio/gpio%i/direction", rp); if((fp = fopen(path, "r")) == NULL) err(1, "%s", path); rv = !!fgets(line, sizeof(line), fp); @@ -22,24 +45,47 @@ static void setport(int p, int v) err(1, "%s", path); fprintf(fp, "out\n"); fflush(fp); if(ferror(fp)) - errx(1, "gpio%i: could not set to output", p); + errx(1, "gpio%i: could not set to output", rp); fclose(fp); } - sprintf(path, "/sys/class/gpio/gpio%i/value", p); + sprintf(path, "/sys/class/gpio/gpio%i/value", rp); if((fp = fopen(path, "w")) == NULL) err(1, "%s", path); fprintf(fp, "%i\n", v); fflush(fp); if(ferror(fp)) - errx(1, "gpio%i: could not set value", p); + errx(1, "gpio%i: could not set value", rp); fclose(fp); } +static void usage(FILE *out) +{ + fprintf(out, "usage: gpio [-hi] PORT=VAL...\n"); +} + int main(int argc, char **argv) { - int i, port, val; + int c, i, port, val; char *p, *e; - for(i = 1; i < argc; i++) { + while((c = getopt(argc, argv, "hi")) >= 0) { + switch(c) { + case 'h': + usage(stdout); + return(0); + case 'i': + umap = imap; + mapn = sizeof(imap) / sizeof(*imap); + break; + default: + usage(stderr); + return(1); + } + } + if(optind >= argc) { + usage(stderr); + return(1); + } + for(i = optind; i < argc; i++) { if((p = strchr(argv[i], '=')) == NULL) { fprintf(stderr, "gpio: %s: not of the form PORT=VAL\n", argv[i]); exit(1); @@ -58,3 +104,9 @@ int main(int argc, char **argv) } return(0); } + +/* + * Local Variables: + * compile-command: "gcc -Wall -g -O2 -march=native -o gpio gpio.c" + * End: + */