Accessing GPIO without root

Discussion in 'UDOO X86' started by Frank Applin, Aug 28, 2017.

  1. Frank Applin

    Frank Applin New Member

    Joined:
    May 12, 2017
    Messages:
    10
    Likes Received:
    3
    Hi,

    Can anyone tell me the procedure(s) to allow me to access the Braswell pins [from the file system] without having to go to root?

    Right now, I have to do Sudo su (and give my password) and then I can do the following and it works (turning an LED on and off):


    # echo 346 > /sys/class/gpio/export
    # echo out > /sys/class/gpio/gpio346/direction
    # echo 1 > /sys/class/gpio/gpio346/value
    # echo 0 > /sys/class/gpio/gpio346/value
    # echo 346 > /sys/class/gpio/unexport

    But, how do I do this without needing root permission?

    Here's the kernel and OS I'm running.

    Linux NODE-UDOOX86 4.4.0-53-generic #74-Ubuntu SMP Fri Dec 2 15:59:10 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

    Frank
     
  2. waltervl

    waltervl UDOOer

    Joined:
    Dec 12, 2015
    Messages:
    2,314
    Likes Received:
    580
    You have to create a user group, give that group the proper access to the gpio folders through the udev rules and add your user to the group. For an example see this discussion: GPIO through /sys/class
     
  3. Frank Applin

    Frank Applin New Member

    Joined:
    May 12, 2017
    Messages:
    10
    Likes Received:
    3
    OK. That was for a Neo that has a different Arduino onboard. So, how do you do it for an x86? Is this process documented somewhere?
     
  4. waltervl

    waltervl UDOOer

    Joined:
    Dec 12, 2015
    Messages:
    2,314
    Likes Received:
    580
    It is not yet documented for the Udoo X86 so you are free to do it for us.
    It has nothing to do with the Arduino part. It is just Linux only. It also depends on the Linux version and distro that people are using.

    You probably just can use the same commands as in the Neo discussion. The location of the udev file is probably different but that is the only difference.

    Please keep us updated!
     
  5. Frank Applin

    Frank Applin New Member

    Joined:
    May 12, 2017
    Messages:
    10
    Likes Received:
    3
    Well, I'm not real sure about these steps because they've kind of been borrowed from several places, but this is what I did (and I have been able to recreate it).

    sudo groupadd gpio
    sudo usermod -G gpio <user>

    I created a file called gpio.rules in the /etc/udev/rules.d directory.

    in the gpio.rules file (and this is where I'm not sure exactly what needs to be in there):

    SUBSYSTEM=="gpio*", PROGRAM="/bin/sh -c 'chown -R root:gpio /sys/class/gpio && chmod -R 770 /sys/class/gpio; chown -R root:gpio
    /sys/devices/virtual/gpio && chmod -R 770 /sys/devices/virtual/gpio'"
    SUBSYSTEM=="gpio", ACTION=="add", PROGRAM="/bin/sh -c 'chown -R root:gpio /sys%p'"
    SUBSYSTEM=="gpio", ACTION=="add", PROGRAM="/bin/sh -c 'chmod -R g+rw /sys%p'"
    SUBSYSTEM=="gpio", ACTION=="add", PROGRAM="/bin/sh -c 'chown -R root:gpio /sys/class/gpio/export'"
    SUBSYSTEM=="gpio", ACTION=="add", PROGRAM="/bin/sh -c 'chmod -R g+w /sys/class/gpio/export'"


    and, reboot.

    If I delete the group gpio - I get permission denied on trying to do one of my file system GPIO calls.
    If I remove the group gpio from the user - I get permission denied on trying to do one of my file system GPIO calls.
    If I remove the gpio.rules file (and reboot) - I get permission denied on trying to do one of my file system GPIO calls.

    But, if I have all of these in place - the file system GPIO calls seem to work.

    I'll look into trying to under the rules files better.

    Frank
     
    Last edited: Aug 28, 2017
    jj_0, waltervl and ccs_hello like this.
  6. waltervl

    waltervl UDOOer

    Joined:
    Dec 12, 2015
    Messages:
    2,314
    Likes Received:
    580
    Good work, thank you!
     

Share This Page