A minimal bootloader for STM32F4 microcontrollers that reads Intel HEX firmware files from an SD card and programs them into sector-based flash memory. This bootloader supports FAT32 file systems and includes robust handling of extended addressing, checksum validation, and automatic flash erasure.
- Parses and flashes Intel HEX files from
/STM32-BOOT/app.hex - Supports Intel HEX record types: DATA, EOF, ELAR (0x04), ESAR (0x02), and tolerates SLAR (0x05)
- Approximates the programmed image span to erase the required flash sectors
- Validates checksum for each HEX record before programming
- FAT32 SD card access through a
sdFat32file abstraction layer - Dynamically saves the application's start address to
/STM32-BOOT/app.addr, eliminating the need for hardcoded addresses and enabling flexible placement of the application in flash memory. - Toggles LED on
GPIOC Pin 13during flashing and error states - Deletes
app.hexafter a successful update to prevent reprogramming
- Format your SD card with FAT32.
- Place the
app.hexfile into a folder namedSTM32-BOOTat the root of the SD card. - Insert the SD card into your STM32F4-based board.
- On power-up or reset, the bootloader will:
- Initialize the SD card and FAT32 file system
- Check for a valid
app.hex - Estimate the programmed span and erase the overlapping flash sectors
- Program the firmware to internal flash memory
- Save the start address to
app.addr - Delete
app.hex - Light the LED to signal completion or turn off on error
The application image must start above the bootloader region. In the current layout,
that means the HEX image must not begin at or below 0x08003FFF.
The bootloader uses a fixed STM32F4 sector map covering:
0x08000000 - 0x08003FFF (Sector 0)
0x08004000 - 0x08007FFF (Sector 1)
...
0x080E0000 - 0x080FFFFF (Sector 11)
Sector 0 (0x08000000 - 0x08003FFF) is reserved for the bootloader itself.
The application image should be linked to start in sector 1 or above.
The erase range is estimated from the Intel HEX file size, then aligned to the flash sectors that overlap the target address range.
- Blinking: Flash programming in progress
- ON: Flashing completed successfully
- OFF: Error during flashing (e.g., checksum or HAL failure)
This bootloader depends on the sdFat32 library for FAT32 file access. Ensure it's included and integrated properly.
To clone this project with submodules:
git clone --recurse-submodules https://github.com/pdlsurya/stm32f4-sd-bootloader.git| Function | Description |
|---|---|
bootloaderInit() |
Initializes the SD and FAT32 system |
firmwareUpdateAvailable() |
Checks for presence of app.hex |
updateFirmware() |
Erases flash, writes firmware, saves app address, deletes hex |
getAppStartAddress() |
Loads the application start address from app.addr |
APP_START(addr) |
Jumps to application at specified address |
MIT License – feel free to use, modify, and distribute this project.