2008-01-09 19:08:33 +03:00
|
|
|
/* $NetBSD: gpioctl.c,v 1.5 2008/01/09 16:08:33 xtraeme Exp $ */
|
2005-09-27 06:54:27 +04:00
|
|
|
/* $OpenBSD: gpioctl.c,v 1.2 2004/08/08 00:05:09 deraadt Exp $ */
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2004 Alexander Yurchenko <grange@openbsd.org>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Program to control GPIO devices.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/gpio.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#define _PATH_DEV_GPIO "/dev/gpio0"
|
|
|
|
|
2008-01-09 19:06:21 +03:00
|
|
|
static const char *device = _PATH_DEV_GPIO;
|
|
|
|
static int devfd = -1;
|
|
|
|
static int quiet = 0;
|
2005-09-27 06:54:27 +04:00
|
|
|
|
2008-01-09 19:06:21 +03:00
|
|
|
static void getinfo(void);
|
|
|
|
static void pinread(int);
|
|
|
|
static void pinwrite(int, int);
|
|
|
|
static void pinctl(int, char *[], int);
|
|
|
|
static void usage(void);
|
2005-09-27 06:54:27 +04:00
|
|
|
|
2008-01-09 19:06:21 +03:00
|
|
|
static const struct bitstr {
|
2005-09-27 06:54:27 +04:00
|
|
|
unsigned int mask;
|
|
|
|
const char *string;
|
|
|
|
} pinflags[] = {
|
|
|
|
{ GPIO_PIN_INPUT, "in" },
|
|
|
|
{ GPIO_PIN_OUTPUT, "out" },
|
|
|
|
{ GPIO_PIN_INOUT, "inout" },
|
|
|
|
{ GPIO_PIN_OPENDRAIN, "od" },
|
|
|
|
{ GPIO_PIN_PUSHPULL, "pp" },
|
|
|
|
{ GPIO_PIN_TRISTATE, "tri" },
|
|
|
|
{ GPIO_PIN_PULLUP, "pu" },
|
2008-01-09 18:56:27 +03:00
|
|
|
{ GPIO_PIN_PULLDOWN, "pd" },
|
|
|
|
{ GPIO_PIN_INVIN, "iin" },
|
|
|
|
{ GPIO_PIN_INVOUT, "iiout" },
|
2005-09-27 06:54:27 +04:00
|
|
|
{ 0, NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
char *ep;
|
|
|
|
int do_ctl = 0;
|
|
|
|
int pin = 0, value = 0;
|
|
|
|
|
2008-01-09 19:06:21 +03:00
|
|
|
setprogname(argv[0]);
|
|
|
|
|
2005-09-27 06:54:27 +04:00
|
|
|
while ((ch = getopt(argc, argv, "cd:hq")) != -1)
|
|
|
|
switch (ch) {
|
|
|
|
case 'c':
|
|
|
|
do_ctl = 1;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
device = optarg;
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
quiet = 1;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (argc > 0) {
|
|
|
|
pin = strtol(argv[0], &ep, 10);
|
|
|
|
if (*argv[0] == '\0' || *ep != '\0' || pin < 0)
|
2008-01-09 19:06:21 +03:00
|
|
|
errx(EXIT_FAILURE, "%s: invalid pin", argv[0]);
|
2005-09-27 06:54:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((devfd = open(device, O_RDWR)) == -1)
|
2008-01-09 19:06:21 +03:00
|
|
|
err(EXIT_FAILURE, "%s", device);
|
2005-09-27 06:54:27 +04:00
|
|
|
|
|
|
|
if (argc == 0 && !do_ctl) {
|
|
|
|
getinfo();
|
|
|
|
} else if (argc == 1) {
|
|
|
|
if (do_ctl)
|
|
|
|
pinctl(pin, NULL, 0);
|
|
|
|
else
|
|
|
|
pinread(pin);
|
|
|
|
} else if (argc > 1) {
|
|
|
|
if (do_ctl) {
|
|
|
|
pinctl(pin, argv + 1, argc - 1);
|
|
|
|
} else {
|
|
|
|
value = strtol(argv[1], &ep, 10);
|
|
|
|
if (*argv[1] == '\0' || *ep != '\0')
|
2008-01-09 19:06:21 +03:00
|
|
|
errx(EXIT_FAILURE, "%s: invalid value",
|
|
|
|
argv[1]);
|
2005-09-27 06:54:27 +04:00
|
|
|
pinwrite(pin, value);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
usage();
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
2008-01-09 19:06:21 +03:00
|
|
|
return EXIT_SUCCESS;
|
2005-09-27 06:54:27 +04:00
|
|
|
}
|
|
|
|
|
2008-01-09 19:08:33 +03:00
|
|
|
static void
|
2005-09-27 06:54:27 +04:00
|
|
|
getinfo(void)
|
|
|
|
{
|
|
|
|
struct gpio_info info;
|
|
|
|
|
2008-01-09 19:06:21 +03:00
|
|
|
memset(&info, 0, sizeof(info));
|
2005-09-27 06:54:27 +04:00
|
|
|
if (ioctl(devfd, GPIOINFO, &info) == -1)
|
2008-01-09 19:06:21 +03:00
|
|
|
err(EXIT_FAILURE, "GPIOINFO");
|
2005-09-27 06:54:27 +04:00
|
|
|
|
|
|
|
if (quiet)
|
|
|
|
return;
|
|
|
|
|
|
|
|
printf("%s: %d pins\n", device, info.gpio_npins);
|
|
|
|
}
|
|
|
|
|
2008-01-09 19:08:33 +03:00
|
|
|
static void
|
2005-09-27 06:54:27 +04:00
|
|
|
pinread(int pin)
|
|
|
|
{
|
|
|
|
struct gpio_pin_op op;
|
|
|
|
|
2008-01-09 19:06:21 +03:00
|
|
|
memset(&op, 0, sizeof(op));
|
2005-09-27 06:54:27 +04:00
|
|
|
op.gp_pin = pin;
|
|
|
|
if (ioctl(devfd, GPIOPINREAD, &op) == -1)
|
2008-01-09 19:06:21 +03:00
|
|
|
err(EXIT_FAILURE, "GPIOPINREAD");
|
2005-09-27 06:54:27 +04:00
|
|
|
|
|
|
|
if (quiet)
|
|
|
|
return;
|
|
|
|
|
|
|
|
printf("pin %d: state %d\n", pin, op.gp_value);
|
|
|
|
}
|
|
|
|
|
2008-01-09 19:08:33 +03:00
|
|
|
static void
|
2005-09-27 06:54:27 +04:00
|
|
|
pinwrite(int pin, int value)
|
|
|
|
{
|
|
|
|
struct gpio_pin_op op;
|
|
|
|
|
|
|
|
if (value < 0 || value > 2)
|
2008-01-09 19:06:21 +03:00
|
|
|
errx(EXIT_FAILURE, "%d: invalid value", value);
|
2005-09-27 06:54:27 +04:00
|
|
|
|
2008-01-09 19:06:21 +03:00
|
|
|
memset(&op, 0, sizeof(op));
|
2005-09-27 06:54:27 +04:00
|
|
|
op.gp_pin = pin;
|
|
|
|
op.gp_value = (value == 0 ? GPIO_PIN_LOW : GPIO_PIN_HIGH);
|
|
|
|
if (value < 2) {
|
|
|
|
if (ioctl(devfd, GPIOPINWRITE, &op) == -1)
|
2008-01-09 19:06:21 +03:00
|
|
|
err(EXIT_FAILURE, "GPIOPINWRITE");
|
2005-09-27 06:54:27 +04:00
|
|
|
} else {
|
|
|
|
if (ioctl(devfd, GPIOPINTOGGLE, &op) == -1)
|
2008-01-09 19:06:21 +03:00
|
|
|
err(EXIT_FAILURE, "GPIOPINTOGGLE");
|
2005-09-27 06:54:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (quiet)
|
|
|
|
return;
|
|
|
|
|
|
|
|
printf("pin %d: state %d -> %d\n", pin, op.gp_value,
|
|
|
|
(value < 2 ? value : 1 - op.gp_value));
|
|
|
|
}
|
|
|
|
|
2008-01-09 19:06:21 +03:00
|
|
|
static void
|
2005-09-27 06:54:27 +04:00
|
|
|
pinctl(int pin, char *flags[], int nflags)
|
|
|
|
{
|
|
|
|
struct gpio_pin_ctl ctl;
|
|
|
|
int fl = 0;
|
|
|
|
const struct bitstr *bs;
|
|
|
|
int i;
|
|
|
|
|
2008-01-09 19:06:21 +03:00
|
|
|
memset(&ctl, 0, sizeof(ctl));
|
2005-09-27 06:54:27 +04:00
|
|
|
ctl.gp_pin = pin;
|
|
|
|
if (flags != NULL) {
|
|
|
|
for (i = 0; i < nflags; i++)
|
|
|
|
for (bs = pinflags; bs->string != NULL; bs++)
|
|
|
|
if (strcmp(flags[i], bs->string) == 0) {
|
|
|
|
fl |= bs->mask;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ctl.gp_flags = fl;
|
|
|
|
if (ioctl(devfd, GPIOPINCTL, &ctl) == -1)
|
2008-01-09 19:06:21 +03:00
|
|
|
err(EXIT_FAILURE, "GPIOPINCTL");
|
2005-09-27 06:54:27 +04:00
|
|
|
|
|
|
|
if (quiet)
|
|
|
|
return;
|
|
|
|
|
|
|
|
printf("pin %d: caps:", pin);
|
|
|
|
for (bs = pinflags; bs->string != NULL; bs++)
|
|
|
|
if (ctl.gp_caps & bs->mask)
|
|
|
|
printf(" %s", bs->string);
|
|
|
|
printf(", flags:");
|
|
|
|
for (bs = pinflags; bs->string != NULL; bs++)
|
|
|
|
if (ctl.gp_flags & bs->mask)
|
|
|
|
printf(" %s", bs->string);
|
|
|
|
if (fl > 0) {
|
|
|
|
printf(" ->");
|
|
|
|
for (bs = pinflags; bs->string != NULL; bs++)
|
|
|
|
if (fl & bs->mask)
|
|
|
|
printf(" %s", bs->string);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2008-01-09 19:06:21 +03:00
|
|
|
static void
|
2005-09-27 06:54:27 +04:00
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "usage: %s [-hq] [-d device] [pin] [0 | 1 | 2]\n",
|
2008-01-09 19:06:21 +03:00
|
|
|
getprogname());
|
2005-09-27 06:54:27 +04:00
|
|
|
fprintf(stderr, " %s [-hq] [-d device] -c pin [flags]\n",
|
2008-01-09 19:06:21 +03:00
|
|
|
getprogname());
|
2005-09-27 06:54:27 +04:00
|
|
|
|
2008-01-09 19:06:21 +03:00
|
|
|
exit(EXIT_FAILURE);
|
2005-09-27 06:54:27 +04:00
|
|
|
}
|