2017-10-19 16 views
1

상태를 변경할 때마다 GPIO 값을 읽으려고합니다. 내가 /sys/class/gpio/gpio499/edge을 설정 한GPIO FD가 변경된 경우에도 폴링이 반환되지 않음

/sys/class/gpio/gpio499/value 

모두

내가 한 별도의 스레드에서 여론 조사 명령을 사용하여 값의 변화를 모니터링하기 위해 노력하고 있습니다.

내가 POLLIN 로 이벤트를 설정하면 내가 직면하고 문제가
void PIN_gpio_poll(size_t gpio)  //GPIO 499 
{ 
     char path[30]; 
     char cValue; 
     int fd; 
     int ret_poll; 
     int ret_read; 
     struct pollfd pollfd; 
     int i; 

     pollfd.events = POLLPRI | POLLERR; /* look for GPIO status change. */ 


     snprintf(path, 30, PHDRIVER_LINUX_CFG_DIR "/gpio%u/value", gpio); 
     fd = open(path, O_RDONLY); 
     if (fd == -1) 
     { 
       printf("Gpio_poll _ERROR\r\n"); 
     } 

     pollfd.fd = fd; 

     ret_read = read(pollfd.fd, &cValue, 1); // Dummy Read to clear 

     while (1) 
     { 
       lseek(fd, 0, SEEK_SET); 
       ret_read = read(fd, &cValue, 1); 
       printf("Value=%c, RET READ=%d\n",cValue,ret_read); 
//    ret_poll = poll(&pollfd, 1, -1); 
       ret_poll = poll(&pollfd, 1, 10000); //10sec timeout 

       printf("******REVENTS=%x\n",pollfd.revents); 
       if(ret_poll == -1) 
       { 
         printf("Gpio_poll poll failed\r\n"); 
         close(fd); 
       }else{ 
//      if (pollfd.revents & POLLPRI) 
         { 
           lseek(fd, 0, SEEK_SET); 
           ret_read = read(pollfd.fd, &cValue, 1); 
           if(ret_read > 0) 
           { 
            printf("Cvalue = %c\n",cValue); 
           } 
         } 

       } 
     } 
} 

, 여론 조사가 즉시 반환 : 여기에 코드입니다. 이는 항상 읽을 데이터가 (0 또는 1) GPIO이므로 이해할 수 있습니다. 나는 https://www.kernel.org/doc/Documentation/gpio/sysfs.txt을 참조하고 이벤트를 POLLPRI | 폴러. 그러나이 방법에서는 시간 초과 후에 만 ​​폴링이 반환됩니다. GPIO 값이 변경되면 복귀하지 않습니다. 내가 속임수를 놓치고있는 것이 있습니까? 나는 또한 상승, 하강에 /sys/class/gpio/gpio499/edge을 설정했지만 아무 것도 작동하지 않는 것 같습니다.

편집

: 여기 는 grep -r . /sys/class/gpio/gpio499

/sys/class/gpio/gpio499/edge:both 
/sys/class/gpio/gpio499/power/control:auto 
/sys/class/gpio/gpio499/power/runtime_active_time:0 
grep: /sys/class/gpio/gpio499/power/autosuspend_delay_ms: Input/output error 
/sys/class/gpio/gpio499/power/runtime_status:unsupported 
/sys/class/gpio/gpio499/power/runtime_suspended_time:0 
/sys/class/gpio/gpio499/value:1 
/sys/class/gpio/gpio499/active_low:0 
/sys/class/gpio/gpio499/direction:in 

주의 출력 : 게시 된 코드가 예상대로 작동하지 않습니다 poll() : 나는 0

+0

당신이 핀이 구성되어 있는지 확인 했 작동합니다 입력으로? ('cat/sys/class/gpio/gpio499/direction'에 _in_?) – Ctx

+0

아마도'grep -r./sys/class/gpio/gpio499' (질문에 편집 됨) 도움이 될 수 있습니다 – Ctx

+1

[Linux 보드에서 GPIO의 핀 변경을 감지하는 방법] (https://stackoverflow.com/questions/25962574/how- pin-change-of-a-gpio-on-linux-board) – Jackson

답변

0

함수에 1 값을 감지합니다.

제안 : 1) 파일을 읽고 현재 입력 값을 가져옵니다. 2) 값을 읽고, 스핀 루프를 실행과 비슷한 값이 변경 될 때까지 :

readbytes = read(fd, &cValue, 1); 
while(readbytes > 0) 
{ 
    if((off_t)-1 == lseek(fd, 0, SEEK_SET)) 
    { // then lseek failed 
     perror("lseek failed"); 
     exit(EXIT_FAILURE); 
    } 

    // implied else, lseek successful 

    readbytes = read(fd, &new_cValue, 1); 
    if(0 >= readbytes) 
    { 
     perror("read failed"); 
     exit(EXIT_FAILURE); 
    } 

    // implied else, read successful 

    if(cValue != new_cValue) 
    { // then state changed 
     cValue = new_cValue; 
     break; 
    } 
} 

이 루프는 더 많은 CPU 사이클을 구울 않지만,