BeagleBone Black GPIO中断使用Poll()

时间:2015-07-21 02:42:02

标签: c linux beagleboneblack gpio

我正在尝试使用Poll()函数在BeagleBone GPIO上运行基于GPIO的中断。以下是我的代码,但简而言之:

虽然我将引脚接地,但没有任何反应(如预期的那样)。

当我将引脚连接到高电平时,会产生中断。但是,它要么永远不会清除,要么再生得非常快。我实现了一个计数,当它变高时会增加,在几秒钟内计数器就会超过10,000。我在设置中遗漏了什么吗?问题可能出在我的main()函数中,但我已将其他函数包括在内以便完整。

感谢您的帮助,

查尔斯

#define SYSFS_GPIO_DIR "/sys/class/gpio"
#define MAX_BUF 64

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

    fd = open(SYSFS_GPIO_DIR "/export", O_WRONLY);
    if (fd < 0) {
        perror("gpio/export");
        return fd;
    }

    len = snprintf(buf, sizeof(buf), "%d", gpio);
    write(fd, buf, len);
    close(fd);

    return 0;
}

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

    len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR  "/gpio%d/direction", gpio);

    fd = open(buf, O_WRONLY);
    if (fd < 0) {
        perror("gpio/direction");
        return fd;
    }

    if (direction)
        write(fd, "out", 4);
    else
        write(fd, "in", 3);

    close(fd);
    return 0;
}

int gpio_set_edge(unsigned int gpio, char *edge)
{
    int fd, len;
    char buf[MAX_BUF];

    len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/edge", gpio);

    fd = open(buf, O_WRONLY);
    if (fd < 0) {
        perror("gpio/set-edge");
        return fd;
    }

    write(fd, edge, strlen(edge) + 1);
    close(fd);
    return 0;
}

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

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

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

int gpio_fd_close(int fd)
{
    return close(fd);
}

int main()
{
    struct pollfd fdset;
    int nfds = 1;
    int gpio_fd, timeout, rc;
    char *buf[MAX_BUF];
    unsigned int gpio;
    int len;
    char* c;


    gpio = 26;

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

    timeout = 1;

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

        fdset.fd = gpio_fd;
        fdset.events = POLLPRI | POLLERR;

        len = read(fdset.fd, c, 1);

        rc = poll(&fdset, nfds, timeout);

        //printf("POLLPRI is %02x, POLLERR is %02x\r\n", POLLPRI, POLLERR);

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

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

        if (fdset.revents & POLLPRI) {
            if (len==-1)
            {
                printf("Hex of revents is %02x, return code is %02x\r\n", fdset.revents, rc);
            }
            len = read(fdset.fd, buf, MAX_BUF);
        }

        if (fdset.revents & POLLERR)
        {
            printf("errno: %d", errno);
        }
    }

    gpio_fd_close(gpio_fd);
    return 0;
}

1 个答案:

答案 0 :(得分:1)

我在BeagleBone Black上重新安装了Debian图像,它现在正常运行。我不确定为什么它之前没有工作,但看起来这是一个操作系统问题。