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>
26 #include "gpio-utils.h"
28 int hammer_device(const char *device_name, unsigned int *lines, int nlines,
31 struct gpiohandle_data data;
32 char swirr[] = "-\\|/";
36 unsigned int iteration = 0;
38 memset(&data.values, 0, sizeof(data.values));
39 ret = gpiotools_request_linehandle(device_name, lines, nlines,
40 GPIOHANDLE_REQUEST_OUTPUT, &data,
47 ret = gpiotools_get_values(fd, &data);
49 goto exit_close_error;
51 fprintf(stdout, "Hammer lines [");
52 for (i = 0; i < nlines; i++) {
53 fprintf(stdout, "%d", lines[i]);
54 if (i != (nlines - 1))
55 fprintf(stdout, ", ");
57 fprintf(stdout, "] on %s, initial states: [", device_name);
58 for (i = 0; i < nlines; i++) {
59 fprintf(stdout, "%d", data.values[i]);
60 if (i != (nlines - 1))
61 fprintf(stdout, ", ");
63 fprintf(stdout, "]\n");
68 /* Invert all lines so we blink */
69 for (i = 0; i < nlines; i++)
70 data.values[i] = !data.values[i];
72 ret = gpiotools_set_values(fd, &data);
74 goto exit_close_error;
76 /* Re-read values to get status */
77 ret = gpiotools_get_values(fd, &data);
79 goto exit_close_error;
81 fprintf(stdout, "[%c] ", swirr[j]);
83 if (j == sizeof(swirr)-1)
87 for (i = 0; i < nlines; i++) {
88 fprintf(stdout, "%d: %d", lines[i], data.values[i]);
89 if (i != (nlines - 1))
90 fprintf(stdout, ", ");
92 fprintf(stdout, "]\r");
96 if (loops && iteration == loops)
99 fprintf(stdout, "\n");
103 gpiotools_release_linehandle(fd);
108 void print_usage(void)
110 fprintf(stderr, "Usage: gpio-hammer [options]...\n"
111 "Hammer GPIO lines, 0->1->0->1...\n"
112 " -n <name> Hammer GPIOs on a named device (must be stated)\n"
113 " -o <n> Offset[s] to hammer, at least one, several can be stated\n"
114 " [-c <n>] Do <n> loops (optional, infinite loop if not stated)\n"
115 " -? This helptext\n"
118 "gpio-hammer -n gpiochip0 -o 4\n"
122 int main(int argc, char **argv)
124 const char *device_name = NULL;
125 unsigned int lines[GPIOHANDLES_MAX];
126 unsigned int loops = 0;
132 while ((c = getopt(argc, argv, "c:n:o:?")) != -1) {
135 loops = strtoul(optarg, NULL, 10);
138 device_name = optarg;
141 lines[i] = strtoul(optarg, NULL, 10);
151 if (!device_name || !nlines) {
155 return hammer_device(device_name, lines, nlines, loops);