STM32 Bootloader
Customizable Bootloader for STM32 microcontrollers

Macros

#define SYSTEM_MEMORY_ADDRESS   0x1FFF0000U
 
#define FLASH_SIZE_WRAPPER   (uint32_t)(FLASH_SIZE)
 
#define FLASH_BANK_SIZE_WRAPPER   (uint32_t)(FLASH_BANK_SIZE)
 
#define FLASH_PAGE_NB_PER_BANK    (uint32_t)(FLASH_BANK_SIZE_WRAPPER / FLASH_PAGE_SIZE)
 

Functions

void FlashClearErrorFlags (void)
 Clear all flash error flags.
 
bool FlashCheckIfDataFits (const uint32_t address, const uint32_t length)
 Check if an array of data fits into the flash. More...
 
bool FlashCheckIfWriteProtectionEnabled (const uint32_t address, const uint32_t length)
 Check if flash write protection is enabled for the specified region. More...
 
bool FlashDisableWriteProtection (void)
 Disable flash write protection for the application area. More...
 
bool FlashEnableWriteProtection (const uint32_t address, const uint32_t length)
 Enable flash write protection for the specified region. More...
 
bool FlashCompare (const uint8_t *const data, const uint32_t address, const uint32_t length)
 Compare buffer content with microcontroller flash content. More...
 
uint32_t FlashRead (uint8_t *const data, const uint32_t address, const uint32_t length)
 Read data from the microcontroller flash. More...
 
uint32_t FlashWrite (const uint8_t *const data, const uint32_t address, const uint32_t length)
 Write data into the microcontroller flash. More...
 
void FlashRemapMemoryToSystemFlash (void)
 Remap the microcontroller flash memory to the built-in system flash memory. More...
 
static bool FlashPerformErase (const uint32_t address, const uint32_t length)
 Private helper function to perform the actual erase of the required flash pages. More...
 
static uint32_t FlashPerformWrite (const uint8_t *const data, const uint32_t address, const uint32_t length)
 Private helper function to perform the actual writing of the provided data into the flash. More...
 
static uint32_t FlashGetBank (const uint32_t address)
 Get the bank where the provided address is located. More...
 
static uint32_t FlashGetPage (const uint32_t address)
 Get the page number where the provided address is located. More...
 
static bool FlashCheckIfPageRangesOverlap (const uint32_t range1Start, const uint32_t range1End, const uint32_t range2Start, const uint32_t range2End)
 Check if two flash page ranges overlap. More...
 

Detailed Description

Macro Definition Documentation

◆ FLASH_BANK_SIZE_WRAPPER

#define FLASH_BANK_SIZE_WRAPPER   (uint32_t)(FLASH_BANK_SIZE)

Wrapper define for the flash bank size.

Definition at line 51 of file flash.c.

◆ FLASH_PAGE_NB_PER_BANK

#define FLASH_PAGE_NB_PER_BANK    (uint32_t)(FLASH_BANK_SIZE_WRAPPER / FLASH_PAGE_SIZE)

Number of flash pages per bank.

Definition at line 55 of file flash.c.

◆ FLASH_SIZE_WRAPPER

#define FLASH_SIZE_WRAPPER   (uint32_t)(FLASH_SIZE)

Wrapper define for the flash size.

Definition at line 47 of file flash.c.

◆ SYSTEM_MEMORY_ADDRESS

#define SYSTEM_MEMORY_ADDRESS   0x1FFF0000U

Address of System Memory (ST Bootloader)

Definition at line 38 of file flash.h.

Function Documentation

◆ FlashCheckIfDataFits()

bool FlashCheckIfDataFits ( const uint32_t  address,
const uint32_t  length 
)

Check if an array of data fits into the flash.

Parameters
addressThe physical starting address of the data in the flash.
lengthThe length of the data.
Returns
True if the provided data length fits into the flash starting from the provided physical address; otherwise false.

Definition at line 141 of file flash.c.

◆ FlashCheckIfPageRangesOverlap()

static bool FlashCheckIfPageRangesOverlap ( const uint32_t  range1Start,
const uint32_t  range1End,
const uint32_t  range2Start,
const uint32_t  range2End 
)
static

Check if two flash page ranges overlap.

Parameters
range1StartStart page of the first range.
range1EndEnd page of the first range.
range2StartStart page of the second range.
range2EndEnd page of the second range.
Returns
True if the two page ranges overlap; otherwise false.

Definition at line 556 of file flash.c.

◆ FlashCheckIfWriteProtectionEnabled()

bool FlashCheckIfWriteProtectionEnabled ( const uint32_t  address,
const uint32_t  length 
)

Check if flash write protection is enabled for the specified region.

Parameters
addressStarting address of the region to check.
lengthLength of the region in bytes.
Returns
True if write protection is enabled for any part of the specified region; otherwise false.

Definition at line 147 of file flash.c.

◆ FlashCompare()

bool FlashCompare ( const uint8_t *const  data,
const uint32_t  address,
const uint32_t  length 
)

Compare buffer content with microcontroller flash content.

This function is useful for instance during flash write: if the content of the buffer matches with the flash content, the flash does not need to be written.

Note
The function does not check for overflow of the provided buffer.
Parameters
dataBuffer which content is to be compared with the flash.
addressPhysical address of the flash for the comparison.
lengthLength of the compared content.
Returns
True if the contents match, otherwise false. If the provided length is zero, the returned value is false.

Definition at line 367 of file flash.c.

◆ FlashDisableWriteProtection()

bool FlashDisableWriteProtection ( void  )

Disable flash write protection for the application area.

Note
This function triggers a system reset after modifying the option bytes.
Returns
True if write protection was successfully disabled; otherwise false.

Definition at line 224 of file flash.c.

◆ FlashEnableWriteProtection()

bool FlashEnableWriteProtection ( const uint32_t  address,
const uint32_t  length 
)

Enable flash write protection for the specified region.

Note
This function triggers a system reset after modifying the option bytes.
Parameters
addressStarting address of the region to protect.
lengthLength of the region in bytes.
Returns
True if write protection was successfully enabled; otherwise false.

Definition at line 291 of file flash.c.

◆ FlashGetBank()

static uint32_t FlashGetBank ( const uint32_t  address)
static

Get the bank where the provided address is located.

Parameters
addressPhysical address to be checked.
Returns
FLASH_BANK_1 if the address is located in bank 1. FLASH_BANK_2 if the address is located in bank 2.

Definition at line 542 of file flash.c.

◆ FlashGetPage()

static uint32_t FlashGetPage ( const uint32_t  address)
static

Get the page number where the provided address is located.

Note
The returned page number is relative to the actual bank where the page is located. For instance, calling the function with the starting address of the second bank will return the page number 0.
Parameters
addressPhysical address to be checked.
Returns
The page number ranging from 0 to FLASH_PAGE_NB_PER_BANK where the provided address is located.

Definition at line 548 of file flash.c.

◆ FlashPerformErase()

static bool FlashPerformErase ( const uint32_t  address,
const uint32_t  length 
)
static

Private helper function to perform the actual erase of the required flash pages.

Warning
This function does NOT check whether the provided data is within the range of the physical flash area!
Parameters
addressPhysical flash address where the data is erased.
lengthThe length of the data in bytes.
Returns
True if all the required pages are successfully erased; otherwise false.

Definition at line 452 of file flash.c.

◆ FlashPerformWrite()

static uint32_t FlashPerformWrite ( const uint8_t *const  data,
const uint32_t  address,
const uint32_t  length 
)
static

Private helper function to perform the actual writing of the provided data into the flash.

Warning
This function does NOT check whether the provided data is within the range of the physical flash area!
Note
This function does not automatically erase the required flash pages.
Parameters
dataBuffer containing the data to be written.
addressPhysical flash address where the data is written.
lengthThe length of the data to be written in bytes.
Returns
The number of bytes written to the flash.

Definition at line 508 of file flash.c.

◆ FlashRead()

uint32_t FlashRead ( uint8_t *const  data,
const uint32_t  address,
const uint32_t  length 
)

Read data from the microcontroller flash.

Parameters
dataBuffer where the data from the flash is copied into.
addressPhysical address where the data is to be read.
lengthData size (in bytes) to be read from the flash.
Returns
The number of bytes successfully read.

Definition at line 388 of file flash.c.

◆ FlashRemapMemoryToSystemFlash()

void FlashRemapMemoryToSystemFlash ( void  )

Remap the microcontroller flash memory to the built-in system flash memory.

Note
This is required before jumping to the ST built-in system memory bootloader.

Definition at line 445 of file flash.c.

◆ FlashWrite()

uint32_t FlashWrite ( const uint8_t *const  data,
const uint32_t  address,
const uint32_t  length 
)

Write data into the microcontroller flash.

The function automatically erases the required flash pages before writing to the flash. All existing data located in those pages will be erased.

Parameters
dataBuffer containing the data to be written.
addressPhysical flash address where the data is written.
lengthThe length of the data to be written in bytes.
Returns
The number of bytes written to the flash.

Definition at line 412 of file flash.c.