nabil.me

Using the STM32F4 DISCOVERY Board in OSX Lion

October 30, 2011 #electronics

STM32

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: Package manager for OSX. We will need this to compile things like libusb.
  • Binutils: Binary utilities 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
  • binutils-2.21.1
  • newlib-1.19.0
  • gcc-4.6.1
  • gdb-7.3.1
  • Xcode: 4.1

First we need to install the gcc dependencies using MacPorts

sudo port install gmp mpfr libmpc

Next we will build binutils

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 multiple versions of some libs (e.g. soft float vs. hard float)
  • --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 let's 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.
We are bootstrapping gcc by running make all-gcc. We'll have to run make all later.

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

Verify hardware float support

arm-none-eabi-gcc -print-multi-lib

You should see:

.;
thumb/arm7tdmi-s;@mthumb@mcpu=arm7tdmi-s
thumb/cortex-m3;@mthumb@mcpu=cortex-m3
thumb/cortex-m4;@mthumb@mcpu=cortex-m4
thumb/cortex-m4/float-abi-hard/fpuv4-sp-d16;@mthumb@mcpu=cortex-m4@mfloat-abi=hard@mfpu=fpv4-sp-d16

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.

Rious has instructions.
For documentation see the PDF in GitHub.
It currently only supports writing to RAM and not Flash for CortexM4 devices.
See this bug report for more details.

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

← Back to blog