2013-12-13 49 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不知何故执行......一些具体参数 –