GPIO through /sys/class

Discussion in 'UDOO NEO' started by major7, Nov 17, 2015.

  1. major7

    major7 New Member

    Joined:
    Nov 20, 2014
    Messages:
    4
    Likes Received:
    0
    Hi, in the docs it states that you can access GPIO ports through their numbers in

    /sys/class/gpio/gpio<pinnr>

    if I look do ls /sys/class/gpio/ i'm only seeing gpiochip[0 32 64 96 128 160 192]
    How does these relate to the gpio pins?
    Is this still work in progress?
     
  2. Andrea Rovai

    Andrea Rovai Well-Known Member

    Joined:
    Oct 27, 2014
    Messages:
    1,703
    Likes Received:
    240
  3. major7

    major7 New Member

    Joined:
    Nov 20, 2014
    Messages:
    4
    Likes Received:
    0
    I've read and done the export thing but the ports didn't show up
    after I did some more investigations it seems it doesn't work with sudo
    after becoming root it works like stated in the documentation
    which is less then ideal, as i don't want my application to run as root
    but ii did unblock me, thanks
     
  4. Andrea Rovai

    Andrea Rovai Well-Known Member

    Joined:
    Oct 27, 2014
    Messages:
    1,703
    Likes Received:
    240
    You should create a rule under
    Code:
    cat /etc/udev/rules.d/10-imx.rules
    to give these files the permissions of the "udooer" user.
    Try to add these two lines at the bottom:
    Code:
    SUBSYSTEM=="gpio", ACTION=="add", PROGRAM="/bin/sh -c 'chown -R root:ugpio /sys%p'"
    SUBSYSTEM=="gpio", ACTION=="add", PROGRAM="/bin/sh -c 'chmod -R g+rw /sys%p'"
    add the user "udooer" at the group ugpio
     
  5. necromancer

    necromancer New Member

    Joined:
    Nov 10, 2015
    Messages:
    26
    Likes Received:
    8
    I followed the steps you mentioned, but i still cannot run run export without sudo.
     
  6. Andrea Rovai

    Andrea Rovai Well-Known Member

    Joined:
    Oct 27, 2014
    Messages:
    1,703
    Likes Received:
    240
    Dear Necromancer,
    to access GPIO you can use this library made by @Julien.
    Tell us if it works for you.
     
    Julien likes this.
  7. Jarryd Son

    Jarryd Son New Member

    Joined:
    Nov 24, 2015
    Messages:
    3
    Likes Received:
    1
    I have the same problem. I can only use something like sudo sh -c "echo ..." to do anything with the GPIO's or I have to be logged in as root. I tried appending those extra lines but nothing changed.
     
  8. Xykon

    Xykon Member

    Joined:
    Nov 1, 2013
    Messages:
    64
    Likes Received:
    20
    First off make sure you create the group ugpio and add it to the udooer user:

    sudo groupadd -r ugpio
    sudo usermod -a -G ugpio udooer

    When I added the lines mentioned above I could see that the gpios were available for the ugpio group but not /sys/class/gpio/export
    So I added another two lines to the udev file:

    SUBSYSTEM=="gpio", ACTION=="add", PROGRAM="/bin/sh -c 'chown -R root:ugpio /sys/class/gpio/export'"
    SUBSYSTEM=="gpio", ACTION=="add", PROGRAM="/bin/sh -c 'chmod -R g+w /sys/class/gpio/export'"

    Now I can do as user udooer:
    udooer@udooneo:~$ echo 25 > /sys/class/gpio/export
    udooer@udooneo:~$ echo out > /sys/class/gpio/gpio25/direction
    udooer@udooneo:~$ echo 1 > /sys/class/gpio/gpio25/value

    And the LED I connected on header J6 port 24 turns on.
     
    Last edited: Nov 25, 2015
    Julien, Andrea Rovai and Jarryd Son like this.
  9. Jarryd Son

    Jarryd Son New Member

    Joined:
    Nov 24, 2015
    Messages:
    3
    Likes Received:
    1
    I have added everything that you said and it still doesn't work. It keeps saying permission denied.
     
  10. Jarryd Son

    Jarryd Son New Member

    Joined:
    Nov 24, 2015
    Messages:
    3
    Likes Received:
    1
    Nevermind. I forgot to reboot/logout for the changes to take effect. It's working now. Thanks Xykon :)
     
    Andrea Rovai likes this.
  11. Julien

    Julien New Member

    Joined:
    Nov 8, 2015
    Messages:
    21
    Likes Received:
    9
    This is an awesome answer. Do you care if I copy it to a Gist?
     
    Xykon likes this.
  12. Julien

    Julien New Member

    Joined:
    Nov 8, 2015
    Messages:
    21
    Likes Received:
    9
    Thanks a lot for the mention, unfortunately my library also needs root privileges for changing values :(
     
  13. Xykon

    Xykon Member

    Joined:
    Nov 1, 2013
    Messages:
    64
    Likes Received:
    20
    Not at all, everything I post here should be considered "public domain". The -R after the chown and chmod command isn't necessary, as it's only one file (-R is for recursive subdirectories). So this should work too, although I haven't tested it personally as I've got a very busy work week:

    SUBSYSTEM=="gpio", ACTION=="add", PROGRAM="/bin/sh -c 'chown root:ugpio /sys/class/gpio/export'"
    SUBSYSTEM=="gpio", ACTION=="add", PROGRAM="/bin/sh -c 'chmod g+w /sys/class/gpio/export'"

    You might also want to add that a reboot should be performed after these changes... (you can get around that if you know what you're doing but I don't have time to validate the exact steps right now)
     
    Last edited: Dec 3, 2015
    Julien and Andrea Rovai like this.
  14. Julien

    Julien New Member

    Joined:
    Nov 8, 2015
    Messages:
    21
    Likes Received:
    9
    Alright, thanks!
     
  15. TheHat

    TheHat New Member

    Joined:
    Dec 16, 2015
    Messages:
    3
    Likes Received:
    0
    I updated /etc/udev/rules.d/10-imx.rules by adding the following lines to the very end of the file:
    SUBSYSTEM=="gpio", ACTION=="add", PROGRAM="/bin/sh -c 'chown -R root:ugpio /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:ugpio /sys/class/gpio/export'"
    SUBSYSTEM=="gpio", ACTION=="add", PROGRAM="/bin/sh -c 'chmod -R g+w /sys/class/gpio/export'"

    I also did:
    sudo groupadd -r ugpio
    sudo usermod -a -G ugpio udooer

    I then re-booted

    I can change and see the direction change by:
    echo in > /sys/class/gpio/gpio25/direction
    echo out > /sys/class/gpio/gpio25/direction
    cat /sys/class/gpio/gpio25/direction

    but I CAN NOT change the output value, it's always a 0 when I do the following
    echo 1 > /sys/class/gpio/gpio25/value
    cat /sys/class/gpio/gpio25/value

    I have tried below which didn't work either.
    sudo echo 1 > /sys/class/gpio/gpio25/value

    What am I missing?
    Thanks for any help.
     
  16. necromancer

    necromancer New Member

    Joined:
    Nov 10, 2015
    Messages:
    26
    Likes Received:
    8

    How do you know that the value is always 0? Do you have an LED connected? does the LED turn on and off?
    Did you try to read the value by
    if you did, then the document also says the following;
    So try putting an LED in that pin to see the led turn on and off
     
  17. TheHat

    TheHat New Member

    Joined:
    Dec 16, 2015
    Messages:
    3
    Likes Received:
    0
    Thanks for the clarifications... It sounds like the GPIO it's self is not a true bi-directional pin if "read the value is not guaranteed that the kernel value is coherent with the voltage found on the external pinout."
    I am able to see the output change state using a DVM, now. Thanks again.
     
  18. Andrea Rovai

    Andrea Rovai Well-Known Member

    Joined:
    Oct 27, 2014
    Messages:
    1,703
    Likes Received:
    240
    This happens when the GPIO is setted in output and you want to read in, as stated in the docs.

     
  19. waltervl

    waltervl UDOOer

    Joined:
    Dec 12, 2015
    Messages:
    2,314
    Likes Received:
    580
    Just to inform you that this update is not needed anymore with the just released Udoobuntu 2 RC2! GPIO is exported at startup and no need for sudo rights to set value!
     
  20. Julien

    Julien New Member

    Joined:
    Nov 8, 2015
    Messages:
    21
    Likes Received:
    9
    That's great, guess it's time for me to update the little fella. How did you learn about the RC2? I haven't received any email
     

Share This Page