Discussion:
[fedora-arm] using the rpi gpio python module
Arno A. Karner
2017-12-15 23:53:01 UTC
Permalink
what kernel modules need to be loaded, configuration file changes

on Fedora 27 32 bit on pi2 I get
# python2 blinkled.py
Segmentation fault (core dumped)
# python3 blinkled.py
Segmentation fault (core dumped)
# uname -a
Linux f27-arm7-32-rpi2.dmz1.ipa.tnss.com 4.14.3-300.fc27.armv7hl #1 SMP
Mon Dec 4 18:55:57 UTC 2017 armv7l armv7l armv7l GNU/Linux
# rpmq -i gpio
python2-RPi.GPIO:0.6.3:4.fc27:armv7hl:Fedora Project:Fedora Project
python3-RPi.GPIO:0.6.3:4.fc27:armv7hl:Fedora Project:Fedora Project
sgpio:1.2.0.10:19.fc27:armv7hl:Fedora Project:Fedora Project

on Fedora 26 64 bit on pi 3 I get
# python2 blinkled.py
Traceback (most recent call last):
File "blinkled.py", line 14, in <module>
import RPi.GPIO as GPIO
File "/usr/lib64/python2.7/site-packages/RPi/GPIO/__init__.py", line
23, in <module>
from RPi._GPIO import *
RuntimeError: This module can only be run on a Raspberry Pi!
# python3 blinkled.py
Traceback (most recent call last):
File "blinkled.py", line 14, in <module>
import RPi.GPIO as GPIO
File "/usr/lib64/python3.6/site-packages/RPi/GPIO/__init__.py", line
23, in <module>
from RPi._GPIO import *
RuntimeError: This module can only be run on a Raspberry Pi!
# uname -a
Linux f26-arm8-64-aak.dmz1.ipa.tnss.com 4.14.4-200.fc26.aarch64 #1 SMP
Fri Dec 8 20:34:59 UTC 2017 aarch64 aarch64 aarch64 GNU/Linux

Thanks in advance for any help
Arno
_______________________________________________
arm mailing list -- ***@lists.fedoraproject.org
To unsubscribe sen
Aleksandra Fedorova
2017-12-17 13:22:18 UTC
Permalink
I am not an expert but this is what I've got on the topic:

0) There is a package in Fedora repositories: libgpiod-utils. It provides command line interface(gpioset and gpioget commands) to operate GPIO pins. It is simple, it is based on the C library, not Python, and it just works.

But Python is more fun, so we don't stop here.

1) cpuinfo and "RuntimeError: This module can only be run on a Raspberry Pi!"

On Raspbian cpuinfo looks as follows:
---
$ cat /proc/cpuinfo
...
Hardware : BCM2835
Revision : a22082
Serial : ***
---
but Fedora defines itself as a generic device tree based system
---
$ cat /proc/cpuinfo
...
Hardware : Generic DT based system
Revision : 0000
Serial : ***
---
Many GPIO-related libraries are written for Raspbian and they search through /proc/cpuinfo for the system information.

See for example https://sourceforge.net/p/raspberry-gpio-python/code/ci/default/tree/source/cpuinfo.c#l38 Here RPi.GPIO doesn't recognize the platform at all and fails with the "This module can only be run on a Raspberry Pi!"

Originally I expected this to be easy to fix. So I filed a bug https://bugzilla.redhat.com/show_bug.cgi?id=1471731 and then tried to workaround it locally. I patched the code of the RPi.GPIO library simply replacing the "/proc/cpuinfo" path with the "/etc/rpi3-cpuinfo.txt" where I've put the raspbian-style system description.

Now the library can be imported. But the simple script like this:
---
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)

GPIO.setup(12, GPIO.OUT, initial=GPIO.HIGH)
---
fails with the segmentation fault.

So it seems to be more differences than expected, and after looking into the sources again: https://sourceforge.net/p/raspberry-gpio-python/code/ci/default/tree/source/c_gpio.c I gave up on the idea of making it work, since it uses a lot of hardcoded offset parameters and operates on /dev/mem which is much lower level then I could handle.

2) SYSFS

There is another interface to work with GPIO pins, which is supported by some libraries like https://github.com/derekstavis/python-sysfs-gpio It operates by exporting pins via /sys/class/gpio/export and so on.

But is disabled by default on Fedora kernel.

You can enable it by setting CONFIG_GPIO_SYSFS=y in the kernel config and recompiling the kernel.

Note though that this interface is considered to be deprecated in the kernel, and will be removed by 2020, but for now it works.

3) GPIO character device

Now there is a new character device interface provided by the kernel. It exposes set of GPIO pins as a single file descriptor, and you can interact with it via ioctl syscalls (https://github.com/torvalds/linux/blob/master/include/uapi/linux/gpio.h). It is used by the libgpiod library mentioned in 0) above.

I was looking for the Pythonic support for it here or there but there is none. So I created my own very simple wrapper
https://github.com/bookwar/python-gpiodev/

I tested it of F26, F27(both armv7hl) on Raspberry Pi 3, and you can switch LED lights and read button events with it.

--
Aleksandra Fedorova
bookwar
_______________________________________________
arm mailing list -- ***@lists.fedoraproject.org
To unsubscribe send an email to arm-***@lists.fe

Loading...