AnsweredAssumed Answered

Poll() GPIO on IMX6SL

Question asked by Stephan GM on Jun 6, 2016
Latest reply on Jun 7, 2016 by Stephan GM

This may not be the right place to ask but.... Im trying to use poll() on a board using the IMX6SL. It hangs on poll() on the second loop iteration. The commented out code and comment above it shows says what happens when this code is added. Does the interrupt need to be unmasked or something? I've seen code almost identical to mine in a few places, yet this hasn't seemed to work.

 

This is the c code:

 

int read_gpio(char *path, void (*callback)(int)){

   int fd = open(path, O_RDONLY);
   if(fd == -1){

       perror("error opening file");
   return -1;
   }

   char buf[11];
   int res, off;
   char c;
   struct pollfd pfd = {

       .fd = fd,
       .events = POLLPRI,
       .revents = 0
   };
   for(;;){

       LOGD("for begins");
     // dummy read causes poll never to run
     // read(fd, &buf[],1);
    // lseek(fd, 0, SEEK_SET);
       res = poll(&pfd,1,-1);
       LOGD("polling ended");
       if(res == -1){

          perror("error polling");
          break;
   }

 

   if((pfd.revents & POLLPRI) == POLLPRI){

       LOGD("POLLPRI");
       off = lseek(fd, 0, SEEK_SET);
       if(off == -1) break;
       memset(&buf[0], 0, 11);
       read(fd, &buf[0], 10*sizeof(char));
    // These two lines will cause it to poll constantly
    // close(fd);
   // fd = open(path, O_RDONLY);
       LOGD("Before callback");
       callback(atoi(buf));
       LOGD("After Callback");
   }

      LOGD("for ends");
   }

  close(fd);
   LOGD("for exits");

   return 0;
}

Outcomes