title: how to detect a pin change of a GPIO on Linux board
body: <p>I am using 3.12 kernel on an ARM based linux board (imx233 CPU). My purpose is to detect pin change of a GPIO (1 to 0). </p>
  
  <p>I can read the pin value constantly calling the below function (in a while(1) loop)  </p>
  
  <pre><code>int GPIO_read_value(int pin){
      int gpio_value = 0;
      char path[35] = {'\0'};
      FILE *fp;
      sprintf(path, "/sys/class/gpio/gpio%d/value", pin);
      if ((fp = fopen(path,"rb+")) == NULL){ //echo in &gt; direction
           //error
      }
  
      fscanf(fp, "%d", &amp;gpio_value);
      fclose(fp);
      return gpio_value;
  }
  </code></pre>
  
  <p>But it causes too much load to the CPU. I don't use <code>usleep</code> or <code>nanosleep</code>, because the pin change happens for a very short of a time that would cause me to miss the event. </p>
  
  <p>As far as I find out, it is not possible to use <code>poll()</code>. Is there any <code>poll()</code> like function that I can use to detect a pin change of a GPIO?</p>
  
  <p>EDIT: Just in case, if I am doing something wrong,  here is my <code>poll()</code> usage that does not detect the pin change</p>
  
  <pre><code>struct pollfd pollfds;
      int fd;
      int nread, result;
      pollfds.fd = open("/sys/class/gpio/gpio51/value", O_RDWR);
      int timeout = 20000;           /* Timeout in msec. */
      char buffer[128];
  
      if( pollfds.fd &lt; 0 ){
          printf(" failed to open gpio \n");
          exit (1);
      }
  
      pollfds.events = POLLIN;
      printf("fd opens..\n");
      while (1)
      {
              result = poll (&amp;pollfds, 0, timeout);
              switch (result)
              {
                    case 0:
                      printf ("timeout\n");
                      break;
                    case -1:
                      printf ("poll error \n");
                      exit (1);
  
                     default:
                  printf("something is happening..\n");
                      if (pollfds.revents &amp; POLLIN)
                      {
                          nread = read (pollfds.fd, buffer, 8);
                          if (nread == 0) {
                              printf ("result:%d\n", nread);
                              exit (0);
                           } else {
                              buffer[nread] = 0;
                              printf ("read %d from gpio: %s", nread, buffer);
                           }
                       }
                }
       }
       close(fd);
  </code></pre>
  
  <p>EDIT2: the code on <a href="https://developer.ridgerun.com/wiki/index.php/Gpio-int-test.c">https://developer.ridgerun.com/wiki/index.php/Gpio-int-test.c</a> works fine with <code>poll()</code> I needed to define the rising/falling edge for the interrupt and a little bit fix on the definition. It solves my problem, however, it might be good for me and some other people to hear/know the alternative methods.</p>