Skip to content

Commit d2f76f0

Browse files
committed
Merge pull request #229 from c1728p9/pydapaccess_backport
Pydapaccess backport
2 parents 4087bcd + 52c2a31 commit d2f76f0

5 files changed

Lines changed: 58 additions & 28 deletions

File tree

pyOCD/pyDAPAccess/dap_access_usb.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,15 @@
3434
VALUE_MATCH = 1 << 4
3535
MATCH_MASK = 1 << 5
3636

37-
MBED_VID = 0x0d28
38-
MBED_PID = 0x0204
39-
4037

4138
def _get_interfaces():
4239
"""Get the connected USB devices"""
43-
return INTERFACE[usb_backend].getAllConnectedInterface(MBED_VID, MBED_PID)
40+
return INTERFACE[usb_backend].getAllConnectedInterface()
4441

4542

4643
def _get_unique_id(interface):
4744
"""Get the unique id from an interface"""
48-
interface.write([0x80])
49-
raw_id = bytearray(interface.read())
50-
id_start = 2
51-
id_size = raw_id[1]
52-
unique_id = str(raw_id[id_start:id_start + id_size])
53-
return unique_id
45+
return interface.getSerialNumber()
5446

5547

5648
class _Transfer(object):

pyOCD/pyDAPAccess/interface/hidapi_backend.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,32 +47,38 @@ def open(self):
4747
pass
4848

4949
@staticmethod
50-
def getAllConnectedInterface(vid, pid):
50+
def getAllConnectedInterface():
5151
"""
5252
returns all the connected devices which matches HidApiUSB.vid/HidApiUSB.pid.
5353
returns an array of HidApiUSB (Interface) objects
5454
"""
5555

56-
devices = hid.enumerate(vid, pid)
56+
devices = hid.enumerate()
5757

5858
if not devices:
5959
logging.debug("No Mbed device connected")
60-
return
60+
return []
6161

6262
boards = []
6363

6464
for deviceInfo in devices:
65+
product_name = deviceInfo['product_string']
66+
if (product_name.find("CMSIS-DAP") < 0):
67+
# Skip non cmsis-dap devices
68+
continue
69+
6570
try:
6671
dev = hid.device(vendor_id=deviceInfo['vendor_id'], product_id=deviceInfo['product_id'],
6772
path=deviceInfo['path'])
6873
except IOError:
6974
logging.debug("Failed to open Mbed device")
70-
return
75+
continue
7176

7277
# Create the USB interface object for this device.
7378
new_board = HidApiUSB()
7479
new_board.vendor_name = deviceInfo['manufacturer_string']
7580
new_board.product_name = deviceInfo['product_string']
81+
new_board.serial_number = deviceInfo['serial_number']
7682
new_board.vid = deviceInfo['vendor_id']
7783
new_board.pid = deviceInfo['product_id']
7884
new_board.device_info = deviceInfo
@@ -106,6 +112,9 @@ def read(self, timeout=-1):
106112
"""
107113
return self.device.read(64)
108114

115+
def getSerialNumber(self):
116+
return self.serial_number
117+
109118
def close(self):
110119
"""
111120
close the interface

pyOCD/pyDAPAccess/interface/pyusb_backend.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,23 +64,35 @@ def rx_task(self):
6464
self.rcv_data.append(self.ep_in.read(self.ep_in.wMaxPacketSize, -1))
6565

6666
@staticmethod
67-
def getAllConnectedInterface(vid, pid):
67+
def getAllConnectedInterface():
6868
"""
6969
returns all the connected devices which matches PyUSB.vid/PyUSB.pid.
7070
returns an array of PyUSB (Interface) objects
7171
"""
7272
# find all devices matching the vid/pid specified
73-
all_devices = usb.core.find(find_all=True, idVendor=vid, idProduct=pid)
73+
all_devices = usb.core.find(find_all=True)
7474

7575
if not all_devices:
7676
logging.debug("No device connected")
77-
return None
77+
return []
7878

7979
boards = []
8080

8181
# iterate on all devices found
8282
for board in all_devices:
8383
interface_number = -1
84+
try:
85+
# The product string is read over USB when accessed.
86+
# This can cause an exception to be thrown if the device
87+
# is malfunctioning.
88+
product = board.product
89+
except usb.core.USBError as error:
90+
logging.warning("Exception getting product string: %s", error)
91+
continue
92+
if (product is None) or (product.find("CMSIS-DAP") < 0):
93+
# Not a cmsis-dap device so close it
94+
usb.util.dispose_resources(board)
95+
continue
8496

8597
# get active config
8698
config = board.get_active_configuration()
@@ -108,8 +120,6 @@ def getAllConnectedInterface(vid, pid):
108120
else:
109121
ep_out = ep
110122

111-
product_name = usb.util.get_string(board, 2)
112-
vendor_name = usb.util.get_string(board, 1)
113123
"""If there is no EP for OUT then we can use CTRL EP"""
114124
if not ep_in:
115125
logging.error('Endpoints not found')
@@ -119,11 +129,12 @@ def getAllConnectedInterface(vid, pid):
119129
new_board.ep_in = ep_in
120130
new_board.ep_out = ep_out
121131
new_board.dev = board
122-
new_board.vid = vid
123-
new_board.pid = pid
132+
new_board.vid = board.idVendor
133+
new_board.pid = board.idProduct
124134
new_board.intf_number = interface_number
125-
new_board.product_name = product_name
126-
new_board.vendor_name = vendor_name
135+
new_board.product_name = product
136+
new_board.vendor_name = board.manufacturer
137+
new_board.serial_number = board.serial_number
127138
new_board.start_rx()
128139
boards.append(new_board)
129140

@@ -169,6 +180,9 @@ def setPacketCount(self, count):
169180
# No interface level restrictions on count
170181
self.packet_count = count
171182

183+
def getSerialNumber(self):
184+
return self.serial_number
185+
172186
def close(self):
173187
"""
174188
close the interface

pyOCD/pyDAPAccess/interface/pywinusb_backend.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,16 @@ def open(self):
6060
self.device.open(shared=False)
6161

6262
@staticmethod
63-
def getAllConnectedInterface(vid, pid):
63+
def getAllConnectedInterface():
6464
"""
65-
returns all the connected devices which matches PyWinUSB.vid/PyWinUSB.pid.
66-
returns an array of PyWinUSB (Interface) objects
65+
returns all the connected CMSIS-DAP devices
6766
"""
6867
all_devices = hid.find_all_hid_devices()
6968

7069
# find devices with good vid/pid
7170
all_mbed_devices = []
7271
for d in all_devices:
73-
if (d.vendor_id == vid) and (d.product_id == pid):
72+
if (d.product_name.find("CMSIS-DAP") >= 0):
7473
all_mbed_devices.append(d)
7574

7675
boards = []
@@ -83,6 +82,7 @@ def getAllConnectedInterface(vid, pid):
8382
new_board.report = report[0]
8483
new_board.vendor_name = dev.vendor_name
8584
new_board.product_name = dev.product_name
85+
new_board.serial_number = dev.serial_number
8686
new_board.vid = dev.vendor_id
8787
new_board.pid = dev.product_id
8888
new_board.device = dev
@@ -127,6 +127,9 @@ def setPacketCount(self, count):
127127
# No interface level restrictions on count
128128
self.packet_count = count
129129

130+
def getSerialNumber(self):
131+
return self.serial_number
132+
130133
def close(self):
131134
"""
132135
close the interface

test/gdb_test.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import os
2727
import json
28+
import sys
2829
from subprocess import Popen, STDOUT, PIPE
2930

3031
from pyOCD.tools.gdb_server import GDBServerTool
@@ -36,6 +37,17 @@
3637

3738
TEST_PARAM_FILE = "test_params.txt"
3839
TEST_RESULT_FILE = "test_results.txt"
40+
PYTHON_GDB_FOR_OS = {
41+
"linux": "arm-none-eabi-gdb",
42+
"darwin": "arm-none-eabi-gdb-py",
43+
"win": "arm-none-eabi-gdb-py",
44+
}
45+
PYTHON_GDB = None
46+
for prefix, program in PYTHON_GDB_FOR_OS.iteritems():
47+
if sys.platform.startswith(prefix):
48+
PYTHON_GDB = program
49+
break
50+
3951

4052
parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
4153

@@ -116,7 +128,7 @@ def test_gdb(board_id=None):
116128
f.write(json.dumps(test_params))
117129

118130
# Run the test
119-
gdb = ["arm-none-eabi-gdb-py", "--command=gdb_script.py"]
131+
gdb = [PYTHON_GDB, "--command=gdb_script.py"]
120132
with open("output.txt", "wb") as f:
121133
program = Popen(gdb, stdin=PIPE, stdout=f, stderr=STDOUT)
122134
args = ['-p=%i' % test_port, "-f=%i" % test_clock, "-b=%s" % board_id]

0 commit comments

Comments
 (0)