Wednesday 16th November 2011

STM32 development

Over the last year I've been involved in a hardware and firmware development project using an STM32 Cortex M3 micro-controller. I've been using C to write the firmware for the project and we've had custom hardware built.

The micro-controller itself is a great piece of kit and the project as a whole has been challenging as embedded development is all new to me.

Tools

After reviewing a couple of the commercial tools used for embedded development I settled for using eclipse and the codesourcery tool chain. I came to this decision after realizing that the embedded world had really crappy IDE's compared to what I am used to.

I'm a Netbeans guy myself and I've had a tough time with Eclipse. Eclipse is messy, the UI is all over the place, it feels exactly like what it is; a load of cobbled together bits made by a horde of developers. Some of those bits are truly awesome but others make me want to weep. Seriously though The reason I chose Eclipse as my development environment was because of it's awesome tool chain support, and it's ability to make use of OpenOCD's GDB server. These two things made me reluctantly select Eclipse over my beloved Netbeans.

Since I started the project, Netbeans have solved the external tool chain problems, but it still does not connect to the "remote" GDB server that OpenOCD provides.

I found newlib to provide a cut-down version of the core C library suitable for embedded development. In short newlib is an awesome piece of software. Thanks Red Hat for newlib!

How the development environment works

So the first problem I had was figuring out how to write software and get it installed on the micro controller. When you're writing code for PC's or servers, getting your code installed is easy, you just copy it into place and bingo, it's runnable. In the embedded world, life's not so simple. In my situation it was more interesting as I didn't have an operating system running under my code, I was deploying code directly on to the hardware. I program the micro controller using a bit of kit called a Amontec JTagKey2. The JTagKey2 is connected to a JTag header on our PCB and a USB port on my development PC. A piece of software called OpenOCD is able to use the JTagKey2 to control the micro controller, halt it, reset it, as well as a bunch of other things, but most importantly OpenOCD allows you to copy compiled code from your PC to the flash memory in the micro controller.

I've setup OpenOCD to allow me to do this from the command line on my development PC and I've also setup eclipse to send commands via GDB to OpenOCD so that I can install code directly from the IDE.

I control OpenOCD using a single configuration file for my project that looks similar to the following:

# firmware.cfg : a config file for OpenOCD

source [find interface/jtagkey2.cfg]
source [find board/stm3210e_eval.cfg]

init
reset
halt
flash write_image erase firmware.elf
verify_image firmware.elf

reset
shutdown

The file above first loads two other files to configure the JTagKey2 and the board I'm using. Once these first two lines are executed, OpenOCD instructs my board to initialize, reset and then halt, it then copies the firmware on to the board and verifies it. Once done, the board is reset and then OpenOCD shuts down.

The OpenOCD configuration file is invoked using a bash script that looks similar to the following:

#!/bin/bash

openocd -d0 -f firmware.cfg

This could quite easily be part of your projects make file or be as complex as you like; my actual scripts does a release from subversion and labels it and also makes sure everything is properly checked in etc the script itself is indeed invoked from my make file.

Check back for more to come on this page