Sunday 18 October 2015

Setting up the UPS PIco - Raspbian

Please note, the UPS PIco uses GPIO22 & GPIO27 to communicate with the Pi.
These need to be free for it to function.

I2C setup
First enable I2C communication via raspi-config. I am using the root account here but you can use
sudo with the pi account to make these changes instead. E.G. pi@picoberry ~ $ sudo i2cdetect -y 1

root@picoberry:~# raspi-config
-> 8. Advanced Options -> A7. I2C -> <Yes> -> <Ok> (I2C enabled) -> <Yes> -> <Ok> (load I2C by default) then <Finish>

(This basically adds 'dtparam=i2c_arm=on' to the /boot/config.txt file)
You can reboot now or later.

Add the modules 'i2c-bcm2708' and 'i2c-dev' so they are loaded at boot time. My file looks like this…
root@picoberry:~# cat /etc/modules
snd-bcm2835
i2c-bcm2708
i2c-dev

Install the I2C tools program.
root@picoberry:~# apt-get install i2c-tools

Now we need to reboot the Pi. 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@picoberry:~# reboot
.
.
.
root@picoberry:~# i2cdetect -y 1 (or i2cdetect -y 0 (on a A+))












Use the picofssd.py script or create it yourself somewhere, for example at /home/pi/picofssd.py
This script enables and monitors GPIO pins GPIO27 (pulse train / RUN1 pin) and GPIO22 (File Safe
Shutdown Pin).
[+] 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

When your run picofssd.py 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.

We need this script to start after each reboot so add it into /etc/rc.local at the end of the file but
before 'exit 0'

root@picoberry:~# cat /etc/rc.local
.
.
.
python /home/pi/picofssd.py &

exit 0

Serial connection to the PIco UPS
This is an 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. To make the serial line available disable logins on the serial port.
First edit /etc/inittab and comment out '#' the ttyAMA0 line out at the end of the file.

root@picoberry:~# vi /etc/inittab
.
.
.
#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100

Save the modified file.

Next remove this serial port from the boot log output so it looks something like this...
root@picoberry:~# cat /boot/cmdline.txt
dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait

Time to reboot
root@picoberry:~# reboot

We can use the minicom program to connect to the PIco over this serial port
root@picoberry:~# sudo apt-get install minicom

When installed connect to the UPS PIco. Press Ctrl-A followed by z for the help menu.
root@picoberry:~# minicom -b 38400 -o -D /dev/ttyAMA0
Ctrl-A followed by q to quit.

The software RTC
If you need an alternative system clock you can use the one from the PIco.
Edit  /etc/modules and add 'rtc-ds1307' followed by another reboot.
root@picoberry:~# vi /etc/modules
snd-bcm2835
i2c-bcm2708
i2c-dev
rtc-ds1307

Next create the device.
root@picoberry:~# echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device

You can check the current time on the PIco using the 'hwclock' command.
root@picoberry:~# hwclock -r
Write the current system time to the PIco clock.
root@picoberry:~# hwclock -w

To have the system time update from the PIco after a reboot the device path needs to be recreated and
the PIco time copied to the system. Add the following into /etc/rc.local before the 'exit 0'

echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
hwclock -s

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

1 comment:

  1. Alan I am trying to modify my Dashcam to add the UPS Pico. I have all installed and seems to be working, but am confused as how the picofssd.py get run. Will it start automatically when I press the power button, or do I need to add some additional code?
    Thanks,
    Danny

    ReplyDelete