我正在尝试检测 gpio 引脚何时从低电平变为高电平并且遇到问题。从我读过的内容来看,我应该能够以这种方式将引脚配置为输入:
# echo in > /sys/class/gpio/gpio51/direction
# echo rising > /sys/class/gpio/gpio51/edge
接下来,我尝试使用 select 运行等待上升沿的 ac 程序。代码看起来像这样(注意我注释掉了试图读取文件的尝试,因为如果你不设置 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。我有一个使用 poll() 来执行此操作的 python 库,并且 python 按预期工作。我将引脚拉低,调用 python,它阻塞,将引脚拉高,代码继续。所以我认为这不是linux gpio驱动程序的问题。