Python DFU util

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
iabdalkader
Posts: 14
Joined: Fri Mar 28, 2014 11:35 pm

Python DFU util

Post by iabdalkader » Fri Oct 03, 2014 2:12 am

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

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

Re: Python DFU util

Post by dhylands » Fri Oct 03, 2014 4:53 am

I use dfu-util -s :leave

That works with 0.8

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

Re: Python DFU util

Post by dhylands » Fri Oct 03, 2014 5:37 am

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.

iabdalkader
Posts: 14
Joined: Fri Mar 28, 2014 11:35 pm

Re: Python DFU util

Post by iabdalkader » Fri Oct 03, 2014 2:19 pm

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.

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

Re: Python DFU util

Post by dhylands » Fri Oct 03, 2014 2:48 pm

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).

iabdalkader
Posts: 14
Joined: Fri Mar 28, 2014 11:35 pm

Re: Python DFU util

Post by iabdalkader » Fri Oct 03, 2014 3:12 pm

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 :)

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

Re: Python DFU util

Post by dhylands » Mon Oct 13, 2014 3:19 pm

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.

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: Python DFU util

Post by pfalcon » Tue Oct 21, 2014 5:11 pm

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.
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

Damien
Site Admin
Posts: 647
Joined: Mon Dec 09, 2013 5:02 pm

Re: Python DFU util

Post by Damien » Wed Oct 22, 2014 12:30 am

Awesome.

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

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

Re: Python DFU util

Post by dhylands » Wed Oct 22, 2014 2:20 am

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

Post Reply