The 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 https://launchpad.net/gcc-arm-embedded under the win32 download.

I will assume you are using a Debian based distro in which case you will need to install the following packages.

  • build-essential – gives a development 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

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

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 https://github.com/texane/stlink.git
cd stlink
make
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

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.)

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.

#/etc/udev/rules.d/99-stmicro.rules
#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).

stm32cube-extracted
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.

stm32cubemx-installer1
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.

stm32cube-app1
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).

STM32Cube-app2.png
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.

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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