Skip to content

Getting started with STM32F[0|3|4]discovery boards

Andreas "Paul" Pauli edited this page May 28, 2015 · 18 revisions

This is a short guide which will lead you through the first steps with STM32F0/3/4discovery boards from the very beginning to degugging an example project. The document describes development on a Ubuntu-PC.


Of course the first thing you need is RIOT. Check out the wiki if you don't know how to clone the repository. The article about Creating your first RIOT project may be helpful as a reference during the course of this guide.

Next, you will need a toolchain for ARM processors to compile, link, debug etc. your projects for the respecting platform/device (in this case STM32F0/3/4discovery boards). Some possible toolchains are listed in the wiki entry: Family: ARM. However, in my case the gcc-arm-embedded toolchain worked without any problems. You have to add the directory with executables to your PATH variable. For the gcc-arm-embedded example this should look like this:

export PATH=/home/*path_to_file*/gcc-arm-none-eabi-4_8-2014q1/bin:$PATH
export PATH=/home/*path_to_file*/gcc-arm-none-eabi-4_8-2014q1/arm-none-eabi/bin:$PATH

It's safer to add the directory at the beginning of the PATH variable so the operating system won't find another preinstalled version of GCC at first and uses this. You can check if that worked with:

echo $PATH

Note that each time you open a new terminal-window you have to adapt the PATH variable again. If you do not want to do this, you can put this command in a file called .bashrc located in your home directory. Note that you will have to edit this entry when you want to use a different compiler (for example for another board).

The STM32Fxdiscovery boards include an on-board ST-LINK V2 programmer. The easiest way to program the board is to use OpenOCD. Please refere to this page for installation instructions.

To monitor if your example program works, it is comfortable to use a UART to USB Converter so you can control output on a terminal window.

Compiling files

When the preparation is done, you should be able to use the right toolchain for ARM embedded processors. You can check that by typing:

arm-none-eabi-gcc -v

Next, switch to RIOT's examples directory and choose hello-world. Note that you have to change the PROJECT or in newer versions the APPLICATION variable in the Makefile to your own project name if you copy-paste the hello-world example to generate your own example project. The project can be compiled for the board by typing:

BOARD=stm32f4discovery make

(For other boards you type the respective board name). Now the files should have been successfully compiled in the bin folder. A new subfolder carrying the name of your board should have been created there.

Flashing files

In general there are different ways to write software into the mircrocontrollers flash memory, for example by using the OpenOCD or ST-LINK tool. We recommend to use OpenOCD! If you need to use the ST-LINK tool for some reasons, go to this guide to learn how to install and use it.

Flashing a new firmware to the device

Now you have to connect the device to your computer via USB-microUSB to write your program on it. Use the CN1 connector on the STM32F4discovery board for this. Normally, the device shoud be flashed automatically by typing:

BOARD=stm32f4discovery make flash

(For other boards you type the respective board name). This will automatically load the cpu-specific OpenOCD configuration file.

Running program

After the hello-world program has been flashed, it should automatically run on your device. Somtimes it's necessary to reset the board with the B2 push-button, so the program restarts. Now you want to see if it runs. Therefore a UART to USB converter would be nice. To connect such an adapter you need the preconfigured board pins:


  • PA2 = TX Data
  • PA3 = RX Data
  • GND = GND


  • PB6 = TX Data
  • PB7 = RX Data
  • GND = GND

Now you can open any terminal program. In RIOT's directory RIOT/dist/tools/pyterm/ you can find pyterm which can be opened with the appropriate ttyUSB-port:

sudo ./ -p /dev/ttyUSB0

In this case the adapter is connected to ttyUSB-port zero. If there is more than one ttyUSB device connected to your PC, you have to check which port your adapter uses. If you are successfully connected, you should now see "Hello world" on your screen. If not, try to reset the board. With each reset the program starts afresh.

Some might like to add the pyterm tool to the PATH for faster access, but that's not neccessary.

Debugging program

To check some debugging functions it could be helpful to generate your own example project (for example by copy-pasting the hello-world example) and write a little test script in the main.c. Compiling and flashing must be done again for the new project. Don't forget to change the APPLICATION variable in the makefile to your own project name if you created a new folder in the examples directory! So, for the debugging test I wrote:

		printf("%d\n", i);
			i = 0;

Now that all above steps are done, you can start start debugging your program with:

BOARD=stm32f4discovery make debug

(For other boards you type the respective board name). If you like to enable the "Text User interface" option provided by gdb just type:

TUI=1 BOARD=stm32f4discovery make debug

This runs a terminal interface which shows the source file, assembly output, program registers etc. and makes it more comfortable to debug.

Congratulations! You should now be able to debug your program using the GDB-commands.

Clone this wiki locally
You can’t perform that action at this time.