20
20
21
21
from time import sleep
22
22
from board import Board
23
- from pyOCD .interface import INTERFACE , usb_backend
23
+ from pyOCD .transport . daplink import DapLink
24
24
25
25
class BoardInfo (object ):
26
26
def __init__ (self , name , target , binary ):
@@ -65,13 +65,15 @@ class MbedBoard(Board):
65
65
Particularly, this class allows you to dynamically determine
66
66
the type of all boards connected based on the id board
67
67
"""
68
- def __init__ (self , interface , board_id , unique_id , target = None , transport = "cmsis_dap" , frequency = 1000000 ):
68
+ def __init__ (self , link , target = None , frequency = 1000000 ):
69
69
"""
70
70
Init the board
71
71
"""
72
72
self .name = None
73
73
self .native_target = None
74
74
self .test_binary = None
75
+ unique_id = link .get_unique_id ()
76
+ board_id = unique_id [0 :4 ]
75
77
if board_id in BOARD_ID_TO_INFO :
76
78
board_info = BOARD_ID_TO_INFO [board_id ]
77
79
self .name = board_info .name
@@ -85,7 +87,7 @@ def __init__(self, interface, board_id, unique_id, target=None, transport="cmsis
85
87
if target is None :
86
88
raise Exception ("Unknown board target" )
87
89
88
- super (MbedBoard , self ).__init__ (target , target , interface , transport , frequency )
90
+ super (MbedBoard , self ).__init__ (target , target , link , frequency )
89
91
self .unique_id = unique_id
90
92
self .target_type = target
91
93
@@ -120,11 +122,12 @@ def getInfo(self):
120
122
return Board .getInfo (self ) + " [" + self .target_type + "]"
121
123
122
124
@staticmethod
123
- def listConnectedBoards (transport = "cmsis_dap" ):
125
+ def listConnectedBoards (link_class = DapLink ):
124
126
"""
125
127
List the connected board info
126
128
"""
127
- all_mbeds = MbedBoard .getAllConnectedBoards (close = True , blocking = False )
129
+ all_mbeds = MbedBoard .getAllConnectedBoards (link_class , close = True ,
130
+ blocking = False )
128
131
index = 0
129
132
if len (all_mbeds ) > 0 :
130
133
for mbed in all_mbeds :
@@ -134,74 +137,44 @@ def listConnectedBoards(transport="cmsis_dap"):
134
137
print ("No available boards are connected" )
135
138
136
139
@staticmethod
137
- def getAllConnectedBoards (transport = "cmsis_dap" , close = False , blocking = True ,
138
- target_override = None , frequency = 1000000 ):
140
+ def getAllConnectedBoards (link_class = DapLink , close = False , blocking = True ,
141
+ target_override = None , frequency = 1000000 ):
139
142
"""
140
143
Return an array of all mbed boards connected
141
144
"""
142
- first = True
143
- while True :
144
- while True :
145
- if not first :
146
- # Don't eat up all the cpu if an unsupported board is connected.
147
- # Sleep before getting connected interfaces. This way if a keyboard
148
- # exception comes in there will be no resources to close
149
- sleep (0.2 )
150
-
151
- all_mbeds = INTERFACE [usb_backend ].getAllConnectedInterface (mbed_vid , mbed_pid )
152
- if all_mbeds == None :
153
- all_mbeds = []
154
-
155
- if not blocking :
156
- # No blocking so break from loop
157
- break
158
-
159
- if len (all_mbeds ) > 0 :
160
- # A board has been found so break from loop
161
- break
162
145
163
- if (first == True ):
164
- logging .info ("Waiting for a USB device connected" )
165
- first = False
146
+ mbed_list = []
147
+ while True :
166
148
167
- mbed_boards = []
168
- for mbed in all_mbeds :
169
- try :
170
- mbed .write ([0x80 ])
171
- u_id_ = mbed .read ()
172
- board_id = array .array ('B' , [i for i in u_id_ [2 :6 ]]).tostring ()
173
- unique_id = array .array ('B' , [i for i in u_id_ [2 :2 + u_id_ [1 ]]]).tostring ()
174
- if board_id not in BOARD_ID_TO_INFO :
175
- logging .info ("Unsupported board found: %s" % board_id )
176
- if target_override is None :
177
- # TODO - if no board can be determined treat this as a generic cortex-m device
178
- logging .info ("Target could not be determined. Specify target manually to use board" )
179
- mbed .close ()
180
- continue
149
+ connected_daplinks = link_class .get_connected_devices ()
150
+ for daplink in connected_daplinks :
151
+ new_mbed = MbedBoard (daplink , target_override , frequency )
152
+ mbed_list .append (new_mbed )
181
153
182
- new_mbed = MbedBoard (mbed , board_id , unique_id , target_override , transport , frequency )
183
- logging .info ("new board id detected: %s" , unique_id )
184
- mbed_boards .append (new_mbed )
185
- if close :
186
- mbed .close ()
187
- except :
188
- #TODO - close all boards when an exception occurs
189
- mbed .close ()
190
- raise
154
+ #TODO - handle exception on open
155
+ if not close :
156
+ for daplink in connected_daplinks :
157
+ daplink .open ()
191
158
192
- if len (mbed_boards ) > 0 or not blocking :
193
- return mbed_boards
159
+ if not blocking :
160
+ break
161
+ elif len (mbed_list ) > 0 :
162
+ break
163
+ else :
164
+ sleep (0.01 )
165
+ assert len (mbed_list ) == 0
194
166
195
- if (first == True ):
196
- logging .info ("Waiting for a USB device connected" )
197
- first = False
167
+ return mbed_list
198
168
199
169
@staticmethod
200
- def chooseBoard (transport = "cmsis_dap" , blocking = True , return_first = False , board_id = None , target_override = None , frequency = 1000000 , init_board = True ):
170
+ def chooseBoard (link_class = DapLink , blocking = True , return_first = False ,
171
+ board_id = None , target_override = None , frequency = 1000000 ,
172
+ init_board = True ):
201
173
"""
202
174
Allow you to select a board among all boards connected
203
175
"""
204
- all_mbeds = MbedBoard .getAllConnectedBoards (transport , False , blocking , target_override , frequency )
176
+ all_mbeds = MbedBoard .getAllConnectedBoards (link_class , False , blocking ,
177
+ target_override , frequency )
205
178
206
179
# If a board ID is specified close all other boards
207
180
if board_id != None :
@@ -210,7 +183,7 @@ def chooseBoard(transport="cmsis_dap", blocking=True, return_first=False, board_
210
183
if mbed .unique_id == (board_id ):
211
184
new_mbed_list .append (mbed )
212
185
else :
213
- mbed .interface .close ()
186
+ mbed .link .close ()
214
187
assert len (new_mbed_list ) <= 1
215
188
all_mbeds = new_mbed_list
216
189
@@ -225,7 +198,7 @@ def chooseBoard(transport="cmsis_dap", blocking=True, return_first=False, board_
225
198
# Select first board and close others if True
226
199
if return_first :
227
200
for i in range (1 , len (all_mbeds )):
228
- all_mbeds [i ].interface .close ()
201
+ all_mbeds [i ].link .close ()
229
202
all_mbeds = all_mbeds [0 :1 ]
230
203
231
204
# Ask use to select boards if there is more than 1 left
@@ -255,7 +228,7 @@ def chooseBoard(transport="cmsis_dap", blocking=True, return_first=False, board_
255
228
# close all others mbed connected
256
229
for mbed in all_mbeds :
257
230
if mbed != all_mbeds [ch ]:
258
- mbed .interface .close ()
231
+ mbed .link .close ()
259
232
all_mbeds = all_mbeds [ch :ch + 1 ]
260
233
261
234
assert len (all_mbeds ) == 1
@@ -264,12 +237,6 @@ def chooseBoard(transport="cmsis_dap", blocking=True, return_first=False, board_
264
237
try :
265
238
mbed .init ()
266
239
except :
267
- mbed .interface .close ()
240
+ mbed .link .close ()
268
241
raise
269
242
return mbed
270
-
271
- def getPacketCount (self ):
272
- """
273
- Return the number of commands the remote device's buffer can hold.
274
- """
275
- return self .transport .info ('PACKET_COUNT' )
0 commit comments