First Steps with STM32

A few month ago I ordered a nice little development board containing an STM32F103C8T6 and an stlink compatible programmer. Now I wanted to try it and today I successfully got the green LED blinking on this board. After the break I describe all the steps needed to get this far.

Install GCC

The most important tool is the compiler together with all the needed tools to create a binary which can be downloaded to the microcontroller. It comes packaged for most systems, here the install command for Debian:

apt-get install gcc-arm-none-eabi

Install libopencm3

libopencm3 is a library containing many helpful macros and functions to control the hardware of ARM microcontroller. Since there is no package I had to download the source and compile it myself:

mkdir -p ~/src/libopencm3
cd ~/src/libopencm3
wget https://github.com/libopencm3/libopencm3/archive/master.zip
unzip master.zip
D=`date +%Y-%m-%d`
mv master.zip libopencm3-$D.zip
mv libopencm3-master libopencm3-$D
rm -f libopencm3
ln -s libopencm3-$D libopencm3
cd libopencm3-$D
make

Install stlink

stlink is a tool to download the compiled binary to the microcontroller. Since there is no package I had to download the source and compile it myself too:

sudo apt-get install libusb-dev libusb-1.0-0-dev
mkdir -p ~/src/stlink
cd ~/src/stlink
wget https://github.com/texane/stlink/archive/master.zip
unzip master.zip
D=`date +%Y-%m-%d`
mv master.zip stlink-$D.zip
mv stlink-master stlink-$D
cd stlink-$D
mkdir build
cd build
cmake -DCMAKE_INSTALL_PREFIX=$HOME/installed -DSTLINK_MODPROBED_DIR=$HOME/installed/stlink/modprobe -DSTLINK_UDEV_RULES_DIR=$HOME/installed/stlink/udev ..
make
make install

Note: It might be better to install this to /usr/local somewhere, but this has to be done as root.

Install OpenOCD

OpenOCD is an on-chip debugger. I just installed it for later use, but did not try it yet.

apt-get install openocd

Testing the Connection

Connect the development board to the programmer. The pins with the same name shave to be connected. I had to bend the pins of the development board upwards to be able to see the names. Notice that they are nto in the same order on the programmer. Also be careful, there is 5V on the last pin of the programmer on the right side in the picture, connecting this one will damage the development board.

Plug in the stlink programmer with the development board connected.

$ st-info --probe
Found 1 stlink programmers
serial: 353f6b063054373437131343
openocd: "\x35\x3f\x6b\x06\x30\x54\x37\x34\x37\x13\x13\x43"
flash: 65536 (pagesize: 1024)
sram: 20480
chipid: 0x0410
descr: F1 Medium-density device

On my debain it just worked without any problems. If this does not work you might need to fiddle with those modprobe and udev files.

A simple LED blinker

The following is a simple C programm blink the green LED connected to PC13 of the development board.

#include #include #include

static void my_delay_1( void )
{
  int i = 72e6/2/6;

  while( i > 0 )
  {
    i--;
    __asm__( "nop" );
  }
}

int main( void )
{
  rcc_clock_setup_in_hse_8mhz_out_72mhz();
  rcc_periph_clock_enable( RCC_GPIOC );
  gpio_set_mode( GPIOC, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13 );

  while( 1 )
  {
    gpio_toggle( GPIOC, GPIO13 );
    my_delay_1();
  }
}

To compile this code I’ve taken some makefiles and linker scripts from the libopencm3-examples and slightly modified them. Here is the list of files including the C source code:

Run make to compile and link the code:

$ make
make[1]: Entering directory 'hello-world'
CC blink-green-led.c
LD blink-green-led.elf
OBJCOPY blink-green-led.bin
make[1]: Leaving directory 'hello-world'

Program the software:

$ st-flash write blink-green-led.bin 0x08000000
st-flash 1.3.0-dev
2017-01-15T20:02:11 INFO /home/mf/src/stlink/stlink-2017-01-14/src/common.c: Loading device parameters....
2017-01-15T20:02:11 INFO /home/mf/src/stlink/stlink-2017-01-14/src/common.c: Device connected is: F1 Medium-density device, id 0x20036410
2017-01-15T20:02:11 INFO /home/mf/src/stlink/stlink-2017-01-14/src/common.c: SRAM size: 0x5000 bytes (20 KiB), Flash: 0x10000 bytes (64 KiB) in pages of 1024 bytes
2017-01-15T20:02:11 INFO /home/mf/src/stlink/stlink-2017-01-14/src/common.c: Attempting to write 1228 (0x4cc) bytes to stm32 address: 134217728 (0x8000000)
Flash page at addr: 0x08000400 erased
2017-01-15T20:02:11 INFO /home/mf/src/stlink/stlink-2017-01-14/src/common.c: Finished erasing 2 pages of 1024 (0x400) bytes
2017-01-15T20:02:11 INFO /home/mf/src/stlink/stlink-2017-01-14/src/common.c: Starting Flash write for VL/F0/F3 core id
2017-01-15T20:02:11 INFO /home/mf/src/stlink/stlink-2017-01-14/src/flash_loader.c: Successfully loaded flash loader in sram
1/1 pages written
2017-01-15T20:02:11 INFO /home/mf/src/stlink/stlink-2017-01-14/src/common.c: Starting verification of write complete
2017-01-15T20:02:12 INFO /home/mf/src/stlink/stlink-2017-01-14/src/common.c: Flash written and verified! jolly good!

Watch the green LED blink with around 0.7Hz.

Now the system is ready to create some more interesting programs. I’ll write a follow up on using OpenOCD as soon as I have something to debug…

Happy hacking!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>