STM8

Compiler Comparison
RTOS Availability

Tutorials

STM8L-DISCOVERY

LED Timer
Serial

STM8S-DISCOVERY

LED Timer
Serial

STM8A-DISCOVERY

STM8AF LED Timer
STM8AF Serial
STM8AF Dhry. Whet.
STM8AL LED Timer
STM8AL Serial

EX-STM8-Q64a-207

LED Timer
Serial
Dhrystone Whetstone

Open8S208Q80

LED Timer
Serial
Dhrystone Whetstone

EX-STM8-Q48a-105

LED Timer
Serial

STM8/128-EVAL

LED Timer
Serial
Dhrystone Whetstone

STM8L101-EVAL

Serial

colecovision.eu

ColecoVision

STM8

MCS-51

LLVM+SDCC

Contact

Getting started with STM8 development using free software: LED and timer on the STEVAL-ILL075V1

This short tutorial is about a simple LED and timer demo for the STEVAL-ILL075V1 board. The author used a Debian GNU/Linux system, but the tutorial should work for other Linux distributions, *BSD or other Unices.

The tools we use are

Hardware setup

STEVAL-ILL075V1 with Demo running

The STEVAL-ILL075V1 is connected to power via the mini-USB port. To write our program to the board, an ST-LINK/V2 is attached to the SWIM port.

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.

SDCC binaries or a source tarball can be downloaded from its website.

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 2-bit LED counter once per second. This demonstrates setting up and using an accurate timer with interrupts and doing basic I/O. Here is the C code:

// Source code under CC0 1.0
#include <stdint.h>

#define CLK_CKDIVR	(*(volatile uint8_t *)0x50c6)
#define CLK_PCKENR1	(*(volatile uint8_t *)0x50c7)

#define STMR_CR1	(*(volatile uint8_t *)0x5340)
#define STMR_IER	(*(volatile uint8_t *)0x5341)
#define STMR_SR1	(*(volatile uint8_t *)0x5342)
#define STMR_EGR	(*(volatile uint8_t *)0x5343)
#define STMR_CNTH	(*(volatile uint8_t *)0x5344)
#define STMR_CNTL	(*(volatile uint8_t *)0x5345)
#define STMR_PSCL	(*(volatile uint8_t *)0x5346)
#define STMR_ARRH	(*(volatile uint8_t *)0x5347)
#define STMR_ARRL	(*(volatile uint8_t *)0x5348)

#define P0_ODR	(*(volatile uint8_t *)0x5000)
#define P0_DDR	(*(volatile uint8_t *)0x5002)
#define P0_CR1	(*(volatile uint8_t *)0x5003)

static volatile unsigned long c;

unsigned long clock(void)
{
	return(c);
}

static void stmr(void) __interrupt(23)
{
	c++;
	STMR_SR1 = 0x00;
}

void main(void)
{
	CLK_CKDIVR = 0x00;  // Set the frequency to 16 MHz
	CLK_PCKENR1 = 0xff; // Enable peripherals

	STMR_PSCL = 0x07;   // Timer frequency 125 kHz (lowest possible).
	STMR_ARRH = 0;
	STMR_ARRL = 125;    // Event frequency 1 kHz.
	STMR_EGR = 0x01;    // Re-initialize timer.
	STMR_CR1 = 0x05;    // Start timer.
	STMR_IER = 0x01;    // Enable timer interrupts.

	P0_DDR = 0x30;
	P0_CR1 = 0x30;

	__asm;
	rim                 // Enable interrupts.
	__endasm;

	for(;;)
		P0_ODR = ((clock() / 1000) << 4) & 0x30;
}

SDCC is a freestanding, not a hosted implementation of C, and allows main to return void. We set up the timer to increment once 125 per millisecond, and generate an interrupt for every 125 increments, which allows us to implement a basic clock() function. This function is used to control the blinking of the LEDs.

The demo can be compiled simply by invocing 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 stlux385a -w led.ihx will write the demo onto the board. It will run and count up to 3 on the LEDs, the start again at 0.


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

SDCC is a C compiler that aims to be compliant with the C standards.

Important compiler options for STM8 developers include: