python-3.xlinuxgpiolibgpiod

Problem with 'event_wait' in python3-libgpiod


I am working with a Debian 11 (bullseye) based system on a aarch64 chip. The affected libraries are:

libgpiod2 1.6.2-1

python3-libgpiod 1.6.2-1

The Problem occurs in the following example:

import gpiod

GPIO_CHIP = "/dev/gpiochip0"
INPUT = 26

def main():
    chip = gpiod.Chip(GPIO_CHIP)
    line = chip.get_line(INPUT)
    line.request(consumer="GPOUT", type=gpiod.LINE_REQ_DIR_IN, flags=gpiod.LINE_REQ_FLAG_BIAS_PULL_UP)
    while True:
        if line.event_wait(600):
            event = line.event_read()
            print(event)

main()

The problem is that the method event_wait from the class gpiod.Line immediately with a True returns and the call of event_read then leads to the following error:

Traceback (most recent call last):
  File "/home/tegeth1/./IO_test_event.py", line 18, in <module>
    main()
  File "/home/tegeth1/./IO_test_event.py", line 15, in main
    event = line.event_read()
PermissionError: [Errno 1] Operation not permitted

because there is no event in the event-queue.

This behaviour does not occur if I just use:

$ gpiomon 0 26 

on the command line. Even this other python example, works as expected:

import gpiod
import select

GPIO_CHIP = "/dev/gpiochip0"
INPUT = 26

def main():
    chip = gpiod.Chip(GPIO_CHIP)
    line = chip.get_line(INPUT)
    line.request(consumer="GPOUT", type=gpiod.LINE_REQ_EV_BOTH_EDGES, flags=gpiod.LINE_REQ_FLAG_BIAS_PULL_UP)
    fd = line.event_get_fd()
    poll = select.poll()
    poll.register(fd)
    while True:
        poll.poll(None)
        event = line.event_read()
        print(event)

main()

I have already tried different biases and executed the script also on a Raspberry Pi (same chip architecture, comparable OS) with the same result. What am I doing wrong? I would appreciate any help.

Many thanks and best regards,

Cone

Edit: If you ask, why I am not simply using the second solution, look here


Solution

  • As per the second Python example, to receive event the line must be requested with type being one of the LINE_REQ_EV_ types, such as LINE_REQ_EV_BOTH_EDGES.

    What you do in your first example is request it as a plain input line, with type LINE_REQ_DIR_IN, which does not support interrupts or generate edge events. Instead it returns errors when you try to use it in a way that you did not request it for.