Getting started with stm8 development using free software

This short tutorial shows how to compile the Dhrystone benchmark and execute it on an STM8/128-EVAL board. The author used a Debian GNU/Linux system, but the tutorial should work for other Linux distributions, *BSD or other Unices.

The tools we use are

Get sdcc

Depending on your operating system there might be an easy way to install sdcc 3.4.0 or newer using a package system or similar. At the time of the writing of this tutorial sdcc 3.4.0 has not yet found its way into Debian packages.

sdcc binaries or a source tarball can be downloaded from its website.

Get stm8flash

The stm8flash source can be found at its GitHub location, where there is also a download link for a zip archive of the sources. To compile it, a C compiler, such as gcc, pkg-config and libusb need to be installed. Unzip the archive (e.g. using unzip stm8flash-master.zip) change into the directory stm8flash-master and type "make". In case there are any errors, such as header files not found, check that pkg-config andf development files for libusb are installed.

Get dhrystone

Download a version of the dhrystone adapted for use with sdcc and the STM8/128-EVAL board.
Download, and unpack it using tar -xzf dhrystone-stm8-sdcc-3.4.0.tar.gz change into he directory dhrystone-stm8-sdcc-3.4.0.
You can then compile dhrystone by typing "make". The Makefile assumes that sdcc has been installed into the default locations in /usr/local. In case sdcc is installed in a different place and there are errors messages about symbols not being found, please adjust the library path in the Makefile. In the end there should be a file dhrystone.ihx.

Put dhrystone onto the board

Assuming stm8flash and dhrystone.ihx are in the same directory, the board is attached through an stlinkv2 device, "./stm8flash -c stlinkv2 -p stm8s208 -w dhrystone.ihx" will write dhrystone onto the board. Dhrystone will run and report its results vie USART1. You can see them by attaching a nullmodem cable to the USART1 on the board, and using a terminal program configured for 9600 baud, no parity, 8 bits, 1 stop bit and no flow control. They should look like this (the benchmark numbers may vary depending on the sdcc version used to compile dhrystone):

Dhrystone Benchmark, Version 2.1 (Language: C)

Program compiled with 'register' attribute

Please give the number of runs through the benchmark: 
Execution starts, 20000 runs through Dhrystone
Execution ends

Final values of the variables used in the benchmark:

Int_Glob:            5
        should be:   5
Bool_Glob:           1
        should be:   1
Ch_1_Glob:           A
        should be:   A
Ch_2_Glob:           B
        should be:   B
Arr_1_Glob[8]:       7
        should be:   7
Arr_2_Glob[8][7]:    20010
        should be:   Number_Of_Runs + 10
Ptr_Glob->
  Ptr_Comp:          5131
        should be:   (implementation-dependent)
  Discr:             0
        should be:   0
  Enum_Comp:         2
        should be:   2
  Int_Comp:          17
        should be:   17
  Str_Comp:          DHRYSTONE PROGRAM, SOME STRING
        should be:   DHRYSTONE PROGRAM, SOME STRING
Next_Ptr_Glob->
  Ptr_Comp:          5131
        should be:   (implementation-dependent), same as above
  Discr:             0
        should be:   0
  Enum_Comp:         1
        should be:   1
  Int_Comp:          18
        should be:   18
  Str_Comp:          DHRYSTONE PROGRAM, SOME STRING
        should be:   DHRYSTONE PROGRAM, SOME STRING
Int_1_Loc:           5
        should be:   5
Int_2_Loc:           13
        should be:   13
Int_3_Loc:           7
        should be:   7
Enum_Loc:            1
        should be:   1
Str_1_Loc:           DHRYSTONE PROGRAM, 1'ST STRING
        should be:   DHRYSTONE PROGRAM, 1'ST STRING
Str_2_Loc:           DHRYSTONE PROGRAM, 2'ND STRING
        should be:   DHRYSTONE PROGRAM, 2'ND STRING

Microseconds for one run through Dhrystone: 226 
Dhrystones per Second:                      4421

More about stm8flash

stm8flash was written by Valentin Dudouyt. It works both with stlink (including the one integrated on the discovery boards) and stlinkv2 devices. The programmer can be selected using "-c stlink" or "-c stlinkv2". The target device is selected using the "-p" option (to get a list of target devices, use the "-p" option with an option argument that is not an stm8 device, e.g. "-p help". stm8flash will treat filenames ending in .ihx a Intel hex, and other filenames as binaries.

More about sdcc

sdcc was initially written by Sandeep Dutta for the mcs51, and has a relatively conservative architecture (see Sandeep Dutta, "Anatomy of a Compiler"). It has been extended by various contributors and more recently, incorporated some cutting-edge technologies, in particular in register allocation (see Philipp Klaus Krause, "Optimal Register Allocation in Polynomial Time"). The stm8 backend was mostly written by Philipp Klaus Krause for his research into bytewise register allocation and spilling.

sdcc is a C compiler that aims to be compliant with the C standards.

Important compiler options for stm8 developers include: