gpio-hammer.c 3.8 KB

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