Linux Audio

Check our new training course

Loading...
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * gpio-hammer - example swiss army knife to shake GPIO lines on a system
  4 *
  5 * Copyright (C) 2016 Linus Walleij
  6 *
 
 
 
 
  7 * Usage:
  8 *	gpio-hammer -n <device-name> -o <offset1> -o <offset2>
  9 */
 10
 11#include <unistd.h>
 12#include <stdlib.h>
 13#include <stdbool.h>
 14#include <stdio.h>
 15#include <dirent.h>
 16#include <errno.h>
 17#include <string.h>
 18#include <poll.h>
 19#include <fcntl.h>
 20#include <getopt.h>
 21#include <sys/ioctl.h>
 22#include <linux/gpio.h>
 23#include "gpio-utils.h"
 24
 25int hammer_device(const char *device_name, unsigned int *lines, int num_lines,
 26		  unsigned int loops)
 27{
 28	struct gpio_v2_line_values values;
 29	struct gpio_v2_line_config config;
 30	char swirr[] = "-\\|/";
 31	int fd;
 32	int ret;
 33	int i, j;
 34	unsigned int iteration = 0;
 35
 36	memset(&config, 0, sizeof(config));
 37	config.flags = GPIO_V2_LINE_FLAG_OUTPUT;
 38
 39	ret = gpiotools_request_line(device_name, lines, num_lines,
 40				     &config, "gpio-hammer");
 41	if (ret < 0)
 42		goto exit_error;
 43	else
 44		fd = ret;
 45
 46	values.mask = 0;
 47	values.bits = 0;
 48	for (i = 0; i < num_lines; i++)
 49		gpiotools_set_bit(&values.mask, i);
 50
 51	ret = gpiotools_get_values(fd, &values);
 52	if (ret < 0)
 53		goto exit_close_error;
 54
 55	fprintf(stdout, "Hammer lines [");
 56	for (i = 0; i < num_lines; i++) {
 57		fprintf(stdout, "%d", lines[i]);
 58		if (i != (num_lines - 1))
 59			fprintf(stdout, ", ");
 60	}
 61	fprintf(stdout, "] on %s, initial states: [", device_name);
 62	for (i = 0; i < num_lines; i++) {
 63		fprintf(stdout, "%d", gpiotools_test_bit(values.bits, i));
 64		if (i != (num_lines - 1))
 65			fprintf(stdout, ", ");
 66	}
 67	fprintf(stdout, "]\n");
 68
 69	/* Hammertime! */
 70	j = 0;
 71	while (1) {
 72		/* Invert all lines so we blink */
 73		for (i = 0; i < num_lines; i++)
 74			gpiotools_change_bit(&values.bits, i);
 75
 76		ret = gpiotools_set_values(fd, &values);
 77		if (ret < 0)
 78			goto exit_close_error;
 79
 80		/* Re-read values to get status */
 81		ret = gpiotools_get_values(fd, &values);
 82		if (ret < 0)
 83			goto exit_close_error;
 84
 85		fprintf(stdout, "[%c] ", swirr[j]);
 86		j++;
 87		if (j == sizeof(swirr) - 1)
 88			j = 0;
 89
 90		fprintf(stdout, "[");
 91		for (i = 0; i < num_lines; i++) {
 92			fprintf(stdout, "%d: %d", lines[i],
 93				gpiotools_test_bit(values.bits, i));
 94			if (i != (num_lines - 1))
 95				fprintf(stdout, ", ");
 96		}
 97		fprintf(stdout, "]\r");
 98		fflush(stdout);
 99		sleep(1);
100		iteration++;
101		if (loops && iteration == loops)
102			break;
103	}
104	fprintf(stdout, "\n");
105	ret = 0;
106
107exit_close_error:
108	gpiotools_release_line(fd);
109exit_error:
110	return ret;
111}
112
113void print_usage(void)
114{
115	fprintf(stderr, "Usage: gpio-hammer [options]...\n"
116		"Hammer GPIO lines, 0->1->0->1...\n"
117		"  -n <name>  Hammer GPIOs on a named device (must be stated)\n"
118		"  -o <n>     Offset[s] to hammer, at least one, several can be stated\n"
119		" [-c <n>]    Do <n> loops (optional, infinite loop if not stated)\n"
120		"  -?         This helptext\n"
121		"\n"
122		"Example:\n"
123		"gpio-hammer -n gpiochip0 -o 4\n"
124	);
125}
126
127int main(int argc, char **argv)
128{
129	const char *device_name = NULL;
130	unsigned int lines[GPIOHANDLES_MAX];
131	unsigned int loops = 0;
132	int num_lines;
133	int c;
134	int i;
135
136	i = 0;
137	while ((c = getopt(argc, argv, "c:n:o:?")) != -1) {
138		switch (c) {
139		case 'c':
140			loops = strtoul(optarg, NULL, 10);
141			break;
142		case 'n':
143			device_name = optarg;
144			break;
145		case 'o':
146			/*
147			 * Avoid overflow. Do not immediately error, we want to
148			 * be able to accurately report on the amount of times
149			 * '-o' was given to give an accurate error message
150			 */
151			if (i < GPIOHANDLES_MAX)
152				lines[i] = strtoul(optarg, NULL, 10);
153
154			i++;
155			break;
156		case '?':
157			print_usage();
158			return -1;
159		}
160	}
 
161
162	if (i >= GPIOHANDLES_MAX) {
163		fprintf(stderr,
164			"Only %d occurrences of '-o' are allowed, %d were found\n",
165			GPIOHANDLES_MAX, i + 1);
166		return -1;
167	}
168
169	num_lines = i;
170
171	if (!device_name || !num_lines) {
172		print_usage();
173		return -1;
174	}
175	return hammer_device(device_name, lines, num_lines, loops);
176}
v4.17
 
  1/*
  2 * gpio-hammer - example swiss army knife to shake GPIO lines on a system
  3 *
  4 * Copyright (C) 2016 Linus Walleij
  5 *
  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.
  9 *
 10 * Usage:
 11 *	gpio-hammer -n <device-name> -o <offset1> -o <offset2>
 12 */
 13
 14#include <unistd.h>
 15#include <stdlib.h>
 16#include <stdbool.h>
 17#include <stdio.h>
 18#include <dirent.h>
 19#include <errno.h>
 20#include <string.h>
 21#include <poll.h>
 22#include <fcntl.h>
 23#include <getopt.h>
 24#include <sys/ioctl.h>
 25#include <linux/gpio.h>
 26#include "gpio-utils.h"
 27
 28int hammer_device(const char *device_name, unsigned int *lines, int nlines,
 29		  unsigned int loops)
 30{
 31	struct gpiohandle_data data;
 
 32	char swirr[] = "-\\|/";
 33	int fd;
 34	int ret;
 35	int i, j;
 36	unsigned int iteration = 0;
 37
 38	memset(&data.values, 0, sizeof(data.values));
 39	ret = gpiotools_request_linehandle(device_name, lines, nlines,
 40					   GPIOHANDLE_REQUEST_OUTPUT, &data,
 41					   "gpio-hammer");
 
 42	if (ret < 0)
 43		goto exit_error;
 44	else
 45		fd = ret;
 46
 47	ret = gpiotools_get_values(fd, &data);
 
 
 
 
 
 48	if (ret < 0)
 49		goto exit_close_error;
 50
 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, ", ");
 56	}
 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, ", ");
 62	}
 63	fprintf(stdout, "]\n");
 64
 65	/* Hammertime! */
 66	j = 0;
 67	while (1) {
 68		/* Invert all lines so we blink */
 69		for (i = 0; i < nlines; i++)
 70			data.values[i] = !data.values[i];
 71
 72		ret = gpiotools_set_values(fd, &data);
 73		if (ret < 0)
 74			goto exit_close_error;
 75
 76		/* Re-read values to get status */
 77		ret = gpiotools_get_values(fd, &data);
 78		if (ret < 0)
 79			goto exit_close_error;
 80
 81		fprintf(stdout, "[%c] ", swirr[j]);
 82		j++;
 83		if (j == sizeof(swirr)-1)
 84			j = 0;
 85
 86		fprintf(stdout, "[");
 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, ", ");
 91		}
 92		fprintf(stdout, "]\r");
 93		fflush(stdout);
 94		sleep(1);
 95		iteration++;
 96		if (loops && iteration == loops)
 97			break;
 98	}
 99	fprintf(stdout, "\n");
100	ret = 0;
101
102exit_close_error:
103	gpiotools_release_linehandle(fd);
104exit_error:
105	return ret;
106}
107
108void print_usage(void)
109{
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"
116		"\n"
117		"Example:\n"
118		"gpio-hammer -n gpiochip0 -o 4\n"
119	);
120}
121
122int main(int argc, char **argv)
123{
124	const char *device_name = NULL;
125	unsigned int lines[GPIOHANDLES_MAX];
126	unsigned int loops = 0;
127	int nlines;
128	int c;
129	int i;
130
131	i = 0;
132	while ((c = getopt(argc, argv, "c:n:o:?")) != -1) {
133		switch (c) {
134		case 'c':
135			loops = strtoul(optarg, NULL, 10);
136			break;
137		case 'n':
138			device_name = optarg;
139			break;
140		case 'o':
141			lines[i] = strtoul(optarg, NULL, 10);
 
 
 
 
 
 
 
142			i++;
143			break;
144		case '?':
145			print_usage();
146			return -1;
147		}
148	}
149	nlines = i;
150
151	if (!device_name || !nlines) {
 
 
 
 
 
 
 
 
 
152		print_usage();
153		return -1;
154	}
155	return hammer_device(device_name, lines, nlines, loops);
156}