如何解决poll()函数检查GPIO中断的错误返回问题

时间:2017-01-03 07:47:16

标签: linux interrupt gpio

我尝试通过检查/ sys / class / gpio / gpioN /值来使用GPIO中断。

但是检查中断事件的poll()函数返回异常值。

首先,我将gpioN设置为init.rc中的正确状态

  
      
  • 写/ sys / class / gpio / export“N”
  •   
  • write / sys / class / gpio / gpioN / active_low“1”
  •   
  • 写入/ sys / class / gpio / gpioN / direction“in”
  •   
  • 写/ sys / class / gpio / gpioN / edge“下降”
  •   

检查中断事件的代码如下所示。

jint Java_kr_iges_wallpad_gpiotest_MainActivity_GPIORead(JNIEnv *env, jobject obj, jchar port)
{
    int fd, ret;
    struct pollfd fdset[1];
    char buf[32];

    if ((fd = gpio_fd_open(port)) == -1 ){
        LOGE("Read Port open error");
        return -1;
    }

    gpio_poll_fd.fd = fd;
    gpio_poll_fd.events = POLLPRI;
    gpio_poll_fd.revents = 0;

    while (1) {
        ret = poll(&gpio_poll_fd, 1, 3000);
        if (ret > 0) {
            if ((gpio_poll_fd.revents & POLLPRI) == POLLPRI){
                LOGD("GPIO interrupt!");

                ret = 0;
                break;
            } else {
                LOGW("No GPIO interrupt detected, weird.");
            }
        } else if (ret == 0) {
            LOGE("poll call timed out, should not be possible!");
        } else {
            LOGE("poll call failed - %s.", strerror(errno));
            break;
        }
    }

    close(fd);
    return ret;
}


int gpio_fd_open(unsigned int gpio)
{
    int fd, len;
    char buf[MAX_BUF];

    len = snprintf(buf, sizeof(buf), GPIO_PATH "/gpio%d/value", gpio);

    fd = open(buf, O_RDONLY | O_NONBLOCK );
    if (fd < 0) {
        perror("gpio/fd_open");
    }

    return fd;
}

问题是poll()的gpio_poll_fd.revents总是POLLPRI(总是GPIO中断)。

/ sys / class / gpio / gpioN /值根据GPIO输入稳定变化。

如何解决此问题?

感谢。

1 个答案:

答案 0 :(得分:0)

“在poll(2)返回之后,要么lseek(2)到sysfs的开头 文件并读取新值或关闭文件然后重新打开 读取值。”

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