jimg Posted April 17, 2017 Posted April 17, 2017 I'm trying to use a few of the GPIO pins as output pins on an Orange Pi PC+ running Armbian 5.26 and legacy kernel 3.4.113-sun8i. When I look at the FEX file, it appears that the GPIO pins have all been disable d by default: [gpio_para] gpio_used = 0 gpio_num = 0 I don't recall them being disabled by default in previous versions of Armbian. I s this a recent change? Anyway, I edited the file and changed it to: [gpio_para] gpio_used = 1 gpio_num = 3 gpio_pin_1 = port:PA13<1><default><default><default> gpio_pin_2 = port:PA14<1><default><default><default> gpio_pin_3 = port:PD14<1><default><default><default> then did the following: $ fex2bin /tmp/custom.fex /boot/bin/custom.bin $ ln -sf /boot/bin/custom.bin /boot/script.bin <reboot> $ modprobe gpio-sunxi But I cannot seem to get the GPIO pins working: $ sudo echo 1 > /sys/class/gpio/export -bash: /sys/class/gpio/export: Permission denied Any advice on what I'm doing wrong?
zador.blood.stained Posted April 17, 2017 Posted April 17, 2017 gpio_sunxi module and settings in the FEX file are obsolete for this kernel and should not be used. Instead pin numbers for export should be calculated from the pin name according to mainline formula in the wiki: https://linux-sunxi.org/GPIO PA13 will be pin number 13 and PD14 will be pin number 110
jimg Posted April 19, 2017 Author Posted April 19, 2017 What is now the correct way to change the pin settings on kernel 3.4.113 if not modifying the FEX file? I didn't think it used Device Tree since it's below V 3.10. It still uses FEX files for other features (e.g., I got uart3 working on ttyS3 by simply editing the FEX file). FWIW, I had also tried using the mainline pin numbers to turn them on and off but still had the same problem. The example Accessing the GPIO pins through sysfs on sunxi-3.4 on the sunxi wiki uses a sequential numbering system defined by the FEX file to access the pins, not the mainline pin numbering system. That's why I tried using the method I did.
jimg Posted April 23, 2017 Author Posted April 23, 2017 You are correct: it's not necessary to modify the FEX file to use the GPIO pins, and you should use the mainline formula to calculate pin numbers. I'll share what I learned attempting to solve this problem in case anyone else runs into it. As the error states, it's a permissions problem. Using "sudo echo" won't work because the redirection occurs before sudo is started, which ends up you trying to write to a file you don't have permissions to. To do a one-off manipulation of a GPIO pin, you have to start a separate shell as a superuser first, then use echo. For instance to turn pin A10 on: $ sudo sh # echo 10 > /sys/class/gpio/export # echo out > /sys/class/gpio/gpio10/direction # echo 1 > /sys/class/gpio/gpio10/value Or you can use tee to avoid creating a subshell: $ echo 10 | sudo tee /sys/class/gpio/export $ echo out | sudo tee /sys/class/gpio/gpio10/direction $ echo 1 | sudo tee /sys/class/gpio/gpio10/value A better, more permanent solution is to create a separate GPIO group, add yourself as a user to that group, and then give the GPIO group permissions to modify the GPIO pin directories: $ sudo groupadd gpio $ sudo usermod -aG gpio <your_username> $ su <your_username> $ sudo chgrp gpio /sys/class/gpio/export $ sudo chgrp gpio /sys/class/gpio/unexport $ sudo chmod 775 /sys/class/gpio/export $ sudo chmod 775 /sys/class/gpio/unexport You'll also have to modify the permissions on the directory of each GPIO pin you add. Again using pin A10 as an example: $ echo 10 > /sys/class/gpio/export $ chgrp -HR /sys/class/gpio/gpio10 $ chmod -R 775 /sys/class/gpio/gpio10 Then you can change that pin as necessary without being a superuser: $ echo out > /sys/class/gpio/gpio10/direction # set as output pin $ echo 1 > /sys/class/gpio/gpio10/value # set high $ echo 10 > /sys/class/gpio/gpio/unexport # reset the pin 1
jimg Posted April 23, 2017 Author Posted April 23, 2017 Quote $ chgrp -HR /sys/class/gpio/gpio10 The chgrp command is incorrect. It should be: $ sudo chgrp -HR gpio /sys/class/gpio/gpio10
zador.blood.stained Posted April 23, 2017 Posted April 23, 2017 33 minutes ago, jimg said: As the error states, it's a permissions problem. Using "sudo echo" won't work because the redirection occurs before sudo is started, which ends up you trying to write to a file you don't have permissions to. You can use "tee" command like this: echo 10 | sudo tee /sys/class/gpio/export 34 minutes ago, jimg said: A better, more permanent solution is to create a separate GPIO group, add yourself as a user to that group, and then give the GPIO group permissions to modify the GPIO pin directories Or this can be solved with udev rules, but it needs to be tested on different kernels to be included in default Armbian installations
Recommended Posts