Currently, libraries such as RPi.GPIO are accessing /dev/mem to get to the GPIO registers directly, which works very well but has a number of issues, not the least of them the fact that you have to run as root whenever you touch GPIO.
As a first step away from this, we're adding a device, /dev/gpiomem, which gives you access to the GPIO registers without root permission. It will always map your pointer to the correct location of the registers, so there is no need to parse /proc/devicetree to try and figure out where they are.
See this pull request on raspberrypi/linux for more details.
I've attached a patch for c_gpio.c which will first check for /dev/gpiomem and then fall back to requiring root privileges if it doesn't exist. /dev/gpiomem will appear in the next Raspbian release (or sooner via rpi-update), at which point RPi.GPIO and similar tools will no longer require root.
This hopefully be part of the next release - ETA w/c 24/8/2015.
Awesome! Thanks.
Coded it ready for testing... waiting for the new kernel with /dev/gpiomem to compile!
Tested - passes unit test suite as root.
Does not work as a normal user - file permissions on /dev/gpiomem don't look right:
crw------- 1 root root 244, 0 Jan 1 1970 /dev/gpiomem
Did a sudo chmod 666 /dev/gpiomem and ran unit test suite again.
GPIO.setup / GPIO.input / GPIO.output work OK. Nothing works that uses /sys/class/gpio i.e. events.
On my system, the gpio group doesn't exist (never has done). It is a very old installation of Raspbian that has just been dist-upgraded for the last couple of years. In other words, the gpio group and udev rules for /sys/class/gpio don't exist on my system. These used to be created by installing the pifacecommon raspbian package. See http://piface.github.io/pifacecommon/installation.html . I can't find out where or how it is set up now - it has now moved somewhere else! Has anyone any ideas?
Last edit: Ben Croston 2015-08-31
I had no
gpio
group and wrong permission on my ArchLinux system. I solved byand the following udev rules, which I copied from this thread post
Hope this helps.
After fixing permission, I'm still unable to use events: I suspect that this is a timing issue with the
/sys/class/gpio
udev rule. Permission are changed right after the relevant files (/sys/class/gpio/gpioXX/edge
...) are created, but too late for access by the python code.Last edit: Anonymous 2015-09-12
Have found the following in spindle (this is how it gets in the Foundation Raspbian image):
https://github.com/RPi-Distro/spindle/blob/master/wheezy-stage3
Edit: the /sys/class/gpio/gpioXX/* files do not have the correct owner/permissions with the above (default in latest Raspbian)
Also found in spindle:
Last edit: Ben Croston 2015-09-13
Created 0.6.0a2 - should work around the timing issues discovered by Stefano Miccoli above.
Have also added create_gpio_user_permissions.py that creates the gpio group and udev rules that work (thanks again to Stefano Miccoli). Note that the udev settings in the Foundation Raspbian Wheezy do not work properly. I will patch spindle an create a pull request. In the meantime, I will enhance create_gpio_user_permissions.py so that it detects and replaces incorrect udev settings. Should this be run automatically on installation (by pip and apt-get)?
Last edit: Ben Croston 2015-09-13
While
create_gpio_user_permissions.py
is a good idea for helping inexperienced users and therefore could be included in the distribution by aclause in
setup.py
, I think that it should not be run automatically at install time.With
pip
usally one installs in an isolated virtual environment (created via virtualenv or pyvenv), thereforepip
should not tamper with system files, or require superuser privileges for running.As what regards Debian, the
gpio
group should be created by thepostinst
script . You can runto have a list of examples.
The
/etc/udev/rules.d/99-gpio.rules
should be handled bydpkg
via theconffiles
control file. See also https://www.debian.org/doc/debian-policy/ch-files.html#s-config-filesLast edit: Anonymous 2015-09-14
Any update on when this new version will be released?
Raspbian Jessie .deb packages of 0.6.0a3 have been produced/tested and will hopefully be in the next Foundation Raspbian release (due out shortly I believe). For other Linux distributions, testing is still ongoing.
Release 0.6.1 has been made for Jessie and Wheezy. You do not need sudo under Jessie.
In Wheezy, you do not need sudo once user groups and udev settings have been modified. At the moment this has to be done manually.
Other distributions will need the gpio group, udev settings and correct kernel that provides /dev/gpiomem adding for sudoless operation.