gpt4 book ai didi

linux - Poll() 以监视 BeagleBone Black 上的引脚。即使未连接引脚也能连续输出。

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:38:55 26 4
gpt4 key购买 nike

我想在 Beaglebone Black 的其中一个引脚上出现上升沿时触发一个事件。问题是,即使我没有将该引脚连接到任何东西,输出仍继续打印,发生中断,发生中断。我遇到了问题 Interrupts in Beaglebone在 stackoverflow 上并尝试按照这些步骤操作。有一个链接到 Program它实现了功能。我阅读了有关 poll() 的信息,并且我对程序进行了细微的更改,因为我只想监视一个引脚。更改后的代码是:

int main(int argc, char **argv, char **envp)
{
struct pollfd fdset[1]; // fdset[2] changed to fdset[1] since I will monitor just 1 pin
int nfds = 1; // nfds changed from 2 to 1
int gpio_fd, timeout, rc;
char *buf[MAX_BUF];
unsigned int gpio;
int len;



if (argc < 2) {
printf("Usage: gpio-int <gpio-pin>\n\n");
printf("Waits for a change in the GPIO pin voltage level or input on stdin\n");
exit(-1);
}

gpio = atoi(argv[1]);

gpio_export(gpio);
gpio_set_dir(gpio, 0);
gpio_set_edge(gpio, "rising");
gpio_fd = gpio_fd_open(gpio);

timeout = POLL_TIMEOUT;

while (1) {
memset((void*)fdset, 0, sizeof(fdset));

fdset[0].fd = gpio_fd; // This is the pin to be monitored
fdset[0].events = POLLIN;

//fdset[1].fd = gpio_fd; // commented since I do not need this
//fdset[1].events = POLLPRI;

rc = poll(fdset, nfds, timeout);

if (rc < 0) {
printf("\npoll() failed!\n");
return -1;
}

if (rc == 0) {
printf(".");
}

if (fdset[0].revents & POLLIN) {
len = read(fdset[0].fd, buf, MAX_BUF);
printf("\npoll() GPIO %d interrupt occurred\n", gpio);
}

// ****Commented block****
//if (fdset[0].revents & POLLIN) {
// (void)read(fdset[0].fd, buf, 1);
// printf("\npoll() stdin read 0x%2.2X\n", (unsigned int) buf[0]);
//}

fflush(stdout);
}

gpio_fd_close(gpio_fd);
return 0;
}

在 Beaglebone 黑色上运行 Angstrom。

最佳答案

https://www.kernel.org/doc/Documentation/gpio/sysfs.txt

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. If you use select(2), set the file descriptor in exceptfds. After poll(2) returns, either lseek(2) to the beginning of the sysfs file and read the new value or close the file and re-open it to read the value.

您还没有设置事件 POLLPRI 和 POLLERR。

struct pollfd fdset[1];
memset((void*)fdset, 0, sizeof(fdset));
fdset[0].fd = fd;
fdset[0].events = POLLPRI | POLLERR;
...
poll()
...
lseek(fdset[0].fd, 0, SEEK_SET);
const ssize_t rc = read(fdset[0].fd, buf, MAX_BUF);

上面的代码适用于运行 Debian 和 linux 3.8.13-bone47 的 BeagleBone Black Rev.C。

关于linux - Poll() 以监视 BeagleBone Black 上的引脚。即使未连接引脚也能连续输出。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19434157/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com