dfu-to-bin-tool Linux

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
cyrano1960
Posts: 39
Joined: Fri Mar 29, 2019 7:08 pm

dfu-to-bin-tool Linux

Post by cyrano1960 » Tue May 04, 2021 10:34 am

Hi there,

I have been using the dfuse-demo (STSW-STM32080) for converting the dfu files into bin- oder hex-files but it is replaced by STM32CubProgrammer and this tool doesn´t offer dfu-to-bin-conversion, also the STSW-STM32080 only works on Windows. So is there an alternative for the old dfu file manager and what should I use on Linux. BTW: why is the firmware offered in this format, wouldn´t it be more convenient to offer it in bin- oder hex-format?

Thanks and best regards!
cyr#

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: dfu-to-bin-tool Linux

Post by dhylands » Tue May 04, 2021 10:07 pm

I normally use pydfu.py (included with Micropython) when using linux. You could also use dfu-util.

If you're building the firmware yourself, using:

Code: Select all

make BOARD=PYBV11 deploy
will use pydfu.py by default. Here's the output (done on a Mac but linux does the same thing):

Code: Select all

$ make BOARD=PYBV11 deploy V=1
python3 ../../py/makeversionhdr.py build-PYBV11/genhdr/mpversion.h
GEN build-PYBV11/genhdr/qstrdefs.collected.h
python3 ../../py/makeqstrdefs.py cat qstr _ build-PYBV11/genhdr/qstr build-PYBV11/genhdr/qstrdefs.collected.h
QSTR not updated
python3 ../../tools/makemanifest.py -o build-PYBV11/frozen_content.c -v "MPY_DIR=../.." -v "MPY_LIB_DIR=../../../micropython-lib" -v "PORT_DIR=/Users/davehylands/micropython/micropython/ports/stm32" -v "BOARD_DIR=boards/PYBV11" -b "build-PYBV11" -f"-march=armv7m" boards/manifest.py
Writing build-PYBV11/firmware.dfu to the board
python3 ../../tools/pydfu.py --vid 0x0483 --pid 0xDF11 -u build-PYBV11/firmware.dfu
File: build-PYBV11/firmware.dfu
    b'DfuSe' v1, image size: 361213, targets: 1
    b'Target' 0, alt setting: 0, name: "ST...", size: 360928, elements: 2
      0, address: 0x08000000, size: 14592
      1, address: 0x08020000, size: 346320
    usb: 0483:df11, device: 0x0000, dfu: 0x011a, b'UFD', 16, 0xc350b1a7
Writing memory...
0x08000000   14592 [=========================] 100% 
0x08020000  346320 [=========================] 100% 
Exiting DFU...
Finished
and here's the same output when using dfu-util:

Code: Select all

$ make BOARD=PYBV11 deploy USE_PYDFU=0 V=1
python3 ../../py/makeversionhdr.py build-PYBV11/genhdr/mpversion.h
GEN build-PYBV11/genhdr/qstrdefs.collected.h
python3 ../../py/makeqstrdefs.py cat qstr _ build-PYBV11/genhdr/qstr build-PYBV11/genhdr/qstrdefs.collected.h
QSTR not updated
python3 ../../tools/makemanifest.py -o build-PYBV11/frozen_content.c -v "MPY_DIR=../.." -v "MPY_LIB_DIR=../../../micropython-lib" -v "PORT_DIR=/Users/davehylands/micropython/micropython/ports/stm32" -v "BOARD_DIR=boards/PYBV11" -b "build-PYBV11" -f"-march=armv7m" boards/manifest.py
Writing build-PYBV11/firmware.dfu to the board
dfu-util -a 0 -d 0x0483:0xDF11 -D build-PYBV11/firmware.dfu
dfu-util 0.10

Copyright 2005-2009 Weston Schmidt, Harald Welte and OpenMoko Inc.
Copyright 2010-2020 Tormod Volden and Stefan Schmidt
This program is Free Software and has ABSOLUTELY NO WARRANTY
Please report bugs to http://sourceforge.net/p/dfu-util/tickets/

Opening DFU capable USB device...
ID 0483:df11
Run-time device DFU version 011a
Claiming USB DFU Interface...
Setting Alternate Setting #0 ...
Determining device status: state = dfuERROR, status = 10
dfuERROR, clearing status
Determining device status: state = dfuIDLE, status = 0
dfuIDLE, continuing
DFU mode device DFU version 011a
Device returned transfer size 2048
DfuSe interface name: "Internal Flash  "
file contains 1 DFU images
Please note that the next version of dfu-util will automatically
set alternate interfaces based on the DfuSe file images!
parsing DFU image 1
Target name: ST...
image for alternate setting 0, (2 elements, total size = 360928)
parsing element 1, address = 0x08000000, size = 14592
Erase   	[=========================] 100%        14592 bytes
Erase    done.
Download	[=========================] 100%        14592 bytes
Download done.
parsing element 2, address = 0x08020000, size = 346320
Erase   	[=========================] 100%       346320 bytes
Erase    done.
Download	[=========================] 100%       346320 bytes
Download done.
Done parsing DfuSe file
Note that under linux you want your user to be a member of the dialup group (may vary depending on the distribution being used).

cyrano1960
Posts: 39
Joined: Fri Mar 29, 2019 7:08 pm

Re: dfu-to-bin-tool Linux

Post by cyrano1960 » Thu May 06, 2021 12:03 pm

Hi Dave,

thanks a lot. When I make my own build it works fine, because I got all binary formats I need (.bin, .hex and .dfu) But I am looking for a simple way to upload the firmware for my students. They use the binary download files on the Micropython page. So they only got the .dfu version and they are working with STM32L476RG NUCLEO64 boards. So the easiest way (IMHO) is to convert the .dfu file into a .bin format and copy the file by drag and drop to the board.

But I can´t find a way to convert it either with pydfu.py nor with dfu-util like I can do with Dfu file manager on Windows systems. Do you have any idea?

Thanks and best regards,
Werner

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: dfu-to-bin-tool Linux

Post by dhylands » Thu May 06, 2021 4:29 pm

I'm not sure how you would "drag and drop" a bin file. The problem with bin files is that they don't contain any information about where they're supposed to get programmed to.

You can use dfu.py with the -d option to create 2 .bin files. For example:

Code: Select all

$ cd build-PYBV11
$ ../../../tools/dfu.py -d firmware.dfu 
File: "firmware.dfu"
DfuSe v1, image size: 361213, targets: 1
Target 0, alt setting: 0, name: "ST...", size: 360928, elements: 2
  0, address: 0x08000000, size: 14592
    DUMPED IMAGE TO "firmware.dfu.target0.image0.bin"
  1, address: 0x08020000, size: 346320
    DUMPED IMAGE TO "firmware.dfu.target0.image1.bin"
usb: 0483:df11, device: 0x0000, dfu: 0x011a, UFD, 16, 0xc350b1a7
The filesystem lives between the place that the 2 bin files get flashed to.

cyrano1960
Posts: 39
Joined: Fri Mar 29, 2019 7:08 pm

Re: dfu-to-bin-tool Linux

Post by cyrano1960 » Sun Oct 24, 2021 1:19 pm

Hi Dave, long time ago (normally a good sign :mrgreen: ) but now I have tried the way you proposed. I have typed in the command as you do but I got the following error message:
server:~/micropython/tools$ python3 dfu.py -d NUCLEO_L476RG-20200902-v1.13.dfu
File: "/mnt/c/Users/werner/OneDrive/microPython/MicroPython_Firmware/STM32/NUCLEO_L476RG-20200902-v1.13.dfu"
b'DfuSe' v1, image size: 336901, targets: 1
Traceback (most recent call last):
File "dfu.py", line 160, in <module>
parse(infile, dump_images=options.dump_images)
File "dfu.py", line 42, in parse
tprefix["name"] = cstring(tprefix["name"])
File "dfu.py", line 23, in cstring
return string.split("\0", 1)[0]
TypeError: a bytes-like object is required, not 'str'
Do you have any idea what went wrong? Thanks in advance!
Werner

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: dfu-to-bin-tool Linux

Post by dhylands » Mon Oct 25, 2021 5:46 pm

It looks like dfu.py needs a slight update:
https://github.com/micropython/micropyt ... 313dbd60e7

I created a PR to fix it:
https://github.com/micropython/micropython/pull/7931

Until then, you can edit line 23 of dfu.py and change "\0" to b"\0"

cyrano1960
Posts: 39
Joined: Fri Mar 29, 2019 7:08 pm

Re: dfu-to-bin-tool Linux

Post by cyrano1960 » Tue Nov 09, 2021 5:06 pm

Dear Dave,

sorry for the long delay, but I checked it today and it works fine. Thank you very much for your support.

Best regards,
Werner

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: dfu-to-bin-tool Linux

Post by dhylands » Tue Nov 09, 2021 5:09 pm

My fix was merged into master about a week ago, so if you update to the latest, you'll pick up the change automatically.

Post Reply