STM32Cube library – Part 1 Toolchain

This series of posts will hopefully help someone looking to set up and use the STM32Cube libraries provided by st-micro. This first post will cover setting up a development toolchain on a Linux system and the next post will cover making a hello world application.

What is this Cube library anyway?

The Cube library is a HAL (Hardware Abstraction Layer) developed by st-microelectronics to take some of the pain out of development with ARM microcontrollers. Their success in goal is debatable, but it does make porting code from other microcontrollers in their products easier. My main gripe is that it has a large memory footprint, and coming from the very space limited world of 8-bit microcontrollers I find the wastefulness annoying. That being said, I have come to use it as it considerably decreases my development time.

Each family of STM32 microcontrollers has a separate library descriptively named after the line of microcontrollers it targets (STM32CubeF0, STM32CubeF1, etc.) There is also a GUI (graphical) configuration tool for configuring the pin functions and clocks (STM32CubeMX). This tool is the main reason I use the Cube libraries as it takes most of the mystery out of picking the correct pins and corresponding alternate functions.

Toolchain Setup

Before playing with any of the stm32 code, a cross development toolchain must be installed. As usual, this is easier on linux than it is on Windows.

If you are using Windows I recommend this tutorial. The free arm compiler is available at under the win32 download. You will also need to install GNU Make and its dependencies (I would just use the windows installer).  Additionally you will need the tools to flash the microcontroller (DfuSe software and ST-Link software)

From now on I will assume you are using a Debian based distro in which case you will need to install the following packages (other distros will be similar, but have slightly different package names).

  • build-essential – gives a development x86 C library, make, and regular gcc
  • gcc-arm-none-eabi – arm cross compiler and other useful utilities.
  • libnewlib-arm-none-eabi – a striped down C library for arm microcontrollers (allows printf etc)
  • libstdc++-arm-none-eabi-newlib – (optional) for those of you who can’t live without c++ 🙂
  • git – code revision tool useful for getting code from other peoples projects.
  • cmake – for building stlink
  • libusb-1.0-0-dev – needed for dfu-util to work
  • dfu-util – for flashing microcontrollers in dfu update mode

Get the tools required for for compiling – this will take a lot of disk space.

sudo apt-get install build-essential gcc-arm-none-eabi libnewlib-arm-none-eabi libstdc++-arm-none-eabi-newlib

Get the tools required for flashing (downloading to the microcontroller)

sudo apt-get install git cmake libusb-1.0-0-dev dfu-util

Installing stlink

stlink is a utility that allows for programing and debuging stm32 microcontrollers from a stlink-v1 or v2 programmer (such as the stm nucleo line of development boards f072 board here). To install the utility run the following commands. compilation instructions available on their site.

git clone
cd stlink
cd build/Release
sudo make install

You are now able to use stlink to flash and debug stm32 microcontrollers. The package provides two utilities st-flash, which flashes files to the microcontroller, and st-util, which talks to the stlink programmer and allowing it to be used with gdb.

Here is an example using st-flash

st-flash write binaryFile.bin 0x08000000

This command writes the binary file to the address 0x08000000 which is the point where the microcontroller starts executing code.

The other command st-util is used for in system debugging, which will be covered later in the series.

Using dfu-util

If you are using a microcontroller with built in USB support like the stm32f042 or stm32f072, simply use the dfu-util utility for linux, if available this is probably the most convenient method for programming devices. The one downside to this method is it does not allow for in chip debugging.

dfu-util is a command line utility for writing the program to the microcontroller

sudo dfu-util -a 0 -s 0x08000000 -D binaryFile.bin

The -s option sets when where to start writing to the flash (0x8000000 is the start of boot flash space). The -D flag tells the utility what file to flash (a .bin file). (I have no clue what the -a flag does.)
EDIT: The -a flag tells dfu-util to write to flash memory and not the option bytes. (Option bytes are -a 1.)

if you want to avoid using sudo while flashing binaries you need to make a new udev rule into /etc/udev/rules.d. I have copied the contents of the file on my computer here.

#STMicroelectronics STM Device in DFU Mode (and clones)
SUBSYSTEM=="usb", ATTR{idVendor}=="0483", ATTR{idProduct}=="df11", MODE="660", GROUP="plugdev"

This will allow users in the plugdev group to access devices that match these parameters.

Getting the Cube libraries

So this is all very well and good, but by now you are probably wondering where the libraries I was talking about are. Rest assured they are coming.

Download the STM32CubeMX software (the graphical development tool) from st. If the link every goes dead search for STM32CubeMX on the st website. This contains an installer for Linux, Windows, and Mac. After extracting the files you should be greeted with several installers double click on the one that applies to you (there is also a Readme file which could be helpful as well).

So many choices!

The installer is pretty typical, accept the EULA blah, blah,blah. The program defaults to install in your home directory, so you don’t need root permission to install it.

A typical installation window (Hey its Java!)

Now run over to your installation directory and start the application you should be greeted by this window.

Make sure all your applications are happy applications.

Make a new project select the microcontroller that you want to use or the evaluation board that you are using (I’m selecting the stm32F4Discovery board).

Lots of choices

Hit OK and you are finally able to configure the chip how you want it. This process is fairly self explanatory, so I will just attach a few pictures. The various peripherals are on the left bar on the side of the screen. You can also right click on pins to set their function.

When you are ready, enter the configuration settings window at project->settings. I have provided the settings I use below. Just be sure to select TrueSTUDIO as your toolchain (it makes a linker file that gcc can accept). If you want each peripheral to have its own set of files select “Generate peripheral initialization as a pair of ‘.c/.h’ files per IP”.

Then click project->generate code to generate code for the project (amazing right). If this is your first time using the line of chips then the application will ask you to download the library. It might complain about proxy stuff, so select “no proxy” then click “check connection” if it checks out alright click “OK” and the library will download.

When it finishes it will generate code for you in the project directory that you entered.

I think that is enough for this post so I will end it here. Nothing groundbreaking, but hopefully this will prove helpful to someone.

Continue to Part 2: STM32Cube Library – Part 2 Hello World

Continue to Part 3: STM32Cube Library – Part 3 Compiling


One thought on “STM32Cube library – Part 1 Toolchain

  1. Pingback: STM32Cube Library – Part 3 Compiling – Nebk Electronics

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