Program STM8S Development Board with ST-Link v2

I successfully programmed development board having STM8S103F3P6. It is quite simple to make it blink LED. To me it was second easiest after Arduino Uno to get started. I used SDCC as the C compiler, ST-Link v2 clone as programmer, and stm8flash to flash the binary file through ST-Link.

STM8S103F3P6 minimal development board
STM8S103F3P6 minimal development board

Required Softwares

As I already mentioned, just two softwares are required to program STM8 microcontrollers in Linux machines: SDCC and stm8flash.

SDCC

SDCC (Small Device C Compiler) is an open source C compiler supporting many microcontrollers including STM8 series. We can get binary distribution from their website http://sdcc.sourceforge.net/ At the time of this writing, it is 3.6.0 version I used.

stm8flash

It is a utility program helping to transfer compiled binary file to STM8 microcontroller's flash memory through ST-Link V1 or V2. It is hosted at github repository https://github.com/vdudouyt/stm8flash.

Required Hardwares

  1. (Obviously) STM8 Development Board (The one I used is having STM8S103F3P6. I assume it will work with any STM8)
  2. ST-Link V1 or V2 (I used cheap Chinese clone of ST-Link V2)
ST-Link V2 for programming STM8 and STM32 microcontrollers
ST-Link V2 for programming STM8 and STM32 microcontrollers

The development board is having a built in LED connected to PB5 pin. So, I did not use any external LEDs here. Thanks to Sduino team for the schematic at https://tenbaht.github.io/sduino/hardware/stm8blue/, which helped me to identify what pin the test LED is connected to. It was difficult for me to identify it myself.

As per the schematic test LED is active low, which means it will be ON when pin is LOW.

Program

#define PB_ODR      *(unsigned char*)0x5005
#define PB_DDR      *(unsigned char*)0x5007
#define PB_CR1      *(unsigned char*)0x5008

// Unsigned int is 16 bit in STM8.
// So, maximum possible value is 65536.
unsigned long int dl; // Delay

int main() {
    PB_ODR = 0x00;  // Turn all pins of port D to low
    PB_DDR |= 1 << 5; // 0x00100000 PB5 is now output
    PB_CR1 |= 1 << 5; // 0x00100000 PB5 is now pushpull

    while(1) {
        PB_ODR ^= 1 << 5; // Toggle PB5 output

        for (dl = 0; dl < 18000; dl++) {}
    }
}

It is a modified version of the program I received from YouTube video https://www.youtube.com/watch?v=6HkXPIhG9Xw.

This program does not depend on any other library and can be compiled directly.

Compile

Run following command to compile above program using SDCC, assuming file is named as blink.c.

sdcc blink.c -lstm8 -mstm8 --out-fmt-ihx -oBLINK.ihx

If successful, above command will output nothing!

Flashing

Connect ST Link V2 with STM8 board as following:

ST-Link -> STM8 board
RST => NRST
GND => GND
SWIM => SWIM
3.3V => 3V3

Connect ST-Link V2 in USB port of the computer and run given command to flash the program.

stm8flash -c stlinkv2 -p stm8s103f3 -w BLINK.ihx

Please change the microcontroller name in above command as needed.

Hope it helps!