Skip to content

Commit

Permalink
Merge pull request #8 from mossmann/detach-timeout
Browse files Browse the repository at this point in the history
Use timeout instead of delay after DFU_DETACH
  • Loading branch information
mossmann authored Jul 24, 2024
2 parents 048b7fd + 14de95e commit 8672ec4
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions fwup/dfu.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def find_dfu_devices(cls, *args, **kwargs):



def __init__(self, index=0, *args, **kwargs):
def __init__(self, index=0, detach=True, timeout=5000, *args, **kwargs):
""" Creates a new class representing a DFU target.
Accepts the same specifier arguments as pyusb's usb.core.find(); plus an index argument that gets
Expand All @@ -145,25 +145,39 @@ def __init__(self, index=0, *args, **kwargs):

# ... and ensure the relevant configuration is active.
try:
self.device.set_configuration(self.configuration)
self.device.detach_kernel_driver(self.interface)
except:
pass

try:
self.device.set_configuration(self.configuration)
except:
pass

# Read the device's download parameters.
self.__read_device_info()

# If the device is in runtime mode, send a DFU detach request first.
if self.runtime_mode and (self.attributes & self.DFU_WILL_DETACH):
if detach and self.runtime_mode and (self.attributes & self.DFU_WILL_DETACH):
try:
self.__dfu_out_request(self.DFU_DETACH, self.interface, None)
except:
pass
else:
# Disconnect device, wait for reenumeration and start over.
usb.util.dispose_resources(self.device)
time.sleep(3)
self.__init__(index=index, *args, **kwargs)
start = time.time()
while True:
try:
self.__init__(index=index, detach=False, *args, **kwargs)
except BoardNotFoundError:
pass
else:
if not self.runtime_mode:
break
if ((time.time() - start) * 1000) >= timeout:
raise BoardNotFoundError("Device not found after DFU_DETACH.")
time.sleep(0.1)


def __read_device_info(self):
Expand Down

0 comments on commit 8672ec4

Please sign in to comment.