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
$ 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
$ 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]
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 :)

the begin

I’ve started my adventure with embedded programming/electronics many years ago with 8051. For a long time I’ve been using 8-bit mcu - 8051 at the beginning and HC08 after that. During that time assembler was my primary language – I couldn’t force myself to switch to C. Because of that (or maybe thanks to that) I had to read datasheet many times to understand every detail. This approach can give anyone strong background on how this works.

From here I’ve moved to MSP430 – 16-bit mcu from TI. They offer great development board called MSP430 Launchpad. It works under Linux without problems – there are many tools available. I’ve used gcc port for MSP430 and mspdebug for programming. There is also gdb that might be helpful for debugging. msbdebug can serve as a server for remote gdb connection – it is really easy to program new code and debug it right away with them. There is now official MSP430 port for gcc supported by RedHat.

After two years of playing with MSP430 I saw an ARM development board STM32F4Discovery. It was cheap and easy to use – and what is most important for me – there is software for using/program this board under Linux. Board contains ST/Link2 that can be used as a standalone programmer for another systems. For development I’m using STM32F1xx. They are very powerful when comparing to mcus I’ve used before. At the beginning of 2011 one of my friends encourage me to build a hexacopter. Till now I haven’t build it but work are still in progress. During last two years I’ve learned a lot about ARM and had a great time playing with them.

I’ll try to write down my adventures in this blog. Lets begin …