114
|
1 import struct, time
|
129
|
2 from usb import UsbContext, UsbDevice
|
128
|
3 from devices import Interface, STLinkException, registerInterface
|
113
|
4
|
115
|
5 ST_VID, STLINK2_PID = 0x0483, 0x3748
|
|
6
|
113
|
7 def checkDevice(device):
|
|
8 return device.VendorId == ST_VID and device.ProductId == STLINK2_PID
|
|
9
|
115
|
10 DFU_MODE, MASS_MODE, DEBUG_MODE = 0, 1, 2
|
117
|
11
|
|
12 CORE_RUNNING = 0x80
|
|
13 CORE_HALTED = 0x81
|
|
14
|
114
|
15 # Commands:
|
|
16 GET_VERSION = 0xf1
|
|
17 DEBUG_COMMAND = 0xf2
|
|
18 DFU_COMMAND = 0xf3
|
|
19 GET_CURRENT_MODE = 0xf5
|
|
20
|
|
21 # dfu commands:
|
|
22 DFU_EXIT = 0x7
|
113
|
23
|
114
|
24 # debug commands:
|
|
25 DEBUG_ENTER = 0x20
|
|
26 DEBUG_EXIT = 0x21
|
|
27 DEBUG_ENTER_SWD = 0xa3
|
|
28 DEBUG_GETSTATUS = 0x01
|
143
|
29 DEBUG_FORCEDEBUG = 0x02
|
116
|
30 DEBUG_RESETSYS = 0x03
|
124
|
31 DEBUG_READALLREGS = 0x04
|
115
|
32 DEBUG_READREG = 0x5
|
|
33 DEBUG_WRITEREG = 0x6
|
|
34 DEBUG_READMEM_32BIT = 0x7
|
|
35 DEBUG_WRITEMEM_32BIT = 0x8
|
117
|
36 DEBUG_RUNCORE = 0x9
|
|
37 DEBUG_STEPCORE = 0xa
|
114
|
38
|
115
|
39 JTAG_WRITEDEBUG_32BIT = 0x35
|
114
|
40 JTAG_READDEBUG_32BIT = 0x36
|
|
41
|
|
42 # cortex M3
|
|
43 CM3_REG_CPUID = 0xE000ED00
|
|
44
|
128
|
45 @registerInterface((ST_VID, STLINK2_PID))
|
|
46 class STLink2(Interface):
|
|
47 """ STlink2 interface implementation. """
|
119
|
48 def __init__(self, stlink2=None):
|
129
|
49 self.devHandle = None
|
119
|
50 if not stlink2:
|
|
51 context = UsbContext()
|
|
52 stlink2s = list(filter(checkDevice, context.DeviceList))
|
|
53 if not stlink2s:
|
128
|
54 raise STLinkException('Could not find an ST link 2 interface')
|
119
|
55 if len(stlink2s) > 1:
|
|
56 print('More then one stlink2 found, picking first one')
|
|
57 stlink2 = stlink2s[0]
|
129
|
58 assert isinstance(stlink2, UsbDevice) # Nifty type checking
|
124
|
59 assert checkDevice(stlink2)
|
119
|
60 self.stlink2 = stlink2
|
130
|
61 def __del__(self):
|
|
62 if self.IsOpen:
|
|
63 if self.CurrentMode == DEBUG_MODE:
|
|
64 self.exitDebugMode()
|
|
65 self.close()
|
129
|
66 def __str__(self):
|
130
|
67 if self.IsOpen:
|
129
|
68 return 'STlink2 device version {0}'.format(self.Version)
|
|
69 else:
|
|
70 return 'STlink2 device'
|
113
|
71 def open(self):
|
130
|
72 if self.IsOpen:
|
129
|
73 return
|
119
|
74 self.devHandle = self.stlink2.open()
|
114
|
75 if self.devHandle.Configuration != 1:
|
|
76 self.devHandle.Configuration = 1
|
|
77 self.devHandle.claimInterface(0)
|
116
|
78
|
|
79 # First initialization:
|
|
80 if self.CurrentMode == DFU_MODE:
|
|
81 self.exitDfuMode()
|
|
82 if self.CurrentMode != DEBUG_MODE:
|
|
83 self.enterSwdMode()
|
132
|
84 #self.reset()
|
114
|
85 def close(self):
|
130
|
86 if self.IsOpen:
|
|
87 self.devHandle.close()
|
|
88 self.devHandle = None
|
|
89 @property
|
|
90 def IsOpen(self):
|
|
91 return self.devHandle != None
|
115
|
92 # modes:
|
113
|
93 def getCurrentMode(self):
|
114
|
94 cmd = bytearray(16)
|
|
95 cmd[0] = GET_CURRENT_MODE
|
|
96 reply = self.send_recv(cmd, 2) # Expect 2 bytes back
|
|
97 return reply[0]
|
113
|
98 CurrentMode = property(getCurrentMode)
|
114
|
99 @property
|
|
100 def CurrentModeString(self):
|
|
101 modes = {DFU_MODE: 'dfu', MASS_MODE: 'massmode', DEBUG_MODE:'debug'}
|
|
102 return modes[self.CurrentMode]
|
|
103 def exitDfuMode(self):
|
|
104 cmd = bytearray(16)
|
115
|
105 cmd[0:2] = DFU_COMMAND, DFU_EXIT
|
114
|
106 self.send_recv(cmd)
|
|
107 def enterSwdMode(self):
|
|
108 cmd = bytearray(16)
|
115
|
109 cmd[0:3] = DEBUG_COMMAND, DEBUG_ENTER, DEBUG_ENTER_SWD
|
114
|
110 self.send_recv(cmd)
|
|
111 def exitDebugMode(self):
|
|
112 cmd = bytearray(16)
|
115
|
113 cmd[0:2] = DEBUG_COMMAND, DEBUG_EXIT
|
114
|
114 self.send_recv(cmd)
|
|
115
|
|
116 def getVersion(self):
|
|
117 cmd = bytearray(16)
|
|
118 cmd[0] = GET_VERSION
|
|
119 data = self.send_recv(cmd, 6) # Expect 6 bytes back
|
|
120 # Parse 6 bytes into various versions:
|
|
121 b0, b1, b2, b3, b4, b5 = data
|
|
122 stlink_v = b0 >> 4
|
|
123 jtag_v = ((b0 & 0xf) << 2) | (b1 >> 6)
|
|
124 swim_v = b1 & 0x3f
|
|
125 vid = (b3 << 8) | b2
|
|
126 pid = (b5 << 8) | b4
|
|
127 return 'stlink={0} jtag={1} swim={2} vid:pid={3:04X}:{4:04X}'.format(\
|
|
128 stlink_v, jtag_v, swim_v, vid, pid)
|
|
129 Version = property(getVersion)
|
|
130
|
|
131 @property
|
|
132 def ChipId(self):
|
|
133 return self.read_debug32(0xE0042000)
|
|
134 @property
|
|
135 def CpuId(self):
|
|
136 u32 = self.read_debug32(CM3_REG_CPUID)
|
|
137 implementer_id = (u32 >> 24) & 0x7f
|
|
138 variant = (u32 >> 20) & 0xf
|
|
139 part = (u32 >> 4) & 0xfff
|
|
140 revision = u32 & 0xf
|
|
141 return implementer_id, variant, part, revision
|
117
|
142 def getStatus(self):
|
114
|
143 cmd = bytearray(16)
|
115
|
144 cmd[0:2] = DEBUG_COMMAND, DEBUG_GETSTATUS
|
114
|
145 reply = self.send_recv(cmd, 2)
|
|
146 return reply[0]
|
117
|
147 Status = property(getStatus)
|
|
148 @property
|
|
149 def StatusString(self):
|
|
150 s = self.Status
|
|
151 statii = {CORE_RUNNING: 'CORE RUNNING', CORE_HALTED: 'CORE HALTED'}
|
|
152 if s in statii:
|
|
153 return statii[s]
|
132
|
154 return 'Unknown status'
|
117
|
155
|
116
|
156 def reset(self):
|
|
157 cmd = bytearray(16)
|
|
158 cmd[0:2] = DEBUG_COMMAND, DEBUG_RESETSYS
|
|
159 self.send_recv(cmd, 2)
|
113
|
160
|
115
|
161 # debug commands:
|
114
|
162 def step(self):
|
|
163 cmd = bytearray(16)
|
115
|
164 cmd[0:2] = DEBUG_COMMAND, DEBUG_STEPCORE
|
114
|
165 self.send_recv(cmd, 2)
|
|
166 def run(self):
|
|
167 cmd = bytearray(16)
|
115
|
168 cmd[0:2] = DEBUG_COMMAND, DEBUG_RUNCORE
|
114
|
169 self.send_recv(cmd, 2)
|
138
|
170 def halt(self):
|
143
|
171 cmd = bytearray(16)
|
|
172 cmd[0:2] = DEBUG_COMMAND, DEBUG_FORCEDEBUG
|
|
173 self.send_recv(cmd, 2)
|
115
|
174
|
114
|
175 # Helper 1 functions:
|
115
|
176 def write_debug32(self, address, value):
|
|
177 cmd = bytearray(16)
|
|
178 cmd[0:2] = DEBUG_COMMAND, JTAG_WRITEDEBUG_32BIT
|
119
|
179 cmd[2:10] = struct.pack('<II', address, value)
|
115
|
180 self.send_recv(cmd, 2)
|
114
|
181 def read_debug32(self, address):
|
|
182 cmd = bytearray(16)
|
115
|
183 cmd[0:2] = DEBUG_COMMAND, JTAG_READDEBUG_32BIT
|
114
|
184 cmd[2:6] = struct.pack('<I', address) # pack into u32 little endian
|
|
185 reply = self.send_recv(cmd, 8)
|
|
186 return struct.unpack('<I', reply[4:8])[0]
|
115
|
187 def write_reg(self, reg, value):
|
|
188 cmd = bytearray(16)
|
|
189 cmd[0:3] = DEBUG_COMMAND, DEBUG_WRITEREG, reg
|
|
190 cmd[3:7] = struct.pack('<I', value)
|
|
191 r = self.send_recv(cmd, 2)
|
|
192 def read_reg(self, reg):
|
|
193 cmd = bytearray(16)
|
|
194 cmd[0:3] = DEBUG_COMMAND, DEBUG_READREG, reg
|
|
195 reply = self.send_recv(cmd, 4)
|
|
196 return struct.unpack('<I', reply)[0]
|
124
|
197 def read_all_regs(self):
|
|
198 cmd = bytearray(16)
|
|
199 cmd[0:2] = DEBUG_COMMAND, DEBUG_READALLREGS
|
|
200 reply = self.send_recv(cmd, 84)
|
|
201 fmt = '<' + 'I' * 21 # unpack 21 register values
|
|
202 return list(struct.unpack(fmt, reply))
|
116
|
203 def write_mem32(self, address, content):
|
|
204 assert len(content) % 4 == 0
|
|
205 cmd = bytearray(16)
|
|
206 cmd[0:2] = DEBUG_COMMAND, DEBUG_WRITEMEM_32BIT
|
119
|
207 cmd[2:8] = struct.pack('<IH', address, len(content))
|
116
|
208 self.send_recv(cmd)
|
|
209 self.send_recv(content)
|
115
|
210 def read_mem32(self, address, length):
|
|
211 assert length % 4 == 0
|
|
212 cmd = bytearray(16)
|
|
213 cmd[0:2] = DEBUG_COMMAND, DEBUG_READMEM_32BIT
|
119
|
214 cmd[2:8] = struct.pack('<IH', address, length)
|
115
|
215 reply = self.send_recv(cmd, length) # expect memory back!
|
|
216 return reply
|
114
|
217
|
|
218 # Helper 2 functions:
|
|
219 def send_recv(self, tx, rxsize=0):
|
115
|
220 """ Helper function that transmits and receives data in bulk mode. """
|
114
|
221 # TODO: we could use here the non-blocking libusb api.
|
|
222 tx = bytes(tx)
|
116
|
223 #assert len(tx) == 16
|
114
|
224 self.devHandle.bulkWrite(2, tx) # write to endpoint 2
|
|
225 if rxsize > 0:
|
|
226 return self.devHandle.bulkRead(1, rxsize) # read from endpoint 1
|
|
227
|
|
228 if __name__ == '__main__':
|
|
229 # Test program
|
128
|
230 sl = STLink2()
|
114
|
231 sl.open()
|
132
|
232 sl.reset()
|
114
|
233 print('version:', sl.Version)
|
|
234 print('mode before doing anything:', sl.CurrentModeString)
|
|
235 if sl.CurrentMode == DFU_MODE:
|
|
236 sl.exitDfuMode()
|
|
237 sl.enterSwdMode()
|
|
238 print('mode after entering swd mode:', sl.CurrentModeString)
|
|
239
|
|
240 i = sl.ChipId
|
129
|
241 print('chip id: 0x{0:X}'.format(i))
|
114
|
242 print('cpu: {0}'.format(sl.CpuId))
|
|
243
|
117
|
244 print('status: {0}'.format(sl.StatusString))
|
114
|
245
|
115
|
246 # test registers:
|
124
|
247 sl.write_reg(0, 0xdeadbeef)
|
|
248 sl.write_reg(1, 0xcafebabe)
|
|
249 sl.write_reg(2, 0xc0ffee)
|
115
|
250 sl.write_reg(3, 0x1337)
|
124
|
251 sl.write_reg(5, 0x1332)
|
117
|
252 sl.write_reg(6, 0x12345)
|
115
|
253 assert sl.read_reg(3) == 0x1337
|
124
|
254 assert sl.read_reg(5) == 0x1332
|
117
|
255 assert sl.read_reg(6) == 0x12345
|
124
|
256 regs = sl.read_all_regs()
|
|
257 for i in range(len(regs)):
|
|
258 print('R{0}=0x{1:X}'.format(i, regs[i]))
|
114
|
259
|
|
260 sl.exitDebugMode()
|
|
261 print('mode at end:', sl.CurrentModeString)
|
|
262
|
|
263 sl.close()
|
119
|
264 print('Test succes!')
|
114
|
265
|