stm32f1xx on linux

To build software for STM32F1xx you’ll need few things:

  • gcc toolchain
  • programmer
  • hardware

Currently I’m using C to create code. For building flash image I’m using prebuilt gcc toolchain for ARM from Launchpad. It contains all necessary tools to build software for ARM that I’m using (Cortex-M3, Cortex-M4). To flash the target board I’m using OpenOCD that is present in Debian – version 0.7.0-1.And of course you’ll need some hardware to work on. There are many STM32FxDiscovey boards available – easiest way to start is to buy one and try it. Later you can use it as a programmer for your custom hardware using built in ST-Link.

Now you can start writing your first led blinker for STM32. But it would be easier to use some library that will contain drivers for all/most peripherals of your ARM. I personally use ChibiOS – it is real-time kernel with hardware drivers layer. Almost all peripherals of STM32F1xx are supported and there are many examples ready to use (i.e.: USB-CDC).

Quick start:

$ wget https://launchpadlibrarian.net/151487636/gcc-arm-none-eabi-4_7-2013q3-20130916-linux.tar.bz2
$ sudo mkdir /usr/local/gcc-arm-none-eabi
$ sudo chown $USER: /usr/local/gcc-arm-none-eabi
$ tar xjf gcc-arm-none-eabi-4_7-2013q3-20130916-linux.tar.bz2 -C /usr/local/gcc-arm-none-eabi --strip-components=1
$ sudo ln -s /usr/local/gcc-arm-none-eabi/bin/arm-none-eabi-* /usr/local/bin
$ git clone https://github.com/ChibiOS-Upstream/ChibiOS-RT
$ cd ChibiOS-RT/testhal/STM32F1xx/USB_CDC
$ make

Congratulations! You’ve just compiled your first code for ARM. Next step is to flash that code – for that you’ll need OpenOCD. My custom board file for OpenOCD and STM32 looks like below:

source [find interface/stlink-v2.cfg]
set WORKAREASIZE 0x2000
source [find target/stm32f1x_stlink.cfg]

# use hardware reset, connect under reset
reset_config srst_only srst_nogate

Open another console, create above file, connect your development board to USB port and run:


$ openocd -f path_to_file.cfg

openocd should detect your CPU and displays some basic information. If it doesn’t please check your connections and make sure that you have access to usb device. If everything goes weel openocd should listen on port 3333.

From previous console run gdb:


$ arm-none-eabi-gdb -ex 'target remote :3333' -c 'monitor reset init' -c 'monitor reset halt' build/ch.elf

This should connect to openocd and initialize debug port on ARM. You can now type load to flash code. After that type monitor reset to reset CPU and start executing custom code. To load new code type following command sequence:

(gdb) monitor reset halt
(gdb) load
(gdb) monitor reset

Press ^C to type another command. You can use normal gdb commands to set breakpoints, display memory and/or variables, etc.

It’s that simple to start programming ARM :)