ST AN2659 Application note

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

August 2011

Doc ID 14153 Rev 3

1/25

www.st.com

Contents

AN2659

 

 

Contents

1

Operation theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

5

2

STM8 devices with built-in ROM-bootloader . . . . . . . . . . . . . . . . . . . . .

6

 

2.1

Implementation details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

6

 

2.2

Adapting IAP master side to ROM-bootloader protocol . . . . . . . . . . . . . . .

7

3

User-bootloader for STM8 devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

8

3.1 User-bootloader firmware example description . . . . . . . . . . . . . . . . . . . . . 9 3.2 Configuring the user-bootloader firmware example . . . . . . . . . . . . . . . . . 12

4

Memory management for IAP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

13

 

4.1 Memory protection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

13

4.1.1 Flash memory protection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 4.1.2 User boot code protection (UBC) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 4.1.3 Vector table redirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

4.2 Block versus word programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 4.3 RAM versus Flash programming code location . . . . . . . . . . . . . . . . . . . . 15

4.3.1 Programming the data EEPROM area . . . . . . . . . . . . . . . . . . . . . . . . . . 15

4.4 Library support for Flash programming . . . . . . . . . . . . . . . . . . . . . . . . . . 16

4.4.1 Flash programming function list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

5

Configuring the Cosmic compiler for RAM code execution . . . . . . . .

18

 

5.1

Creating a segment in the STVD project . . . . . . . . . . . . . . . . . . . . . . . . .

18

 

5.2

Creating a memory segment in the Cosmic linker file . . . . . . . . . . . . . . .

19

 

5.3

Finishing and checking the configuration . . . . . . . . . . . . . . . . . . . . . . . . .

20

6

Setting up your application firmware for user-bootloader use . . . . . .

22

7

Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

23

 

7.1

Features in the final user-bootloader application . . . . . . . . . . . . . . . . . . .

23

8

Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

24

2/25

Doc ID 14153 Rev 2

AN2659

List of tables

 

 

List of tables

Table 1. Document revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

Doc ID 14153 Rev 2

3/25

List of figures

AN2659

 

 

List of figures

Figure 1. Typical bootloader application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Figure 2. Example of STM8S208xx bootloader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Figure 3. Example of user-bootloader implementation in the Flash memory. . . . . . . . . . . . . . . . . . . . 8 Figure 4. Example of the user-bootloader package provided . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Figure 5. Bootloader flowchart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Figure 6. User boot code area and user application area . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Figure 7. Define linker memory section in STVD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Figure 8. Setting the project start and vector table addresses in STVD . . . . . . . . . . . . . . . . . . . . . . 22

4/25

Doc ID 14153 Rev 2

ST AN2659 Application note

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 usefull when a microcontroler 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

RS232

STM8

 

RS232/TTL

Bootloader enable

converter

jumper

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 2

5/25

STM8 devices with built-in ROM-bootloader

AN2659

 

 

2 STM8 devices with built-in ROM-bootloader

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

2.1Implementation 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

X

"OOT2/-

X &&

X

&LASHAPROGRAM MEMORY

X &&&

AI

6/25

Doc ID 14153 Rev 2

AN2659

STM8 devices with built-in ROM-bootloader

 

 

2.2Adapting 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 2

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

X

)NTERRUPTPVECTORTTABLE

X

5SER BOOTLOADER

X

5"#"PROTECTEDOAREA

AI

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 (STM8A, STM8L, and STM8S). 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 the user-bootloader firmware.

8/25

Doc ID 14153 Rev 2

Loading...
+ 17 hidden pages