May 14, 2024

Programming STM32 ARM microcontrollers in Arch Linux

Once upon a time, not so long ago, the 8-bit microcontrollers were ruling the hobbyist embedded world. But today, the 32-bit ARM Cortex Microcontrollers are so inexpensive and power efficient that there is no good reason to ignore them. Here, let us see how to program a STM32 ARM cortex Microcontroller in Linux environment. The specific microcontroller used here is an STM32F103C8 (ARM cortex M3) programmed in Arch linux.

Components

  • Generic STM32F103 board (blue pill)
  • STLINK-V2 (STM32 programmer)
  • Female-Female connectors
All the above components can be bought from ebay for less than $10 total. The STLINK-V2 is optional since you can use any of USB-SERIAL converters like FT232, CP2102, PL2303, CH340 and the built-in UART bootloader of STM32 chip to program. So if you already have any of the above serial converters, you don’t really need STLINK to program the STM32F103 microcontroller. But STLINK helps if you plan to use in circuit debugging functionalities.

Software
The system is going to be a 64-bit Arch linux distribution. I am aware of STM32duino nd we are NOT going to use any Arduino based IDE to program STM32 here; Instead, we are going with libopencm3

Let us install the compilers and packages required to build libopencm3 in Arch.

sudo pacman -Syu # update and upgrade the system
sudo pacman -S arm-none-eabi-gcc arm-none-eabi-gdb arm-none-eabi-binutils arm-none-eabi-newlib
Now let us clone the libopencm3 repo in your installation location.
git clone https://github.com/libopencm3/libopencm3-examples.git
make

This will most probably throw an error requiring you to run couple of commands before you can make all the libraries
git submodule init
git submodule update
make
Now you should have all the libraries installed. 
Putting Together
Connect the STLINK-V2 to STM32’s using the female headers. The generic STM32 board has 4 male headers at its tail. Just map those pins to those in STLINK board.

           

To test our setup, let us program a sample blink led program to the board.

cd examples/stm32/f1/stm32-h103/miniblink/

vim miniblink.c

The miniblink.c toggles GPIO12 of PORTC but our generic STM32 board has the LED hooked to PC13. So change the pin number accordingly in the file. ie. GPIO12 changed to GPIO13.

Now type
make clean all
make miniblink.stlink-flash

Now you can see the STLINK programming the STM32 making the on board RED LED blinking.