STM32 Bootloader
Customizable Bootloader for STM32 microcontrollers
bootloader.h File Reference

This file contains the bootloader configuration parameters, function prototypes and other required macros and definitions. More...

#include "stm32l4xx.h"

Go to the source code of this file.

Macros

#define STM32L4
 
#define USE_CHECKSUM   0
 
#define USE_WRITE_PROTECTION   0
 
#define SET_VECTOR_TABLE   1
 
#define CLEAR_RESET_FLAGS   1
 
#define APP_ADDRESS   (uint32_t)0x08008000
 
#define END_ADDRESS   (uint32_t)0x080FFFFB
 
#define CRC_ADDRESS   (uint32_t)0x080FFFFC
 
#define SYSMEM_ADDRESS   (uint32_t)0x1FFF0000
 
#define APP_SIZE   (uint32_t)(((END_ADDRESS - APP_ADDRESS) + 3) / 4)
 
#define FLASH_PAGE_NBPERBANK   (256)
 
#define RAM_BASE   SRAM1_BASE
 
#define RAM_SIZE   SRAM1_SIZE_MAX
 

Enumerations

enum  eBootloaderErrorCodes {
  BL_OK = 0,
  BL_NO_APP,
  BL_SIZE_ERROR,
  BL_CHKS_ERROR,
  BL_ERASE_ERROR,
  BL_WRITE_ERROR,
  BL_OBP_ERROR
}
 
enum  eFlashProtectionTypes {
  BL_PROTECTION_NONE = 0,
  BL_PROTECTION_WRP = 0x1,
  BL_PROTECTION_RDP = 0x2,
  BL_PROTECTION_PCROP = 0x4
}
 

Functions

uint8_t Bootloader_Init (void)
 This function initializes bootloader and flash. More...
 
uint8_t Bootloader_Erase (void)
 This function erases the user application area in flash. More...
 
uint8_t Bootloader_FlashBegin (void)
 Begin flash programming: this function unlocks the flash and sets the data pointer to the start of application flash area. More...
 
uint8_t Bootloader_FlashNext (uint64_t data)
 Program 64bit data into flash: this function writes an 8byte (64bit) data chunk into the flash and increments the data pointer. More...
 
uint8_t Bootloader_FlashEnd (void)
 Finish flash programming: this function finalizes the flash programming by locking the flash. More...
 
uint8_t Bootloader_GetProtectionStatus (void)
 This function returns the protection status of flash. More...
 
uint8_t Bootloader_ConfigProtection (uint32_t protection)
 This function configures the wirte protection of flash. More...
 
uint8_t Bootloader_CheckSize (uint32_t appsize)
 This function checks whether the new application fits into flash. More...
 
uint8_t Bootloader_VerifyChecksum (void)
 This function verifies the checksum of application located in flash. If USE_CHECKSUM configuration parameter is disabled then the function always returns an error code. More...
 
uint8_t Bootloader_CheckForApplication (void)
 This function checks whether a valid application exists in flash. The check is performed by checking the very first DWORD (4 bytes) of the application firmware. In case of a valid application, this DWORD must represent the initialization location of stack pointer - which must be within the boundaries of RAM. More...
 
void Bootloader_JumpToApplication (void)
 This function performs the jump to the user application in flash. More...
 
void Bootloader_JumpToSysMem (void)
 This function performs the jump to the MCU System Memory (ST Bootloader). More...
 
uint32_t Bootloader_GetVersion (void)
 This function returns the version number of the bootloader library. Semantic versioning is used for numbering. More...
 

Detailed Description

This file contains the bootloader configuration parameters, function prototypes and other required macros and definitions.

STM32 Bootloader Header

Author
Akos Pasztor
See also
Please refer to README for detailed information.

Definition in file bootloader.h.

Macro Definition Documentation

◆ APP_SIZE

#define APP_SIZE   (uint32_t)(((END_ADDRESS - APP_ADDRESS) + 3) / 4)

Size of application in DWORD (32bits or 4bytes)

Definition at line 71 of file bootloader.h.

◆ FLASH_PAGE_NBPERBANK

#define FLASH_PAGE_NBPERBANK   (256)

Number of pages per bank in flash

Definition at line 74 of file bootloader.h.

◆ RAM_BASE

#define RAM_BASE   SRAM1_BASE

Start address of RAM

Definition at line 77 of file bootloader.h.

◆ RAM_SIZE

#define RAM_SIZE   SRAM1_SIZE_MAX

RAM size in bytes

Definition at line 78 of file bootloader.h.

Enumeration Type Documentation

◆ eBootloaderErrorCodes

Bootloader error codes

Enumerator
BL_OK 

No error

BL_NO_APP 

No application found in flash

BL_SIZE_ERROR 

New application is too large for flash

BL_CHKS_ERROR 

Application checksum error

BL_ERASE_ERROR 

Flash erase error

BL_WRITE_ERROR 

Flash write error

BL_OBP_ERROR 

Flash option bytes programming error

Definition at line 82 of file bootloader.h.

◆ eFlashProtectionTypes

Flash Protection Types

Enumerator
BL_PROTECTION_NONE 

No flash protection

BL_PROTECTION_WRP 

Flash write protection

BL_PROTECTION_RDP 

Flash read protection

BL_PROTECTION_PCROP 

Flash propietary code readout protection

Definition at line 94 of file bootloader.h.

Function Documentation

◆ Bootloader_CheckForApplication()

uint8_t Bootloader_CheckForApplication ( void  )

This function checks whether a valid application exists in flash. The check is performed by checking the very first DWORD (4 bytes) of the application firmware. In case of a valid application, this DWORD must represent the initialization location of stack pointer - which must be within the boundaries of RAM.

Returns
Bootloader error code eBootloaderErrorCodes
Return values
BL_OKif first DWORD represents a valid stack pointer location
BL_NO_APPfirst DWORD value is out of RAM boundaries

Definition at line 395 of file bootloader.c.

◆ Bootloader_CheckSize()

uint8_t Bootloader_CheckSize ( uint32_t  appsize)

This function checks whether the new application fits into flash.

Parameters
appsizesize of application
Returns
Bootloader error code eBootloaderErrorCodes
Return values
BL_OKif application fits into flash
BL_SIZE_ERRORif application does not fit into flash

Definition at line 338 of file bootloader.c.

◆ Bootloader_ConfigProtection()

uint8_t Bootloader_ConfigProtection ( uint32_t  protection)

This function configures the wirte protection of flash.

Parameters
protectionprotection type eFlashProtectionTypes
Returns
Bootloader error code eBootloaderErrorCodes
Return values
BL_OKupon success
BL_OBP_ERRORupon failure

Definition at line 263 of file bootloader.c.

◆ Bootloader_Erase()

uint8_t Bootloader_Erase ( void  )

This function erases the user application area in flash.

Returns
Bootloader error code eBootloaderErrorCodes
Return values
BL_OKupon success
BL_ERRupon failure

Definition at line 56 of file bootloader.c.

◆ Bootloader_FlashBegin()

uint8_t Bootloader_FlashBegin ( void  )

Begin flash programming: this function unlocks the flash and sets the data pointer to the start of application flash area.

See also
README for futher information
Returns
Bootloader error code eBootloaderErrorCodes
Return values
BL_OKis returned in every case

Definition at line 100 of file bootloader.c.

◆ Bootloader_FlashEnd()

uint8_t Bootloader_FlashEnd ( void  )

Finish flash programming: this function finalizes the flash programming by locking the flash.

See also
README for futher information
Returns
Bootloader error code eBootloaderErrorCodes
Return values
BL_OKis returned in every case

Definition at line 159 of file bootloader.c.

◆ Bootloader_FlashNext()

uint8_t Bootloader_FlashNext ( uint64_t  data)

Program 64bit data into flash: this function writes an 8byte (64bit) data chunk into the flash and increments the data pointer.

See also
README for futher information
Parameters
data64bit data chunk to be written into flash
Returns
Bootloader error code eBootloaderErrorCodes
Return values
BL_OKupon success
BL_WRITE_ERRORupon failure

Definition at line 120 of file bootloader.c.

◆ Bootloader_GetProtectionStatus()

uint8_t Bootloader_GetProtectionStatus ( void  )

This function returns the protection status of flash.

Returns
Flash protection status eFlashProtectionTypes

Definition at line 171 of file bootloader.c.

◆ Bootloader_GetVersion()

uint32_t Bootloader_GetVersion ( void  )

This function returns the version number of the bootloader library. Semantic versioning is used for numbering.

See also
Semantic versioning: https://semver.org
Returns
Bootloader version number combined into an uint32_t:
  • [31:24] Major version
  • [23:16] Minor version
  • [15:8] Patch version
  • [7:0] Release candidate version

Definition at line 471 of file bootloader.c.

◆ Bootloader_Init()

uint8_t Bootloader_Init ( void  )

This function initializes bootloader and flash.

Returns
Bootloader error code eBootloaderErrorCodes
Return values
BL_OKis returned in every case

Definition at line 37 of file bootloader.c.

◆ Bootloader_JumpToApplication()

void Bootloader_JumpToApplication ( void  )

This function performs the jump to the user application in flash.

The function carries out the following operations:

  • De-initialize the clock and peripheral configuration
  • Stop the systick
  • Set the vector table location (if SET_VECTOR_TABLE is enabled)
  • Sets the stack pointer location
  • Perform the jump

Definition at line 410 of file bootloader.c.

◆ Bootloader_JumpToSysMem()

void Bootloader_JumpToSysMem ( void  )

This function performs the jump to the MCU System Memory (ST Bootloader).

The function carries out the following operations:

  • De-initialize the clock and peripheral configuration
  • Stop the systick
  • Remap the system flash memory
  • Perform the jump

Definition at line 439 of file bootloader.c.

◆ Bootloader_VerifyChecksum()

uint8_t Bootloader_VerifyChecksum ( void  )

This function verifies the checksum of application located in flash. If USE_CHECKSUM configuration parameter is disabled then the function always returns an error code.

Returns
Bootloader error code eBootloaderErrorCodes
Return values
BL_OKif calculated checksum matches the application checksum
BL_CHKS_ERRORupon checksum mismatch or when USE_CHECKSUM is disabled

Definition at line 353 of file bootloader.c.