GPIO permissions for libgpiod sudo or not

Discussion in 'UDOO X86' started by Quack, Aug 23, 2020.

  1. Quack

    Quack UDOOer

    Joined:
    Jun 27, 2020
    Messages:
    16
    Likes Received:
    4
    In a previous post https://www.udoo.org/forum/threads/using-braswell-gpio-i2c-and-uart-pins.32423/ I discussed the use of the libgpiod library, which provides a CLI as well as Python and C++ bindings to replace the depreciated sysfs system. It creates 4 devices which you can see using ls /dev/gpio* as /dev/gpiochip0-4. If you examine the permissions they are root only and you have to run any programs using super user privileges. e.g. sudo gpiodetect etc.

    This is for good reason! There are actually over 200 gpio lines managed by the chips which control the other chips, output and input ports and switches etc. Changing some of them inadvertently can really muck things up. For this reason I strongly suggest the following practice when programming. Assign aliases or names using define in your C headers e.g. #define D24_chip = 0 D24_line = 47 or names in your Python files to refer to the chip and line for a pin. e.g. D24 = (3,47) (python tuple). Use your aliases not numbers in your calls to the library.

    All but 3 of the External GPIO pins are managed by gpiochip3. To run without having superuser privileges, you need to do the following: Create a gpiod group, add user to it and add a file to the /etc/udev/rules.d/ to assign the gpiochips to the gpiod group and give rw privileges. The following works on linux Mint 20

    sudo groupadd gpiod
    sudo usermod -G gpiod <user>

    then create a file in the /etc/udev/rules.d named 60-gpiod.rules containing the following:
    # udev rules for gpio port access through libgpiod
    SUBSYSTEM=="gpio", KERNEL=="gpiochip[0-4]", GROUP="gpiod", MODE="0660"

    From command line the following should work:
    sudo echo '# udev rules for gpio port access through libgpiod
    SUBSYSTEM=="gpio", KERNEL=="gpiochip[0-4]", GROUP="gpiod", MODE="0660"' > /etc/udev/rules.d/60-gpiod.rules

    Then reboot.
     
    Maximo and waltervl like this.
  2. Quack

    Quack UDOOer

    Joined:
    Jun 27, 2020
    Messages:
    16
    Likes Received:
    4
    As I noted above, the permissions give you the ability to cause major havoc. If the you look at the gpio table, all but 1 of the external gpio pins are controlled by gpiochip 3 (pin # 40 uses gpiochip 1). The UART and i2c pins have their permission set if you are accessing them through their interfaces so you don't need access to them unless you're using libgpiod to write your own drivers for them.

    For gpiochip 3 (all but pin #40): Create /etc/udev/rules.d/60-gpiod.rules
    with:

    # udev rules for gpio port access through libgpiod
    SUBSYSTEM=="gpio", KERNEL=="gpiochip3", GROUP="gpiod", MODE="0660"


    if you want to use pin # 40 change it to :

    # udev rules for gpio port access through libgpiod
    SUBSYSTEM=="gpio", KERNEL=="gpiochip[13]", GROUP="gpiod", MODE="0660"


    Verify you have permission by ll /dev/gpio*.

    This breaks the CLI permissions for the libgiod utilities, gpiodetect, gpioset etc. You will need to use sudo for them, but you can run you progams without needing sudo.

    I've included an updated table.
     

    Attached Files:

  3. Quack

    Quack UDOOer

    Joined:
    Jun 27, 2020
    Messages:
    16
    Likes Received:
    4
    Oops! I mis-wrote my addendum. There are 3 external gpio lines not controlled by gpiochip 3. Pins 36 and 37 are on gpiochip 0, pin 40 on gpiochip 1. If you want to use them use
    "SUBSYSTEM=="gpio", KERNEL=="gpiochip[013]", GROUP="gpiod", MODE="0660"
    In your udev file. This prevents you from interfering with chips 2 and 4.
     
  4. Quack

    Quack UDOOer

    Joined:
    Jun 27, 2020
    Messages:
    16
    Likes Received:
    4
    Updated pinout table for gpio,I2c and Uarts
     

    Attached Files:

Share This Page