Category Archives: Electronics

STM8 meets GNU/Linux System

STM8 Development Tools Set Up in GNU/Linux Systems

STM8 family of controllers provides a lot of variety to our 8-bit controller based projects. STM8 family includes member devices of all price, packages and features. So, the developers have the option to choose the right controller depending upon specific application and cost constraints. But, when it comes to software tools, options are kind of limited. If you are working on Windows environment, you can either choose Cosmic or Raisonance compilers and both of them require license. Thanks to SDCC (Small Device C Compiler) we now have a open source, free compiler for STM8 series of controllers. SDCC is such a beautiful compiler, I am using SDCC from a long time now, right from my old 8051 days (some 10 years back), I was very happy when I came to know about SDCC supporting STM8 controllers. I was using STVD with Cosmic Compiler and STVP for Programming on windows for STM8 development. Recently, I tried to use SDCC compiler for STM8 and it worked quite smoothly both on windows as well as GNU/Linux systems. I used STM8FLASH application for device programming in GNU/Linux systems.

STM8 meets GNU/Linux System

STM8 meets GNU/Linux System

Step -1 Install SDCC

First, you need to install SDCC on your machine. SDCC is available directly from ubuntu repository. A direct command on terminal will install SDCC.

$ sudo apt-get install sdcc

To check the SDCC version, enter following command into the terminal, if it indicates support for STM8, we are ready to go, otherwise we will have to update the SDCC version.

$ sdcc -v

Expected output should look like this, with stm8 listed in supported devices

SDCC mcs51/z80/z180/r2k/r3ka/gbz80/tlcs90/ds390/pic16/pic14/TININative/ds400/hc08/s08/stm8 3.4.0 #8981 (Jan 10 2015) (Linux)
published under GNU General Public License (GPL)

If your SDCC installation is an older version and does not support STM8 famaily, use PPA repository to update it to latest version 3.4.0.

sudo add-apt-repository ppa:laczik/ppa
sudo apt-get update
sudo apt-get remove sdcc sdcc-libraries
sudo apt-get install sdcc

To compile any source code for STM8 controller, following SDCC attributes will be required.

$ sdcc -lstm8 -mstm8 --out-fmt-ihx blinky.c

Apart from SDCC compiler support to STM8 devices, SDCC package also includes -sstm8 simulator and -sdasstm8, which is a STM8 assembler. It also includes packihx utility to convert any .ihx file into .hex file. sstm8 simulator is of great interest, though stm8 port is not well documented for this simulator. I will try to tinker with it and post it, if I find something worth sharing.

Step – 2 Install stm8flash

With SDCC, we are done with compilation part, we have generated the machine file that can be downloaded on the device flash. All we need is a program that can perform this job. stm8flash is the most easiest and only known way to do that in GNU/Linux systems. stm8flash is a program written by Valentine Dudouyt and it uses standard programmers like st-link-v1 or st-link-v2 to program stm8 devices.

Download and install stm8flash directly from Github.

git clone
cd stm8flash
sudo make install

Test if the installation was successful

$ stm8flash

Output should look like this

Usage: stm8flash [-c programmer] [-p partno] [-s memtype] [-b bytes] [-r|-w|-v] [filename]
 -? Display this help
 -c programmer Specify programmer used (stlink or stlinkv2)
 -p partno Specify STM8 device
 -l List supported STM8 devices
 -s memtype Specify memory type (flash, eeprom, ram, opt or explicit address)
 -b bytes Specify number of bytes
 -r [filename] Read data from device to file
 -w [filename] Write data from file to device
 -v [filename] Verify data in device against file
Special note for Discovery Board users

Use programmer as stlink while working with any of the STM8 Discovery Boards. However stlinkv2 can be used if a stand alone programmer like ST-Link v2 dongle is being used.

Step – 3 Usage

Write your C Code in a file, let’s say main.c
#include "stm8l.h"
int main() 
   int d,c;
   // Configure pins
   PD_DDR = 0x01;
   PD_CR1 = 0x01;
   // Loop
   do {
        PD_ODR ^= 0x01;
        for(d = 0; d<19000; d++) 
           for(c = 0; c<5; c++) ;
      } while(1);

Compile the code using SDCC
$ sdcc -lstm8 -mstm8 --out-fmt-ihx main.c

Download the machine code onto Device flash

I am using this command for STM8S Discovery board which has STM8S105C6T6 device and LED connected to PD0. You must be inside the directory containing the .ihx file in order to execute the following command, else you may provide the full path to the .ihx file in the command itself.

$ stm8flash -c stlink -p stm8s105?6 -w blinky.ihx

If everything goes right, you should see following on your terminal

$ sudo stm8flash -c stlink -p stm8s105?6 -w blinky.ihx
Determine FLASH area
Writing Intel hex file 199 bytes at 0x8000... OK
Bytes written: 199

LED connected to PD0 on STM8S-Discovery board should now blink at a rate specified in the program delay loop. For any support on this use the comment section below or write to me at devesh[at]electroons[dot]com.