Workshop
The MAGEEC project has designed a board for high frequency measurement of energy usage. This workshop will guide you through setting up and using the MAGEEC energy measurement boards.
At its simplest, the power measurement shield works by sampling the voltage drop across a resistor wire inline with a power supply. It is implemented as a shield for a ST Microelectronics STM32F4 Discovery board. The STM32F4 is used to process the raw data and pass it out over USB. The shield can measure up to three external targets simultaneously, as well as measuring its own energy usage.
The hardware is a fully open design by Dr Simon Hollis of Bristol University. It may be downloaded from GitHub: https://github.com/mageec/powersense-shield.
Contents
Software Requirements
Skip this section if you are running from the MAGEEC live CD.
This section sets up the packages required to compile the firmware for the energy monitor board, run the python scripts to communicate with the board and toolchain to compile for an arduino target board.
Install packages required to build the firmware for the energy monitor board
add-apt-repository ppa:terry.guo/gcc-arm-embedded
If on Ubuntu 13.10, the following is required
sed -i 's/saucy/raring/' /etc/apt/sources.list.d/terry_guo-gcc-arm-embedded-saucy.list
Install an ARM toolchain
sudo apt-get install gcc-arm-none-eabi
If the target board is a shrimping kit / arduino / AVR
sudo apt-get install gcc-avr gdb-avr avr-libc avrdude arduino
Install other libraries needed
sudo apt-get install git python-numpy python-scipy build-essential python-pip autoconf automake autotools-dev libtool libusb-1.0-0-dev libusb-0.1-4 sudo pip install pyusb
The following udev rules will need to be install, to use the board without being root
https://github.com/jpallister/stm32f4-energy-monitor/blob/pyusb/53-energy-monitor.rules https://github.com/jpallister/stlink/blob/master/49-stlinkv2.rules
Connecting the hardware
First mount the shield as shown in the following image. There will probably be a jumper supplied with the STM32F4, which will need removing, with the pins slotting into a socket on the underside of the shield.
For each measurement point there are two headers. One 4x1 offering pins labelled GND, OUT and IN (more on the 4th one later) and one 5x2 for selecting an inline resistor (see below).
The target power supply should be intercepted, and wired to the IN pin, with the OUT pin then connected to the power supply on the target board. GND should be connected to ground on the target board. Power supplies up to 5V are supported. Although the voltage drop is sufficient to give the current measurement, a GND connection (not shown in the above picture) is needed to get an absolute voltage measurement.
The 4th pin next to IN is a jumper to bypass an inductor. Sometimes the only way to intercept the power supply on a target board is to cut out the inductor. In this case, remove the jumper to use the inductor on the shield instead. The target device can be powered from the shield if desired. In this case IN should be connected to the 3.3V or 5V supply on the shield (the above shows this, with the brown wire picking up the 3.3V supply). A maximum of 100mA draw is available in total.
The choice of resistor depends on the target maximum power consumption. Values of 0.05Ω, 0.5Ω, 1Ω and 5Ω are provided (the 5th slot is unused and can be used to supply your own resistor). The objective is a maximum 50mV drop.
To power the board without the programming cable connected, a wire should be connected from the PA9 pin to the 5V pin. Warning: don't connect cables to both USB ports with a power cable connected.
Software setup
The first thing to do is to flash the STM32F4DISCOVERY board with the firmware required to take energy measurements. This firmware sets up the ADCs, USB and other peripherals, so that the board can talk to the host machine via USB. It performs the accumulation and conversion of the analog energy values coming from the power measurement shield.
In the following steps, skip any git clone commands if you are using the MAGEEC tools environment - all of the files are already provided on the desktop.
The energy monitor firmware uses a library called libopencm3 to interface to the hardware peripherals provided by the board. This needs to be downloaded and compiled into the toolchain:
$ git clone https://github.com/libopencm3/libopencm3 $ cd libopencm3 $ make $ sudo make DETECT_TOOLCHAIN=1 install $ cd ..
Next, we need to obtain the energy monitor sources.
$ git clone https://github.com/jpallister/stm32f4-energy-monitor $ cd stm32f4-energy-monitor $ git checkout pyusb
Then configure and compile specifying that we want to compile ARM firmware:
$ ./configure --host=arm-none-eabi $ make
This will produce a file energy_monitor in src/firmware/. The next step is to flash this to the STM32F4 board. The board has two USB interfaces, one for programming and the other for the firmware's use. The programming USB interface is at the top of the board, connected with a USB mini cable. Make sure this is plugged in, and follow the steps below to flash the board.
To communicate with the board, we need a gdb server to enable communication between the debugger, GDB, and the target board. This is provided by the opensource st-link utility.
$ cd .. $ git clone https://github.com/jpallister/stlink $ cd stlink $ ./autogen.sh $ ./configure $ make
Now, we need to run this program and leave it running in - it will talk to GDB and the STM32F4 in the background.
$ ./st-util
In a new terminal, we can now flash the firmware to the board:
$ arm-none-eabi-gdb
Choose the firmware file, tell GDB to talk to the GDB server (st-util) on port 4242 and load the firmware onto the board.
> file stm32f4-energy-monitor/src/firmware/energy_monitor > target extended-remote :4242 > load
Congratulations. You now have a programmed energy monitor board. At this point, you can disconnect the mini-USB programming cable. The micro-USB cable can now be connected to the board, and you should have a fully functioning energy monitor.
Using the software
A python library is provided which communicates with the USB board, and transfers the energy data back. Two example scripts are provided, read.py and continuous.py
The first script, read.py, sets up a port to trigger the energy measurement (PA0, the blue button). Pressing the button down causes the board to start measuring. Releasing the button causes the board to stop measuring and the data to be transferred back.
> cd stm32f4-energy-monitor/src/python/ > python read.py
The second script, continuous.py, starts measurement point 1, then repeatedly gets the current measurement. This will allow you to see the energy consumed since this beginning of the script.
> python continuous.py
There is one more script, which allows you to see the instantaneous current and voltage values at a particular measurement point
> python debug.py
Next Steps
Next we are going to run some code on our target board and have it trigger the energy measurement. These steps are aimed at one of the STM32F-X boards, and should be similar for each.
Challenges
This section gives some examples for you to try out with the energy measurement kit.
Persistence of Vision
This uses one of the shrimping.it persistence of vision kits. Use the following two links to build one of the kits.
Once you have built the kit, there are a few interesting things you can try
- Do different messages affect how much energy is consumed?
- Does compiling for space (-Os) make the code take less energy?
- Does an interrupt driven version of the code run more efficiently?
Sorting
Try different sorting algorithms. Which is most energy efficient?
BEEBS
We use the a benchmark suite called BEEBS for our research. Try running some of these benchmarks on different hardware targets, with different clock speeds. Which takes the least energy?
Troubleshooting and bugs
- Check the blue light on the board. If this is lit, then an error has occurred and the board needs to be unplugged and then plugged back in. This happens if the host polls the board too frequently.