-
Notifications
You must be signed in to change notification settings - Fork 11
/
cmsis_dap.py
359 lines (293 loc) · 9.4 KB
/
cmsis_dap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#------------------------------------------------------------------------------
"""
CMSIS-DAP Driver
"""
#------------------------------------------------------------------------------
#import struct
from array import array as Array
#import usbdev
#import cortexm
#import iobuf
import hid
#------------------------------------------------------------------------------
# supported devices
# these are the cmsis-dap devices we know about (vid, pid, itf)
known_devices = (
(0x0d28, 0x0204, 3), # NXP MIMXRT1020-EVK
)
def itf_lookup(vid, pid):
"""return the interface to use for a given device"""
for (v, p, i) in known_devices:
if (v == vid) and (p == pid):
return i
return None
def find(sn=None):
"""find the cmsis-dap device, the serial number is used to uniqify the match"""
# get the set of HIDs
hid_devices = hid.enumerate()
# find cmsis-dap devices
devices = []
for (vid, pid, _) in known_devices:
for dev in hid_devices:
# filter out devices that don't match
if vid != dev['vendor_id']:
continue
if pid != dev['product_id']:
continue
if sn is not None and sn != dev['serial_number']:
continue
# we have a match
devices.append(dev)
return devices
#------------------------------------------------------------------------------
# cmsis-dap protocol constants
# CMSIS-DAP General Commands
CMD_DAP_INFO = 0x00
CMD_DAP_LED = 0x01
CMD_DAP_CONNECT = 0x02
CMD_DAP_DISCONNECT = 0x03
CMD_DAP_WRITE_ABORT = 0x08
CMD_DAP_DELAY = 0x09
CMD_DAP_RESET_TARGET = 0x0A
# DAP_CMD_INFO
INFO_ID_VID = 0x01 # Get the Vendor ID (string)
INFO_ID_PID = 0x02 # Get the Product ID (string)
INFO_ID_SN = 0x03 # Get the Serial Number (string)
INFO_ID_FW_VERSION = 0x04 # Get the CMSIS-DAP Firmware Version (string)
INFO_ID_VENDOR_NAME = 0x05 # Get the Target Device Vendor (string)
INFO_ID_DEVICE_NAME = 0x06 # Get the Target Device Name (string)
INFO_ID_DBG_CAPS = 0xF0 # Get information about the Capabilities (BYTE) of the Debug Unit
INFO_ID_TDT_PARMS = 0xF1 # Get the Test Domain Timer parameter information
INFO_ID_SWO_TRACE_SIZE = 0xFD # Get the SWO Trace Buffer Size (WORD)
INFO_ID_MAX_PKT_COUNT = 0xFE # Get the maximum Packet Count (BYTE)
INFO_ID_MAX_PKT_SIZE = 0xFF # Get the maximum Packet Size (SHORT)
# DAP Status Code
DAP_OK = 0
DAP_ERROR = 0xFF
#------------------------------------------------------------------------------
# map register names to cmsis-dap register numbers
regmap = {
'r0':0, 'r1':1, 'r2':2, 'r3':3, 'r4':4, 'r5':5, 'r6':6, 'r7':7,
'r8':8, 'r9':9, 'r10':10, 'r11':11, 'r12':12, 'r13':13, 'r14':14, 'r15':15,
'lr':14, 'pc':15, 'psr':16, 'msp':17, 'psp':18,
# primask
# faultmask
# basepri
# control
# 19 ?
# 20 ?
}
def dump_buf(x):
s = []
for c in x:
s.append('%02x' % c)
print(' '.join(s))
#------------------------------------------------------------------------------
class dap:
"""CMSIS-DAP Device Driver"""
def __init__(self, dev):
self.vid = dev['vendor_id']
self.pid = dev['product_id']
self.sn = dev['serial_number']
self.hid = hid.Device(self.vid, self.pid, self.sn)
self.pkt_size = 64
self.pkt_size = self.get_info_short(INFO_ID_MAX_PKT_SIZE)
self.pkt_count = self.get_info_byte(INFO_ID_MAX_PKT_COUNT)
self.ver = self.get_info_string(INFO_ID_FW_VERSION)
self.caps = self.get_info_byte(INFO_ID_DBG_CAPS)
print('ver %s' % self.ver)
print('caps %d' % self.caps)
print('pkt_count %d' % self.pkt_count)
print('pkt_size %d' % self.pkt_size)
# | close(self)
# | get_feature_report(self, report_id, size)
# | get_indexed_string(self, index, max_length=255)
# | read(self, size, timeout=None)
# | send_feature_report(self, data)
# | write(self, data)
def __del__(self):
self.close()
def close(self):
"""close the HID"""
self.hid.close()
def xfer(self, tx):
"""tx and rx some bytes to the HID"""
self.hid.write(tx)
return self.hid.read(self.pkt_size + 1)
def cmd_dap_info(self, id):
rx = self.xfer(bytes((0, CMD_DAP_INFO, id)))
return rx[1:]
def get_info_string(self, info):
"""get an information string from the debugger"""
rx = self.cmd_dap_info(info)
n = rx[0]
if n:
return rx[1:n].decode("utf-8")
return None
def get_info_byte(self, info):
"""get an information byte/u8 from the debugger"""
rx = self.cmd_dap_info(info)
if rx[0] == 1:
return rx[1]
return None
def get_info_short(self, info):
"""get an information short/u16 from the debugger"""
rx = self.cmd_dap_info(info)
if rx[0] == 2:
return rx[1] + (rx[2] << 8)
return None
def get_info_word(self, info):
"""get an information word/u32 from the debugger"""
rx = self.cmd_dap_info(info)
if rx[0] == 4:
return rx[1] + (rx[2] << 8) + (rx[3] << 16) + (rx[4] << 24)
return None
def led_ctrl(self, val):
"""control the debugger leds"""
rx = self.xfer(bytes((0, CMD_DAP_LED, 0, val)))
assert rx[1] == 0
def dap_connect(self, mode):
rx = self.xfer(bytes((0, CMD_DAP_CONNECT, mode)))
assert rx[1] == mode
def dap_disconnect(self):
rx = self.xfer(bytes((0, CMD_DAP_DISCONNECT)))
assert rx[1] == DAP_OK
#def DAP_HostStatus(self):
#def DAP_Connect(self):
#def DAP_Disconnect(self):
#def DAP_WriteABORT(self):
#def DAP_Delay(self):
#def DAP_ResetTarget(self):
#def DAP_SWJ_Pins(self):
#def DAP_SWJ_Clock(self):
#def DAP_SWJ_Sequence(self):
#def DAP_SWD_Configure(self):
#def DAP_SWD_Sequence(self):
#def DAP_SWO_Transport(self):
#def DAP_SWO_Mode(self):
#def DAP_SWO_Baudrate(self):
#def DAP_SWO_Control(self):
#def DAP_SWO_Status(self):
#def DAP_SWO_ExtendedStatus(self):
#def DAP_SWO_Data(self):
#def DAP_JTAG_Sequence(self):
#def DAP_JTAG_Configure(self):
#def DAP_JTAG_IDCODE(self):
#def DAP_TransferConfigure(self):
#def DAP_Transfer(self):
#def DAP_TransferBlock(self):
#def DAP_TransferAbort(self):
#def DAP_ExecuteCommands(self):
#def DAP_QueueCommands(self):
def rd_reg(self, n):
"""read from a register"""
# TODO
return 0
def wr_reg(self, n, val):
"""write to a register"""
# TODO
def rd_mem8(self, adr, n):
"""read n 8-bit values from memory region"""
# TODO
return [0 for i in range(n)]
def rd_mem16(self, adr, n):
"""read n 16-bit values from memory region"""
assert adr & 1 == 0
# TODO
return [0 for i in range(n)]
def rd_mem32(self, adr, n):
"""read n 32-bit values from memory region"""
assert adr & 3 == 0
# TODO
return [0 for i in range(n)]
def __str__(self):
"""return a string for basic device description"""
s = []
s.append('CMSIS-DAP')
s.append('%04x:%04x' % (self.vid, self.pid))
s.append('version %s' % self.ver)
s.append('serial %r' % self.sn)
return ' '.join(s)
#------------------------------------------------------------------------------
class dbgio:
"""CMSIS-DAP implementation of dbgio cpu interface"""
def __init__(self, dev):
self.dev = dev
self.cpu_name = None
self.menu = (
('info', self.cmd_info),
)
def connect(self, cpu_name, itf):
"""connect the debugger to the target"""
self.cpu_name = cpu_name
self.dbg_itf = itf
self.dap = dap(self.dev)
def disconnect(self):
"""disconnect the debugger from the target"""
self.dap.close()
def cmd_info(self, ui, args):
"""display cmsis-dap information"""
ui.put('%s\n' % self)
def is_halted(self):
"""return True if target is halted"""
# TODO
return True
def rdreg(self, reg):
"""read from the named register"""
n = regmap.get(reg, None)
if n is None:
return None
return self.dap.rd_reg(n)
def wrreg(self, reg, val):
"""write to the named register"""
n = regmap.get(reg, None)
if n is None:
return
self.dap.wr_reg(n, val)
def rdmem32(self, adr, n, io):
"""read n 32-bit words from memory starting at adr"""
max_n = 0x20
while n > 0:
nread = (n, max_n)[n >= max_n]
_ = [io.wr32(x) for x in self.dap.rd_mem32(adr, nread)]
n -= nread
adr += nread * 4
def rdmem16(self, adr, n, io):
"""read n 16-bit words from memory starting at adr"""
max_n = 0x20
while n > 0:
nread = (n, max_n)[n >= max_n]
_ = [io.wr16(x) for x in self.dap.rd_mem16(adr, nread)]
n -= nread
adr += nread * 2
def rdmem8(self, adr, n, io):
"""read n 8-bit words from memory starting at adr"""
# n = 0..0x3c (ok), 0x3d..0x40 (slow), >= 0x41 (fails)
max_n = 0x20
while n > 0:
nread = (n, max_n)[n >= max_n]
_ = [io.wr8(x) for x in self.dap.rd_mem8(adr, nread)]
n -= nread
adr += nread
def rdmem(self, adr, n, io):
"""read a buffer from memory starting at adr"""
if io.has_wr(32):
self.rdmem32(adr, n, io)
elif io.has_wr(16):
self.rdmem16(adr, n, io)
elif io.has_wr(8):
self.rdmem8(adr, n, io)
else:
assert False, 'bad buffer width'
def rd32(self, adr):
"""read 32 bit value from adr"""
return self.dap.rd_mem32(adr, 1)[0]
def rd16(self, adr):
"""read 16 bit value from adr"""
return self.dap.rd_mem16(adr, 1)[0]
def rd8(self, adr):
"""read 8 bit value from adr"""
return self.dap.rd_mem8(adr, 1)[0]
def __str__(self):
return str(self.dap)
#------------------------------------------------------------------------------