Using the STM32F4 DISCOVERY Board in OSX Lion

Update: Check out the new post on programming with JTAG

I recently got the STM32F4DISCOVERY board, which features ARM’s new CortexM4 with 1 MB Flash and 192 KB RAM. For the past couple of years I’ve been using AVRs because you can get open source compilers, libraries and programmers unlike their competitors. The only thing I miss from the ARM world is processor speed. Most of Atmel’s low end chips are less than 100MHz and can cost as much as a 150MHz ARM cpu with similar peripherals. This is one of the reasons why I am switching to ARM for the next prototype of the bicycle computer. Maybe I’ll stay in the family and use Atmel’s new CortexM4 when it comes out.

To use the STM32F4 Discovery in OSX we are going to need a cross compiler. There is a great thread on dangerousprototypes with lots of info, which I will summarize here. Then I’ll show the alternative setup.

Option 1: The easy way?
Rious has instructions on setting up the cross compiler on OSX and Linux using a build script from github. However, I was not able to get this working in Lion and I’m not sure if it is taking advantage of the FPU instruction set so I decided to build one from scratch.

Option 2: The “fun” way
Here is what you need. Get the latest version of each.
MacPorts (the .dmg is fine): Package manager for OSX. We will need this to compile things like libusb.
Binutils: Binary utilies like a linker and assembler
Newlib: Standard C library for embedded systems
GCC: C Compiler source
GDB: C debugger
XCode: Compiler to compile arm-gcc

I’m using:
MacPorts 2.0.3
Xcode: 4.1

First we need to install the gcc dependencies using MacPorts

sudo port install gmp mpfr libmpc

Next we will build binutils. Notice the CC CPP CXX LD environment variables. This tells the Makefile to use gcc-4.2 instead of the llvm-gcc compiler.

Note: UnaClocker in the commets had problems when redefining CC, CPP, CXX and LD. If you have similar errors try removing them.

cd [binutils-build]
CC=/usr/bin/gcc-4.2 CPP=/usr/bin/cpp-4.2 CXX=/usr/bin/g++-4.2 LD=/usr/bin/ld ./configure  --target=arm-none-eabi --prefix=/opt/local/ --enable-interwork --enable-multilib --disable-nls --disable-libssp
make all
sudo make install

Some definitions
–prefix=/opt/local/: Set the install directory. I’m putting it with my MacPorts install.
–enable-interwork: Allows ARM and Thumb code to be used
–enable-multilib: Build multible versions of some libs. E.g. one with soft float and one with hard
–disable-nls: Tells gcc to only support American English output messages
–disable-libssp: Don’t include stack smashing protection

GCC is next
Update: added patch info. Thanks to Msmith+disqus from the comments in the bus blaster post.

First lets apply a patch to gcc to add hardware floating point.
Download the patch from here.

cd [gcc-build]
patch gcc/config/arm/t-arm-elf patch-gcc-config-arm-t-arm-elf.diff

Make sure to set the path of –with-headers. Notice we are bootstrapping gcc by running make all-gcc. We will have to come back and run make all later.
Update: changed sudo make install-gcc to make all-gcc and sudo make install-gcc. Thanks UnaClocker.

cd [gcc-build]
mkdir objdir
cd objdir
CC=/usr/bin/gcc-4.2 CPP=/usr/bin/cpp-4.2 CXX=/usr/bin/g++-4.2 LD=/usr/bin/ld ../configure --target=arm-none-eabi --prefix=/opt/local/ --enable-interwork --enable-multilib --enable-languages="c" --with-newlib --with-headers=[newlibdir]/newlib-1.19.0/newlib/libc/include/ --disable-libssp --with-gmp=/opt/local/ --with-mpfr=/opt/local/ --with-mpc=/opt/local/ --with-libiconv-prefix=/opt/local/ --disable-nls
make all-gcc
sudo make install-gcc

Try running arm-none-eabi-gcc -print-multi-lib to see if you hardware floating point. You should see:


Build Newlib

cd [newlib-build]
CC=/usr/bin/gcc-4.2 CPP=/usr/bin/cpp-4.2 CXX=/usr/bin/g++-4.2 LD=/usr/bin/ld ./configure --target=arm-none-eabi --prefix=/opt/local/ --enable-interwork --enable-multilib --disable-libssp --disable-nls
make all
sudo make install

Finish building GCC

cd [gcc-build]/objdir
make all
sudo make install

Build the debugger

cd [gdb-build]
CC=/usr/bin/gcc-4.2 CPP=/usr/bin/cpp-4.2 CXX=/usr/bin/g++-4.2 LD=/usr/bin/ld ./configure --target=arm-none-eabi --prefix=/opt/local/ --enable-interwork --enable-multilib --disable-libssp --disable-nls
make all
sudo make install

Setting up ST-LINK
ST-LINK lets you debug and program the dev board from a windows box. To get it working in OSX and linux we will need the help of open source software.

Once again Rious has instructions on how to do this. For documentation see the pdf in github. It looks like it only supports writing to RAM and not Flash for CortexM4 devices. Check out the bug report here.

In the next post I’ll upload the modified example source files from ST.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s