Hello STM8, nice to meet you!

With Atmel being bought by Microchip, the age-old flamewar of AVR vs PIC might just be over. Wait, that didn’t come out right. I don’t think that the AVR will die a quick death. Especially the ATmega328 is too prominent in the Arduino community. But I’m not so sure about the other chips’ future. Will Microchip tolerate in-house rivals? I would sorely miss the Tiny13 and Tiny85 if they were to be discontinued. They’re perfect for smaller tasks. And I don’t like the PIC. There, I said it. I like being able to program my AVRs under Linux. I like being able to use cheap, robust tools, not the unstable disaster called PICkit 3.

Hum. Maybe it’s time to look at other architectures. MSP430 comes to mind, but I’d rather stay with 5V components for now. Looking at the STM8 series, there are nice and cheap solutions that could replace the AVR for me. Let’s have a closer look.

The STM8S003F3P6 comes to mind. A kilobyte of RAM, 8 kB of Flash, 128 bytes of EEPROM, 16 GPIOs (14 if you want to connect an external oscillator), 5V tolerant, I2C, SPI and UART for about 0.51 Euros, very interesting. That’s cheaper than the Tiny13, with way better specs. And it’s an accumulator machine, just like my beloved 6502! The sad part about the STM8 is that there are no DIP variants, so goodbye breadboard friendliness. Ah well, there are adapter boards out there, and stlink clones are cheap, let’s go shopping!

… a few days pass …

They’re here! And ohmygodthatssmall! TSSOP20 chips are tiny little buggers, with a total length of 6.6 mm, a challenge to solder by hand. Tip: Use a bigger solder tip and drag a glob of solder across the (fluxed) pins. I used a 2mm chisel with 1mm solder at 320°C and didn’t have any problems. The result: A breadboard-friendly STM8!


Now for the “Hello, World!” program: Let’s just flash a LED. The STM needs a 1µF capacitor on a special pin, then we add a LED/resistor combo on PA3 to ground. No need for a transistor, the STM8 is strong enough for a LED. Connect the stlink to VDD/GND, Reset and SWIM (the ST single wire debug pin), and we’re ready to go!

Looking at the datasheet, we find three memory-mapped registers to use if we just want to turn a pin into a push-pull output: ODR, DDR and CR1. Set a bit in DDR (data direction register?) just like on AVR chips, then set the same bit in CR1 (otherwise we’d get an open drain output). Setting the appropriate bit in ODR will pull the pin up. The ports start at address 0x5000, with 5 bytes for each port. Let’s whip up some code:

#include <stdint.h>

#define PA_ODR  (*(volatile uint8_t *)0x5000)
#define PA_DDR  (*(volatile uint8_t *)0x5002)
#define PA_CR1  (*(volatile uint8_t *)0x5003)

main() {
  uint16_t i;
  PA_DDR = 0x08; // PA3
  PA_CR1 = 0x08;

  while (1) {
    PA_ODR ^= 0x08;
    for (i=0; i<60000; i++) {

The Small Device C Compiler (sdcc) supports a nice selection of architectures starting from the venerable 8051, and including STM8. Let’s compile our little test program:

sdcc -mstm8 one.c

SDCC is nice enough to create an Intel Hex file (.ihx) that we can burn to the chip with the stm8flash utility:

stm8flash -c stlinkv2 -p stm8s003f3 -w one.ihx

Seconds later, the LED starts blinking. Success! I’m not changing the clock settings here, which means that the chip uses its internal 16 MHz clock source with a divider of 8, resulting in a system clock frequency of 2 MHz. Good enough for a first test.


Now for that UART. Gonna play around with my new toy a little more…


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s