Open8S208Q80

EX-STM8-Q48a-105

STM8/128-EVAL

STM8L101-EVAL

Getting started with STM8 development using free software: LED and timer on the STM8S001J3 Reference design

The STMS001 is a rather new addition to ST's STM8 portfolio. At the time this tutorial was written, no STM8S001 evaluation boards were available commercially. Fortunately, an ST engineer has published a reference design, and sent me one boards (it is an early prototype with jumper wires soldered on the underside). The final version of the board has since become available via Rutronik.
The STM8S001 has only 8 pins, and multiple I/O functions are connected to each pin. There is no reset pin, and the SWIM pin is shared with other I/O. Thus the device can easily be bricked by enabling output on an I/O that shares the pin with SWIM early. While writing this tutorial, I made a mistake reading the datasheet, taking a value from a wrong line, accidentally enabling output on an I/O shared with SWIM. Since the STM8S001J3 wasn't easily available then, I had to wait a few weeks before I was able to get and solder a new one onto my board.
For this tutorial, I used a Debian GNU/Linux system, but the tutorial should work for other Linux distributions, MacOS, *BSD or other Unices.

The tools we use are

The C compiler SDCC, version 3.5.0 or later to compile C programs for the stm8.

stm8flash, to write programs onto devices.

Hardware setup

STM8S001J3 Reference design is connected to 5V power via the Mini-USB port. To write our program to the board, an ST-LINK/V2 is attached.

Get SDCC

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.

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 and development files for libusb are installed.

The Demo

We present a simple demo that increments a 3-bit LED counter once per second. This demonstrates setting up and using an accurate timer and doing basic I/O. Here is the C code:

SDCC is a freestanding, not a hosted implementation 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 increment of the LED counter.

The demo can be compiled simply by invoking SDCC using sdcc -mstm8 --std-c99 led.c assuming the C code is in led.c. The option -mstm8 selects the target port (stm8). An .ihx file with a name corresponding to the source file will be generated.

Put the demo onto the board

Assuming stm8flash and led.ihx are in the same directory, the board is attached through an stlinkv2 device, ./stm8flash -c stlinkv2 -p stm8s001j3 -w led.ihx will write the demo onto the board. It will run and count up to 7 on the LEDs 5, 4 and 3, then start again at 0.
The demo can be easily extended to a 5-bit counter that can count to 31 on all five LEDs (and closing JP9 after programming the device). However, one should be very careful to not enable output on bit 6 of port C and bits of 1, 3 and 5 of port D before the LED2 is to be switched on. By enabling output on bit 6 of port C and bits of 1, 3 and 5 of port D only 8 seconds after startup, the STM8S001 can still be reprogrammed using the ST-LINK/V2 in the first 8 seconds after applying power when JP9 is open. If bit 6 of port C or any bit of 1, 3 and 5 of port D is set to output at startup, the program will still work, but no other program can ever be written onto the STM8S001J3 again.

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 or .hex as Intel hex, and other filenames as binaries.

More about SDCC

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).