i trying gpio based interrupts working on beaglebone gpio using poll() function. below code, in short:
while have pin connected ground, nothing happens (as expected).
when connect pin high, interrupt generated. however, either never clears or regenerated extremely fast. implemented count incremented when went high , in matter of few seconds counter on 10,000. missing in setup? issue in main() function, have included other functions completeness.
thanks assistance,
charles
#define sysfs_gpio_dir "/sys/class/gpio" #define max_buf 64 int gpio_export(unsigned int gpio) { int fd, len; char buf[max_buf]; fd = open(sysfs_gpio_dir "/export", o_wronly); if (fd < 0) { perror("gpio/export"); return fd; } len = snprintf(buf, sizeof(buf), "%d", gpio); write(fd, buf, len); close(fd); return 0; } int gpio_set_dir(unsigned int gpio, unsigned int direction) { int fd, len; char buf[max_buf]; len = snprintf(buf, sizeof(buf), sysfs_gpio_dir "/gpio%d/direction", gpio); fd = open(buf, o_wronly); if (fd < 0) { perror("gpio/direction"); return fd; } if (direction) write(fd, "out", 4); else write(fd, "in", 3); 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) { perror("gpio/set-edge"); return fd; } write(fd, edge, strlen(edge) + 1); close(fd); return 0; } int gpio_fd_open(unsigned int gpio) { int fd, len; char buf[max_buf]; len = snprintf(buf, sizeof(buf), sysfs_gpio_dir "/gpio%d/value", gpio); fd = open(buf, o_rdonly | o_nonblock ); if (fd < 0) { perror("gpio/fd_open"); } return fd; } int gpio_fd_close(int fd) { return close(fd); } int main() { struct pollfd fdset; int nfds = 1; int gpio_fd, timeout, rc; char *buf[max_buf]; unsigned int gpio; int len; char* c; gpio = 26; gpio_export(gpio); gpio_set_dir(gpio, 0); gpio_set_edge(gpio, "rising"); gpio_fd = gpio_fd_open(gpio); timeout = 1; while (1) { memset((void*)&fdset, 0, sizeof(fdset)); fdset.fd = gpio_fd; fdset.events = pollpri | pollerr; len = read(fdset.fd, c, 1); rc = poll(&fdset, nfds, timeout); //printf("pollpri %02x, pollerr %02x\r\n", pollpri, pollerr); if (rc < 0) { printf("\npoll() failed!\n"); return -1; } if (rc == 0) { printf("."); } if (fdset.revents & pollpri) { if (len==-1) { printf("hex of revents %02x, return code %02x\r\n", fdset.revents, rc); } len = read(fdset.fd, buf, max_buf); } if (fdset.revents & pollerr) { printf("errno: %d", errno); } } gpio_fd_close(gpio_fd); return 0; }
i reinstalled debian image on beaglebone black , works expected now. i'm not sure why wasn't working before, looks os issue.
Comments
Post a Comment