48л. Updating over USB DFU: the STM32 system DFU, your own DFU bootloader on a USB device, the DfuSe file, tools, WinUSB drivers
The board has USB, and the user has no adapter. So the most convenient way to update the firmware is over the same cable the board works through. There is a standard USB class for this — DFU: the computer sees the device in update mode and writes the image into it in blocks.
This section covers: how the DFU class works — states, requests and block size;
the system DFU in the STM32 bootloader and the DfuSe extension; your own
DFU bootloader in the firmware; the .dfu file format; the dfu-util and
CubeProgrammer tools; drivers on Windows; a Python model of the write time;
pitfalls.
What is not repeated here: the structure of your own bootloader, the memory layout and jumping to the application — 48; image signing — 48а; the system bootloader and its entry via BOOT0 — 48д; USB descriptors, MS OS 2.0 and WinUSB byte by byte — 20д; entering DFU on the BlackPill with buttons — 2ж; what to do if the board does not respond at all — 48к.
Sources (topics only, no retelling of the text): the USB DFU 1.1 class specification, ST application notes on the DFU protocol in the system bootloader and on the DfuSe format, the ST application note on the system bootloader (which series support DFU and with which clocking), the dfu-util documentation. Transfer speed and write times in the model are assumptions, not measurements.
Terms used in the documentation
Term Meaning DFU mode, run-time mode the device's update mode and working mode DFU_DNLOAD, DFU_UPLOAD requests to write and read a block DFU_GETSTATUS, bwPollTimeout the status request and the time until the next poll dfuDNBUSY, dfuDNLOAD-IDLE, dfuMANIFEST states during writing and after it wTransferSize (DFU functional descriptor) the largest block size from the DFU descriptor DfuSe (DFU with ST extensions) DFU with ST extensions: addresses and erase Set Address Pointer, Erase command DfuSe commands: set the address, erase a sector alternate setting string (@Internal Flash) the alternate setting string with the memory map DFU suffix, DfuSe prefix the tail and header of a .dfufile
48л.1 The DFU class: states, requests, block size
DFU is a USB class for firmware updates. A device has two modes: the working mode, in which it does its job, and the update mode, in which it can only receive and send an image. All exchange goes through endpoint zero, with control requests: no separate endpoints are needed for DFU data.
Requests. There are few of them:
DFU_DNLOAD— send an image block to the device;DFU_UPLOAD— read a block from the device;DFU_GETSTATUS— get the state and how long to wait until the next poll (bwPollTimeout, in milliseconds);DFU_CLRSTATUS— clear an error;DFU_ABORT— abort the transfer;DFU_DETACH— ask a device in working mode to switch to update mode.
The block write cycle. The computer sends DFU_DNLOAD with a block. The device
enters the "busy writing" state and answers DFU_GETSTATUS with the
time it needs for writing. The computer waits this time and asks for the state
again; when the device has returned to "ready for the next
block", the next DFU_DNLOAD follows. An empty DFU_DNLOAD marks the end of
the image; the device checks and applies the image and restarts if
needed.
Block size. The device declares the largest block in the
DFU functional descriptor, in the wTransferSize field. The larger the block,
the smaller the polling overhead, but the larger the buffer in the device's
RAM: the whole block must fit in memory before writing. Typical
values are 1024 and 2048 bytes.
48л.2 The STM32 system DFU and DfuSe
The system bootloader of many STM32 series with USB (F2, F4, F7, L4, G4, H7 and others) supports DFU. The entry is the same as in 48д: BOOT0 high and a reset, or a jump to system memory from the firmware. The device appears on USB with VID 0x0483 and PID 0xDF11.
Clocking. On some series the system DFU requires an external crystal of a specific frequency: the bootloader measures it and derives 48 MHz for USB from it. A board without a crystal or with a crystal of a non-standard frequency may not show up in DFU at all. Series with an internal 48 MHz oscillator and trimming from USB (CRS) do without a crystal. Which frequencies the bootloader of your series accepts — see the ST application note on the system bootloader.
The DfuSe extension. Standard DFU writes the image sequentially from the start and does not
know addresses. ST added commands to it that are sent with the same
DFU_DNLOAD with block number 0:
- "set address" (first byte 0x21, then four address bytes) — the following blocks are written from this address;
- "erase" (first byte 0x41 and a sector address) — erase a sector or page; without an address — erase everything;
- "read unprotect" — remove read protection with an erase, as in 48к.
Data blocks come with numbers from 2: the block address is computed from the set address plus the block number minus 2, multiplied by the block size.
The alternate setting string. The device describes its memory map with text in the string descriptor of the alternate setting, for example:
@Internal Flash /0x08000000/04*016Kg,01*064Kg,07*128Kg
This means four sectors of 16 KB, one of 64 KB and seven of 128 KB; the letter
g means "readable, erasable, writable". The tool parses the string and
uses it to know which sectors to erase. Separate alternate
settings cover the option bytes, OTP and system memory.
48л.3 Your own DFU bootloader on a USB device
The system DFU is not available everywhere and needs BOOT0 or a jump from the firmware.
Your own DFU bootloader at the start of flash gives more: entry by a button, by a
flag from the application or by the DFU_DETACH command, checking the image and the
signature before starting it.
Layout. The bootloader takes the first flash sectors, the application starts at the address after them. In its alternate setting string the bootloader declares only the application area: this way the tool physically cannot erase the bootloader itself. The overall bootloader scheme and the jump to the application — 48.
Entering update mode. Three ways, usually together:
- A button held at power-on.
- A flag in RAM or in a backup domain register: the application writes "I want to update" and performs a software reset.
- The application image failed the check — the bootloader stays in itself.
Run-time mode and DFU_DETACH. The application can declare a
run-time DFU interface — then the tool itself sends
DFU_DETACH, the application sets the flag and restarts into the bootloader.
The user does not need to press anything.
Image check. After an empty DFU_DNLOAD the bootloader computes a CRC
or verifies the signature (48а) and only then marks the image as valid. If
power was lost in the middle of writing, the application will not pass the check, and
the bootloader will stay in update mode — the board can be flashed again.
Ready-made implementations. The USB library in the Cube package contains a DFU class with an example; based on it the bootloader takes about 16 KB. On F4 this is exactly the first sector, and the application starts from the second.
48л.4 The DfuSe file
An image for DfuSe is stored in a .dfu file. Unlike .bin, it knows
addresses and can contain several pieces for different areas.
The DfuSe header — 11 bytes: the DfuSe signature, the version, the total file
size and the number of targets.
A target — a 274-byte header: the Target signature, the alternate
setting number, the name and size, then the elements. Each element is an address
(4 bytes), a size (4 bytes) and data.
The DFU tail — 16 bytes at the end of the file: the device version, PID, VID,
the DFU version (0x011A for DfuSe), the UFD signature, the tail length and the CRC32 of the whole
file without the last four bytes. The tool checks the VID and PID from the tail
and refuses to write the image to someone else's device.
dfu-suffix -v 0x0483 -p 0xDF11 -a firmware.dfu
For a plain .bin a DFU tail and an address on the tool's command line
are enough; a .dfu with several elements is convenient when, together with the
firmware, you need to write, for example, a parameter area. Making a .dfu
from a .hex is possible with CubeProgrammer, a tool from the dfu-util package and
a simple Python script.
48л.5 Tools
dfu-util. An open tool for Linux, macOS and Windows. It understands
DfuSe with the -s option and an address:
dfu-util -l
dfu-util -a 0 -s 0x08000000:leave -D firmware.bin
dfu-util -a 0 -s 0x08000000:65536 -U backup.bin
The first command lists the devices and their alternate settings, the second writes the image from address 0x08000000 and starts the application after writing, the third reads 65536 bytes into a file.
STM32CubeProgrammer. It supports DFU through the usb1 port:
STM32_Programmer_CLI -c port=usb1 -d firmware.bin 0x08000000 -v
Options differ between tool versions — check the --help of your
version; the commands require checking.
Your own tool. The protocol is simple, and in Python with the pyusb library a DfuSe write fits in a hundred lines: set the address, erase the sectors, send blocks, poll the state. This is convenient when the update has to be built into your own device configuration program.
48л.6 Drivers on Windows
Linux and macOS work with a DFU device without a driver: dfu-util accesses it through libusb. On Windows the device needs the WinUSB driver, otherwise libusb will not open it.
The STM32 system DFU. CubeProgrammer installs its own driver for VID 0x0483 and PID 0xDF11. dfu-util does not work with this driver — it needs WinUSB. Two drivers cannot be installed for one device at the same time: choose one tool per computer or switch the driver manually in Device Manager.
Your own bootloader. The best way is to declare in the bootloader the MS OS 2.0 descriptors with the WinUSB compatible ID. Then Windows 8.1 and newer install WinUSB by themselves on first connection, without an INF file and without manual actions by the user. How to do it byte by byte — 20д.
If a driver is still needed. A signed INF file for WinUSB that is installed together with the update program. A self-signed driver is not accepted by Windows without test signing mode.
48л.7 Model: write time over USB DFU
The model model48k.py computes how long writing an image takes with different
block sizes. Assumptions: full-speed USB transfers data
with control requests at 400 KB/s; programming one 32-bit
flash word takes 16 µs; the device reports the wait time
rounded up to a millisecond; each block has two
DFU_GETSTATUS polls of one USB frame each. Before writing, the sectors
under the image are erased, with the layout and times as in 48к.
| Block | Transfer | Write | Polls | Per block | Write speed |
|---|---|---|---|---|---|
| 1024 bytes | 2.56 ms | 5 ms | 2 ms | 9.56 ms | 105 KB/s |
| 2048 bytes | 5.12 ms | 9 ms | 2 ms | 16.12 ms | 124 KB/s |
The transfer itself is the smaller part of the block time: the rest goes to writing to flash and polling. A 2048-byte block is faster than a 1024-byte block not twofold: 124 versus 105 KB/s, because the write time per byte does not depend on the block size.
| Image | Block | Blocks | Write | Erase | Total |
|---|---|---|---|---|---|
| 64 KB | 1024 bytes | 64 | 0.6 s | 1.00 s | 1.6 s |
| 64 KB | 2048 bytes | 32 | 0.5 s | 1.00 s | 1.5 s |
| 256 KB | 1024 bytes | 256 | 2.4 s | 2.55 s | 5.0 s |
| 256 KB | 2048 bytes | 128 | 2.1 s | 2.55 s | 4.6 s |
| 512 KB | 1024 bytes | 512 | 4.9 s | 4.55 s | 9.4 s |
| 512 KB | 2048 bytes | 256 | 4.1 s | 4.55 s | 8.7 s |
For a 512 KB image writing and erasing take roughly equal time: 8.7 s with a 2048-byte block. Erasing only the sectors under the image, not the whole memory, is the main saving on chips with large sectors.
48л.8 Pitfalls
The device does not show up in DFU. No crystal of the required frequency for the system DFU, or BOOT0 is not high at the moment of reset. Check the crystal frequency against the bootloader application note.
Windows sees the device, dfu-util does not. The CubeProgrammer driver is installed, not WinUSB (48л.6).
Wrote a .bin from address 0. Without -s dfu-util has no address; DfuSe
will refuse or write to the wrong place. Always give the address.
After writing the application does not start. :leave was not given or the
bootloader waits for a reset. Or the image was built for the bootloader address, not for the
address after it: the vector table is in the wrong place.
The tool erased the bootloader. The alternate setting string declares the whole flash. Declare only the application area.
The update breaks off on large images. The tool's timeout is shorter than the
erase time of a large sector — erase sectors one at a time and
answer polls with an honest bwPollTimeout.
A 2048-byte block, but little RAM. The block buffer must fit entirely in the bootloader's RAM. On small chips 1024 bytes are used.
48л.9 Summary
- DFU is a USB class for updates: everything through endpoint zero,
the requests
DFU_DNLOAD,DFU_GETSTATUSand four more. - The STM32 system DFU — VID 0x0483, PID 0xDF11, entry via BOOT0; on some series a crystal of a specific frequency is required.
- DfuSe adds an address and erase; the memory map is in the alternate setting string.
- Your own bootloader declares only the application area, enters by a
button, a flag or
DFU_DETACHand checks the image before starting it. - A
.dfufile is the DfuSe header, targets with elements and a tail with VID, PID and CRC32. - On Windows WinUSB is needed; the best way is MS OS 2.0 descriptors.
- According to the model a 2048-byte block gives 124 KB/s versus 105 KB/s for a 1024-byte block; a 512 KB image takes 8.7 s, of which erasing is 4.55 s.
Next: section 39 "How to read ST documentation".