我开始使用 Linux 和嵌入式系统(路由器硬件和 openwrt)进行 C 编程。我已经启用了 GPIO 中断,使用轮询工作......几乎。
我可以使用 poll(),如果我按下按钮触发中断,poll() 返回值 > 0。到目前为止一切顺利。现在我尝试在多个 GPIO 上同时使用 poll(),因此想分析每个潜在中断源的事件。虽然中断似乎有效,但我得到了 POLLPRI 和 POLLERR,但我不明白为什么。将 pollfd 结构减少到 1 个条目不会改变任何内容。
char value;
int fd_btn1 = open("/sys/class/gpio/gpio14/value", O_RDONLY);
int fd_left = open("/sys/class/gpio/gpio12/value", O_RDONLY);
int fd_right = open("/sys/class/gpio/gpio13/value", O_RDONLY);
struct pollfd fds[3];
fds[0].fd = fd_btn1;
fds[1].fd = fd_left;
fds[2].fd = fd_right;
fds[0].events = POLLPRI;
fds[1].events = POLLPRI;
fds[2].events = POLLPRI;
read(fd_btn1, &value, 1);
read(fd_left, &value, 1);
read(fd_right, &value, 1);
ret = poll(fds, 1, 10000);
//debugging purpose
printf("ret: %i - revents[0]: %i", ret, fds[0].revents);
如果按下按钮(触发中断): ret=1d, revents=10d
如果没有按下任何东西,两者都是0d
最佳答案
我在以下位置找到了一些暗示您的问题的答案:http://e2e.ti.com/support/dsp/davinci_digital_media_processors/f/716/t/182883
I just ran into the POLLERR thing you're seeing too. Turns out this is how all sysfs files work, and you're using gpio through the sysfs interface.
From the sysfs GPIO kernel document: If the pin can be configured as interrupt-generating interrupt and if it has been configured to generate interrupts (see the description of "edge"), you can poll(2) on that file and poll(2) will return whenever the interrupt was triggered. If you use poll(2), set the events POLLPRI and POLLERR. Also, if you look take a look at the kernel code in fs/sysfs/file.c, you'll see that sysfs_poll returns DEFAULT_POLLMASK | POLLERR | POLLPRI.
All that said, it does seem strange that sysfs files return POLLERR, I'm not sure why they chose to do that.
关于c - poll() 返回 POLLPRI 和 POLLERR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27411013/