Tuesday 13 October 2015

Setting up the UPS PIco - Arch Linux

So I treated myself to a new Raspberry Pi 2B and a UPS PIco. The Pi 2 is nice and quick and allows
me to use a slightly higher resolution on the USB camera. Also, my old Pi which was built into my car
would get fairly hot during the summer. Being somewhat reluctant to relocate it and well, you know,
gadgets, I bought a UPS PIco with the separate fan kit. This means I can cool the Pi and hopefully wire
up the dashcam back to the car ignition. The PIco provides an uninterruptible power supply, lots of
additional functionality and for the cost seems a great solution.

Important note, the UPS PIco uses GPIO22 & GPIO27 to communicate with the Pi.

These need to be free for it to function.

Using the Pi camera, USB camera, GPS and a couple of LEDs I would get around 8 minutes
of run time from a fully charged 300mAh battery. The 3000mAh gave me around 40 minutes but
it is large, about the size of a 9v battery.

Here is my quick setup guide

I2C setup
First install i2c-tools.
[root@picodash ~]# pacman -S i2c-tools

Edit /boot/config.txt to configure the bootloader to enable i2c hardware. It should just need

uncommenting and changing from 'off' to 'on'
device_tree_param=i2c_arm=on

Edit /etc/modules-load.d/raspberrypi.conf and add 'i2c-dev' & 'i2c-bcm2708'. My file looks like this:-

[root@picodash ~]# cat /etc/modules-load.d/raspberrypi.conf
bcm2708-rng
snd-bcm2835
i2c-bcm2708
i2c-dev

Now we need to reboot the Pi and after that we can check if the PIco is recognised. If you have

a newer Pi (2B or B+) then the i2cbus will be number 1, for older Pi's it will be 0.

[root@picodash ~]# reboot

.
.
.
[root@picodash ~]# i2cdetect -y 1 (or i2cdetect -y 0 (on a A+))













Great! Time to enable python to use the GPIO pins.

[root@picodash scripts]# pacman -S python-pip
[root@picodash scripts]# pip install RPi.GPIO

To test the PIco UPS is working run this small python script which enables and monitors GPIO22 and

GPIO27. This program is included with the other scripts or copy and paste it from below.
[root@picodash ]# /root/scripts/picofssd.py
[+] Click to view the script
[-] Hide the script
#!/usr/bin/python
# Import the libraries to use time delays, send os commands and access GPIO pins

import RPi.GPIO as GPIO
import time
import os

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM) # Set pin numbering to board numbering
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Setup pin 27 as an input
GPIO.setup(22, GPIO.OUT) # Setup pin 22 as an output

while True: # Setup a while loop to wait for a button press
        GPIO.output(22,True)
        time.sleep(0.4) # Allow a sleep time of 0.25 second to reduce CPU usage
        GPIO.output(22,False)
        if(GPIO.input(27)==0): # Setup an if loop to run a shutdown command when button press sensed
                os.system("sudo shutdown -h now") # Send shutdown command to os
                break
        time.sleep(0.4) # Allow a sleep time of 0.25 second to reduce CPU usage

The flashing green UPS LED should switch off. After a second or two it will start flashing about
twice a second. The CHG LED will probably also come on. Control-C to stop the program.
So this script runs automatically after boot up we'll create a start up service for it...

[root@picodash scripts]# vi /usr/lib/systemd/system/picoups.service

[Unit]
Description=PIco UPS startup
After=local-fs.target

[Service]

ExecStart=/root/scripts/picofssd.py
Type=simple

[Install]

WantedBy=multi-user.target

Save the file and enable the service.

[root@picodash]# systemctl enable picoups
Start the service to test. The green UPS LED should start flashing as before.
[root@picodash]# systemctl start picoups

To check it really is working first set the number of seconds before the Pi will shut down after power loss.

[root@picodash ]# i2cset -y 1 0x6B 9 20
Pull the power supply from your Pi. After 20 seconds the Pi will shutdown. If you return the power
before then the shutdown of the Pi will be cancelled.

The software RTC
An optional step. If you need an alternative system clock you can use the PIco.
Edit /etc/modules-load.d/raspberrypi.conf and add 'rtc-ds1307' followed by another reboot
[root@picodash ~]# vi /etc/modules-load.d/raspberrypi.conf
bcm2708-rng
snd-bcm2835
i2c-bcm2708
i2c-dev
rtc-ds1307

Create the device (change i2c-1 to i2c-0 for an A+ model Pi)
[root@picodash ~]# echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
Check the clock is working.
[root@picodash ~]# hwclock -r
Write the current system time to this new clock
[root@picodash ~]# hwclock -w
If you would like the PIco to update the Pi's system time after a reboot then you need to create
this device each time and tell it to update the system time from the PIco. For example after
a reboot you would need to run...
[root@picodash ~]# echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
[root@picodash ~]# hwclock -s

Serial connection to the PIco UPS
Another optional step, if you'd like to check the PIco is responding...
The PIco has two jumpers RXD0 & TXD0, which when in place allow you to connect to it via the

serial port. The serial line has to be available by removing the parts that contain ttyAMA0 from the
/boot/cmdline.txt

[root@picodash ~]# cp /boot/cmdline.txt /boot/cmdline.txt.org

Edit this file and remove the "console=ttyAMA0,115200 kgdboc=ttyAMA0,115200" so it
looks something like this...
[root@picodash]# cat /boot/cmdline.txt
root=/dev/mmcblk0p2 rw rootwait console=tty1 selinux=0 plymouth.enable=0 smsc95xx.turbo_mode=N dwc_otg.lpm_enable=0 elevator=noop

Install the minicom program which can be used to communicate with the PIco
[root@picodash ~]# pacman -S minicom

When done reboot the Pi, login and connect to the PIco


[root@picodash]# minicom -b 38400 -o -D /dev/ttyAMA0


When connected press the UPSR (UPS Reset) button on the PIco. It will restart and display

hardware and firmware information and the current status. If the text is garbled type Ctrl+A then z
followed by an x (eXit and reset) and Yes to leave the mincom program. Then reconnect as above.
Here are some example commands (please note, your keystrokes will not be displayed).
@version
@status
@bat

If you need /dev/ttyAMA0 for something else, for example a GPS don't forget to remove the
RXD0 and TXD0 jumpers from the PIco when you have finished and it has been shut down.

Check here for manuals on the UPS PIco and further documentation.

Some useful i2c commands
Battery runtime after power loss before shutdown in seconds (fssd_batime)
# i2cset -y 1 0x6B 9 30
( Must be > 15 secs. & < 255 secs. )

Fan mode (fmode)

# i2cset -y 1 0x6B 15 [ 0x00 (fan off) | 0x01 (fan on)

Fan speed (fspeed)

# i2cset -y 1 0x6B 16 [ 0x00 (0%) | 0x01 (100%) | 0x02 (25%) | 0x03 (50%) | 0x04 (75%) ]
------
Battery voltage (mV)
# i2cget -y 1 0x69 1

Voltage supplying RPi (mV)

# i2cget -y 1 0x69 3

Voltage supply USB  (mV)

# i2cget -y 1 0x69 5

Voltage supply EPR  (mV)

# i2cget -y 1 0x69 7

Temperature in C of SOT-23 sensor (values in BCD format)

# i2cget -y 1 0x69 12

Temperature in C of TO-92 sensor if installed (values in BCD format)

# i2cget -y 1 0x69 13
------
Unconditional FSSP and power off (when battery powered)
# i2cset -y 1 0x6B 0 0xcc

Reset UPS Pico

# i2cset -y 1 0x6B 0 0xee

Voltage supplying Pi on J8 (5v)

# i2cget -y 1 0x6B 2

Battery voltage on UPS Pico error

# i2cget -y 1 0x6B 4

Battery sensor temperature

# i2cget -y 1 0x6B 6
------
Immediately start FSSP and restart
# i2cset -y 1 0x6B 8 0x00

Enable and start counting down the Still Alive Time Counter in seconds ( 0x01 - 0xfe )

# i2cset -y 1 0x6B 8 0x01

Disable counter

# i2cset -y 1 0x6B 8 0xff

No comments:

Post a Comment