Compiler Comparison
RTOS Availability
STM8AF LED Timer
STM8AF Serial
STM8AF Benchmarks
STM8AL LED Timer
STM8AL Serial
This short tutorial is about a simple LED and timer demo for the Hobby components HCDVBD0017 board. While the boards themselves are very cheap, an stlink is not included. I used a cheap ST-Link/V2 device. 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
To write our program to the board, we attach a an ST-Link/V2. The board does have an USB connector, which can be used to power it, but it should only be used when there is no ST-Link attached.
Depending on your operating system there might be an easy way to install SDCC 3.5.0 or newer using a package system or similar (e.g. apt-get install sdcc on Debian). While SDCC 3.4.0 should be sufficient for this tutorial, you might want to try a newer version in case you encounter any bugs. In particular, SDCC 3.4.0 has an issue with the library search path; this can be worked around by explicitly specifying the path to the standard library when linking.
SDCC binaries or a source tarball can be downloaded from its website.
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 and development files for libusb are installed.
We present a simple Demo that blinks one LED, one once per second. This demonstrates setting up and using an accurate timer and doing basic I/O. Here is the C code:
// Source code under CC0 1.0 #include <stdint.h> #define CLK_DIVR (*(volatile uint8_t *)0x50c6) #define CLK_PCKENR1 (*(volatile uint8_t *)0x50c7) #define TIM1_CR1 (*(volatile uint8_t *)0x5250) #define TIM1_CNTRH (*(volatile uint8_t *)0x525e) #define TIM1_CNTRL (*(volatile uint8_t *)0x525f) #define TIM1_PSCRH (*(volatile uint8_t *)0x5260) #define TIM1_PSCRL (*(volatile uint8_t *)0x5261) #define PB_ODR (*(volatile uint8_t *)0x5005) #define PB_DDR (*(volatile uint8_t *)0x5007) #define PB_CR1 (*(volatile uint8_t *)0x5008) unsigned int clock(void) { unsigned char h = TIM1_CNTRH; unsigned char l = TIM1_CNTRL; return((unsigned int)(h) << 8 | l); } void main(void) { CLK_DIVR = 0x00; // Set the frequency to 16 MHz // Configure timer // 1000 ticks per second TIM1_PSCRH = 0x3e; TIM1_PSCRL = 0x80; // Enable timer TIM1_CR1 = 0x01; PB_DDR = 0x20; PB_CR1 = 0x20; for(;;) PB_ODR = (clock() % 1000 < 500) << 5; }
sdcc is a freestanding, not a hosted implemenatation of C, and allows main to return void.
We set up the timer to increment once per millisecond, which allows us to implement a basic clock()
function. This function is used to control the blinking of the LEDs.
The demo can be compiled simply by invocing SDCC using sdcc -mstm8 --std-c99 led.c
assuming the C code is in serial.c. The option -mstm8
selects the target port (stm8). An .ihx file with a name corresponding to the source file will be generated.
Assuming stm8flash and led.ihx are in the same directory, the board is attached through the integrated stlink device, ./stm8flash -c stlink -p stm8s103f3 -w led.ihx
will write the demo onto the board. It will run and blink the green LED once per second, and the blue LED once every two seconds.
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
or .hex
as Intel hex, and other filenames as binaries.
SDCC was initially written by Sandeep Dutta for the MCS-51, and has a relatively conservative architecture (see Sandeep Dutta, "Anatomy of a Compiler", 2000). 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", 2013). The stm8 backend was mostly written by Philipp Klaus Krause for his research into bytewise register allocation and spilling (see Philipp Klaus Krause, "Bytewise Register Allocation", 2015).
SDCC is a C compiler that aims to be compliant with the C standards.
Important compiler options for STM8 developers include:
-c
to compile into object files to be linked later--std-c99
for compilation in C99 mode (some C99 features, e.g. variable-length arrays are not yet supported in sdcc though)--opt-code-size
for optimization for code size--max-allocs-per-node
to select the optimization level. the default value is 3000. Higher values result in more optimized code, longer compiler runtime, and higher memory usage during compilation.