(English | 中文)
This project provides a flexible bootloader implementation for STM8 microcontrollers using the SDCC compiler. The bootloader resides in the option bytes reserved area and enables in-application programming (IAP) capabilities via UART communication.
Note: The bootloader implementation is based on and inspired by the STM8uLoader project, with significant modifications and enhancements for integration into this template structure.
-
Three-stage Bootloader:
- Boot0: Reset/TRAP isr for load boot1
- Boot1: Minimal bootloader stored in reserved option bytes
- Boot2: Full-featured bootloader loaded via serial communication
-
Flexible Configuration:
- Enable/disable bootloader via
ENABLE_OPTION_BOOTLOADERMakefile macro
- Enable/disable bootloader via
-
Complete Toolchain:
- PC-side programming utility for firmware updates
- Support for read, write, go and exec operations
-
Safe Operation:
- Bootloader integrity protected in option bytes
- Fallback to application on timeout or communication failure
This project's bootloader implementation is derived from the excellent STM8uLoader project by ovsp. Key adaptations include:
- Integration into a modular template project structure
- Three-stage bootloader approach (Boot1 in option bytes, Boot2 loaded dynamically)
- Enhanced Makefile system with configuration macros
- Extended command set and error handling
-
Power-on/Reset:
- MCU starts execution at reset vector(0x8000)
- Control transfers to
bootloader_enter()
-
Stage 1 (Boot1):
- Copies Boot1 from option bytes (0x480E-0x483F) to RAM and Run
- Sends synchronization sequence
0x00 0x0Dvia UART - Waits for PC to send Boot2 code
-
Stage 2 (Boot2):
- Receives and validates Boot2 code from PC
- Executes Boot2 which provides full command interface
- Processes PC commands for programming, reading, and device control
-
Application Start:
- On successful programming or timeout, jumps to main application(0x8004)
- Option to stay in bootloader mode for debugging
-
The main application includes
bootloader.h, which redirects the TRAP interrupt vector tobootloader_enter()inbsp/boot0.c. -
During the build process, the Makefile swaps the reset vector (0x8000) and trap vector (0x8004) positions. This ensures that upon startup, the bootloader entry routine executes first.
-
It sends a handshake signal (
0x00 0x0D) via UART1 and waits approximately 200ms for a response. Within the timeout period, execution proceeds to the main application.
- SDCC (Small Device C Compiler) installed
- stm8flash or similar programming tool
- Python 3.x (for PC tools)
Enable bootloader support:
make ENABLE_OPTION_BOOTLOADER=1Disable bootloader (direct application execution):
make ENABLE_OPTION_BOOTLOADER=0# Build both application and bootloader option
make all
# Program device (requires stm8flash)
make flashThe bootloader uses the reserved option byte area for storage:
| Address Range | Content | Size |
|---|---|---|
| 0x4800-0x480A | Device option bytes | 11 bytes |
| 0x480D-0x483F | Boot1 code | 51 bytes |
Important: These addresses are specific to STM8S103/003. Adjust for other STM8 variants.
- Baud Rate: 128000 bps
- Data Bits: 8
- Parity: None
- Stop Bits: 1
| Command | Opcode | Description |
|---|---|---|
| CMD_READ | 0xF1 | Read memory from device |
| CMD_WRITE | 0xF2 | Write memory to device |
| CMD_GO | 0xF3 | Jump address for execution |
| CMD_EXEC | 0xF4 | Execute Machine-Code |
- Boot1 sends sync bytes:
0x00 0x0D - PC responds with Boot2 reversed bytes
- Boot1 receves Boot2 and checksum
- Boot2 executes and prepare to receive cmd
- PC sends commands with appropriate parameters
# 1. Build the application
make flash
# 2. Enter interactive mode
python scripts/stm8loader.py /dev/ttyUSB0
# 3. Use command line:
python scripts/stm8loader.py /dev/ttyUSB0 --write 0x8000 firmware.binCurrently tested with:
- STM8S103/003 (default configuration)
Important: To port to other STM8 variants need verify peripheral register definitions
-
No response from device:
- Verify baud rate settings
- Check UART pin connections (TX/RX swapped?)
- Ensure option bytes are correctly programmed
-
Bootloader not starting:
- Verify
ENABLE_OPTION_BOOTLOADERis set during compilation - Check main.c include
bootloader.h - Check
bsp/boot0.c - Confirm option bytes
- Verify
- Power Stability: Ensure stable power supply during programming
- Watchdog Timer: Disable or properly handle watchdog in bootloader
- Interrupts: Save/restore interrupt context during bootloader operations
- Memory Protection: Never overwrite bootloader area in option bytes
- STM8uLoader
- STM8S Reference Manual
- STM8 CPU programming manual
- SDCC User Guide
- STM8 Bootloader AN2659
Note: This implementation is for educational and development purposes. Always verify bootloader behavior in your specific application context before deployment.