Programming TrueSTUDIO .hex files over DFU on linux

So you have an Intel HEX (.hex) file from your microcontroller firmware (from Atollic TrueSTUDIO for a STM32 micro, for example), but you need a .dfu file in order to program your microcontroller using dfu-util? And can’t (or don’t want to) use the Windows-only DfuSe software? I hope this guide helps you out!

The (relatively straightfoward) manual way
1. Find the base address for the DFU transfer. You can simply retrieve this from the .hex file using objdump:

$ objdump -h yourproject.hex

yourproject.hex:     file format ihex

Sections:
Idx Name          Size      VMA       LMA       File off  Algn
  0 .sec1         00004994  08000000  08000000  00000011  2**0
                  CONTENTS, ALLOC, LOAD

The address is under both VMA and LMA. In my case, it’s 0x08000000. FYI, this corresponds to the base address of FLASH as described in the MCU’s datasheet.

2. Extract the binary content of the HEX file:

$ objcopy -Iihex -Obinary yourproject.hex yourproject.bin

The .bin file now contains the raw firmware, exactly as it needs to be written to the MCU. This means, though, that the .bin file doesn’t contain the offset — which is why we had to pull this information out in step 1.

3. Program the MCU! I’m going to assume the MCU is prepped and ready to accept a DFU transfer (typically, this involves pulling the BOOT0 pin high and resetting the MCU). With that out of the way;

$ dfu-util -a 0 --dfuse-address=0x08000000 -D yourproject.bin

This will give errors about this approach being deprecated, but don’t worry, it will work for now. You might need to prepend “sudo” if you have permissions or “DFU device not found” errors.

The non-deprecated manual way

I’m 99% sure the following will work, I just haven’t got around to trying it out yet. Please let me know if this does or doesn’t work in the comments below, it would be fantastic if you could include example command lines.

1-2. Same as steps 1-2 in the previous section.
3. Use the dfu-prefix and dfu-suffix tools (including with dfu-util) to append dfu prefixes and suffixes to your .bin file. You can then rename it to a .dfu extension if you like; as it’s now a legit .dfu file IIUC.
4. Invoke dfu-util using the .dfu file.

This is nicer because a) you don’t get the deprecation warnings, b) you can distribute the .dfu file along with a significantly simpler command line.

The automatic way

Well, once we figure out the finer details of the previous section, it’s straightforward to build this into a tool, script, or even a Makefile.

Share

Leave a Reply

Your email address will not be published. Required fields are marked *