This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

GPIO poll exiting imeditelly, before any change in GPIO

Hi,


I created a simple code to poll a GPIO sysfs value file, but I'm running into some trouble:


  - In the first poll() call, it exits imediatelly, with both POLLPRI and POLLERR set in revents

  - After this, I lseek() and read() the fd, and loop back to poll()

  - Now poll hangs

This is my code:

#include <stdio.h>
#include <stdlib.h>

#include <poll.h>
#include <sys/fcntl.h>

#define GPIO_DIR "/sys/class/gpio/"
#define GPIO_EXPORT GPIO_DIR "export"

int main(int argc, char **argv)
{
   int gpio_fd;
   struct pollfd gpio_fdset[1];

   char pin_value[] = GPIO_DIR "gpioXYZ/value";
   sprintf(pin_value, GPIO_DIR "gpio%s/value", argv[1]);

   gpio_fd = open(pin_value, O_RDONLY);

   while (1)
   {
      char buffer[64];

      gpio_fdset[0].fd = gpio_fd;
      gpio_fdset[0].events = POLLPRI;
      gpio_fdset[0].revents = 0;

      poll(gpio_fdset, 1, -1);

      if (gpio_fdset[0].revents & POLLPRI)
      {
         printf("revents returned POLLPRI!\n");
         lseek(gpio_fdset[0].fd, 0, SEEK_SET);
         read(gpio_fdset[0].fd, buffer, 64);
      }
   }
}

Before calling it, I export the pin I wish to monitor (GPIO1_30 or 62 in the kernel numbering scheme). I've tried setting this pin as input, output, generating interrupts on rising and falling edges, but the behaviour is always the same. What am I missing?

From what I've read, when I use the sysfs, I should no need to use the gpio_request(), gpio_to_irq() and other function to be able to poll this pin. Is this right?

Regards,

Guilherme