paulv
Posts: 563
Joined: Tue Jan 15, 2013 12:10 pm
Location: Netherlands

RPi.GPIO port kernel registration : bug?

Mon Jan 04, 2016 5:21 am

I just stumbled on what I think is an issue with the RPi.GPIO 0.5.11 software.

My kernel version is :
Linux raspberrypi 4.1.13+ #826 PREEMPT Fri Nov 13 20:13:22 GMT 2015 armv6l GNU/Linux
If you use the sysfs method of declaring a port (as input or output), as in

Code: Select all

sudo sh -c "echo '25' > /sys/class/gpio/export"
the port gets registered in /sys/class/gpio. This registration allows you (a program) to verify that the port is already in use.

You would think that the RPi.GPIO software does the same, but it does not.
Just declaring a port as input or output does not register this in /sys/class/gpio.

If a port is registered in /sys/class/gpio already, you cannot assign it again. As you would expect, you get the warning message that the port is already in use.

Within it's own software environment, (within one Python program) it does also check this, which is why you also can get the warning message that the port is already in use.

All is well, it seems, but can go awry when you use two or more separate programs that are using the GPIO pins. A second program can happily change the settings of a port already in use by another running program, and wreak havoc on your connected hardware.

There is one exception, when you declare a port as having an interrupt, like with "GPIO.wait_for_edge", the particular port does get registered in /sys/class/gpio.
Event detects

Code: Select all

    GPIO.add_event_detect(10, GPIO.FALLING)
    GPIO.add_event_detect(11, GPIO.FALLING, callback=test)
don't work either.

Before I report this as a bug, does anybody else have seen this or do you have comments?

Tks!
Last edited by paulv on Mon Jan 04, 2016 6:48 am, edited 1 time in total.

User avatar
rpdom
Posts: 17275
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: RPi.GPIO port kernel registration : bug?

Mon Jan 04, 2016 6:31 am

It's not a bug. It's just the way the Pi works.

The sysfs (/sys) interface to the gpios is just one way that they can be accessed. Essentially it's just another program that can use the GPIOs, the same as RPi.GPIO and many others do. A GPIO only has to be "registered" in /sys if it is going to be used via /sys. I'm assuming that RPi.GPIO uses that interface for setting up the "wait for edge" method, rather than accessing it directly.

paulv
Posts: 563
Joined: Tue Jan 15, 2013 12:10 pm
Location: Netherlands

Re: RPi.GPIO port kernel registration : bug?

Mon Jan 04, 2016 6:46 am

Thanks rpdom,

If it's not a bug, it's certainly not a feature, and pretty much undocumented to boot.

Is there a preferred way to prevent different programs clobbering each others GPIO pin definitions?

The most obvious problems can present themselves when HAT's use pins that are defined in overlays or blob's and are not registered in /sys/class/gpio.

I can think of a "registration" kludge that programs need to do, ie. write the used ports in a file so it can be checked. There must be a better way...?

Return to “Advanced users”