poll() on raspberry-gpio (sysfs) raspberry [英] poll() on raspberry-gpio (sysfs) raspberry

查看:30
本文介绍了poll() on raspberry-gpio (sysfs) raspberry的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如标题所述,我在将一些用户空间中断代码从另一个 armv7 嵌入式 linux 平台移植到 Raspberry Pi 2 Model B 时遇到问题.

as the title states, I have a problem porting some userspace-interrupt code from another armv7 embedded linux platform onto the Raspberry Pi 2 Model B.

我知道wiringPi 库(并让它以这种方式工作),但出于评估原因,我希望在两个平台上运行尽可能多的相同代码.出于这个原因,我必须手动与 sysfs 交互.

I'm aware of the wiringPi library (and got it to work that way), but for evaluation reasons I want to do run as much identical code as possible on both platforms. For that reason I have to interface with sysfs by hand.

所以,这是相关的代码片段

So, here's the relevant code snippet

#define GPIO_TRIGGER_MODE   "rising"
#define SYS_GPIO_PIN        "2"
#define SYS_GPIO_DIRECTION  "/sys/class/gpio/gpio2/direction"
#define SYS_GPIO_EDGE       "/sys/class/gpio/gpio2/edge"
#define SYS_GPIO_VALUE      "/sys/class/gpio/gpio2/value"
static int fd_gpio;

{...}

//Setup sysfs-Pin
if ((fd_gpio = open("/sys/class/gpio/export", O_WRONLY)) < 0) {
    exit(-1);
} else {
    write(fd_gpio, SYS_GPIO_PIN, strlen((char*) SYS_GPIO_PIN));
    close(fd_gpio);
    if ((fd_gpio = open(SYS_GPIO_DIRECTION, O_WRONLY)) < 0) {
        exit(-1);
    } else {
        write(fd_gpio, "in", strlen("in"));
        close(fd_gpio);

        if ((fd_gpio = open(SYS_GPIO_EDGE, O_WRONLY)) < 0) {
            exit(-1);
        } else {
            write(fd_gpio, GPIO_TRIGGER_MODE, strlen((char*) GPIO_TRIGGER_MODE));
            close(fd_gpio);
        }
    }
}

static int fd_gpio_value;
struct pollfd *fd_poll;

if ((fd_gpio_value = open(SYS_GPIO_VALUE, O_RDWR)) < 0) {
        exit(-1);
} else {
    fd_poll = malloc(sizeof (*fd_poll));
    fd_poll->fd = fd_gpio_value;
    fd_poll->events = POLLPRI;

    char buf;
    while (1) {
        read(fd_gpio_value, &buf, 1);
        if (poll(fd_poll, 1, -1) == -1) { 
            exit(-1);
        } else {
                some_logging_occurs();
        }
    }

所以,Pin 的设置是有效的:(cat/sys/class/gpio/gpio2/$stuff 与正确的设置相呼应).只要没有触发器,程序就会正确等待(按预期在 poll() 上).

So, whats working is the setup of the Pin: (cat /sys/class/gpio/gpio2/$stuff echoes the right settings). As long as there is no Trigger, the programm waits correctly (on poll(), as intended).

在第一个上升沿到来后,poll() 总是立即返回,因此每次都执行我的日志记录功能,而不仅仅是在上升沿.

After the first rising edge came, poll() always returns immediately, and thus executes my logging function everytime, not only on rising edges.

令我感到困惑的是,完全相同的代码在其他平台上完全按照预期工作,并且与 GPIO 的接口相同.

What baffles me, is that the exact same code works exactly as intended on the other platform and it's the same interface to the GPIOs.

推荐答案

终于找到答案:一个简单的

finally found the answer: a simple

lseek(fd_gpio_value,0,SEEK_SET);

在 read() 之前丢失

was missing before read()

这篇关于poll() on raspberry-gpio (sysfs) raspberry的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆