M0AGX / LB9MG

Amateur radio and embedded systems

Banana Pi on battery power

The Banana Pi is a great alternative to the well-known Raspberry Pi. It is not a clone, nor a knock-off, rather a completely different, open single-board computer of a very similar size and good price. Most important features (compared to Raspi) to me are the 3 UARTs (one for the serial console, two free eg. for a GPS, the Raspi has only one in total) and a power management chip with battery charger.

bpi2

Other improvements are: a SATA port, two USB ports (two real ports, not shared by a hub) and a real (non-USB) Ethernet interface. The Cortex-A7 A20 SoC is used mainly for tablets, so this device built around it can be thought of a tablet without a display. Having a power management chip has the benefit of battery operation, charging, automatic switching between battery and DC input (just like every phone and tablet does). Raspberry Pi has only simple regulators and requires external components for battery operation, discharge protection etc.
bpi1

Pinout of the main connector is compatible with the first Raspberry Pi, most add-on boards should also run fine with the Banana Pi, however the composite video-out connector has slightly different placement and may collide with some components.

I want to build an OBD2 datalogger for my car, that will safely save data and shut itself down when ignition is switched off. Having even a small, rechargeable battery easily solves the problem.

Adding a li-ion battery is very simple. There are two dedicated pads named BAT near the power supply micro-USB and SATA ports. Banana can run from a single li-ion cell (typically 3.7-4.2V, several 18650 cells can be connected in parallel). The BAT1 pads can be connected to a non-rechargeable coin-cell (BIOS-type) battery for RTC backup (date and time). The device will keep track of time having the main battery anyway, so the second one is not necessary. Compare it with a Raspberry Pi, that requires external boards with batteries and chips like DS1307 to keep track of time between power shutdowns.

The complete "hack" looks very simple: bpi_batt2

With the batteries attached, Banana Pi can be turned on by pressing the power button. It will also wake up whenever power is supplied via the micro-USB connector.

Software

I use the "evil" sunxi-linux kernel. It is considered evil because it is kept out of mainline, has many patches from Allwinner, is not quite up to date and uses FEX files for configuration instead of standard device tree files, but gets the job done. I found an article about power system monitoring, but it turned out for me, that all power supply parameters can be found in /sys/class/power_supply. Standard cat can be used to view battery voltage, discharge rate, charge/discharge status and the most important - if the device is externally powered. The file is called /sys/class/power_supply/ac/online and contains 0 when running on batteries, 1 otherwise.

I made a simple script that will be run in the background all the time and shut the device down in 100 seconds after turning off external power. If power reappears, the counter will be set again to 100 seconds.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/bin/bash
DEFAULT_TIMEOUT=100 #100 seconds
DELAY=5 #check every n seconds

TIMEOUT=$DEFAULT_TIMEOUT

#set timeout to 15 minutes after a signal is received
#it can be hooked up to ~/.profile by adding killall -SIGUSR1 powerd.sh
#so when a user logs in the system will not shut down immediately
trap TIMEOUT=900 SIGUSR1

while true;
do
    AC=`cat /sys/class/power_supply/ac/online` #get power adapter status
    #AC=`cat /sys/class/power_supply/ADP1/online` #I used this line to test the script on my laptop
    echo timeout=$TIMEOUT AC=$AC

    if [ $AC == 0 ]; then
        let "TIMEOUT = $TIMEOUT - $DELAY"
    else
        TIMEOUT=$DEFAULT_TIMEOUT
    fi

    if (( $TIMEOUT < 1 )); then
        echo Shutting down!
        /sbin/shutdown -h now
        exit 0
    fi

    sleep $DELAY
done

I added a signal handler (line 10), to give myself some time to react and stop the script, when I will want to do some quick administration I can add a killall -SIGUSR1 powerd.sh to shell startup script (~/.profile or ~/.bashrc) and have some time to do the work, without forgetting to shut the device down. I don't know if the system will shutdown cleanly if the batteries run out or will it just yank the power out.

Having acpid running is also handy - system can be cleanly shut down by briefly pressing the power button. The device will not shut down if external power is present it will immediately reboot.