Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apollo_fpga: Add optional timeout to ApolloDebugger._find_device #106

Merged
merged 1 commit into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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