linux - beaglebone black gpio 选择不工作

标签 linux select gpio beagleboneblack

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

# 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;                                                                                                                                                                      
}

无论管脚处于什么状态(高电平或低电平),程序都会立即退出。任何人都可以看出我这样做的方式有问题吗?

附言。我有一个使用 poll() 来执行此操作的 python 库,并且 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("finished\n");                                                                                                                                                                                                          
  }                                                                                                                                                                                                                                
  return 0;                                                                                                                                                                                                                        
}                                                                                                                                                                                                                                  

关于linux - beaglebone black gpio 选择不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20573152/

相关文章:

c++ - 使用 OpenCV 和 BCM2835 在 Raspberry Pi 上工作

linux - 逐行读取并逐行打印匹配项

python-3.x - 将带有依赖项的python3脚本打包到exe中的最简单方法?

windows - 监听多个套接字 : select vs. 多线程

gpio - 写入/sys/class/gpio/export 失败

Linux/AM3352 : setting gpio via sysfs seem not working

c++ - 可移植的方式(linux 和 Windows)使文件只能由 1 个进程修改,而不能由 C/C++ 中的其他进程修改

c - 在 file_operations 中实现 mmap - 将页面数组映射到虚拟空间

sql - 如何将 select 语句的结果存储在变量中?

javascript - jQuery 防止更改选择