gpio-hammer.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. int hammer_device(const char *device_name, unsigned int *lines, int nlines,
  26. unsigned int loops)
  27. {
  28. struct gpiohandle_request req;
  29. struct gpiohandle_data data;
  30. char *chrdev_name;
  31. char swirr[] = "-\\|/";
  32. int fd;
  33. int ret;
  34. int i, j;
  35. unsigned int iteration = 0;
  36. ret = asprintf(&chrdev_name, "/dev/%s", device_name);
  37. if (ret < 0)
  38. return -ENOMEM;
  39. fd = open(chrdev_name, 0);
  40. if (fd == -1) {
  41. ret = -errno;
  42. fprintf(stderr, "Failed to open %s\n", chrdev_name);
  43. goto exit_close_error;
  44. }
  45. /* Request lines as output */
  46. for (i = 0; i < nlines; i++)
  47. req.lineoffsets[i] = lines[i];
  48. req.flags = GPIOHANDLE_REQUEST_OUTPUT; /* Request as output */
  49. strcpy(req.consumer_label, "gpio-hammer");
  50. req.lines = nlines;
  51. ret = ioctl(fd, GPIO_GET_LINEHANDLE_IOCTL, &req);
  52. if (ret == -1) {
  53. ret = -errno;
  54. fprintf(stderr, "Failed to issue GET LINEHANDLE "
  55. "IOCTL (%d)\n",
  56. ret);
  57. goto exit_close_error;
  58. }
  59. /* Read initial states */
  60. ret = ioctl(req.fd, GPIOHANDLE_GET_LINE_VALUES_IOCTL, &data);
  61. if (ret == -1) {
  62. ret = -errno;
  63. fprintf(stderr, "Failed to issue GPIOHANDLE GET LINE "
  64. "VALUES IOCTL (%d)\n",
  65. ret);
  66. goto exit_close_error;
  67. }
  68. fprintf(stdout, "Hammer lines [");
  69. for (i = 0; i < nlines; i++) {
  70. fprintf(stdout, "%d", lines[i]);
  71. if (i != (nlines - 1))
  72. fprintf(stdout, ", ");
  73. }
  74. fprintf(stdout, "] on %s, initial states: [", device_name);
  75. for (i = 0; i < nlines; i++) {
  76. fprintf(stdout, "%d", data.values[i]);
  77. if (i != (nlines - 1))
  78. fprintf(stdout, ", ");
  79. }
  80. fprintf(stdout, "]\n");
  81. /* Hammertime! */
  82. j = 0;
  83. while (1) {
  84. /* Invert all lines so we blink */
  85. for (i = 0; i < nlines; i++)
  86. data.values[i] = !data.values[i];
  87. ret = ioctl(req.fd, GPIOHANDLE_SET_LINE_VALUES_IOCTL, &data);
  88. if (ret == -1) {
  89. ret = -errno;
  90. fprintf(stderr, "Failed to issue GPIOHANDLE SET LINE "
  91. "VALUES IOCTL (%d)\n",
  92. ret);
  93. goto exit_close_error;
  94. }
  95. /* Re-read values to get status */
  96. ret = ioctl(req.fd, GPIOHANDLE_GET_LINE_VALUES_IOCTL, &data);
  97. if (ret == -1) {
  98. ret = -errno;
  99. fprintf(stderr, "Failed to issue GPIOHANDLE GET LINE "
  100. "VALUES IOCTL (%d)\n",
  101. ret);
  102. goto exit_close_error;
  103. }
  104. fprintf(stdout, "[%c] ", swirr[j]);
  105. j++;
  106. if (j == sizeof(swirr)-1)
  107. j = 0;
  108. fprintf(stdout, "[");
  109. for (i = 0; i < nlines; i++) {
  110. fprintf(stdout, "%d: %d", lines[i], data.values[i]);
  111. if (i != (nlines - 1))
  112. fprintf(stdout, ", ");
  113. }
  114. fprintf(stdout, "]\r");
  115. fflush(stdout);
  116. sleep(1);
  117. iteration++;
  118. if (loops && iteration == loops)
  119. break;
  120. }
  121. fprintf(stdout, "\n");
  122. ret = 0;
  123. exit_close_error:
  124. if (close(fd) == -1)
  125. perror("Failed to close GPIO character device file");
  126. free(chrdev_name);
  127. return ret;
  128. }
  129. void print_usage(void)
  130. {
  131. fprintf(stderr, "Usage: gpio-hammer [options]...\n"
  132. "Hammer GPIO lines, 0->1->0->1...\n"
  133. " -n <name> Hammer GPIOs on a named device (must be stated)\n"
  134. " -o <n> Offset[s] to hammer, at least one, several can be stated\n"
  135. " [-c <n>] Do <n> loops (optional, infinite loop if not stated)\n"
  136. " -? This helptext\n"
  137. "\n"
  138. "Example:\n"
  139. "gpio-hammer -n gpiochip0 -o 4\n"
  140. );
  141. }
  142. int main(int argc, char **argv)
  143. {
  144. const char *device_name = NULL;
  145. unsigned int lines[GPIOHANDLES_MAX];
  146. unsigned int loops = 0;
  147. int nlines;
  148. int c;
  149. int i;
  150. i = 0;
  151. while ((c = getopt(argc, argv, "c:n:o:?")) != -1) {
  152. switch (c) {
  153. case 'c':
  154. loops = strtoul(optarg, NULL, 10);
  155. break;
  156. case 'n':
  157. device_name = optarg;
  158. break;
  159. case 'o':
  160. lines[i] = strtoul(optarg, NULL, 10);
  161. i++;
  162. break;
  163. case '?':
  164. print_usage();
  165. return -1;
  166. }
  167. }
  168. nlines = i;
  169. if (!device_name || !nlines) {
  170. print_usage();
  171. return -1;
  172. }
  173. return hammer_device(device_name, lines, nlines, loops);
  174. }