2013-12-13 54 views
3

我試圖檢測何時gpio引腳從低到高,並遇到麻煩。從我讀過什麼,我應該能引腳這種方式配置爲輸入:beaglebone黑色gpio選擇不工作

# echo in > /sys/class/gpio/gpio51/direction 
# echo rising > /sys/class/gpio/gpio51/edge 

接下來,我嘗試運行等待使用select上升沿的C程序。代碼如下所示(請注意我註釋掉試圖只是讀取文件,因爲閱讀是應該阻止,如果你不設置O_NONBLOCK):

#include<stdio.h>                                         
#include<fcntl.h>                                         
#include <sys/select.h>                                       


int main(void) {                                         
    int fd = open("/sys/class/gpio/gpio51/value", O_RDONLY & ~O_NONBLOCK);                           
    //int fd = open("/sys/class/gpio/gpio51/value", O_RDONLY | O_NONBLOCK);                           
    //unsigned char buf[2];                                       
    //int x = read(fd, &buf, 2);                                     
    //printf("%d %d: %s\n", fd, x, buf);                                   
    fd_set exceptfds;                                        
    int res;                                          

    FD_ZERO(&exceptfds);                                       
    FD_SET(fd, &exceptfds);                                       
    //printf("waiting for %d: %s\n", exceptfds);                                 
    res = select(fd+1,                                        
       NULL,    // readfds - not needed                              
       NULL,    // writefds - not needed                              
       &exceptfds,                                      
       NULL);    // timeout (never)                                

    if (res > 0 && FD_ISSET(fd, &exceptfds)) {                                  
    printf("finished\n");                                       
    }                                            
    return 0;                                          
} 

程序退出馬上無論什麼狀態該引腳(高或低)。任何人都可以看到我這樣做的方式有什麼問題嗎?

PS。我有一個python庫,它使用poll()來做到這一點,python按預期工作。我將引腳拉低,調用python,它阻塞,將引腳拉高,代碼繼續。所以我不認爲這是與Linux的GPIO驅動程序的問題。

https://bitbucket.org/cswank/gadgets/src/590504d4a30b8a83143e06c44b1c32207339c097/gadgets/io/poller.py?at=master

回答

4

我想通了。您必須在select調用返回之前從文件描述符中讀取數據。這裏是工作的例子:

#include<stdio.h>                                                     
#include<fcntl.h>                                                     
#include <sys/select.h>                                                    

#define MAX_BUF 64                                                     

int main(void) {                                                     
    int len;                                                       
    char *buf[MAX_BUF];                                                    
    int fd = open("/sys/class/gpio/gpio51/value", O_RDONLY);                                           
    fd_set exceptfds;                                                     
    int res;                                                      

    FD_ZERO(&exceptfds);                                                    
    FD_SET(fd, &exceptfds);                                                   
    len = read(fd, buf, MAX_BUF); //won't work without this read.                                                 
    res = select(fd+1,                                                    
       NULL,    // readfds - not needed                                           
       NULL,    // writefds - not needed                                           
       &exceptfds,                                                   
       NULL);    // timeout (never)                                            

    if (res > 0 && FD_ISSET(fd, &exceptfds)) {                                              
    printf("finished\n");                                                   
    }                                                         
    return 0;                                                       
}                                                         
+1

我不知道這是可能的命令行工具,如貓,尾巴,頭,DD不知何故執行......一些具體參數 –