From 4f25a8b6a4f7761078678876e259674d736768a5 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 27 Nov 2018 16:19:27 +1100 Subject: [PATCH] tools/pydfu.py: Improve DFU reset, and auto-detect USB transfer size. A DFU device must be in the idle state before it can be programmed, and this requires either clearing the status or aborting, depending on its current state. Code is added to do this. And the USB transfer size is now automatically detected so devices with a size less than 2048 bytes work correctly. --- tools/pydfu.py | 47 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/tools/pydfu.py b/tools/pydfu.py index e7f4ab1780..394ecca5e1 100755 --- a/tools/pydfu.py +++ b/tools/pydfu.py @@ -14,6 +14,7 @@ See document UM0391 for a dscription of the DFuse file. from __future__ import print_function import argparse +import collections import re import struct import sys @@ -56,6 +57,9 @@ _DFU_DESCRIPTOR_TYPE = 0x21 # USB device handle __dev = None +# Configuration descriptor of the device +__cfg_descr = None + __verbose = None # USB DFU interface @@ -76,9 +80,18 @@ else: return usb.util.get_string(dev, index) +def find_dfu_cfg_descr(descr): + if len(descr) == 9 and descr[0] == 9 and descr[1] == _DFU_DESCRIPTOR_TYPE: + nt = collections.namedtuple('CfgDescr', + ['bLength', 'bDescriptorType', 'bmAttributes', + 'wDetachTimeOut', 'wTransferSize', 'bcdDFUVersion']) + return nt(*struct.unpack('