What you can do with the PiXi - No. 1
Intelligent Power Switch for the Raspberry Pi
How would you like to turn the power to your Raspberry Pi on and off at the push of a button? Well you can do this with the PiXi thanks to the power of the FPGA. The PiXi integrates an electronic switch in-line with the 5V power supply to the Raspberry Pi on the Raspberry Pi’s GPIO connector. The FPGA on the PiXi has direct control over this switch so we just need to configure the FPGA to control that switch in some way…
The idea of a ‘smart’ power switch for the Raspberry Pi is not a new one, there are already several add-on boards that do this very well. When we came up with the design for the PiXi 2.0 we thought we’d do the same only in this case we can use the FPGA on the PiXi to control the power switch. So why is this way any better?
Firstly we’ve added a function in the FPGA to allow any of the GPIO inputs to the PiXi or the on-board switches to control when the power is turned on or off. So we can also use one of the on-board switches on the PiXi as a power on/off button, or we could add an external switch using any one of the GPIO signals. We could go a little more advanced and look for activity on the RS232 serial interface port on the PiXi, potentially looking for a specific character or string of characters in the serial data to turn the power on or off. We could hook it up to an infra-red receiver so that, for example, when something moves through a beam of infra-red light the Raspberry Pi is automatically switched on and perhaps starts taking a series of pictures or a video of whatever moved through the infra-red beam - or perhaps it starts taking measurement from sensors that might be connected to the Raspberry Pi or to the PiXi. One of the important things to note here is that with the FPGA powered up and running and the Raspberry Pi is switched off, we’re consuming very little power and in this low-power state we can use the FPGA to detect specific events that we want to use to trigger the power-up of the Raspberry Pi so it’s great for battery powered monitoring applications.
For now though, we’ve just implemented a simple power switch with a little extra in the form of an intelligent power-tracking state-machine designed into the FPGA so that we can tell when the Pi has completed booting-up and also when the Pi has completed a halt instruction.
Here’s how it works:
Let’s assume we’re starting in a power off state where we have no power applied to the PiXi or the Raspberry Pi.
1) Firstly we connect the power to the PiXi. This triggers the FPGA programming process which completes in about one second. The intelligent power controller within the FPGA is now ready to work. The Pi remains off at this point because the ‘state-machine’ that controls the Pi-Power switch defaults to the ‘off’ state. In the ‘off’ state an LED flashes once every five seconds to indicate that the Pi is currently switched off. This LED can be one of the PiXi’s on-board LEDs or it could remotely mounted LED that’s connected to an external GPIO signal on the PiXi.
2) Next we press the power button for about half a second - this triggers the switch and turns the Pi on.
3) The Pi-Power ‘state machine’ then enters it’s ‘booting-up’ state and flashes an LED quickly to indicate that the Pi is booting-up. The ‘state machine’ is now waiting for a signal from the Raspberry Pi to indicate that it has finished booting. The FPGA does this by waiting for the Raspberry Pi to write to a register within the FPGA through the SPI interface. We’ve installed a simple script on the Pi which runs at the end of the boot cycle to write to a register in the FPGA and it’s this that tells the PiXi FPGA that booting is complete.
4) So the Pi has written to the register to tell the PiXi FPGA that the boot cycle is complete. The FPGA detects this and the Pi-Power ‘state-machine’ now enters the ‘ON’ state and we stop flashing the LED. The LED now remains permanently on to show that booting and power-up is now complete.
5) The Pi Power state-machine is now in the ON state and is waiting for the power switch to be pressed again, this time it would trigger the power-off process to begin.
6) So now we want to turn the power off, we push the power button for about half a second which puts the state-machine into the ‘Request Halt’ state.
7) In the ‘Request Halt’ state the FPGA drives a GPIO signal to the Pi for a fraction of a second before releasing the drive on that signal. On the Pi we’ve installed a simple Python script to look for an event on the GPIO signal. If the GPIO signal is driven by the PiXi indicating that a halt has been requested then the script responds by driving the same GPIO signal on.
I’ll add a note here to say either the Pi or PiXi FPGA can drive the same signal safely by never actually driving the signal to ‘1’ (on). Instead we configure the GPIO to use a pull-up resistor to make the GPIO go to ‘1’ (high). This way both the Pi and the PiXi can communicate with each other over this GPIO signal by each taking it in turns to either let the signal ‘float’ to ‘1’ (high) or drive the signal to ‘0’ (low) and we never have ‘contention’ on the GPIO signal where the Pi is driving the signal one way and the PiXi FPGA is driving the signal the other way.
8) After the state machine has requested a halt by driving the GPIO signal off (‘0’). It then immediately stops driving that signal and instead allows it to ‘float’ back to ‘1’ thanks to the pull-up resistor and it then start to monitor that same GPIO signal. If the GPIO is then driven off (‘0’) by the Pi then the state machine detects this and recognises this as an acknowledge from the Pi that a halt request has been acted upon by the Pi and that the Pi is now preparing to halt.
9) The FPGA enters the ‘waiting to halt’ state where it flashes the LED slowly to indicate to the user that the Raspberry Pi is powering down.
10) We can detect that the Raspberry Pi has halted in several ways. We can look for the ‘Power Down’ message on the serial port from the Raspberry Pi, we could monitor the state of all GPIO signals waiting for all of them to go off (‘0’) for a set period of time or we could simply start a count-down and power-off after the count-down. Any of these techniques can be shown to work but how well they work can vary with installation. The count-down timer works in every case.
11) Once the FPGA has detected that the Raspberry Pi has halted or that the count-down timer has reached zero, it turns the power off and the state-machine returns to the ‘off’ state.
That’s it! It probably sounds more complicated than it really is :o)
If you want to take a look at the state-diagram for the intelligent power controller, here it is:
One extra thing that we’ve added is the ability to force the power off. This can sometimes be necessary if something has gone wrong and we need to force the power off - much like you can on a typical desktop or laptop PC. Pressing the power button for more than five seconds causes the power to shut off immediately after those five seconds are complete and this happens regardless of what state the power control state-machine is in.
So that’s it. Add intelligent power control to your Raspberry Pi with an FPGA-powered PiXi board. If you want to add a power button to power-up and power-down your Raspberry Pi safely or if you need to run your Raspberry Pi application on very little power until something interesting happens which then turns the power on to the Pi then the PiXi board can help.
Thanks for reading :o) More examples soon!