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
|
|
141
|
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)
|
115
|
170
|
114
|
171 # Helper 1 functions:
|
115
|
172 def write_debug32(self, address, value):
|
|
173 cmd = bytearray(16)
|
|
174 cmd[0:2] = DEBUG_COMMAND, JTAG_WRITEDEBUG_32BIT
|
119
|
175 cmd[2:10] = struct.pack('<II', address, value)
|
115
|
176 self.send_recv(cmd, 2)
|
114
|
177 def read_debug32(self, address):
|
|
178 cmd = bytearray(16)
|
115
|
179 cmd[0:2] = DEBUG_COMMAND, JTAG_READDEBUG_32BIT
|
114
|
180 cmd[2:6] = struct.pack('<I', address) # pack into u32 little endian
|
|
181 reply = self.send_recv(cmd, 8)
|
|
182 return struct.unpack('<I', reply[4:8])[0]
|
115
|
183 def write_reg(self, reg, value):
|
|
184 cmd = bytearray(16)
|
|
185 cmd[0:3] = DEBUG_COMMAND, DEBUG_WRITEREG, reg
|
|
186 cmd[3:7] = struct.pack('<I', value)
|
|
187 r = self.send_recv(cmd, 2)
|
|
188 def read_reg(self, reg):
|
|
189 cmd = bytearray(16)
|
|
190 cmd[0:3] = DEBUG_COMMAND, DEBUG_READREG, reg
|
|
191 reply = self.send_recv(cmd, 4)
|
|
192 return struct.unpack('<I', reply)[0]
|
124
|
193 def read_all_regs(self):
|
|
194 cmd = bytearray(16)
|
|
195 cmd[0:2] = DEBUG_COMMAND, DEBUG_READALLREGS
|
|
196 reply = self.send_recv(cmd, 84)
|
|
197 fmt = '<' + 'I' * 21 # unpack 21 register values
|
|
198 return list(struct.unpack(fmt, reply))
|
116
|
199 def write_mem32(self, address, content):
|
|
200 assert len(content) % 4 == 0
|
|
201 cmd = bytearray(16)
|
|
202 cmd[0:2] = DEBUG_COMMAND, DEBUG_WRITEMEM_32BIT
|
119
|
203 cmd[2:8] = struct.pack('<IH', address, len(content))
|
116
|
204 self.send_recv(cmd)
|
|
205 self.send_recv(content)
|
115
|
206 def read_mem32(self, address, length):
|
|
207 assert length % 4 == 0
|
|
208 cmd = bytearray(16)
|
|
209 cmd[0:2] = DEBUG_COMMAND, DEBUG_READMEM_32BIT
|
119
|
210 cmd[2:8] = struct.pack('<IH', address, length)
|
115
|
211 reply = self.send_recv(cmd, length) # expect memory back!
|
|
212 return reply
|
114
|
213
|
|
214 # Helper 2 functions:
|
|
215 def send_recv(self, tx, rxsize=0):
|
115
|
216 """ Helper function that transmits and receives data in bulk mode. """
|
114
|
217 # TODO: we could use here the non-blocking libusb api.
|
|
218 tx = bytes(tx)
|
116
|
219 #assert len(tx) == 16
|
114
|
220 self.devHandle.bulkWrite(2, tx) # write to endpoint 2
|
|
221 if rxsize > 0:
|
|
222 return self.devHandle.bulkRead(1, rxsize) # read from endpoint 1
|
|
223
|
|
224 if __name__ == '__main__':
|
|
225 # Test program
|
128
|
226 sl = STLink2()
|
114
|
227 sl.open()
|
132
|
228 sl.reset()
|
114
|
229 print('version:', sl.Version)
|
|
230 print('mode before doing anything:', sl.CurrentModeString)
|
|
231 if sl.CurrentMode == DFU_MODE:
|
|
232 sl.exitDfuMode()
|
|
233 sl.enterSwdMode()
|
|
234 print('mode after entering swd mode:', sl.CurrentModeString)
|
|
235
|
|
236 i = sl.ChipId
|
129
|
237 print('chip id: 0x{0:X}'.format(i))
|
114
|
238 print('cpu: {0}'.format(sl.CpuId))
|
|
239
|
117
|
240 print('status: {0}'.format(sl.StatusString))
|
114
|
241
|
115
|
242 # test registers:
|
124
|
243 sl.write_reg(0, 0xdeadbeef)
|
|
244 sl.write_reg(1, 0xcafebabe)
|
|
245 sl.write_reg(2, 0xc0ffee)
|
115
|
246 sl.write_reg(3, 0x1337)
|
124
|
247 sl.write_reg(5, 0x1332)
|
117
|
248 sl.write_reg(6, 0x12345)
|
115
|
249 assert sl.read_reg(3) == 0x1337
|
124
|
250 assert sl.read_reg(5) == 0x1332
|
117
|
251 assert sl.read_reg(6) == 0x12345
|
124
|
252 regs = sl.read_all_regs()
|
|
253 for i in range(len(regs)):
|
|
254 print('R{0}=0x{1:X}'.format(i, regs[i]))
|
114
|
255
|
|
256 sl.exitDebugMode()
|
|
257 print('mode at end:', sl.CurrentModeString)
|
|
258
|
|
259 sl.close()
|
119
|
260 print('Test succes!')
|
114
|
261
|