Fixing GPIO user permission errors
#1
A known issue from Raspberry Pi land that also effects the Pine64 is the need to use sudo in order to manipulate GPIO bits... which gets annoying pretty quickly... and tempts you to stay logged in as the root user, which is a bad idea.

There are some workarounds... i.e. using python GPIO, wiringPi (when available). But there is also a relatively simple fix you can apply in order to fix it up, which is to basically add two startup scripts that automatically fix the permissions. I've just run through this myself, and had to backtrack and check what I'd done, so please let me know if there are any mistakes or corrections needed. So... here we go!

Create a gpio group first:
Code:
groupadd gpio

Then add your username to that group:
Code:
usermod -a -G gpio username

To check that everything is good so far, you can run the below command, and you should see a line that looks a bit like "gpio:x:1001:pfeerick" (last bit should be your user name).
Code:
grep gpio /etc/group

Now, before that change to groups takes effect, you will need to log out of your current session. If you really, really don't want to, you may be able to run "newgrp gpio"... but YMMV.

Now, the final bit is to fix up the permissions of the sysfs GPIO file system. There are two bits that needs to be fixed - the export and unexport 'files' and the actual GPIO 'folders'

For the first bit, it is probably simplest to just add the two following lines to the bottom of
Code:
/etc/rc.local
(before the exit 0 line), although it may be better to put it in a separate script for neatness. They needed to be run at every startup as /sys is a virtual file system, and is created every time the pine64 is booted, so doesn't remember the permission changes.

Code:
chown -R root:gpio /sys/class/gpio
chmod -R ug+rw /sys/class/gpio

At this point, when you restart your pine64, you will be able to export and unexport GPIO pins without needing sudo. And if you don't want to restart, you can just run those commands now at the command prompt. But that is only half the story... we also need to fix the GPIO 'folders' , which have the direction and value 'files''. So, next, run the below command and create following udev rule:

Code:
sudo nano /etc/udev/rules.d/80-gpio-noroot.rules

Code:
# /etc/udev/rules.d/80-gpio-noroot.rules
# Corrects sys GPIO permissions on the Pine64 so non-root users in the gpio group can manipulate bits
#
# Change group to gpio
SUBSYSTEM=="gpio", PROGRAM="/bin/sh -c '/bin/chown -R root:gpio /sys/devices/soc.0/*pinctrl/gpio'"
# Change user permissions to ensure user and group have read/write permissions
SUBSYSTEM=="gpio", PROGRAM="/bin/sh -c '/bin/chmod -R ug+rw /sys/devices/soc.0/*pinctrl/gpio'"

This monitors the GPIO file system and makes the permission changes as needed.

To avoid needing to reboot to run this new file, just run:

Code:
sudo udevadm trigger --subsystem-match=gpio

Everything should work great now!

Concept based on https://www.raspberrypi.org/forums/viewt...6&t=118879
  Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  RPi.GPIO python module for Pine A64/A64+ aquilegia 98 149,714 10-26-2024, 07:17 AM
Last Post: Garesdq1d
  fm transmitter with gpio weasel18 2 5,527 09-10-2019, 04:28 AM
Last Post: desai_amogh
  How to use dts or other setup to declare gpio pin Interrupt (e.g. a button)? dkebler 1 4,051 06-12-2019, 10:37 AM
Last Post: dkebler
Lightbulb Sample GPIO codes highlighting RPi.GPIO-PineA64 and the PI bus MarkHaysHarris777 6 12,505 06-07-2019, 12:37 AM
Last Post: tllim
Star GPIO, SPI and I2C C++ Lib databit 7 12,596 02-04-2019, 05:45 AM
Last Post: Jeff R
Information Howto: Controlling Pine64 GPIO via the filesystem (sysfs) on linux pfeerick 4 12,874 01-24-2019, 03:36 AM
Last Post: Fifth
  GPIO and SPI SamR1 20 35,370 03-15-2018, 10:32 AM
Last Post: jomoengineer
Question GPIO shockr 7 16,438 03-11-2018, 01:52 AM
Last Post: jomoengineer
  Read GPIO problem shworker 14 24,152 08-17-2017, 01:21 PM
Last Post: martinayotte
  GPIO fiq capability joseph 3 6,848 11-10-2016, 06:07 PM
Last Post: joseph

Forum Jump:


Users browsing this thread: 1 Guest(s)