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