Skip to content

Commit

Permalink
Merge pull request #47 from mossmann/ignore-saturn-v
Browse files Browse the repository at this point in the history
apollo_fpga: Ignore devices not running Apollo
  • Loading branch information
mossmann authored Jul 3, 2024
2 parents 8e1065b + dcdeb7b commit 25948fa
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions apollo_fpga/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import time
import usb.core
import platform
import errno

from .jtag import JTAGChain
from .spi import DebugSPIConnection
Expand Down Expand Up @@ -47,6 +48,7 @@ class ApolloDebugger:
if os.getenv("LUNA_USB_IDS"):
LUNA_USB_IDS += [tuple([int(x, 16) for x in os.getenv("LUNA_USB_IDS").split(":")])]

REQUEST_GET_ID = 0xa0
REQUEST_SET_LED_PATTERN = 0xa1
REQUEST_GET_FIRMWARE_VERSION = 0xa2
REQUEST_GET_USB_API_VERSION = 0xa3
Expand Down Expand Up @@ -83,7 +85,7 @@ def __init__(self, force_offline=False):
""" Sets up a connection to the debugger. """

# Try to create a connection to our Apollo debug firmware.
device = self._find_device(self.APOLLO_USB_IDS)
device = self._find_device(self.APOLLO_USB_IDS, custom_match=self._device_has_apollo_id)

# If Apollo VID/PID is not found, try to find a gateware VID/PID with a valid Apollo stub
# interface. If found, request the gateware to liberate the USB port. In devices with a
Expand All @@ -106,7 +108,7 @@ def __init__(self, force_offline=False):

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

Expand Down Expand Up @@ -162,6 +164,23 @@ def _device_has_stub_iface(device, return_iface=False):
return stub_if if return_iface else True
return None if return_iface else False

@staticmethod
def _device_has_apollo_id(device):
""" Checks if a device identifies itself as Apollo."""
request_type = usb.ENDPOINT_IN | usb.RECIP_DEVICE | usb.TYPE_VENDOR
try:
response = device.ctrl_transfer(request_type, ApolloDebugger.REQUEST_GET_ID, data_or_wLength=256, timeout=500)
apollo_id = bytes(response).decode('utf-8').split('\x00')[0]
return True if "Apollo" in apollo_id else False
except usb.USBError as e:
if e.errno == errno.EPIPE:
# A pipe error occurs when the device does not implement a
# vendor request with a number matching REQUEST_GET_ID. This is
# the expected result if the device is running Saturn-V.
return False
else:
raise

def detect_connected_version(self):
""" Attempts to determine the revision of the connected hardware.
Expand Down

0 comments on commit 25948fa

Please sign in to comment.