Project overview
STM32F1-DFU is a compact USB DFU bootloader for the STM32F103 (128 kB Flash, 20 kB SRAM). It lets you update the application firmware over USB with standard tools such as dfu-util or ST DfuSe, without an ST-LINK. The bootloader is derived from the open-source dfuse bootloader by David Guillen Fandos (GPLv3) and adds a firmware checksum and an update path from an external SPI SRAM.
It presents itself as an STM32 DFU device (0483:DF11, DFU class 1.1, WinUSB support, upload enabled) and uses the chip's unique ID as the USB serial number.
Memory layout
Defined in Core/Inc/flash_config.h and stm32f103.ld:
| Area | Address | Size |
|---|---|---|
| Bootloader | 0x08000000 |
8 kB |
| Application (payload) | 0x08002000 |
120 kB |
| Bootloader RAM | 0x20000000 |
20 kB - 4 B |
The last 4 bytes of RAM (the bottom of the stack, _stack) are kept free so the application can pass a command across a reset. The DFU interface string @Internal Flash /0x08000000/8*001Ka,120*001Kg tells the host that the first 8 pages are read-only and the 120 pages after them are writable.
Boot decision
main() in Core/Src/main.c decides whether to start the application or stay in DFU mode. DFU mode is entered when any of these is true:
- the application requested it by writing the magic value
0xCC00FFEEto the reserved RAM word (reboot_into_bootloader()inreboot.h) and resetting - the reset was caused only by the reset pin (
ENABLE_PINRST_DFU_BOOT) - the last reset came from the watchdog (
ENABLE_WATCHDOG) - the stored image size is larger than the payload area
- an optional GPIO pin is read high at boot (
ENABLE_GPIO_DFU_BOOT,PA0by default inboard.h)
Otherwise the bootloader checks that the first word of the application (the stack pointer) points into RAM, validates the checksum, clears the reboot flags, sets VTOR and the stack pointer, and jumps to the reset handler at APP_ADDRESS + 4. An empty or invalid application therefore always ends up in DFU mode.
In DFU mode the clock is set to 72 MHz from an 8 MHz HSE, the USB D+ line is pulled low to force re-enumeration, and main() polls USB in a loop. Two LEDs on PC14/PC15 blink alternately.
Firmware checksum
With ENABLE_CHECKSUM the application image carries its own size and checksum in the vector table area:
- offset
0x1C: 32-bit checksum - offset
0x20: image size in 32-bit words
The checksum is the XOR of all 32-bit words of the image, started from 0xB4DC0FEE; the result over the complete image is zero when it is valid. The script Releas-PRG/checksum.py pads the binary to a multiple of 4 bytes, patches both fields and rewrites the file. Run it on every application binary before flashing.
Optional protections
Selected at build time:
ENABLE_WRITEPROT- write-protects the bootloader pages using the option bytesENABLE_PROTECTIONS- enables read-out protection and disables JTAG/SWDENABLE_WATCHDOG- starts the independent watchdog before jumping to the application, so a hung application reboots into DFU modeENABLE_SAFEWRITE- safer flash writing
The active options are listed in the USB configuration string, for example Bootloader config: FW-CRC.
Update from SPI SRAM
With ENABLE_SPI_SRAM_IMAGE (version 02.00) the bootloader can also install firmware stored in an external SPI SRAM (23LCV type, pins PB12 CS, PB13 SCK, PB14 MISO, PB15 MOSI):
- at boot it reads a 12-byte header at SRAM offset 0 (magic
0x4D414253, image offset, image size) - if the header is valid it erases the needed 1 kB flash pages from
0x08002000 - it clears the header so the update is not repeated after a reset
- it copies the image in 256-byte chunks and programs it into flash
- it clears the reboot flags and resets into the new firmware
This allows a running application to download a new image into SRAM and let the bootloader install it.
Using the bootloader with dfu-util
Check that the device is detected:
dfu-util -l
Typical output:
Found DFU: [0483:df11] ver=0200, devnum=29, cfg=1, intf=0, path="1-1.3",
alt=0, name="@Internal Flash /0x08000000/8*001Ka,120*001Kg",
serial="54FF6C068687545516541567"
Prepare and flash the application:
py checksum.py SW_v0101_DFU.bin
dfu-util -d 0x0483:0xDF11 -D SW_v0101_DFU.bin -s 0x08002000:leave
The :leave suffix makes the device leave DFU mode and start the new application. The warning Invalid DFU suffix signature is harmless for raw .bin files.
Application requirements
The application must be linked to start at 0x08002000 with a flash length of 120 kB and must relocate the vector table:
// linker script
FLASH (rx) : ORIGIN = 0x08002000, LENGTH = 120K
// startup / system init
#define VECT_TAB_OFFSET 0x2000
To enter DFU mode from the application, write 0xCC00FFEE to the last RAM word (see reboot.h) and perform a system reset.
Build and first flash
- Open the project in STM32CubeIDE and build the Release configuration
- Flash the bootloader once through ST-LINK to
0x08000000(ready-made imagesDFU_v0100_PRG.binandDFU_v0200_SRAM_PRG.binare inReleas-PRG/) - From then on, update the application over USB
Notes
- The bootloader is GPLv3 code; keep the original copyright headers when you reuse it
- For production add signature verification and rollback protection; the XOR checksum only detects accidental corruption
- Additional tools and documents are in
documents/(dfu-utilfor Windows, DfuSe, the ST DFU application note)