Home | Notes | Projects > STM32 MCU Custom Bootloader

STM32 MCU Custom Bootloader

Source code: https://github.com/kyungjae-lee/stm32-mcu-custom-bootloader.

 

Introduction

 

Development Environment

 

Bootloader-Host Communication over USART Peripheral Interface

Overview

 

bootloader-host-communication-interfaces

 

Host Application

 

host-application-menu

 

 

Bootloader Design

Bootloader Code Placement in Flash (Main) Memory

 

custom-bootloader-memory-placement

 

Supported Bootloader Commands

Host SendsCommand CodeBootloader RespondsNotes
BL_GET_VER0x51Bootloader version number (1 byte)Used to read the bootloader version from the MCU
BL_GET_HELP0x52All supported command codes (1 byte per supported command)Used to retrieve all the commands that are supported by the bootloader
BL_GET_CID0x53Chip identification number (2 bytes)Used to get the MCU chip identification number
BL_GO_TO_ADDR0x54Success or error code (1 byte)Used to jump bootloader to specified address
BL_ERASE_FLASH0x55Success or error code (1 byte)Used to mass-erase or sector-erase the user Flash
BL_READ_MEM0x56Memory contents of length asked by the hostUsed to read data from different memories of the MCU
BL_WRITE_MEM0x57Success or error code (1 byte)Used to write data into different memories of the MCU
BL_GET_RDP_LEVEL0x58Flash Read Protection (RDP) level (1 byte)Used to read the Flash Read Protection (RDP) level
BL_SET_RDP_LEVEL0x59Success or error code (1 byte)Used to set the Flash Read Protection (RDP) level
BL_ENABLE_WRP0x5ASuccess or error code (1 byte)Used to enable the Write Protection (WRP) for the selected sectors of the user Flash memory
BL_DISABLE_WRP0x5BSuccess or error code (1 byte)Used to disable the Write Protection (WRP) for all the sectors of the user Flash (i.e., Restores the default protection state)
BL_GET_WRP_STATUS0x5CAll sectors' Write Protection (WRP) statusUsed to read the Write Protection (WRP) status of all the sectors of the user Flash memory
BL_READ_OTP0x5DOTP contentsUsed to read the OTP contents

BL_GO_TO_ADDR: When entering the target address to the host application, make sure to account for the T-bit setting. For example, If the address of the user application's reset handler is 0x08008229, then enter 0x08008228. Please see the bootloader source code for more details.

 

Host-Bootloader Communication Sequence

 

host-bootloader-communication-sequence-diagram

 

Custom Bootloader Flow Chart

 

custom-bootloader-flow-chart

 

Bootloader Command Handling Flow Chart

 

bootloader-command-handling-flow-chart

 

 

Bootloader Commands in Detail

BL_GET_VER

 

bl_get_ver

 

BL_GET_HELP

 

bl_get_help

 

GL_GET_CID

 

bl_get_cid

 

BL_GO_TO_ADDR

 

bl_go_to_addr

 

BL_ERASE_FLASH

 

bl_erase_flash

 

BL_READ_MEM

 

bl_read_mem

 

BL_WRITE_MEM

 

bl_write_mem

 

BL_GET_RDP_LEVEL

 

bl_get_rdp_level

 

BL_SET_RDP_LEVEL

 

bl_set_rdp_level

 

BL_ENABLE_WRP

 

bl_enable_wrp

 

BL_DISABLE_WRP

 

bl_disable_wrp

 

BL_GET_WRP_STATUS

 

bl_get_wrp_status