Skip to content

Commit

Permalink
Merge pull request #111 from mossmann/adc-info
Browse files Browse the repository at this point in the history
apollo_fpga: Add ADC reading to print_info()
  • Loading branch information
miek authored Jul 30, 2024
2 parents 28e87f1 + 9c716a7 commit 1b56e9d
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions apollo_fpga/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class DebuggerNotFound(IOError):
pass


class USBAPIRequirement(Exception):
pass


def create_ila_frontend(ila, *, use_cs_multiplexing=False):
""" Convenience method that instantiates an Apollo debug session and creates an ILA frontend from it.
Expand Down Expand Up @@ -61,6 +65,7 @@ class ApolloDebugger:
REQUEST_SET_LED_PATTERN = 0xa1
REQUEST_GET_FIRMWARE_VERSION = 0xa2
REQUEST_GET_USB_API_VERSION = 0xa3
REQUEST_GET_ADC_READING = 0xa4
REQUEST_RECONFIGURE = 0xc0
REQUEST_FORCE_FPGA_OFFLINE = 0xc1
REQUEST_ALLOW_FPGA_TAKEOVER_USB = 0xc2
Expand Down Expand Up @@ -397,21 +402,36 @@ def close(self):


def get_firmware_version(self):
"""Return attached device's firmware version string."""
c_string = self.in_request(self.REQUEST_GET_FIRMWARE_VERSION, length=256)
return c_string.decode('utf-8').split('\x00')[0]


def get_usb_api_version(self):
"""Return attached device's USB API version as a (major, minor) tuple."""
raw_api_version = self.in_request(self.REQUEST_GET_USB_API_VERSION, length=2)
api_major = int(raw_api_version[0])
api_minor = int(raw_api_version[1])
return (api_major, api_minor)


def get_usb_api_version_string(self):
"""Return attached device's USB API version as a string."""
(api_major, api_minor) = self.get_usb_api_version()
return (f"{api_major}.{api_minor}")

def require_usb_api_version(self, minimum):
"""Verify that the attached device meets a minimum USB API version requirement."""
(dev_major, dev_minor) = self.get_usb_api_version()
(req_major, req_minor) = minimum
if dev_major < req_major or (dev_major == req_major and dev_minor < req_minor):
raise USBAPIRequirement(f"Apollo debugger USB API {self.get_usb_api_version_string()} "
f"does not meet minimum required {minimum}. Please upgrade firmware.")

def get_adc_reading(self):
"""Return raw ADC reading from attached device."""
self.require_usb_api_version((1, 2))
reading_bytes = self.in_request(self.REQUEST_GET_ADC_READING, length=2)
return (reading_bytes[0] << 8) + reading_bytes[1]

@classmethod
def print_info(cls, ids=None, stub_ids=None, force_offline=False, timeout=5000, out=print):
""" Print information about Apollo and all connected Apollo devices.
Expand Down Expand Up @@ -475,6 +495,10 @@ def print_info(cls, ids=None, stub_ids=None, force_offline=False, timeout=5000,
out(f"\tVendor ID: {device.idVendor:04x}")
out(f"\tProduct ID: {device.idProduct:04x}")
out(f"\tbcdDevice: {device.bcdDevice:04x}")
try:
out(f"\tADC reading: {debugger.get_adc_reading()}")
except USBAPIRequirement:
pass
out(f"\tFirmware version: {debugger.get_firmware_version()}")
out(f"\tUSB API version: {debugger.get_usb_api_version_string()}")
if force_offline:
Expand Down

0 comments on commit 1b56e9d

Please sign in to comment.