Page 1 of 2

Python DFU util

Posted: Fri Oct 03, 2014 2:12 am
by iabdalkader
Hey, I've been looking for a Python DFU implementation for STM32, but I couldn't find one, so I implemented some of the DFU protocol here:

https://github.com/iabdalkader/openmv/b ... r/pydfu.py

Unlike dfu-util this actually exits DFU mode and jumps to the main application after finishing the upload (dfu-util 0.7 just gets stuck) even if the jumper is still connected, so it's a one command upload/reset thing.

There's a few extra functions in the script to do single pages at a time (ex. erase pages vs. mass erase, write page vs. write whole binary) this makes it easier to use it from GUIs (for progress bar updates etc...) you can safely ignore those...

There's one drawback, however, It doesn't actually parse dfu files :D so:

Code: Select all

arm-none-eabi-objcopy -Obinary firmware.elf firmware.bin
./pydfu.py firmware.bin

Re: Python DFU util

Posted: Fri Oct 03, 2014 4:53 am
by dhylands
I use dfu-util -s :leave

That works with 0.8

Re: Python DFU util

Posted: Fri Oct 03, 2014 5:37 am
by dhylands
iabdalkader wrote:There's one drawback, however, It doesn't actually parse dfu files :D so:

Code: Select all

arm-none-eabi-objcopy -Obinary firmware.elf firmware.bin
./pydfu.py firmware.bin
And that will have the side effect of wiping out the internal flash disk.

On the pyboard, the first 16K is for the IRQ vectors and some code. Then 112K of internal disk storage followed by the rest of the firmware.

Re: Python DFU util

Posted: Fri Oct 03, 2014 2:19 pm
by iabdalkader
dhylands wrote:
iabdalkader wrote: And that will have the side effect of wiping out the internal flash disk.

On the pyboard, the first 16K is for the IRQ vectors and some code. Then 112K of internal disk storage followed by the rest of the firmware.
Yeah I know, actually I do a mass erase first anyway, that can be improved later. The point is it's one less dependency and I can keep track of what's happening from the GUI.

Re: Python DFU util

Posted: Fri Oct 03, 2014 2:48 pm
by dhylands
I see that there is pyelftoolls available. Perhaps reading the elf directly would be easier?

I'd be willing to take a stab to integrate support for that (or for that matter supporing .dfu files).

Re: Python DFU util

Posted: Fri Oct 03, 2014 3:12 pm
by iabdalkader
dhylands wrote:I see that there is pyelftoolls available. Perhaps reading the elf directly would be easier?

I'd be willing to take a stab to integrate support for that (or for that matter supporing .dfu files).
I think there's a function to parse DFU files in tools/dfu.py script ? if you would like to try that would be awesome, thanks :)

Re: Python DFU util

Posted: Mon Oct 13, 2014 3:19 pm
by dhylands
pydfu.py (found here: https://github.com/iabdalkader/openmv/b ... r/pydfu.py can now program the pyboard using .dfu files.

To find available devices in FU mode:
./pydfu.py -l

To upload some firmware:
./pydfu.py -u build-PYBV0/firmware.dfu

It automatically exits DFU mode when finished. It does require pyusb to be installed. I've only tested under linux, it would be interesting to see if this works under Winows nd on the Mac.

Re: Python DFU util

Posted: Tue Oct 21, 2014 5:11 pm
by pfalcon
Great work guys! That's how I'd like to have all those tools working with obscure (and not so obscure) protocols - in Python, so easily hackable.

Btw going to move this thread to Devel subforum in a week or too, as I guess mostly developers will appreciate this code.

Re: Python DFU util

Posted: Wed Oct 22, 2014 12:30 am
by Damien
Awesome.

Can this script live somewhere in the micropython repo (eg tools/ subdir), so that "make deploy" just works?

Re: Python DFU util

Posted: Wed Oct 22, 2014 2:20 am
by dhylands
Sure - we could just copy it from openmvcam

openmvcam uses micropython as a sub-module, so openmv could use the one from micropython (if they want).

I'll do a PR to add it to mp