2 * gpio-hammer - example swiss army knife to shake GPIO lines on a system
4 * Copyright (C) 2016 Linus Walleij
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
11 * gpio-event-mon -n <device-name> -o <offset>
25 #include <sys/ioctl.h>
26 #include <linux/gpio.h>
28 int monitor_device(const char *device_name,
30 u_int32_t handleflags,
34 struct gpioevent_request req;
35 struct gpiohandle_data data;
41 ret = asprintf(&chrdev_name, "/dev/%s", device_name);
45 fd = open(chrdev_name, 0);
48 fprintf(stderr, "Failed to open %s\n", chrdev_name);
49 goto exit_close_error;
52 req.lineoffset = line;
53 req.handleflags = handleflags;
54 req.eventflags = eventflags;
55 strcpy(req.consumer_label, "gpio-event-mon");
57 ret = ioctl(fd, GPIO_GET_LINEEVENT_IOCTL, &req);
60 fprintf(stderr, "Failed to issue GET EVENT "
63 goto exit_close_error;
66 /* Read initial states */
67 ret = ioctl(req.fd, GPIOHANDLE_GET_LINE_VALUES_IOCTL, &data);
70 fprintf(stderr, "Failed to issue GPIOHANDLE GET LINE "
71 "VALUES IOCTL (%d)\n",
73 goto exit_close_error;
76 fprintf(stdout, "Monitoring line %d on %s\n", line, device_name);
77 fprintf(stdout, "Initial line value: %d\n", data.values[0]);
80 struct gpioevent_data event;
82 ret = read(req.fd, &event, sizeof(event));
84 if (errno == -EAGAIN) {
85 fprintf(stderr, "nothing available\n");
89 fprintf(stderr, "Failed to read event (%d)\n",
95 if (ret != sizeof(event)) {
96 fprintf(stderr, "Reading event failed\n");
100 fprintf(stdout, "GPIO EVENT %" PRIu64 ": ", event.timestamp);
102 case GPIOEVENT_EVENT_RISING_EDGE:
103 fprintf(stdout, "rising edge");
105 case GPIOEVENT_EVENT_FALLING_EDGE:
106 fprintf(stdout, "falling edge");
109 fprintf(stdout, "unknown event");
111 fprintf(stdout, "\n");
120 perror("Failed to close GPIO character device file");
125 void print_usage(void)
127 fprintf(stderr, "Usage: gpio-event-mon [options]...\n"
128 "Listen to events on GPIO lines, 0->1 1->0\n"
129 " -n <name> Listen on GPIOs on a named device (must be stated)\n"
130 " -o <n> Offset to monitor\n"
131 " -d Set line as open drain\n"
132 " -s Set line as open source\n"
133 " -r Listen for rising edges\n"
134 " -f Listen for falling edges\n"
135 " [-c <n>] Do <n> loops (optional, infinite loop if not stated)\n"
136 " -? This helptext\n"
139 "gpio-event-mon -n gpiochip0 -o 4 -r -f\n"
143 int main(int argc, char **argv)
145 const char *device_name = NULL;
146 unsigned int line = -1;
147 unsigned int loops = 0;
148 u_int32_t handleflags = GPIOHANDLE_REQUEST_INPUT;
149 u_int32_t eventflags = 0;
152 while ((c = getopt(argc, argv, "c:n:o:dsrf?")) != -1) {
155 loops = strtoul(optarg, NULL, 10);
158 device_name = optarg;
161 line = strtoul(optarg, NULL, 10);
164 handleflags |= GPIOHANDLE_REQUEST_OPEN_DRAIN;
167 handleflags |= GPIOHANDLE_REQUEST_OPEN_SOURCE;
170 eventflags |= GPIOEVENT_REQUEST_RISING_EDGE;
173 eventflags |= GPIOEVENT_REQUEST_FALLING_EDGE;
181 if (!device_name || line == -1) {
186 printf("No flags specified, listening on both rising and "
188 eventflags = GPIOEVENT_REQUEST_BOTH_EDGES;
190 return monitor_device(device_name, line, handleflags,