diff --git a/pyOCD/pyDAPAccess/dap_access_usb.py b/pyOCD/pyDAPAccess/dap_access_usb.py index 3147aa2c1..377cb69dd 100644 --- a/pyOCD/pyDAPAccess/dap_access_usb.py +++ b/pyOCD/pyDAPAccess/dap_access_usb.py @@ -34,23 +34,15 @@ VALUE_MATCH = 1 << 4 MATCH_MASK = 1 << 5 -MBED_VID = 0x0d28 -MBED_PID = 0x0204 - def _get_interfaces(): """Get the connected USB devices""" - return INTERFACE[usb_backend].getAllConnectedInterface(MBED_VID, MBED_PID) + return INTERFACE[usb_backend].getAllConnectedInterface() def _get_unique_id(interface): """Get the unique id from an interface""" - interface.write([0x80]) - raw_id = bytearray(interface.read()) - id_start = 2 - id_size = raw_id[1] - unique_id = str(raw_id[id_start:id_start + id_size]) - return unique_id + return interface.getSerialNumber() class _Transfer(object): diff --git a/pyOCD/pyDAPAccess/interface/hidapi_backend.py b/pyOCD/pyDAPAccess/interface/hidapi_backend.py index b4c6297a5..e23ae25f3 100644 --- a/pyOCD/pyDAPAccess/interface/hidapi_backend.py +++ b/pyOCD/pyDAPAccess/interface/hidapi_backend.py @@ -47,32 +47,38 @@ def open(self): pass @staticmethod - def getAllConnectedInterface(vid, pid): + def getAllConnectedInterface(): """ returns all the connected devices which matches HidApiUSB.vid/HidApiUSB.pid. returns an array of HidApiUSB (Interface) objects """ - devices = hid.enumerate(vid, pid) + devices = hid.enumerate() if not devices: logging.debug("No Mbed device connected") - return + return [] boards = [] for deviceInfo in devices: + product_name = deviceInfo['product_string'] + if (product_name.find("CMSIS-DAP") < 0): + # Skip non cmsis-dap devices + continue + try: dev = hid.device(vendor_id=deviceInfo['vendor_id'], product_id=deviceInfo['product_id'], path=deviceInfo['path']) except IOError: logging.debug("Failed to open Mbed device") - return + continue # Create the USB interface object for this device. new_board = HidApiUSB() new_board.vendor_name = deviceInfo['manufacturer_string'] new_board.product_name = deviceInfo['product_string'] + new_board.serial_number = deviceInfo['serial_number'] new_board.vid = deviceInfo['vendor_id'] new_board.pid = deviceInfo['product_id'] new_board.device_info = deviceInfo @@ -106,6 +112,9 @@ def read(self, timeout=-1): """ return self.device.read(64) + def getSerialNumber(self): + return self.serial_number + def close(self): """ close the interface diff --git a/pyOCD/pyDAPAccess/interface/pyusb_backend.py b/pyOCD/pyDAPAccess/interface/pyusb_backend.py index 856af856f..682fda6d1 100644 --- a/pyOCD/pyDAPAccess/interface/pyusb_backend.py +++ b/pyOCD/pyDAPAccess/interface/pyusb_backend.py @@ -64,23 +64,35 @@ def rx_task(self): self.rcv_data.append(self.ep_in.read(self.ep_in.wMaxPacketSize, -1)) @staticmethod - def getAllConnectedInterface(vid, pid): + def getAllConnectedInterface(): """ returns all the connected devices which matches PyUSB.vid/PyUSB.pid. returns an array of PyUSB (Interface) objects """ # find all devices matching the vid/pid specified - all_devices = usb.core.find(find_all=True, idVendor=vid, idProduct=pid) + all_devices = usb.core.find(find_all=True) if not all_devices: logging.debug("No device connected") - return None + return [] boards = [] # iterate on all devices found for board in all_devices: interface_number = -1 + try: + # The product string is read over USB when accessed. + # This can cause an exception to be thrown if the device + # is malfunctioning. + product = board.product + except usb.core.USBError as error: + logging.warning("Exception getting product string: %s", error) + continue + if (product is None) or (product.find("CMSIS-DAP") < 0): + # Not a cmsis-dap device so close it + usb.util.dispose_resources(board) + continue # get active config config = board.get_active_configuration() @@ -108,8 +120,6 @@ def getAllConnectedInterface(vid, pid): else: ep_out = ep - product_name = usb.util.get_string(board, 2) - vendor_name = usb.util.get_string(board, 1) """If there is no EP for OUT then we can use CTRL EP""" if not ep_in: logging.error('Endpoints not found') @@ -119,11 +129,12 @@ def getAllConnectedInterface(vid, pid): new_board.ep_in = ep_in new_board.ep_out = ep_out new_board.dev = board - new_board.vid = vid - new_board.pid = pid + new_board.vid = board.idVendor + new_board.pid = board.idProduct new_board.intf_number = interface_number - new_board.product_name = product_name - new_board.vendor_name = vendor_name + new_board.product_name = product + new_board.vendor_name = board.manufacturer + new_board.serial_number = board.serial_number new_board.start_rx() boards.append(new_board) @@ -169,6 +180,9 @@ def setPacketCount(self, count): # No interface level restrictions on count self.packet_count = count + def getSerialNumber(self): + return self.serial_number + def close(self): """ close the interface diff --git a/pyOCD/pyDAPAccess/interface/pywinusb_backend.py b/pyOCD/pyDAPAccess/interface/pywinusb_backend.py index 79d0eb28a..4b40630ab 100644 --- a/pyOCD/pyDAPAccess/interface/pywinusb_backend.py +++ b/pyOCD/pyDAPAccess/interface/pywinusb_backend.py @@ -60,17 +60,16 @@ def open(self): self.device.open(shared=False) @staticmethod - def getAllConnectedInterface(vid, pid): + def getAllConnectedInterface(): """ - returns all the connected devices which matches PyWinUSB.vid/PyWinUSB.pid. - returns an array of PyWinUSB (Interface) objects + returns all the connected CMSIS-DAP devices """ all_devices = hid.find_all_hid_devices() # find devices with good vid/pid all_mbed_devices = [] for d in all_devices: - if (d.vendor_id == vid) and (d.product_id == pid): + if (d.product_name.find("CMSIS-DAP") >= 0): all_mbed_devices.append(d) boards = [] @@ -83,6 +82,7 @@ def getAllConnectedInterface(vid, pid): new_board.report = report[0] new_board.vendor_name = dev.vendor_name new_board.product_name = dev.product_name + new_board.serial_number = dev.serial_number new_board.vid = dev.vendor_id new_board.pid = dev.product_id new_board.device = dev @@ -127,6 +127,9 @@ def setPacketCount(self, count): # No interface level restrictions on count self.packet_count = count + def getSerialNumber(self): + return self.serial_number + def close(self): """ close the interface diff --git a/test/gdb_test.py b/test/gdb_test.py index 035a3ed2f..24507358e 100644 --- a/test/gdb_test.py +++ b/test/gdb_test.py @@ -25,6 +25,7 @@ import os import json +import sys from subprocess import Popen, STDOUT, PIPE from pyOCD.tools.gdb_server import GDBServerTool @@ -36,6 +37,17 @@ TEST_PARAM_FILE = "test_params.txt" TEST_RESULT_FILE = "test_results.txt" +PYTHON_GDB_FOR_OS = { + "linux": "arm-none-eabi-gdb", + "darwin": "arm-none-eabi-gdb-py", + "win": "arm-none-eabi-gdb-py", +} +PYTHON_GDB = None +for prefix, program in PYTHON_GDB_FOR_OS.iteritems(): + if sys.platform.startswith(prefix): + PYTHON_GDB = program + break + parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) @@ -116,7 +128,7 @@ def test_gdb(board_id=None): f.write(json.dumps(test_params)) # Run the test - gdb = ["arm-none-eabi-gdb-py", "--command=gdb_script.py"] + gdb = [PYTHON_GDB, "--command=gdb_script.py"] with open("output.txt", "wb") as f: program = Popen(gdb, stdin=PIPE, stdout=f, stderr=STDOUT) args = ['-p=%i' % test_port, "-f=%i" % test_clock, "-b=%s" % board_id]