Getting to Blinky with the STM32 and Ubuntu Linux!

After a few years now of doing Arduino, and everyone telling me that I needed to use ARM I thought I would dive right in with a much more powerful board.

Now before my fellow Techspacers rush in to jump on the bandwagon…. repeat after me…. “this is not Arduino, this is not Arduino”…. now don’t say I didn’t warn you! Arduino is still awesome and for the average hobbiest this kind of thing is overkill.

Now, this is a bit of a return to the past for me as my first job out of Uni was an embedded Software Engineer at 3Com. We worked on the Motorola 68k and had proper debug boards and all the professional tools…. pulling out this ARM board (with proper JTAG!) was a real blast from the past.

Everyone told me to chose a dev board from the manufacturer when you get started so I selected the STM32 F3 Discovery board. The boards are super cheap, sold as a loss leader as the manufacturers are trying to tempt you into their platform. Mine was about $14 from element14.

The software that is supported by the board is Windows only. I’m a Linux user and use only Libre & Open Source Software as a rule so I decided to go the (unsupported) open source route. It was surprisingly simple to get a simple example – there are a lot of different paths but here is a nice path that worked for me. For the record I currently run Ubuntu 13.10 32bit on an old Lenovo Thinkpad edge laptop. For the purposes of getting started and starting simple I am only using command line tools.

Debugger

First you need to install your debugger and programmer – there are a few alternatives, but I chose Openocd. It’s in the Ubuntu repos so just do

sudo apt-get install openocd

you’re not quite done, you need to give your regular user access to the USB port. So add a udev rule using

sudo vi /etc/udev/rules.d/99-stlink.rules

and add the following line:

ATTRS{idVendor}=="0483", ATTRS{idProduct}=="3748", MODE="0666"

You can then do

sudo udevadm control --reload-rules

To reload the rules. Plug in your dev board and run lsusb to check that the you can see a line that looks similar to your udev rule. You can run:

openocd -f /usr/share/openocd/scripts/board/stm32f3discovery.cfg

and you should see it the debugger fire up with no errors like this

Open On-Chip Debugger 0.7.0 (2013-05-15-17:44)
Licensed under GNU GPL v2
For bug reports, read
 http://openocd.sourceforge.net/doc/doxygen/bugs.html
srst_only separate srst_nogate srst_open_drain connect_deassert_srst
Info : This adapter doesn't support configurable speed
Info : STLINK v2 JTAG v16 API v2 SWIM v0 VID 0x0483 PID 0x3748
Info : Target voltage: 2.915198
Info : stm32f3x.cpu: hardware has 6 breakpoints, 4 watchpoint

Toolchain

Next you need to install your toolchain. There are a few choices here, but you can’t use the one in the normal repos as this is meant for arm linux cross-compilation. You need a raw compiler like gcc-arm-embedded. Luckily is has a PPA for 13.10 – add it with

sudo add-apt-repository ppa:terry.guo/gcc-arm-embedded
sudo apt-get update
sudo apt-get install gcc-arm-none-eabi

I think if you are running 14.04 this is now in standard repos.

Firmware

In order to test our board – we need some sort of firmware to upload. There are many available and some are very commercial. A popular open source one that looks very well maintained is opencm3. It comes with a lot of examples. Just clone the example repo using git (apt-get install git if you don’t have it). The examples repo includes a submodule to the main firmware so you just have to do:

git clone https://github.com/libopencm3/libopencm3-examples.git
cd libopencm3-examples
git submodule init
git submodule update
make

Run It!

Assuming everything worked we should have some binaries to upload to the board.

cd libopencm3-examples/examples/stm32/f3/stm32f3-discovery/miniblink
openocd -f /usr/share/openocd/scripts/board/stm32f3discovery.cfg

The debuger is running listening on port 4444, to do stuff you need to telnet to this port and issue some commands:

telnet 4444
reset halt
flash write_image erase miniblink.elf
reset

All going well, the code should upload and you should have a blinking blue light!

Hope this is useful to people – please let me know how you go. My next step is to try the same thing on the STM8!