This tutorial follows the same basic outline as this post on cu.rious.org but some things have changed since that was written such that it no longer works unmodified, and the various similar tutorials leave some bits out. My goal here is to get some custom code on the STM32F4DISCOVERY board as quickly as possible, hopefully paving the way in the near future for a full eclipse-based IDE setup.
Install the toolchain
sudo add-apt-repository ppa:terry.guo/gcc-arm-embedded sudo apt-get update sudo apt-get install gcc-arm-none-eabi
Verify that arm-none-eabi-gcc works:
you should get a slew of configuration parameters and something like
gcc version 4.7.4 20130613 (release)... in the last line.
Well, now you’ve got a toolchain! Wasn’t that easy!
Now we need a utility to actually talk to the discovery board. I intend to look into OpenOCD as well, but for now the ticket is Texane’s stlink. First, we need to install some prerequisites. Some of these are actually for the next step.
sudo apt-get install libusb-1.0-0-dev autoconf git python-yaml cd mkdir Development && cd Development mkdir embedded && cd embedded mkdir tools && cd tools git clone https://github.com/texane/stlink.git cd stlink ./autogen.sh ./configure make # Make the stlinkv2 load when you plug it in. sudo cp 49-stlinkv2.rules /etc/udev/rules.d sudo udevadm control --reload-rules
Install the firmware lib and examples
This isn’t strictly within the scope of setting up a dev environment, although I guess one could make the argument. The main point of including it is to get you (me) up and running with some example code to play with ASAP. ST distributes the STM32F4DISCOVERY with a firmware library that’s pretty useful and complete, but I haven’t really looked much into the licensing, and anyway it’s very much oriented towards the commercial toolchains and really development environments ST supports out of the box (Keil MDK-ARM, IAR EWARM, Atollic TrueStudio, and Altium TASKING VX). I found it was quicker simply to use libopencm3, the new kid free-as-in-speech-and-beer ARM firmware library. Conveniently, it includes a bunch of good examples too.
cd ~/Development/embedded/ git clone https://github.com/libopencm3/libopencm3-examples.git cd libopencm3-examples git submodule init git submodule update make
We should now have all the examples, as well as libopencm3 itself in a subdirectory, so let’s go try to build one and load it to our dev board.
cd examples/stm32/f4/stm32f4-discovery/miniblink/ make
Everything went alright? Great! OK actually I lied, when we ran
make before that actually built all the examples as well. Anyway, it was required to compile the library componets before making the examples, so now if you modify miniblink.c you can easily remake it. Anyway, we’ve now got a .elf to load. You should plug in your dev board now. In a new terminal, start stlink:
cd ~/Development/embedded/tools/stlink/ ./st-util
It ought to connect to the on-board ST-LinkV2, listening for a debug connection on local port 4242. In the previous terminal where you made the miniblink example:
from the (gdb) prompt:
(gdb) target extended-remote :4242 (gdb) load miniblink.elf (gdb) continue
This should start blinking the green LED on the dev board. Now, edit miniblink.c, remake, and get going on more fun stuff! (HINT: The green LED corresponds to GPIO12. The orange, red, and blue LEDs are GPIO13, GPIO14, and GPIO15, respectively. They all happen to be on port GPIOD).
Coming soon! Using the STM32F4DISCOVERY on Mac OSX. Effectively the exact same tutorial, but targeting OSX 10.8.4 Mountain Lion.