Import userland utility for playing with gpio devices. From OpenBSD.
This commit is contained in:
parent
7ad695546c
commit
825ff1848a
@ -1,4 +1,4 @@
|
||||
# $NetBSD: Makefile,v 1.205 2005/08/28 19:37:58 thorpej Exp $
|
||||
# $NetBSD: Makefile,v 1.206 2005/09/27 02:54:27 jmcneill Exp $
|
||||
# from: @(#)Makefile 5.20 (Berkeley) 6/12/93
|
||||
|
||||
.include <bsd.own.mk>
|
||||
@ -6,8 +6,8 @@
|
||||
SUBDIR= ac accton altq amd apm apmd arp bad144 bind bootp catman \
|
||||
chown chroot chrtbl cnwctl cron dev_mkdb \
|
||||
dhcp diskpart dumpfs dumplfs edquota eeprom \
|
||||
envstat eshconfig etcupdate extattrctl fssconfig fwctl grfconfig \
|
||||
grfinfo gspa hilinfo ifwatchd inetd installboot \
|
||||
envstat eshconfig etcupdate extattrctl fssconfig fwctl gpioctl \
|
||||
grfconfig grfinfo gspa hilinfo ifwatchd inetd installboot \
|
||||
iopctl iostat ipwctl irdaattach isdn iteconfig iwictl\
|
||||
kgmon lastlogin link lpr mailwrapper makefs \
|
||||
map-mbone mdconfig memswitch mlxctl mopd mountd moused \
|
||||
|
8
usr.sbin/gpioctl/Makefile
Normal file
8
usr.sbin/gpioctl/Makefile
Normal file
@ -0,0 +1,8 @@
|
||||
# $NetBSD: Makefile,v 1.1 2005/09/27 02:54:27 jmcneill Exp $
|
||||
|
||||
PROG= gpioctl
|
||||
SRCS= gpioctl.c
|
||||
|
||||
MAN= gpioctl.8
|
||||
|
||||
.include <bsd.prog.mk>
|
175
usr.sbin/gpioctl/gpioctl.8
Normal file
175
usr.sbin/gpioctl/gpioctl.8
Normal file
@ -0,0 +1,175 @@
|
||||
.\" $NetBSD: gpioctl.8,v 1.1 2005/09/27 02:54:27 jmcneill Exp $
|
||||
.\" $OpenBSD: gpioctl.8,v 1.5 2004/12/02 05:11:40 grange 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.
|
||||
.\"
|
||||
.Dd September 26, 2005
|
||||
.Dt GPIOCTL 8
|
||||
.Os
|
||||
.Sh NAME
|
||||
.Nm gpioctl
|
||||
.Nd control GPIO devices
|
||||
.Sh SYNOPSIS
|
||||
.Nm gpioctl
|
||||
.Op Fl hq
|
||||
.Op Fl d Ar device
|
||||
.Op Ar pin
|
||||
.Op Ar 0 | 1 | 2
|
||||
.Nm gpioctl
|
||||
.Op Fl hq
|
||||
.Op Fl d Ar device
|
||||
.Fl c
|
||||
.Ar pin
|
||||
.Op Ar flags
|
||||
.Sh DESCRIPTION
|
||||
The
|
||||
.Nm
|
||||
program allows manipulation of
|
||||
.Tn GPIO
|
||||
(General Purpose Input/Output) device pins.
|
||||
Such devices can be either part of the chipset or embedded
|
||||
.Tn CPU ,
|
||||
or a separate chip.
|
||||
The usual way of using
|
||||
.Tn GPIO
|
||||
is to connect some simple devices such as LEDs, 1-wire thermal sensors,
|
||||
etc., to its pins.
|
||||
.Pp
|
||||
Each
|
||||
.Tn GPIO
|
||||
device has an associated device file in the
|
||||
.Pa /dev
|
||||
directory.
|
||||
By default
|
||||
.Nm
|
||||
uses
|
||||
.Pa /dev/gpio0 ,
|
||||
which corresponds to the first found
|
||||
.Tn GPIO
|
||||
device in the system.
|
||||
If more than one
|
||||
.Tn GPIO
|
||||
device is present, an alternative device file can be specified with the
|
||||
.Fl d
|
||||
option
|
||||
in order to access a particular
|
||||
.Tn GPIO
|
||||
device.
|
||||
.Pp
|
||||
When executed without any arguments,
|
||||
.Nm
|
||||
reads information about the
|
||||
.Tn GPIO
|
||||
device and displays it.
|
||||
.Pp
|
||||
.Tn GPIO
|
||||
pins can be either
|
||||
.Dq read
|
||||
or
|
||||
.Dq written
|
||||
with the values of logical 0 or 1.
|
||||
If only a
|
||||
.Ar pin
|
||||
number is specified on the command line, the pin state will be read
|
||||
from the
|
||||
.Tn GPIO
|
||||
controller and displayed.
|
||||
To write to a pin, a value must be specified after the
|
||||
.Ar pin
|
||||
number.
|
||||
Values can be either 0 or 1.
|
||||
A value of 2 has a special meaning: it
|
||||
.Dq toggles
|
||||
the pin, i.e. changes its state to the opposite.
|
||||
.Pp
|
||||
Each pin can be configured with different flags with the
|
||||
.Fl c
|
||||
option.
|
||||
The following configuration flags are supported by the
|
||||
.Tn GPIO
|
||||
framework:
|
||||
.Pp
|
||||
.Bl -tag -width XXXXXXX -offset indent -compact
|
||||
.It in
|
||||
input direction
|
||||
.It out
|
||||
output direction
|
||||
.It inout
|
||||
bi-directional
|
||||
.It od
|
||||
open-drain output
|
||||
.It pp
|
||||
push-pull output
|
||||
.It tri
|
||||
tri-state (output disabled)
|
||||
.It pu
|
||||
internal pull-up enabled
|
||||
.El
|
||||
.Pp
|
||||
Note that not all the flags can be supported by the particular
|
||||
.Tn GPIO
|
||||
controller.
|
||||
The list of supported flags is always displayed when executing
|
||||
.Nm
|
||||
with the
|
||||
.Fl c
|
||||
option.
|
||||
If only a
|
||||
.Ar pin
|
||||
number is specified on the command line, the current pin flags will be
|
||||
displayed.
|
||||
To change pin flags, a new flags set separated by spaces must be
|
||||
specified after the
|
||||
.Ar pin
|
||||
number.
|
||||
.Pp
|
||||
The
|
||||
.Fl q
|
||||
option causes
|
||||
.Nm
|
||||
to operate quietly i.e. nothing is printed to stdout.
|
||||
The
|
||||
.Fl h
|
||||
option displays usage summary.
|
||||
.Sh FILES
|
||||
.Bl -tag -width "/dev/gpiou" -compact
|
||||
.It /dev/gpio Ns Ar u
|
||||
GPIO device unit
|
||||
.Ar u
|
||||
file.
|
||||
.El
|
||||
.Sh EXAMPLES
|
||||
Configure pin 20 to have push-pull output:
|
||||
.Pp
|
||||
.Dl # gpioctl -c 20 out pp
|
||||
.Pp
|
||||
Write logical 1 to pin 20:
|
||||
.Pp
|
||||
.Dl # gpioctl 20 1
|
||||
.Sh SEE ALSO
|
||||
.Xr elansc 4 ,
|
||||
.Xr gpio 4 ,
|
||||
.Xr gscpcib 4 ,
|
||||
.Xr nsclpcsio 4
|
||||
.Sh HISTORY
|
||||
The
|
||||
.Nm
|
||||
command first appeared in
|
||||
.Ox 3.6 .
|
||||
.Sh AUTHORS
|
||||
The
|
||||
.Nm
|
||||
program was written by
|
||||
.An Alexander Yurchenko Aq grange@openbsd.org .
|
232
usr.sbin/gpioctl/gpioctl.c
Normal file
232
usr.sbin/gpioctl/gpioctl.c
Normal file
@ -0,0 +1,232 @@
|
||||
/* $NetBSD: gpioctl.c,v 1.1 2005/09/27 02:54:27 jmcneill Exp $ */
|
||||
/* $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"
|
||||
|
||||
char *device = _PATH_DEV_GPIO;
|
||||
int devfd = -1;
|
||||
int quiet = 0;
|
||||
|
||||
void getinfo(void);
|
||||
void pinread(int);
|
||||
void pinwrite(int, int);
|
||||
void pinctl(int, char *[], int);
|
||||
|
||||
__dead void usage(void);
|
||||
|
||||
const struct bitstr {
|
||||
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" },
|
||||
{ 0, NULL },
|
||||
};
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int ch;
|
||||
char *ep;
|
||||
int do_ctl = 0;
|
||||
int pin = 0, value = 0;
|
||||
|
||||
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)
|
||||
errx(1, "%s: invalid pin", argv[0]);
|
||||
}
|
||||
|
||||
if ((devfd = open(device, O_RDWR)) == -1)
|
||||
err(1, "%s", device);
|
||||
|
||||
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')
|
||||
errx(1, "%s: invalid value", argv[1]);
|
||||
pinwrite(pin, value);
|
||||
}
|
||||
} else {
|
||||
usage();
|
||||
/* NOTREACHED */
|
||||
}
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
void
|
||||
getinfo(void)
|
||||
{
|
||||
struct gpio_info info;
|
||||
|
||||
bzero(&info, sizeof(info));
|
||||
if (ioctl(devfd, GPIOINFO, &info) == -1)
|
||||
err(1, "GPIOINFO");
|
||||
|
||||
if (quiet)
|
||||
return;
|
||||
|
||||
printf("%s: %d pins\n", device, info.gpio_npins);
|
||||
}
|
||||
|
||||
void
|
||||
pinread(int pin)
|
||||
{
|
||||
struct gpio_pin_op op;
|
||||
|
||||
bzero(&op, sizeof(op));
|
||||
op.gp_pin = pin;
|
||||
if (ioctl(devfd, GPIOPINREAD, &op) == -1)
|
||||
err(1, "GPIOPINREAD");
|
||||
|
||||
if (quiet)
|
||||
return;
|
||||
|
||||
printf("pin %d: state %d\n", pin, op.gp_value);
|
||||
}
|
||||
|
||||
void
|
||||
pinwrite(int pin, int value)
|
||||
{
|
||||
struct gpio_pin_op op;
|
||||
|
||||
if (value < 0 || value > 2)
|
||||
errx(1, "%d: invalid value", value);
|
||||
|
||||
bzero(&op, sizeof(op));
|
||||
op.gp_pin = pin;
|
||||
op.gp_value = (value == 0 ? GPIO_PIN_LOW : GPIO_PIN_HIGH);
|
||||
if (value < 2) {
|
||||
if (ioctl(devfd, GPIOPINWRITE, &op) == -1)
|
||||
err(1, "GPIOPINWRITE");
|
||||
} else {
|
||||
if (ioctl(devfd, GPIOPINTOGGLE, &op) == -1)
|
||||
err(1, "GPIOPINTOGGLE");
|
||||
}
|
||||
|
||||
if (quiet)
|
||||
return;
|
||||
|
||||
printf("pin %d: state %d -> %d\n", pin, op.gp_value,
|
||||
(value < 2 ? value : 1 - op.gp_value));
|
||||
}
|
||||
|
||||
void
|
||||
pinctl(int pin, char *flags[], int nflags)
|
||||
{
|
||||
struct gpio_pin_ctl ctl;
|
||||
int fl = 0;
|
||||
const struct bitstr *bs;
|
||||
int i;
|
||||
|
||||
bzero(&ctl, sizeof(ctl));
|
||||
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)
|
||||
err(1, "GPIOPINCTL");
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
void
|
||||
usage(void)
|
||||
{
|
||||
extern char *__progname;
|
||||
|
||||
fprintf(stderr, "usage: %s [-hq] [-d device] [pin] [0 | 1 | 2]\n",
|
||||
__progname);
|
||||
fprintf(stderr, " %s [-hq] [-d device] -c pin [flags]\n",
|
||||
__progname);
|
||||
|
||||
exit(1);
|
||||
}
|
Loading…
Reference in New Issue
Block a user