Background
I am installing a Le Potato on my network to run Pi Hole. To prevent the SD card from getting corrupted during a power outage, I decided to get a UPS for the Le Potato. Doing some research I found this intelligent UPS hat on LiFePO4wered.com. It comes with a daemon that can be installed on Raspbian which communicates with the hat over I2C.
Disclaimer
I am just a hobbyist with no affiliation to Libre Computer or LiFePO4wered. I can’t guarantee that these steps will work for everyone, but I just wanted to share the steps I took, to make it easier for anyone who wants to do something similar.
1. Le Potato Setup
-
Install and boot Raspbian on the Le Potato and configure it how you want. (I setup SSH, and installed Pi-Hole at this stage.)
-
To communicate with the HAT you will need to enable the I2C wrapper. To do this run the following command:
sudo ldto merge i2c-ao
- (optional) The controller in the Lifepo4wered hat monitors the TX pin (pin 8) on the 40 pin header to determine if the SBC has shutdown before dropping power to it. Unfortunately due to how the boot loader operates in the Le Potato, I could not get this feature to work. But if you do need to use the UART pins for your project, you can enable the UART wrapper using the command below. Just be sure to follow the extra configuration step 3. LiFePO4wered Configuration/Step 5
sudo ldto merge uart-a
- Reboot your Le Potato to enable I2C and the optional UART headers.
2. LiFePO4wered Daemon Setup
- Follow the Readme instructions at the link below, up to the make commands.
sudo apt update && sudo apt-get -y install build-essential git libsystemd-dev
git clone https://github.com/xorbit/LiFePO4wered-Pi.git
cd LiFePO4wered-Pi/
- Open the lifepo4wered-access.c file with your preferred text editor. Change the I2C_BUS preprocessor definition from 1 to 0. Then save and close the file. (*)
/* LiFePO4wered/Pi access constants */
#define I2C_BUS 1
#define I2C_ADDRESS 0x43
#define I2C_WR_UNLOCK 0xC9
to
/* LiFePO4wered/Pi access constants */
#define I2C_BUS 0
#define I2C_ADDRESS 0x43
#define I2C_WR_UNLOCK 0xC9
- Open the lifepo4wered-daemon.c file with your preferred text editor. Change the second parameter of the call to systemd for a shutdown from “poweroff” to “halt”. Then save and close the file. (**)
void shut_down(void) {
...
#elif defined SYSTEMD
char *params[3] = {"systemctl", "poweroff", NULL};
execv("/bin/systemctl", params);
#else
...
}
to
void shut_down(void) {
...
#elif defined SYSTEMD
char *params[3] = {"systemctl", "halt", NULL};
execv("/bin/systemctl", params);
#else
...
}
- Follow the rest of the commands from the github readme to make and install the daemon.
make all
make user-install
3. LiFePO4wered Configuration
-
Power off your Le potato and install the hat. While doing this, I had to desolder the IR sensor on the Le Potato since it collided with the power switch on the LiFePO4wered hat. If you need the IR sensor there are other alternatives such as removing the power button on the hat and installing a new one on top of the hat with the SW SMD pads or by using the through hole BTN pads.
-
Plug power into the LiFePO4wered hat and hold the power button down for 2 seconds. The PWR led will illuminate and then blink when it applies power to the SBC and waits for it to boot. If everything is working correctly from the previous steps, after some time the PWR LED will go solid indicating that the daemon connected to the Hat and told it that the SBC was powered up.
-
Log onto the Le Potato and verify that the hat can be seen on the I2C bus by running the following command. This should print out all the parameters of the hat that can be accessed from the I2C bus. If it returns with a -1 or -2 check the github Readme for more information.
lifepo4wered-cli get
- Once you can connect to the hat over I2C you will need to set the PI_SHDN_TO parameter to 30 seconds using the command below. This will cause the Hat wait 30 seconds after the daemon tells it the SBC is shutting down before the hat drops power to the SBC. If the SBC reboots within those 30 seconds and the Daemon reports to the hat that it is running within those 30 seconds, the hat will stop its shutdown procedure and continue to power the SBC until the next shutdown call is made. In my testing 30 seconds seemed to work well. (**)
lifepo4wered-cli set PI_SHDN_TO 30
- (Optional) If you enabled the UART pins you will also need to set the SHDN_DELAY parameter to 256 to allow a reboot command to work properly without shutting your Le Potato down during a reboot. Doing this will cause the hat to wait approximatly 32 to seconds from seeing the TX UART drop out until it drops power to the SBC. If the daemon reports that it is running, within those 32 seconds the shutdown procedure stops and the hat will continue to power the SBC. In my testing the value 256 seemed to work well.
lifepo4wered-cli set SHDN_DELAY 256
Footnotes
(*) When enabling the I2C wrapper you can use “sudo ldto enable i2c-ao”. The I2C interface will then appear at /dev/i2c-1. However, this command is temporary and is reset on the the next shutdown. The LiFePO4wered daemon reports to the HAT that it is powered soon after it boots. So you will need to have the I2C interface open before the daemon runs. To do this you will use the command “sudo ldto merge i2c-ao” to make the wrapper permanent. However, on a reboot, after this command is issued, the I2C interface will now be at /dev/I2C-0. Thus the I2C bus needs to be changed from 1 to 0 in the daemon.
(**) When shutting down the Le Potato the proper method is to halt it. If you use the power down method, the boot loader with restart the SBC similar to a reboot. Thus the “halt” parameter is used. However, when doing this the TX UART signal is never dropped out, because the hardware never powers down, and thus the LiFePO4wered hat never detects that the SBC is shutdown. To get around this the Shutdown Timeout parameter is used.