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-hammer -n <device-name> -o <offset1> -o <offset2>
24 #include <sys/ioctl.h>
25 #include <linux/gpio.h>
27 int hammer_device(const char *device_name, unsigned int *lines, int nlines,
30 struct gpiohandle_request req;
31 struct gpiohandle_data data;
33 char swirr[] = "-\\|/";
37 unsigned int iteration = 0;
39 ret = asprintf(&chrdev_name, "/dev/%s", device_name);
43 fd = open(chrdev_name, 0);
46 fprintf(stderr, "Failed to open %s\n", chrdev_name);
47 goto exit_close_error;
50 /* Request lines as output */
51 for (i = 0; i < nlines; i++)
52 req.lineoffsets[i] = lines[i];
53 req.flags = GPIOHANDLE_REQUEST_OUTPUT; /* Request as output */
54 strcpy(req.consumer_label, "gpio-hammer");
56 ret = ioctl(fd, GPIO_GET_LINEHANDLE_IOCTL, &req);
59 fprintf(stderr, "Failed to issue GET LINEHANDLE "
62 goto exit_close_error;
65 /* Read initial states */
66 ret = ioctl(req.fd, GPIOHANDLE_GET_LINE_VALUES_IOCTL, &data);
69 fprintf(stderr, "Failed to issue GPIOHANDLE GET LINE "
70 "VALUES IOCTL (%d)\n",
72 goto exit_close_error;
74 fprintf(stdout, "Hammer lines [");
75 for (i = 0; i < nlines; i++) {
76 fprintf(stdout, "%d", lines[i]);
77 if (i != (nlines - 1))
78 fprintf(stdout, ", ");
80 fprintf(stdout, "] on %s, initial states: [", device_name);
81 for (i = 0; i < nlines; i++) {
82 fprintf(stdout, "%d", data.values[i]);
83 if (i != (nlines - 1))
84 fprintf(stdout, ", ");
86 fprintf(stdout, "]\n");
91 /* Invert all lines so we blink */
92 for (i = 0; i < nlines; i++)
93 data.values[i] = !data.values[i];
95 ret = ioctl(req.fd, GPIOHANDLE_SET_LINE_VALUES_IOCTL, &data);
98 fprintf(stderr, "Failed to issue GPIOHANDLE SET LINE "
99 "VALUES IOCTL (%d)\n",
101 goto exit_close_error;
103 /* Re-read values to get status */
104 ret = ioctl(req.fd, GPIOHANDLE_GET_LINE_VALUES_IOCTL, &data);
107 fprintf(stderr, "Failed to issue GPIOHANDLE GET LINE "
108 "VALUES IOCTL (%d)\n",
110 goto exit_close_error;
113 fprintf(stdout, "[%c] ", swirr[j]);
115 if (j == sizeof(swirr)-1)
118 fprintf(stdout, "[");
119 for (i = 0; i < nlines; i++) {
120 fprintf(stdout, "%d: %d", lines[i], data.values[i]);
121 if (i != (nlines - 1))
122 fprintf(stdout, ", ");
124 fprintf(stdout, "]\r");
128 if (loops && iteration == loops)
131 fprintf(stdout, "\n");
136 perror("Failed to close GPIO character device file");
141 void print_usage(void)
143 fprintf(stderr, "Usage: gpio-hammer [options]...\n"
144 "Hammer GPIO lines, 0->1->0->1...\n"
145 " -n <name> Hammer GPIOs on a named device (must be stated)\n"
146 " -o <n> Offset[s] to hammer, at least one, several can be stated\n"
147 " [-c <n>] Do <n> loops (optional, infinite loop if not stated)\n"
148 " -? This helptext\n"
151 "gpio-hammer -n gpiochip0 -o 4\n"
155 int main(int argc, char **argv)
157 const char *device_name = NULL;
158 unsigned int lines[GPIOHANDLES_MAX];
159 unsigned int loops = 0;
165 while ((c = getopt(argc, argv, "c:n:o:?")) != -1) {
168 loops = strtoul(optarg, NULL, 10);
171 device_name = optarg;
174 lines[i] = strtoul(optarg, NULL, 10);
184 if (!device_name || !nlines) {
188 return hammer_device(device_name, lines, nlines, loops);