beaglebone black gpio select不起作用



我正在尝试检测gpio引脚何时从低电平变为高电平,并且出现问题。根据我所读到的内容,我应该能够以这种方式将引脚配置为输入:

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

接下来,我尝试运行一个c程序,使用select等待上升沿。代码看起来是这样的(注意,我注释掉了一次只读取文件的尝试,因为如果你不设置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: %sn", fd, x, buf);                                                                                                                                           
  fd_set exceptfds;                                                                                                                                                              
  int    res;                                                                                                                                                                    
  FD_ZERO(&exceptfds);                                                                                                                                                           
  FD_SET(fd, &exceptfds);                                                                                                                                                        
  //printf("waiting for %d: %sn", 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("finishedn");                                                                                                                                                        
  }                                                                                                                                                                              
  return 0;                                                                                                                                                                      
}

无论引脚处于何种状态(高或低),程序都会立即退出。有人能看出我这样做有什么不对吗?

PS。我有一个python库,它使用poll()来实现这一点,并且python按预期工作。我把引脚拉低,调用python,它阻塞,把引脚拉高,代码继续。所以我不认为这是linux gpio驱动程序的问题。

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

我想明白了。在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("finishedn");                                                                                                                                                                                                          
  }                                                                                                                                                                                                                                
  return 0;                                                                                                                                                                                                                        
}                                                                                                                                                                                                                                  

最新更新