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