New Linux GPIO User Space GPIO Pin Polling

166 Views Asked by At

I am trying to figure out how to use the new Linux GPIO User Space API (v2). The problem I have is how to properly do polling with the new v2 interface as the one example I have found uses v1.

I am working on a RPi4 running retropie 4.8.

Here is what I have so far. I have confirmed that the pin is configured as an input and that the state is changing on button push but this doesn't pick up the change.

void gpio_poll(int fd)
{
    struct gpio_v2_line_request req;
    struct pollfd pfd;
    int offset = 17;

    req.offsets[0] = offset;
    req.config.flags = GPIO_V2_LINE_EVENT_RISING_EDGE;

    int ret = ioctl(fd, GPIO_V2_GET_LINE_IOCTL, &req);
    
    pfd.fd = req.fd;
    pfd.events = POLLIN;
    
   while (1) {
        usleep(50000);
        ret = poll(&pfd, 1, -1);
        
        if (ret == -1) {
            printf("Error while polling event from GPIO: %s", strerror(errno));
        } else if (pfd.revents & POLLIN) {
            printf("falling edge event on GPIO offset: %d, of %s\n", offset, DEV_NAME);
        } else {
            printf("Polling On: %d, of %s\n", offset, DEV_NAME);     
        }
    }
}
0

There are 0 best solutions below