[Pkg-raspi-maintainers] GPIO support / Ubuntu images

Lucas Nussbaum lucas at debian.org
Thu Mar 21 20:52:26 GMT 2019


On 21/03/19 at 10:41 +0100, Lucas Nussbaum wrote:
> Hi,
> 
> I noticed that Ubuntu ships a patch for rpi.gpio that is supposed
> (according to the patch description -- I haven't tested) to add GPIO
> support.
> https://patches.ubuntu.com/r/rpi.gpio/rpi.gpio_0.6.5-1ubuntu1.patch
> 
> I'm wondering if somebody looked at Ubuntu images (provided on
> https://wiki.ubuntu.com/ARM/RaspberryPi)? There might be other things to
> import back into Debian.

So I dug a bit deeper.

The Ubuntu image uses a custom kernel:
https://launchpad.net/ubuntu/+source/linux-raspi2
I suppose it's the stock Ubuntu kernel + rpi-specific patches.

GPIO work on it with RPi.GPIO (which AFAIK is the lower-level library).
It does not work with gpiozero.

To get GPIO to work on the Debian image:


I applied the patch from Ubuntu that fixes auto-detection. There's
also a different patch upstream which does more things.
https://sourceforge.net/p/raspberry-gpio-python/code/ci/03be41933c1b4fab3aa0daf2931adb4561029bd1/

Then what RPi.GPIO does is that it mmaps the memory at the address where
GPIOs are mapped by the broadcom chip. On Ubuntu, /dev/gpiomem is
available, so that's easy.

On Debian, there's no /dev/gpiomem because it's not in the vanilla
kernel. RPi.GPIO needs to open /dev/mem at the correct address. This
fails with a segmentation because of
https://sourceforge.net/p/raspberry-gpio-python/tickets/166/
Because of that bug, the error handling fails and it segfaults.

So, it fails to open /dev/mem. That's because the Debian kernel is
built with CONFIG_DEVMEM_STRICT=y. To work around that, we need to add
iomem=relaxed to the kernel command line in /boot/firmware/cmdline.txt

After that, RPi.GPIO just works.

To debug this, it's also useful to use
https://github.com/RPi-Distro/raspi-gpio which doesn't have the cast
bug described above. Maybe it's worth packaging in Debian.

For reference, I'm testing with:
------------------------
import RPi.GPIO as gpio
import time

gpio.setmode(gpio.BCM)
gpio.setup(18, gpio.OUT)

while True:
            gpio.output(18, gpio.HIGH)
            time.sleep(1)
            gpio.output(18, gpio.LOW)
            time.sleep(1)
------------------------
and a circuit like this one:
https://www.makeuseof.com/tag/raspberry-pi-control-led/

Lucas



More information about the Pkg-raspi-maintainers mailing list