STM32F4 – Behold the Project Wizard!

I know about a project wizard that can create a C/C++11 project template for STM32F407 device and GCC toolchain in a snap of a finger. Furthermore, he delivers breathtaking magic by supporting mbed SDK including mbed-RTOS, FreeRTOS and SafeRTOS!

mbed-sdk-open-sourceFreeRTOSSafeRTOSNo doubt, his magic is strong, and luckily, he is on your side. ^^ He relieves you of several boring development tasks. You may now finally focus on more important tasks like coding and testing. And it just happens I know his whereabouts, so you can meet him in person right away.

ProjectWizardMore seriously, in this tutorial I show how to use my project creation utility mbed-project-wizard that can do the following.

  • Creates a new project.
    • With working 3rd party code mbed SDK, mbed-RTOS, FreeRTOS, SafeRTOS.
    • Can be used as an extensible project template.
    • Provides a generic Makefile that is easily-customizable.
  • Builds your code.
    • Also builds static libraries from the 3rd party code to improve overall build time.
  • Size-optimizes your and 3rd party source code.

In this tutorial you will create, build and deploy several demo application on your STM32F4-Discovery board with only 3-4 commands in less than a minute.

1. Prerequisites

  • Ubuntu 14.04 LTS (x86 architecture).
  • STM32F4 Discovery Board (ARM architecture, costs less than 20 EUR).
  • Complete the mbed Your Code: Zero Switching Costs When Changing ARM Platforms tutorial.
    • Or at least go through chapter 2. Below is a quick-and-dirty instruction summary if you decide to stay on this page.
      cd ~
      # Remove the official package
      sudo apt-get purge binutils-arm-none-eabi \
                         gcc-arm-none-eabi \
                         gdb-arm-none-eabi \
      # Add 3rd party repository
      sudo add-apt-repository ppa:terry.guo/gcc-arm-embedded
      sudo apt-get update
      # Check the GCC package version in the PPA repository
      sudo apt-cache policy gcc-arm-none-eabi
      # Install software requirements
      sudo apt-get install build-essential git openocd \
       gcc-arm-none-eabi qemu-system-arm \
       symlinks expect
      # Clone my git repository and init submodules
      git clone
      cd ~/stm32
      git submodule update --init

2. The Simplest Spell: A Bare-metal Project with mbed Library

The mbed-project-wizard is easy to use. Just follow these steps to create a simple bare-metal project with mbed library.

  1. Create new (and empty) project folder.
    mkdir -p ~/stm32/examples/test-mbed-none
    cd ~/stm32/examples/test-mbed-none
  2. Run the wizard to initialize a new bare-metal project with mbed library support. Pitfall: Run the script from the new project directory, otherwise it will not work as intended.
    export PATH=$PATH:~/stm32/mbed-project-wizard mbed-none
    # The generated directory structure
    # tree
    # .
    # ├── bin # Built binary files
    # ├── inc # Add your header files here
    # ├── lib # Add 3rd party code here
    # │    └── mbed # Added by project-wizard
    # ├── src # Add you source files here
    # │    └── main.cpp
    # ├── Makefile # DON'T TOUCH
    # └── STM32F407.ld # DON'T TOUCH
  3. It should generate the above (easy to understand) directory structure with mbed library and main.cpp sample application. The project can be used as a template. It is extensible and build automatically without the need of editing Makefile as long as you keep the directory structure intact and follow these simple rules:
    • You may add or remove header files (*.h) from the inc folder, add subdirectories or even use symbolic links. The Makefile script automagically finds appropriate files.
    • You may add or remove C (*.c) or Assembly (*.s) source files from the src folder, add subdirectories or use symbolic links.
    • You may add additional 3rd party code in the lib folder. Hopefully, it will build without Makefile modifications.
    • Don’t touch the Makefile, unless necessary.
  4. Build and deploy the project.
make clean
make -j4 # Size Optimized Build
# Other build options
# make -j4 release-memopt # Size Optimized Build
# make -j4 release-memopt-blame # Analyse Mem. Footpr.
# make -j4 release # Non-optimized Build
# make -j4 debug # Debug Build

sudo make deploy # Deploy to Target

Q: Deployment fails repeatedly due to some OpenOCD issue. 😦 Is there a workaround?
A: Yes, the current official Ubuntu package (Aug 2014) contains a prehistoric OpenOCD version. You should build a newer version from scratch. I provide step-by-step instructions here (2nd section, it takes less than 3 minutes to build it). When you are done, just return here and continue as nothing happened. It will work out of the box. 🙂

3. Advanced Spells

There are also more advanced spells that don’t require any special extra knowledge from you, except a free minute per project (and no more than 6 commands!). 🙂

3.1 A mbed-RTOS Project with mbed Library

Rejoice! Now, you own the keys to multitasking.

# export PATH=$PATH:~/stm32/mbed-project-wizard
mkdir -p ~/stm32/examples/test-mbed-mbedrtos
cd ~/stm32/examples/test-mbed-mbedrtos mbed-mbedrtos

make clean
make -j4
sudo make deploy


3.2 A FreeRTOS Project with mbed Library

Rejoice, again! Now, you own the second pair of keys to multitasking.

# export PATH=$PATH:~/stm32/mbed-project-wizard
mkdir -p ~/stm32/examples/test-mbed-freertos
cd ~/stm32/examples/test-mbed-freertos mbed-freertos

make clean
make -j4
sudo make deploy

3.3 SafeRTOS Project

SafeRTOS is not included in the git repository due to the license restrictions. To install the source follow these steps:

  1. Fill out the following web download form here and read the license restrictions.
  2. You will receive an e-mail from GMail users should check the spam folder. 😉
  3. Download STM32F4xx_atollic_SafeRTOS_Library_Demo.exe and install it via wine. Use default settings or update the project generation script. The project generation wizard works with SafeRTOS v4.7.
    sudo apt-get install wine
    wine ~/Downloads/STM32F4xx_atollic_SafeRTOS_Library_Demo.exe
  4. Once again, rejoice!
# export PATH=$PATH:~/stm32/mbed-project-wizard
mkdir -p ~/stm32/examples/test-safertos
cd ~/stm32/examples/test-safertos none-safertos

make clean
make -j4
#  text data   bss   dec  hex filename
# 17440 1756 20480 39676 9afc bin/outp.elf

sudo make deploy

3.4 Projects with Static Libraries

Static libraries allow to link to programs without the need to recompile. They allow developers to share only the binary code rather than the original source code. The wizard initializes a library project by appending “-lib” to the chosen project.

# export PATH=$PATH:~/stm32/mbed-project-wizard
mkdir -p ~/stm32/examples/test-mbed-none-lib
cd ~/stm32/examples/test-mbed-none-lib mbed-none-lib

# The generated directory structure
# tree
# .
# ├── bin
# ├── inc
# ├── lib
# │    └── mbed
# │         └── Makefile # 2nd level Makefile (to build library)
# ├── src
# │  └── main.cpp
# ├── Makefile # Top-level Makefile
# └── STM32F407.ld

make clean
make -j4 libs # Builds lib directory first
make -j4 # Build src afterwards
sudo make deploy

There are some differences compared to the ordinary project. Multi-level Makefiles are used to build libraries and the application separately. First, appropriate libraries should be built. Second, the libraries are used at link time by top-level makefile to create application binary.

3.4 Symlinking Files instead of Copy

The project wizard copies mbed and FreeRTOS files to project target directory. Sometimes symlinking is more appropriate behaviour that can be invoked using the link keyword.

# export PATH=$PATH:~/stm32/mbed-project-wizard mbed-none link

3.5 Other Project Options

# export PATH=$PATH:~/stm32/mbed-project-wizard

Usage: <project template> [link]

 Project Templates:

 mbed-none ...... creates a bare-metal project with mbed SDK
 mbed-none-BB ... creates a bare-metal project with mbed SDK,
                  cpputest support and redirected STDIO
                  tailored to STM32F4-BB expansion board

 mbed-none-sh[|cpput] ...... creates a bare-metal project
                             with mbed SDK, cpputest and
                             ARM semihosting support

 mbed-none-shgtest ......... creates a bare-metal project
                             with mbed SDK, googletest and
                             ARM semihosting support

 mbed-none-shsim[|cpput] ... creates a bare-metal project
                             with mbed SDK and cpputest
                             suitable for (ARM semihosted)
                             QEMU simulation

 mbed-none-shsimgtest ...... creates a bare-metal project
                             with mbed SDK and gtest support
                             suitable for (ARM semihosted)
                             QEMU simulation

 mbed-none-lib ....... creates a bare-metal proj. with mbed SDK
 mbed-freertos ....... creates a FreeRTOS project with mbed SDK
 mbed-freertos-lib ... creates a FreeRTOS project with mbed SDK
 mbed-mbedrtos ....... creates a mbedRTOS project with mbed SDK
 mbed-mbedrtos-lib ... creates a mbedRTOS project with mbed SDK
 none-safertos ....... creates a SafeRTOS project

 link ................ creates symlinks instead of file copies

About istarc

Embedded Systems Developer.
This entry was posted in Embedded Systems, STM32F4 and tagged , , , , , , , , , , . Bookmark the permalink.

1 Response to STM32F4 – Behold the Project Wizard!

  1. nelsonsneves says:

    Wow, this sure looks interesting, been using MBed for quick prototyping but have some extra STM32F4 boards that I really enjoy but never had something useful to use with them. I’m really interested in your recipe, it provides you the required boilerplate to get things compiled locally and from a Linux box, I’ll sure look into this with more detail!

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s