Hi,
I want to monitor a pin on BBB for interrupt. I chose pin 117 (pin 25 on P9 → GPIO3_21 = 32*3+21 = 117. I hope that’s correct?)
I came across this question on stackoverflow. And This is my question on stackoverflow
I am following this program from ridgerun and made minor changes since I want to monitor just one pin and not any other files. Changes are highlighted in the code below which I am trying to run. (I have pasted just the main. Rest is the same as the code from the site)
The problem is, when I run the program, it continuously outputs “poll() GPIO 117 interrupt occurred” even though I haven’t connected anything to that particular pin and I have to do ctrl+C to stop the execution. POLLPRI does not work and the program prints just … after every timeout and I think POLLIN is proper since it is not a priority data.
Where am I going wrong?
I read about potential problems and the program seems to take care of the below.
- The poll() doesn’t fail.
- read() is called after every poll() so data should be consumed. ( I just want the program to notify whenever there is a rising edge saying there is an event occured. I do not want to read data from pin. but included this to avoid potential pitfalls)
- struct pollfd is being reset inside while loop ( memset((void*)fdset, 0, sizeof(fdset)); )
`
int main(int argc, char **argv)
{
**struct pollfd fdset[1];**
**int nfds = 1;**
int gpio_fd, timeout, rc;
char *buf[MAX_BUF];
unsigned int gpio;
int len;
if (argc < 2) {
printf("Usage: gpio-int <gpio-pin>\n\n");
printf("Waits for a change in the GPIO pin voltage level or input on stdin\n");
exit(-1);
}
gpio = atoi(argv[1]);
gpio_export(gpio);
gpio_set_dir(gpio, 0);
gpio_set_edge(gpio, "rising");
gpio_fd = gpio_fd_open(gpio);
timeout = POLL_TIMEOUT;
while (1) {
memset((void*)fdset, 0, sizeof(fdset));
**fdset[0].fd = gpio_fd;**
** fdset[0].events = POLLIN;**
rc = poll(fdset, nfds, timeout);
if (rc < 0) {
printf("\npoll() failed!\n");
return -1;
}
if (rc == 0) {
printf(".");
}
**if (fdset[0].revents & POLLIN) {**
** len = read(fdset[0].fd, buf, MAX_BUF);**
printf("\npoll() GPIO %d interrupt occurred\n", gpio);
}
fflush(stdout);
}
gpio_fd_close(gpio_fd);
return 0;
}
`
Thanks,
sudhir