AN2659
Application note
STM8 in-application programming (IAP)
using a customized user-bootloader
Introduction
This application note is intended for STM8 firmware and system designers who need to
implement an in-application programming (IAP) feature in the product they are developing
with the STM8 microcontroller.
The STM8 is an 8-bit microcontroller family with a Flash memory for storing the user
program code or firmware. IAP makes it possible to update the firmware ‘in situ’, after the
microcontroller has been embedded in the final product. The advantage is that the
microcontroller board can stay inside its product enclosure. No mechanical intervention is
needed to make the update.
IAP is extremely useful for distributing new firmware versions. It makes it easy to add new
product features and correct problems throughout the product life cycle.
The user-bootloader firmware source code provided with this application note shows an
example of how to implement IAP for the STM8 microcontroller. Use this code as a
reference when integrating IAP in your STM8 application. It includes the following features:
●
Bootloader activated by external pin (jumper on PCB)
Flash block programing by executable RAM code management
Read while write (RWW) feature
High level C-language usage
Reduced size of the code (optimized code)
Support for multiple communication interfaces (SPI, I2C, and UART)
UART code compatible with ST Flash loader demonstrator software
●
●
●
●
●
●
Table 1.
Applicable products
Product family
Part numbers
Microcontrollers
– STM8S003xx, STM8S005xx, STM8S007C8
– STM8S103xx, STM8S903xx, STM8S105xx, STM8S207xx, STM8S208xx,
– STM8AF6x26/4x/66/68, STM8AF5xxx, STM8AF6x69/7x/8x/9x/Ax
– STM8L05xxx
– STM8L101xx
– STM8L151C2/K2/G2/F2 and STM8L151C3/K3/G3/F3
– STM8L151x4, STM8L151x6, STM8L152x4, STM8L152x6
– STM8L151x8, STM8L152x8, STM8L151R6, STM8L152R6, STM8L162R8,
STM8L162M8
– STM8AL313x, STM8AL314x, STM8AL316x, STM8AL3L4x, STM8AL3L6x
– STM8TL5xxx
November 2012
Doc ID 14153 Rev 4
1/25
www.st.com
Contents
Contents
AN2659
1
2
3
4
5
6
7
8
Operation theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
STM8 devices with built-in ROM-bootloader . . . . . . . . . . . . . . . . . . . . . 6
Implementation details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.1
2.2
Adapting IAP master side to ROM-bootloader protocol . . . . . . . . . . . . . . . 7
User-bootloader for STM8 devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
User-bootloader firmware example description . . . . . . . . . . . . . . . . . . . . . 9
3.1
3.2
Configuring the user-bootloader firmware example . . . . . . . . . . . . . . . . . 12
Memory management for IAP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Memory protection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
4.1
Flash memory protection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
4.1.1
User boot code protection (UBC) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
4.1.2
4.1.3
Vector table redirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Block versus word programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
RAM versus Flash programming code location . . . . . . . . . . . . . . . . . . . . 15
4.3.1
Programming the data EEPROM area . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Library support for Flash programming . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.4.1
Flash programming function list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.2
4.3
4.4
Configuring the Cosmic compiler for RAM code execution . . . . . . . . 18
Creating a segment in the STVD project . . . . . . . . . . . . . . . . . . . . . . . . . 18
5.1
Creating a memory segment in the Cosmic linker file . . . . . . . . . . . . . . . 19
5.2
5.3
Finishing and checking the configuration . . . . . . . . . . . . . . . . . . . . . . . . . 20
Setting up your application firmware for user-bootloader use . . . . . . 22
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Features in the final user-bootloader application . . . . . . . . . . . . . . . . . . . 23
7.1
Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2/25
Doc ID 14153 Rev 4
AN2659
List of tables
List of tables
Table 1.
Table 2.
Applicable products . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Document revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Doc ID 14153 Rev 4
3/25
List of figures
List of figures
AN2659
Figure 1.
Figure 2.
Figure 3.
Figure 4.
Figure 5.
Figure 6.
Figure 7.
Figure 8.
Typical bootloader application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Example of STM8S208xx bootloader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Example of user-bootloader implementation in the Flash memory. . . . . . . . . . . . . . . . . . . . 8
Example of the user-bootloader package provided . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Bootloader flowchart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
User boot code area and user application area . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Define linker memory section in STVD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Setting the project start and vector table addresses in STVD . . . . . . . . . . . . . . . . . . . . . . 22
4/25
Doc ID 14153 Rev 4
AN2659
Operation theory
1
Operation theory
In practice, IAP requires a bootloader implemented in the STM8 firmware that can
communicate with an external master (such as a PC) via a suitable communication
interface. The new code can be downloaded into the microcontroller through this interface.
The microcontroller then programs this code into its Flash memory.
IAP can also be used to update the content of the internal data EEPROM memory, and the
internal RAM memory.
This operation is useful when a microcontroller is already soldered in its final application and
needs a firmware update.
Figure 1 shows a typical bootloader application.
Figure 1.
Typical bootloader application
STM8 board
STM8
RS232/TTL
converter
Bootloader enable
jumper
RS232
PC
The bootloader is that part of the code which runs immediately after a microcontroller reset
and which waits for an activation signal (for example, from grounding a specific pin or
receiving a token from a communication interface). If activation is successful the code enters
bootloader mode. If activation fails (for example due to a timeout or the jumper on the pin not
being present) the bootloader jumps directly to the user application code.
In bootloader mode, the microcontroller communicates with the external master device
through one of the serial communication interfaces available in it (UART, SPI, I2C, CAN)
using a set of commands. These commands are usually:
● Write to Flash
●
Erase Flash
Verify Flash
Additional operations such as read memory and execute code from a given address
(jump to given address).
●
●
The ST proprietary bootloader can be used. It is embedded in the ROM memory of STM8
devices with a program memory greater than 8 Kbytes. In this case, no code development is
needed. To use the proprietary bootloader, enable it via the option bytes.
Alternatively, develop a customized bootloader using, for example, a serial communication
interface that is not supported in ST versions or in devices where the ST bootloader is not
present. Such a bootloader should be stored in the user boot code area (UBC) in the
microcontroller. This guarantees protection against unintentional write operations.
Doc ID 14153 Rev 4
5/25
STM8 devices with built-in ROM-bootloader
AN2659
2
STM8 devices with built-in ROM-bootloader
2.1
Most STM8 devices have an internal bootROM memory which contains an ST proprietary
bootloader. Consequently, they already have a built in IAP implementation (see UM0560:
STM8 bootloader).
Implementation details
The built-in ROM-bootloader is located in a dedicated part of the memory called the
BootROM. The ROM-bootloader code is fixed (not rewritable) and is specific for each
device. The communication interface supported depends on the peripherals present in the
given STM8 device and whether they are implemented in the ROM-bootloader. For example,
some devices support firmware download through UART/LIN and CAN, some devices
support only UART, and others only SPI. Information concerning the supported interfaces
can be found in the relevant device datasheet.
Activation of the built-in ROM-bootloader is made by programming the BL[7:0] option byte
described in the option byte section of the device datasheet. The bootROM bootloader
checks this option byte and if it is enabled, it runs its own code (it waits for the host to send
commands/data). If the BL[7:0] option byte is inactive, the bootrom bootloader jumps to the
user reset address (0x8000).
Figure 2.
Example of STM8S208xx bootloader
6/25
Doc ID 14153 Rev 4
AN2659
2.2
STM8 devices with built-in ROM-bootloader
Adapting IAP master side to ROM-bootloader protocol
To be able to download firmware into the device, the host and bootloader must communicate
through the same protocol. This bootloader protocol for STM8 devices is specified in the
STM8 bootloader (UM0560) user manuals available from http://www.st.com. The same
protocol is used in the firmware example provided with this application note. The UM0560
user manual describes all bootloader protocol properties including used interfaces,
timeouts, command formats, packet formats, and error management.
Doc ID 14153 Rev 4
7/25
User-bootloader for STM8 devices
AN2659
3
User-bootloader for STM8 devices
For STM8 microcontrollers which do not include a built-in bootloader or which use a
communication protocol (I2C) not yet supported in the built-in bootloader, user-bootloader
firmware can be added and customized at the beginning of the Flash memory.
Figure 3.
Example of user-bootloader implementation in the Flash memory
For this purpose, an example of a user-bootloader firmware is provided with this application
note. This package is divided into three main subdirectories, each one dedicated to one
STM8 family member: STM8AF, STM8L and STM8S. For the STM8AL family, please use
the STM8L directory. Each directory is composed of the following components:
●
Sources: containing the firmware source code
Includes: containing the firmware header file (main.h). This file can be edited to
configure your user-bootloader (see Section 3.2: Configuring the user-bootloader
firmware example).
Flash_loader_demonstrator_files: contain all map files to add in the Flashloader
demonstrator install directory to be compatible with the user-bootloader.
STVD: containing a prebuilt project for STVD using a Cosmic or raisonance compiler
●
●
●
Figure 4 shows an example of the user-bootloader firmware.
8/25
Doc ID 14153 Rev 4