Skip to content

Commit 07f181a

Browse files
pi-anldpgeorge
authored andcommitted
Revert "tools/pydfu.py: Respect longer timeouts requested by DFU dev..."
This reverts commit 4d6f60d. This implementation used the timeout as a maximum amount of time needed for the operation, when actually the spec and other tools suggest that it's the minumum delay needed between subsequent USB transfers.
1 parent 494bcad commit 07f181a

File tree

1 file changed

+10
-42
lines changed

1 file changed

+10
-42
lines changed

Diff for: tools/pydfu.py

+10-42
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,13 @@
2222
import usb.core
2323
import usb.util
2424
import zlib
25-
import time
26-
import math
2725

2826
# VID/PID
2927
__VID = 0x0483
3028
__PID = 0xDF11
3129

3230
# USB request __TIMEOUT
3331
__TIMEOUT = 4000
34-
__NEXT_TIMEOUT = 0
35-
__STATUS_TIMEOUT = 20000
3632

3733
# DFU commands
3834
__DFU_DETACH = 0
@@ -99,13 +95,6 @@ def get_string(dev, index):
9995
return usb.util.get_string(dev, index)
10096

10197

102-
def timeout():
103-
global __NEXT_TIMEOUT
104-
t = max(__TIMEOUT, __NEXT_TIMEOUT)
105-
__NEXT_TIMEOUT = 0
106-
return int(math.ceil(t))
107-
108-
10998
def find_dfu_cfg_descr(descr):
11099
if len(descr) == 9 and descr[0] == 9 and descr[1] == _DFU_DESCRIPTOR_TYPE:
111100
nt = collections.namedtuple(
@@ -161,45 +150,24 @@ def init():
161150

162151
def abort_request():
163152
"""Sends an abort request."""
164-
__dev.ctrl_transfer(0x21, __DFU_ABORT, 0, __DFU_INTERFACE, None, timeout())
153+
__dev.ctrl_transfer(0x21, __DFU_ABORT, 0, __DFU_INTERFACE, None, __TIMEOUT)
165154

166155

167156
def clr_status():
168157
"""Clears any error status (perhaps left over from a previous session)."""
169-
__dev.ctrl_transfer(0x21, __DFU_CLRSTATUS, 0, __DFU_INTERFACE, None, timeout())
158+
__dev.ctrl_transfer(0x21, __DFU_CLRSTATUS, 0, __DFU_INTERFACE, None, __TIMEOUT)
170159

171160

172161
def get_status():
173162
"""Get the status of the last operation."""
174-
_timeout = time.time() + max(__STATUS_TIMEOUT, timeout())
175-
stat = None
176-
while time.time() < _timeout:
177-
try:
178-
stat = __dev.ctrl_transfer(
179-
0xA1, __DFU_GETSTATUS, 0, __DFU_INTERFACE, 6, int(_timeout - time.time())
180-
)
181-
break
182-
except usb.core.USBError as ex:
183-
# If the firmware is blocked the transfer can timeout much quicker than
184-
# the supplied timeout. If so, retry until the overall timeout is used up.
185-
if "Operation timed out" not in str(ex):
186-
raise
187-
188-
if stat is None:
189-
raise SystemExit("DFU: get_status timed out")
163+
stat = __dev.ctrl_transfer(0xA1, __DFU_GETSTATUS, 0, __DFU_INTERFACE, 6, 20000)
190164

191165
# firmware can provide an optional string for any error
192166
if stat[5]:
193167
message = get_string(__dev, stat[5])
194168
if message:
195169
print(message)
196170

197-
# firmware can send a longer timeout request while it's performing slow operation eg. erase
198-
timeout_ms = stat[1] << 16 | stat[2] << 8 | stat[3]
199-
if timeout_ms:
200-
global __NEXT_TIMEOUT
201-
__NEXT_TIMEOUT = __TIMEOUT + timeout_ms
202-
203171
return stat[4]
204172

205173

@@ -212,9 +180,9 @@ def check_status(stage, expected):
212180
def mass_erase():
213181
"""Performs a MASS erase (i.e. erases the entire device)."""
214182
# Send DNLOAD with first byte=0x41
215-
__dev.ctrl_transfer(0x21, __DFU_DNLOAD, 0, __DFU_INTERFACE, "\x41", timeout())
183+
__dev.ctrl_transfer(0x21, __DFU_DNLOAD, 0, __DFU_INTERFACE, "\x41", __TIMEOUT)
216184

217-
# Execute erase and wait until complete
185+
# Execute last command
218186
check_status("erase", __DFU_STATE_DFU_DOWNLOAD_BUSY)
219187

220188
# Check command state
@@ -228,7 +196,7 @@ def page_erase(addr):
228196

229197
# Send DNLOAD with first byte=0x41 and page address
230198
buf = struct.pack("<BI", 0x41, addr)
231-
__dev.ctrl_transfer(0x21, __DFU_DNLOAD, 0, __DFU_INTERFACE, buf, timeout())
199+
__dev.ctrl_transfer(0x21, __DFU_DNLOAD, 0, __DFU_INTERFACE, buf, __TIMEOUT)
232200

233201
# Execute last command
234202
check_status("erase", __DFU_STATE_DFU_DOWNLOAD_BUSY)
@@ -241,7 +209,7 @@ def set_address(addr):
241209
"""Sets the address for the next operation."""
242210
# Send DNLOAD with first byte=0x21 and page address
243211
buf = struct.pack("<BI", 0x21, addr)
244-
__dev.ctrl_transfer(0x21, __DFU_DNLOAD, 0, __DFU_INTERFACE, buf, timeout())
212+
__dev.ctrl_transfer(0x21, __DFU_DNLOAD, 0, __DFU_INTERFACE, buf, __TIMEOUT)
245213

246214
# Execute last command
247215
check_status("set address", __DFU_STATE_DFU_DOWNLOAD_BUSY)
@@ -275,7 +243,7 @@ def write_memory(addr, buf, progress=None, progress_addr=0, progress_size=0):
275243
# Send DNLOAD with fw data
276244
chunk = min(__cfg_descr.wTransferSize, xfer_total - xfer_bytes)
277245
__dev.ctrl_transfer(
278-
0x21, __DFU_DNLOAD, 2, __DFU_INTERFACE, buf[xfer_bytes : xfer_bytes + chunk], timeout()
246+
0x21, __DFU_DNLOAD, 2, __DFU_INTERFACE, buf[xfer_bytes : xfer_bytes + chunk], __TIMEOUT
279247
)
280248

281249
# Execute last command
@@ -299,7 +267,7 @@ def write_page(buf, xfer_offset):
299267
set_address(xfer_base + xfer_offset)
300268

301269
# Send DNLOAD with fw data
302-
__dev.ctrl_transfer(0x21, __DFU_DNLOAD, 2, __DFU_INTERFACE, buf, timeout())
270+
__dev.ctrl_transfer(0x21, __DFU_DNLOAD, 2, __DFU_INTERFACE, buf, __TIMEOUT)
303271

304272
# Execute last command
305273
check_status("write memory", __DFU_STATE_DFU_DOWNLOAD_BUSY)
@@ -317,7 +285,7 @@ def exit_dfu():
317285
set_address(0x08000000)
318286

319287
# Send DNLOAD with 0 length to exit DFU
320-
__dev.ctrl_transfer(0x21, __DFU_DNLOAD, 0, __DFU_INTERFACE, None, timeout())
288+
__dev.ctrl_transfer(0x21, __DFU_DNLOAD, 0, __DFU_INTERFACE, None, __TIMEOUT)
321289

322290
try:
323291
# Execute last command

0 commit comments

Comments
 (0)