民意调查()对树莓,GPIO(sysfs中)覆盆子 [英] poll() on raspberry-gpio (sysfs) raspberry

查看:257
本文介绍了民意调查()对树莓,GPIO(sysfs中)覆盆子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为标题的状态,我有移植从另一个ARMv7的嵌入式Linux平台用户空间的一些中断code到树莓派2 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库(并得到它的工作方式),但评价的原因,我想在两个平台上做运行尽可能多的相同code越好。出于这个原因我有手的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.

所以,这里的相关code段

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();
        }
    }

那么,什么是工作引脚的设置:(CAT / SYS /班/ GPIO / GPIO2 / $东西呼应了正确的设置)。只要没有触发时,PROGRAMM正确等待(在民调(),按预期)。

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).

第一个上升沿来到后,民意调查()总是立即返回,从而执行我的日志记录功能,每次,不仅在上升沿。

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

什么令我感到困惑,是完全一样的code工程完全按照预期的其他平台上,它是同一个接口的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);

失踪前阅读()

这篇关于民意调查()对树莓,GPIO(sysfs中)覆盆子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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