August 16, 2020

Orange Pi PC 2 - blink the onboard LED's

I am investigating this in detail because I am unable to blink the onboard LED's from Kyu. I had no problem with them on my H3 Orange Pi board.

The board is sitting on my desk running the latest version of Armbian. One green LED is lit up, and there is another red LED somewhere. (The red LED is right next to the green one, as we discover below).

Linux has this whacko GPIO interface, and that can be used via the filesystem to screw with the LED's.
cd /sys/class/leds
uname -a
Linux orangepipc2 5.4.43-sunxi64 #20.05.2 SMP Tue Jun 2 17:20:17 CEST 2020 aarch64 GNU/Linux

tom@orangepipc2:/sys/class/leds$ ls -l
total 0
lrwxrwxrwx 1 root root 0 Jan  1  1970 orangepi:green:pwr -> ../../devices/platform/leds/leds/orangepi:green:pwr
lrwxrwxrwx 1 root root 0 Jan  1  1970 orangepi:red:status -> ../../devices/platform/leds/leds/orangepi:red:status
Following the above links, we see:
cd /sys/devices/platform/leds/leds
ls -l
drwxr-xr-x 3 root root 0 Jan  1  1970 orangepi:green:pwr
drwxr-xr-x 3 root root 0 Jan  1  1970 orangepi:red:status
ls -l *
'orangepi:green:pwr':
total 0
-rw-r--r-- 1 root root 4096 Aug 16 22:28 brightness
lrwxrwxrwx 1 root root    0 Aug 16 22:28 device -> ../../../leds
-r--r--r-- 1 root root 4096 Aug 16 22:28 max_brightness
drwxr-xr-x 2 root root    0 Aug 11 03:01 power
lrwxrwxrwx 1 root root    0 Jan  1  1970 subsystem -> ../../../../../class/leds
-rw-r--r-- 1 root root 4096 Aug 16 22:28 trigger
-rw-r--r-- 1 root root 4096 Jan  1  1970 uevent

'orangepi:red:status':
total 0
-rw-r--r-- 1 root root 4096 Aug 16 22:28 brightness
lrwxrwxrwx 1 root root    0 Aug 16 22:28 device -> ../../../leds
-r--r--r-- 1 root root 4096 Aug 16 22:28 max_brightness
drwxr-xr-x 2 root root    0 Aug 11 03:01 power
lrwxrwxrwx 1 root root    0 Jan  1  1970 subsystem -> ../../../../../class/leds
-rw-r--r-- 1 root root 4096 Aug 16 22:28 trigger
-rw-r--r-- 1 root root 4096 Jan  1  1970 uevent
Typing the following turns on the red LED and off the green LED:
echo 1 >/sys/class/leds/orangepi\:red\:status/brightness
echo 0 >/sys/class/leds/orangepi\:green\:pwr/brightness

What about GPIO

su
/sys/class/gpio
tom@orangepipc2:/sys/class/gpio$ ls -al
total 0
drwxr-xr-x  2 root root    0 Jan  1  1970 .
drwxr-xr-x 58 root root    0 Jan  1  1970 ..
--w-------  1 root root 4096 Jan  1  1970 export
lrwxrwxrwx  1 root root    0 Jan  1  1970 gpiochip0 -> ../../devices/platform/soc/1c20800.pinctrl/gpio/gpiochip0
lrwxrwxrwx  1 root root    0 Jan  1  1970 gpiochip352 -> ../../devices/platform/soc/1f02c00.pinctrl/gpio/gpiochip352
--w-------  1 root root 4096 Jan  1  1970 unexport
To access a pin, you have to write the number to the export "file"
echo 2 > /sys/class/gpio/export
After doing this, a new "file" appears:
lrwxrwxrwx 1 root root    0 Aug 17 03:55 gpio2 -> ../../devices/platform/soc/1c20800.pinctrl/gpiochip1/gpio/gpio2

echo out > /sys/class/gpio/gpio2/direction
echo 1 > /sys/class/gpio/gpio2/value
This is all done via sysfs
root@orangepipc2:/sys/class/gpio# mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
However, sysfs for gpio is now deprecated:

Orange Pi GPIO hardware

In theory there are 10 chips with 32 bits each. I call the ten chips A through J. However, not all chips exist, and not all bits on each chip are functional. The first 7 chips (A-G) do exist, and the last (J) does also. The last (J) is "special" and is at a separate base address from all the rest.

0x01C20800 is the base address for the first 7 (A-G).
0x01F02c00 is the base address for the last "special" GPIO (J).

The red LED is the "status" LED and is bit 15 on GPIO A
The green LED is the "power" LED and is bit 10 on GPIO J (the special GPIO).
These bit numbers start counting at 0.


Have any comments? Questions? Drop me a line!

Tom's electronics pages / tom@mmto.org