Skip to content

Commit

Permalink
Merge pull request #106 from mndza/add-find-device-timeout
Browse files Browse the repository at this point in the history
apollo_fpga: Add optional timeout to `ApolloDebugger._find_device`
  • Loading branch information
mossmann authored Jul 18, 2024
2 parents 730982c + 3115967 commit 3788be1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
22 changes: 14 additions & 8 deletions apollo_fpga/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ def __init__(self, force_offline=False):
raise DebuggerNotFound(f"Handoff request failed: {e.strerror}")

# Wait for Apollo to enumerate and try again
time.sleep(2)
device = self._find_device(self.APOLLO_USB_IDS, custom_match=self._device_has_apollo_id)
device = self._find_device(self.APOLLO_USB_IDS, custom_match=self._device_has_apollo_id, timeout=5000)
if device is None:
raise DebuggerNotFound("Handoff was requested, but Apollo is not available")

Expand Down Expand Up @@ -149,7 +148,7 @@ def _request_handoff(cls, device):
device.ctrl_transfer(request_type, REQUEST_APOLLO_ADV_STOP, wIndex=intf_number, timeout=5000)

@staticmethod
def _find_device(ids, custom_match=None):
def _find_device(ids, custom_match=None, timeout=0):
import usb.backend.libusb1

# In Windows, we need to specify the libusb library location to create a backend.
Expand All @@ -168,11 +167,18 @@ def _find_device(ids, custom_match=None):
backend = usb.backend.libusb1.get_backend()

# Find the device.
for vid, pid in ids:
device = usb.core.find(backend=backend, idVendor=vid, idProduct=pid, custom_match=custom_match)
if device is not None:
return device

wait = 0
while True:
for vid, pid in ids:
device = usb.core.find(backend=backend, idVendor=vid, idProduct=pid, custom_match=custom_match)
if device is not None:
return device
# Should we wait and try again?
if wait >= timeout:
break
time.sleep(0.5)
wait += 500

return None

@staticmethod
Expand Down
3 changes: 0 additions & 3 deletions apollo_fpga/commands/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,6 @@ def program_flash_fast(device, args):
# Let the LUNA gateware take over in devices with shared USB port
device.allow_fpga_takeover_usb()

# Wait for flash bridge enumeration
time.sleep(2)

# Program SPI flash memory using the configured bridge
bridge = FlashBridgeConnection()
programmer = ECP5FlashBridgeProgrammer(bridge=bridge)
Expand Down
3 changes: 2 additions & 1 deletion apollo_fpga/gateware/flash_bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,8 @@ def __init__(self):
# Try to create a connection to our configuration flash bridge.
device = ApolloDebugger._find_device(
ids=[(VENDOR_ID, PRODUCT_ID)],
custom_match=self._find_cfg_flash_bridge
custom_match=self._find_cfg_flash_bridge,
timeout=5000
)

# If we couldn't find the bridge, bail out.
Expand Down

0 comments on commit 3788be1

Please sign in to comment.