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