1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
| #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <fcntl.h> #include <errno.h> #include <poll.h>
#define SYSFS_GPIO_DIR "/sys/class/gpio" #define MAX_BUF 255
int gpio_export(unsigned int gpio) { int fd, len; char buf[MAX_BUF];
fd = open(SYSFS_GPIO_DIR "/export", O_WRONLY);
if (fd < 0) { fprintf(stderr, "Can't export GPIO %d pin: %s\n", gpio, strerror(errno)); return fd; }
len = snprintf(buf, sizeof(buf), "%d", gpio); write(fd, buf, len); close(fd);
return 0; }
int gpio_unexport(unsigned int gpio) { int fd, len; char buf[MAX_BUF];
fd = open(SYSFS_GPIO_DIR "/unexport", O_WRONLY);
if (fd < 0) { fprintf(stderr, "Can't unexport GPIO %d pin: %s\n", gpio, strerror(errno)); return fd; }
len = snprintf(buf, sizeof(buf), "%d", gpio); write(fd, buf, len); close(fd);
return 0; }
int gpio_set_edge(unsigned int gpio, char *edge) { int fd, len; char buf[MAX_BUF];
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/edge", gpio);
fd = open(buf, O_WRONLY);
if (fd < 0) { fprintf(stderr, "Can't set GPIO %d pin edge: %s\n", gpio, strerror(errno)); return fd; }
write(fd, edge, strlen(edge)+1); close(fd);
return 0; }
int gpio_set_pull(unsigned int gpio, char *pull) { int fd, len; char buf[MAX_BUF];
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/pull", gpio);
fd = open(buf, O_WRONLY);
if (fd < 0) { fprintf(stderr, "Can't set GPIO %d pin pull: %s\n", gpio, strerror(errno)); return fd; }
write(fd, pull, strlen(pull)+1); close(fd);
return 0; }
int main(int argc, char *argv[]) { struct pollfd fdset[2]; int fd, ret, gpio; char buf[MAX_BUF];
if (argc < 3 || argc > 4) { fprintf(stdout, "usage : sudo sysfs_irq_test <gpio> <edge> [pull]\n"); fflush(stdout); return -1; }
gpio = atoi(argv[1]); if (gpio_export(gpio)) { fprintf(stdout, "error : export %d\n", gpio); fflush(stdout); return -1; }
if (gpio_set_edge(gpio, argv[2])) { fprintf(stdout, "error : edge %s\n", argv[2]); fflush(stdout); return -1; }
if (argv[3] && gpio_set_pull(gpio, argv[3])) { fprintf(stdout, "error : pull %s\n", argv[3]); fflush(stdout); return -1; }
snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio); fd = open(buf, O_RDWR); if (fd < 0) goto out;
while (1) { memset(fdset, 0, sizeof(fdset)); fdset[0].fd = STDIN_FILENO; fdset[0].events = POLLIN; fdset[1].fd = fd; fdset[1].events = POLLPRI; ret = poll(fdset, 2, 3*1000);
if (ret < 0) { perror("poll"); break; }
fprintf(stderr, ".");
if (fdset[1].revents & POLLPRI) { char c; (void)read (fd, &c, 1) ; lseek (fd, 0, SEEK_SET) ; fprintf(stderr, "\nGPIO %d interrupt occurred!\n", gpio); }
if (fdset[0].revents & POLLIN) break;
fflush(stdout); }
close(fd); out: if (gpio_unexport(gpio)) { fprintf(stdout, "error : unexport %d\n", gpio); fflush(stdout); }
return 0; }
|