gpio-hammer.c 3.7 KB

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