STM32 Starter Guide

From Eleccelerator Wiki
Jump to: navigation, search

I use a lot of the STM32 family microcontrollers. This page will have some info on how to start using them.


Other Website's Tutorials


I use Eclipse with the CDT (these help with C and C++ code). Some plugins can be installed from within Eclipse itself, some must be manually installed. Here are the list of plugins I have installed:

Using the GNU ARM Plugin

Once you start a project, the plugin gives your projects a ton of additional settings. Access them through "Properties for ProjectName" -> "C/C++ Build" -> "Settings". This gives you a GUI for the various settings available for the GNU GCC toolchain, plus the additional settings for ARM. Make sure you select the correct target processor (and whether or not you are using the THUMB instruction set). Make sure you have at least checked off "Create Flash Image".

Under the general settings for the linker, make sure you specify the linker script file (it's a *.ld file, you can find one in the ST libraries and examples). Check off "-nostartfiles".

For optimization, make sure all of the optimization options are enabled in the compiler optimization page. Use "-Os" if possible. Tell the linker general options to "remove unused sections".

Play around with the other settings.


I do not use OpenOCD, but if you want to use it, go ahead. Most of the online tutorials involving GDB will also involve OpenOCD.

I use a J-Link with the J-Link GDB server, with GDB for ARM. I just plug in the J-Link and run the server, and GDB will work with it. J-Link has very wide chip support and most of the configuration is already done for you.


Discovery Boards

ST has a family of Discovery boards, which are dirt cheap ($8 is the cheapest). Each Discovery board has a ST Link V2 built into it, which can do SWD debugging. You can disconnect the ST Link and use it for other projects. This is a great backup tool as well.

The schematics for the Discovery boards are always available. They are a great starting point for when you need to design your own circuit with STM32 chips. There are also example code from ST written specifically for Discovery boards.


CoLinkEx is also very cheap but it does not support all of the STM32 family. I support CooCox's open source efforts but I cannot recommend this debugger because some of my favorite STM32 chips are not supported yet. Once the support has been added, then I can recommend it.


I own a J-Link EDU, which is reasonably priced at around $70. This is what I use. J-Link has a GDB server application that works with the GDB plugins in Eclipse.

Debugging Using Trace Messages

I am talking about using "printf" for debugging.

If you want to use the serial port for debug messages, go ahead, there are certain situations when this is suitable. However, a better alternative for ARM is using SWO, which is a single wire dedicated to debug messages, it's faster than UART but it does require a debugger that uses SWD. Think of it as just a faster version of UART, really. My J-Link is great with this, is has a application called JLinkSWOViewer that is meant for this task.

This forum post (the second post) on the Segger website has some example code that uses the ITM inside ARM Cortex CPUs to output things via SWO.

Compiler Toolchain

I use GCC ARM Embedded, because "ARM employees are maintaining this project", it is very up-to-date right now. The GNU ARM plugin for Eclipse does support it. It is also very similar to YAGARTO, which is nice. YAGARTO basically got turned into GCC ARM Embedded. You can tell the plugin to use YAGARTO even if you are actually using GCC ARM Embedded.

Tools and Documents from ST

Remember to read the following documents for your specific chip

  • Datasheet, tells you about most of the electrical specifications, pin mappings, package details, etc
  • Reference Manual, tells you how to use the internal peripherals, etc
  • Programming Manual, not required to read but it is useful to understand how the flash memory is organized and written


Almost all STM32 chips can enter a factory stock bootloader that is stored in ROM (meaning it cannot be overwritten or deleted accidentally), and activated by a pin called BOOT0. To learn how to activate this bootloader mode, look for a document named STM32 microcontroller system memory boot mode. The bootloader uses UART (aka serial port) for communication, and it can also use USB if the chip has USB abilities.

This ROM bootloader usually works with a tool called "Flash Loader Demo", also known as STSW-MCU005. The protocol is also documented so that you can write your own application, but this is not required.

If you want to use the USB version of the bootloader, look for a tool called "DfuSe", also known as STSW-STM32080. The protocol is also documented, and the source code for the DfuSe tool is available as well.

If you want to implement a custom version of the USB bootloader, ST does provide source code for the bootloader firmware (provided with their USB library), and it is compatible with DfuSe.

Linux? Mac?

stm32flash, command line utility that is similar to Flash Loader Demo.

bootstm32, command line utility that is similar to Flash Loader Demo.

dfu-util, similar to DfuSe Demo.

Clock Configuration Tool

For different STM32 chips, ST provides an Excel spreadsheet with macros. You have to enter your parameters such as input crystal frequency and desired output frequency. The spreadsheet macro will use your info to generate a file that you can compile into your project. This file will setup the clock sources and PLLs for you. To find these files, you need to search for "Clock configuration tool for" on the ST website, there is a different tool for each chip.

Standard Peripheral Libraries

ST provides libraries that help you use the features of their microcontrollers. You need to find the specific library for your specific chip by searching for "Standard Peripheral Library" on the ST website.

Inside the library download, you will also find CMSIS support files, a startup file written in assembly, and linker scripts for GCC's linker. You need these files to build any project.

You may also find a clock configuration file inside the examples, but keep in mind that it might be designed for a frequency that is different from the frequency you need.

The library also comes with a ton of examples that compiles under many toolchains. If you get lost, start with an example.

ST supports Atollic TrueSTUDIO, which is an expensive IDE that is based on Eclipse and GNU GCC, so any files that works for TrueSTUDIO will also work using our GNU GCC build process.

USB Device and Host Library

ST provides a half decent USB library. It's not perfect but it gets the job done. Also known as STSW-STM32046.

Personal tools