August 16, 2018

Blink the on-board LED from Linux on the Nanopi Fire3

The Wiki says the status light should blink. Mine just stays on all of the time. Does this mean the hardware is busted? Maybe my particular kernel is not blinking the LED like the Wiki says.

I have two boards, so I pulled out the second one (a good chance to test it) and it doesn't blink either. Both boards probably aren't defective (and as you will learn, they are not, apparently the Wiki is inaccurate and the kernel no longer blinks the LED).

Linux GPIO

Let's look at the wretched linux filesystem driven GPIO scheme.

cd /sys/class/gpio
ls -l
--w------- 1 root root 4096 Jan  1  1970 export
lrwxrwxrwx 1 root root    0 Jan  1  1970 gpiochip0 -> ../../devices/platform/c0000000.soc/c001a000.pinctrl/gpio/gpiochip0
lrwxrwxrwx 1 root root    0 Jan  1  1970 gpiochip128 -> ../../devices/platform/c0000000.soc/c001a000.pinctrl/gpio/gpiochip128
lrwxrwxrwx 1 root root    0 Jan  1  1970 gpiochip160 -> ../../devices/platform/c0000000.soc/c001a000.pinctrl/gpio/gpiochip160
lrwxrwxrwx 1 root root    0 Jan  1  1970 gpiochip32 -> ../../devices/platform/c0000000.soc/c001a000.pinctrl/gpio/gpiochip32
lrwxrwxrwx 1 root root    0 Jan  1  1970 gpiochip64 -> ../../devices/platform/c0000000.soc/c001a000.pinctrl/gpio/gpiochip64
lrwxrwxrwx 1 root root    0 Jan  1  1970 gpiochip96 -> ../../devices/platform/c0000000.soc/c001a000.pinctrl/gpio/gpiochip96
--w------- 1 root root 4096 Jan  1  1970 unexport

cd gpiochip32
ls -l
-r--r--r-- 1 root root 4096 Aug 16 19:53 base
lrwxrwxrwx 1 root root    0 Aug 16 19:53 device -> ../../../c001a000.pinctrl
-r--r--r-- 1 root root 4096 Aug 16 19:53 label
-r--r--r-- 1 root root 4096 Aug 16 19:53 ngpio
drwxr-xr-x 2 root root    0 Aug 16 19:53 power
lrwxrwxrwx 1 root root    0 Jan  1  1970 subsystem -> ../../../../../../class/gpio
-rw-r--r-- 1 root root 4096 Jan  1  1970 uevent

cat label
gpiob
cat base
32
cat ngpio
32
And there you have it, the starting point for the usual haywire linux gpio setup. If you have kernel sources handy, you can read linux/Documentation/gpio.txt. Or, just use this link:

Use Linux GPIO to blink the Fire3 on board LED

My information indicates that the status light is controlled by GPIO_B_12. This would be 32+12 = 44 (in a count from 0 .. 159 on this board. So, to gain access to this bit, you do this:
cd /sys/class/gpio
echo 44 > export
Indeed, after doing this, a new directory "gpio44" magically appears! In addition, the LED goes out!!
cd gpio44
ls -l
ls -al
-rw-r--r-- 1 root root 4096 Aug 16 20:12 active_low
lrwxrwxrwx 1 root root    0 Aug 16 20:12 device -> ../../../c001a000.pinctrl
-rw-r--r-- 1 root root 4096 Aug 16 20:12 direction
-rw-r--r-- 1 root root 4096 Aug 16 20:12 edge
drwxr-xr-x 2 root root    0 Aug 16 20:12 power
lrwxrwxrwx 1 root root    0 Aug 16 20:12 subsystem -> ../../../../../../class/gpio
-rw-r--r-- 1 root root 4096 Aug 16 20:11 uevent
-rw-r--r-- 1 root root 4096 Aug 16 20:12 value
Now let's fiddle:
cat direction
in
echo out >direction
cat direction
out
And after this, the LED is back on again!
cat value
0
echo 1 >value
echo 0 >value
echo 1 >value
cat value
1
Indeed, this lets me turn on and off the green status LED. So, let's package up this knowledge as a nice little ruby script. Here is a link to download it, and if your browser will cooperate, a listing you could copy and paste if that works better. This blinks until you type Control-C, then cleans up and exits.
#!/usr/bin/ruby

# Must be run as root

# Blink the status LED on a Nanopi Fire3
# This is on GPIO B12, so bit 32+12 = 44

$bit = 44.to_s
$base = "/sys/class/gpio"
$export = "/sys/class/gpio/export"
$unexport = "/sys/class/gpio/unexport"

$gpiodir = $base + "/gpio" + $bit
$gpioval = $gpiodir + "/value"

def setup
	system "echo \"#{$bit}\" > #{$export}"
end

def cleanup
	system "echo \"#{$bit}\" > #{$unexport}"
	puts ""
	puts "All done"
end

def blink
	loop {
		sleep 0.5
		system "echo 0 >#{$gpioval}"
		sleep 0.5
		system "echo 1 >#{$gpioval}"
	}
end

setup

begin
	blink
rescue Interrupt
	cleanup
end

# THE END

Lessons Learned

Controlling the on-board LED from linux is sort of easy. At least it works like it is supposed to. The hardware on my board is therefore just fine. I still have a mystery to solve to find out why I cannot control the LED from code in U-Boot (now solved).


Have any comments? Questions? Drop me a line!

Tom's electronics pages / tom@mmto.org