Template project for STM32 on Linux

I am a beginner in electronics and programming for microcontrollers, so traditionally I started from driving an LED which is a ‘Hello World!’ project in microcontrollers world. I started with STM32 controller (STM32F103) which is ARM, and I can use C language.

When my LED finally started blinking, I thought it might be useful from me (and hope I can be useful for someone else) if I create a simple re-usable template project. As a result, now there is one more repository on GitHub.

I use Linux, no fancy GUI, sorry.

Before I started, of course I asked Google how people work with STM32 on Linux. I found two good articles which helped me a lot:

  1. STM32 Discovery Development On Linux — this article is about STM32F4Discovery which has an ST-LINK/V2 debugger/programmer. But I had only STM32F103 board which is similar, but still a little bit different.
  2. Programming ARM Cortex (STM32) under GNU/Linux — this one is about STM32F4 as well.

Probably there are articles about STM32F103 on the Internet, but I gave up googling.

STM32, what else?

Let’s talk about hardware.

To be honest, it’s much easier to use some STM32 Discovery board. But it’s so big … Some of these boards even contain an LCD display which is cool but … Is there anything smaller?

Luckily there is a Maple Mini project which is based on STM32 microcontroller. But I found a clone from guys in China … It was only $1.90. I was so happy, so I ordered two. Ah, no free shipping … fine, it’s $4.24 in total. Not too much I guess.

This board is based on STM32F103C8T6, but it doesn’t have a built-in debugger/programmer like STM32F4Discovery. So I needed a programmer then. Guys from China saved me again. I found a clone of ST-LINK/V2 programmer. It was only $3.45. And free shipping, hooray!

I also needed an LED and a resistor. The resistor may be 200 or 330 Ohms. I wrote a separate post about connecting an LED to a microcontroller, and choosing a current limiting resistor.

By the way, all stuff arrived in a couple of weeks which I think is pretty fast for AliExpress. But maybe I just was lucky. An LED should be connected to pin PA1.

We need a C compiler for ARM

As a sanity check, you can just print arm-none-eabi-gcc version:

We have hardware, compiler, that’s all?

I needed STLINK software to upload the code to microcontroller. Sources are available on GitHub. I just cloned the repo, and built it. It needs a couple of tools and libs to install. On Ubuntu 16.04 I did the following:

When it’s done, STLINK can be built with the following commands:

Can we already start driving an LED?

STMicroelectronics provides STM32 standard peripheral library which contains APIs to work with STM32 microcontrollers. I used version 3.5.0. The library can be downloaded here. Another option is to use STM32Cube which seems to be recommended by STMicroelectronics.

Now we can finally start writing code. As I mentioned earlier, I uploaded the code on GitHub:

I wanted to keep it simple, so it doesn’t have much stuff.

main.c is the main program. First, main() function initializes pin PA1. Then, it starts an infinite loop which turns on/off the LED with a delay.

main.h is a header file which includes a couple of other header files from STM32 standard peripheral library.

stm32f10x_conf.h is a header file which is used by STM32 standard peripheral library. It contains only assert_param() function which does nothing.

Compilation fails without this function with errors like the following:

Makefile is a makefile. Nothing surprising.

It has three targets:

  • “all” target compiles sources
  • “clean” remove binaries which were produced by “all” target
  • “burn” runs st-flash utility which uploads binaries to a microcontroller. It requires super-user permissions.

Note: the flags seems to be a bit redundant, please also see Ivan’s comment below about flags.

SOURCES variable has a list of source files to be compiled. Besides of main.c, it contains a couple of files from STM32 standard peripheral library.

Finally, the code can be build and uploaded to the device with commands like the following:

STD_PERIPH_LIBS variable contains a path to STM32 standard peripheral library which was downloaded before. ST_FLASH variable contains a path to st-flash utility which was built earlier.

You can see something like the following if everything went smoothly:

That’s pretty much it. Have a next day!

Originally published at https://blog.gypsyengineer.com on July 12, 2016.

I write about Java, security, electronics and DIY

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store