Re: using the rpi gpio python module

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 -- arm@xxxxxxxxxxxxxxxxxxxxxxx
To unsubscribe send an email to arm-leave@xxxxxxxxxxxxxxxxxxxxxxx




[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Linux ARM (Vger)]     [Linux ARM]     [ARM Kernel]     [Fedora User Discussion]     [Older Fedora Users Discussion]     [Fedora Advisory Board]     [Fedora Security]     [Fedora Maintainers]     [Fedora Devel Java]     [Fedora Legacy]     [Fedora Desktop]     [ATA RAID]     [Fedora Marketing]     [Fedora Mentors]     [Fedora Package Announce]     [Fedora Package Review]     [Fedora Music]     [Fedora Packaging]     [Centos]     [Fedora SELinux]     [Coolkey]     [Yum Users]     [Tux]     [Yosemite News]     [Linux Apps]     [KDE Users]     [Fedora Tools]     [Fedora Art]     [Fedora Docs]     [Asterisk PBX]

Powered by Linux