How to Program STMicro STM8S $1 Board in Linux

In January, I discovered there was such thing as a one dollar development board based on STMicro STM8S103F3P6 8-bit MCU with 1KB SRAM, 8KB flash, and 640 bytes EEPROM, some GPIOs as well as I2C, UART, SPI, ADC, and PWM signals. Links to documentation and source code were provided, but development tools were only Windows based. However, one of my reader informed me SDCC (Small Devices C Compiler) supported STM8, and development in Linux should be feasible. So I decided to buy the board on eBay for $1.62, as well as an ST_link V2 programmer for STM8 / STM32 for $4.52 in order to flash the firmware.

The board came pretty quickly, i.e. within 2 to 3 weeks.

STM8S103F3_BoardBut due to a lost package, the programmer took nearly 3 months to reach me, as the seller had to re-send after I failed to receive it within 2 months.

ST-Link_V2_ProgrammerIt comes with 4 wires that need to be connected to RST/NRST, 3.3V, SWIM, and GND. To do so, I had to solder a 4-pin header on the side of the board.

To get started with STM8 on Linux, I found some information on ColeVision website where they explained how to run Drystone on STM8/128-EVAL board using SDCC as the compiler, and stm8flash to program the board.

So I’ve given it a try on Ubuntu 14.04 with the simple goal of blinking the TEST LED on the board. sdcc is an Ubuntu package, so it’s pretty easy to install:


Let’s check the version and some more information:

sdcc --version
SDCC : mcs51/gbz80/z80/z180/r2k/r3ka/ds390/pic16/pic14/TININative/ds400/hc08/s08 3.3.0 #8604 (Dec 30 2013) (Linux)

That’s version 3.3.0 released in December 2013 and it lacks STM8 supports, but the latest version (sdcc v3.4.0) has been released in April 2014, and we can get it with a PPA, so let’s use that one instead:


If you are using Ubuntu 14.10 or greater, you’ll already get the latest version.

Let’s double check stm8 is indeed supported:

sdcc --version
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)

Great! stm8 is now present in the list of supported MCUs.

For the next step was to install stm8flash tool to program the device:


Now I had to find some code samples to blink the LED. I started but checking the samples for ST Visual Programmer and IAR tools, combine with a modified version of Drystone source code, but I always had some issues with the header files, and it seems a bit more work than expected. But then I discovered that Valentin Dudouyt, stm8flash developer, also provided code samples in his github account, including a blinky sample:


First, I tried to compile the samples, and they failed because sp_test.c was missing, so I edited the Makefile to remove sp_test.ihx, and build was successful. The LED on the board is connected to B5 GPIO, so at that point I had to check out STM8S103F3 datasheet to find out more about the registers used in the sample. The register map is in section 6.2 of the document, and I need to use three registers:

  • PB_ODR – Port B data output latch register (Sets pin HIGH or LOW)
  • PB_DDR – Port B data direction register (Sets direction to INPUT or OUTPUT)
  • PB_CR1 – Port B control register 1

Since pin 5 correspond to 00100000 (0x20) I updated blinky.c sample as follows:


I typed make again to generate an updated firmware (blinky.ihx), and flash it as follows:


All good and the LED started blinking…
STM8S_Blink_LED

So now you should be able to write simple program to control other GPIOs, I2C, SPI, UART with the board. But if your program is a little more complex a debugger could be useful, and there’s Texane STLink working on Windows, Mac, and Linux, that can run gdbserver for STM32… But I tried it for STM8.


Then I ran st-link:


Hmmm, it does not look good, and sadly there’s no support for STM8 yet, as STM32 and STM8 use different interfaces (SWD vs SWIM).

So if you have troubles debugging your program, you may have to revert to Windows based tools, at least for now, unless you’re up for the task and want to add STM8 support to Texane.

Share this:

Support CNX Software! Donate via cryptocurrencies, become a Patron on Patreon, or purchase goods on Amazon or Aliexpress

ROCK 5 ITX Rockchip RK3588 mini-ITX motherboard
Subscribe
Notify of
guest
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.
31 Comments
oldest
newest
zoobab
9 years ago

I saw a AMS1117 voltage regulator on the back of the board. Does the chip runs in 3.3v or 5v?

fuyi zhang
fuyi zhang
6 years ago

The chip runs at DC3.3V

Zygmunt Krynicki
9 years ago

What is the USB port for? Reading the datasheet (quickly) seems to indicate that there is no USB device on that micro. Is it used for serial? If so, what’s doing the serial translation?

zoobab
9 years ago

@Zygmunt Krynicki
Probably the USB is doing power only. It would be stupid for a manufacturer to have a USB stack on the chip, and not to use it in the bootloader to be able to reflash the chip with something else.

Anton Fosselius
9 years ago

Use openocd! Its so simple and so efficient!

I actually used one of those stlink to flash a kinetis k10.

There is a qstlink that is quite sweet as well.

Can?
Can?
9 years ago

Con emulate PS3?
At least can run Super Street Figther 4?

Jose
Jose
9 years ago

Great article. Just got my board and programmer in the mail, solder headers and I was good to go. Compiled a new version of sdcc on Linux Mint 13 and was able to compile your test program and flashed it. LED blinks! One question, how did you know B5 was hooked up to the LED? Do you have the schematic or did you just trace it with a multimeter? The docs on eBay points to a link where the documentation are all in Chinese. Thanks!

P.
P.
9 years ago

There are also a few tutorials (led counter, UART, benchmarks) at http://colecovision.eu/stm8/, currently for two evaluation boards (STM8L-DISCOVERTY and STM8/128-EVAL); I hope I’ll find to get the versions for the other boards up sometime.

Philipp

Chris
Chris
9 years ago

Hello, Really nice tutorial! very helpfull for a starter/make everything works ok project. I have also verified that the LED is on pin B5 with a mutlimeter. but even if i change the registers in code my LED doesn’t flash. I am thinking that my board is dead, but writing with stm8flash seems to be done with no errors, i would expect that to not happen at all with a dead chip, right? Do you have any pointers on what i should check next? or maybe a different .c file that tests the chip functionallity? P.S I did also check… Read more »

Chris
Chris
9 years ago

I tried to maybe connect a new LED on A2 Pin, next to ground.

But the Datasheet is hard for my inexperienced eyes to read in order to decide what registers to use…

Can you help me a bit on that? i also though of changing the problem a bit, and keep the B5 high all the time.
Since i don’t own a logic analyzer ( yet ), maybe i can just keep it high and measure it with my mutlimeter?

Thanks for the fast reply 🙂

Andrade
Andrade
8 years ago

Is there a way to programm this board without the st-link?

Andrew
Andrew
8 years ago

@Chris
My LED also does not blink. Did you ever figure this out?
Iv’e tested compiling and uploading with sdcc on Linux and Windows. Uploading works fine on both OS’s but the LED doesn’t blink.

Iv’e ordered a couple more from Ebay and Ali-express just to make sure its not the board.

Andrew
Andrew
8 years ago


Well I tested the LED and the red one is working but not the green one. So idk if thats the only problem or if there is more.
I ordered 4 more from different places so I can confirm later when they arrive. Thanks for the tip.

JJFlash
JJFlash
8 years ago

Because of the price I bought a STM8S board to. I’m started with discription on your website (and other websites too). In the first it was a good entry point, but I spend a lot of time to discover out, that the adresses declared in stm8l.h are different to the adresses for stm8s. This was find out by reading the reference manual. For the GPIO it doesn’t matter, but if you want to use the USART1, it won’t work for receiving datas. Another thing is the clock setup. In your example the STM8S the CPU is clocked by internal 2… Read more »

JJFlash
JJFlash
8 years ago

/* ———————————————– Registeradressen fuer einen STM8S MCU Originaldatei war fuer einen STM8L – geaenderte Registeradressen fuer CLK 18.05.2016 R. Seelig ———————————————– */ // GPIO #define PA_ODR *(unsigned char*)0x5000 #define PA_IDR *(unsigned char*)0x5001 #define PA_DDR *(unsigned char*)0x5002 #define PA_CR1 *(unsigned char*)0x5003 #define PA_CR2 *(unsigned char*)0x5004 #define PB_ODR *(unsigned char*)0x5005 #define PB_IDR *(unsigned char*)0x5006 #define PB_DDR *(unsigned char*)0x5007 #define PB_CR1 *(unsigned char*)0x5008 #define PB_CR2 *(unsigned char*)0x5009 #define PC_ODR *(unsigned char*)0x500A #define PC_IDR *(unsigned char*)0x500B #define PC_DDR *(unsigned char*)0x500C #define PC_CR1 *(unsigned char*)0x500D #define PC_CR2 *(unsigned char*)0x500E #define PD_ODR *(unsigned char*)0x500F #define PD_IDR *(unsigned char*)0x5010 #define PD_DDR *(unsigned char*)0x5011 #define PD_CR1 *(unsigned char*)0x5012… Read more »

Peter
Peter
8 years ago

@Anton Fosselius
I checked http://openocd.org/ and saw no mention of the STM8 family, only the STM32. What can you use openocd for here please?

Lazlo Lozla
Lazlo Lozla
7 years ago

Hello! When I try to install stm8flash in lubuntu 16.10 LTS. In the terminal appears this error after typing ‘make’:
cc -g -O0 –std=gnu99 –pedantic pkg-config --cflags libusb-1.0 -c -o stlink.o stlink.c
Package libusb-1.0 was not found in the pkg-config search path.
Perhaps you should add the directory containing `libusb-1.0.pc’
to the PKG_CONFIG_PATH environment variable
No package ‘libusb-1.0’ found
In file included from stlink.c:14:0:
pgm.h:6:21: fatal error: libusb.h: No existe el archivo o el directorio
compilation terminated.
: fallo en las instrucciones para el objetivo ‘stlink.o’
make: *** [stlink.o] Error 1

Someone who can help me! Thank you!

Lazlo Lozla
Lazlo Lozla
7 years ago

That was the problem. I have to look more closely at the messages. Thank you!

zoobab
7 years ago
André
André
7 years ago

@Peter

There are patches to add STM8 support to OpenOCD and GDB, and improve SDCC. https://stm8-binutils-gdb.sourceforge.io/ (I haven’t tested them yet)

André
André
7 years ago

@zoobab

I’ve never heard of versaloon, but looking at that page, it makes me think about the Black Magic Probe. ( https://github.com/blacksphere/blackmagic/wiki ). The firmware can also be flashed to the cheap ST-Link clones as in the picture in the article. However, there is no STM8 support, only ARM support. GDB directly connects to the Black Magic Probe, without the need of OpenOCD. However, even though GDB itself seems to work fine with BMP, I haven’t gotten eclipse to work with it yet.

gungsukma
gungsukma
7 years ago

I tried on Windows and success
– got sdcc and stm8flash and libusb-1.0.dll from https://github.com/tenbaht/sduino-windowstools
– add sduino-windowstools-master\tools\bin to %PATH%
– install stlink driver (en.stsw-link009.zip)
– my module is locked, so https://github.com/vdudouyt/stm8flash/files/372158/optdefault.bin.txt
>stm8flash.exe -c stlinkv2 -p stm8s103f3 -s opt -w optdefault.bin.txt
– got that blinky.c and stm8l.h
>sdcc.exe -lstm8 -mstm8 –out-fmt-ihx blinky.c
>stm8flash.exe -c stlinkv2 -p stm8s103f3 -w blinky.ihx

gungsukma
gungsukma
7 years ago

My flash is locked, so (https://github.com/vdudouyt/stm8flash/issues/38)

echo “00 00 ff 00 ff 00 ff 00 ff 00 ff” | xxd -r -p > factory_defaults.bin

stm8flash -c stlinkv2 -p stm8s103f3 -s opt -w factory_defaults.bin

Boardcon Rockchip RK3588S SBC with 8K, WiFI 6, 4G LTE, NVME SSD, HDMI 2.1...