114
|
1 import struct, time
|
113
|
2 from usb import UsbContext
|
|
3
|
|
4 class STLinkException(Exception):
|
|
5 pass
|
|
6
|
|
7 def checkDevice(device):
|
|
8 ST_VID=0x0483
|
|
9 STLINK2_PID=0x3748
|
|
10 return device.VendorId == ST_VID and device.ProductId == STLINK2_PID
|
|
11
|
114
|
12 DFU_MODE, MASS_MODE, DEBUG_MODE = range(3)
|
|
13 # Commands:
|
|
14 GET_VERSION = 0xf1
|
|
15 DEBUG_COMMAND = 0xf2
|
|
16 DFU_COMMAND = 0xf3
|
|
17 GET_CURRENT_MODE = 0xf5
|
|
18
|
|
19 # dfu commands:
|
|
20 DFU_EXIT = 0x7
|
113
|
21
|
114
|
22 # debug commands:
|
|
23 DEBUG_ENTER = 0x20
|
|
24 DEBUG_EXIT = 0x21
|
|
25 DEBUG_ENTER_SWD = 0xa3
|
|
26 DEBUG_GETSTATUS = 0x01
|
|
27
|
|
28 JTAG_READDEBUG_32BIT = 0x36
|
|
29
|
|
30 # cortex M3
|
|
31 CM3_REG_CPUID = 0xE000ED00
|
|
32
|
|
33 class STLink:
|
113
|
34 def __init__(self):
|
|
35 self.context = UsbContext()
|
|
36 def open(self):
|
|
37 context = UsbContext()
|
|
38 stlink2s = list(filter(checkDevice, context.DeviceList))
|
|
39 if not stlink2s:
|
|
40 raise STLinkException('Could not find an ST link')
|
|
41 if len(stlink2s) > 1:
|
|
42 print('More then one stlink2 found, picking first one')
|
|
43 stlink2 = stlink2s[0]
|
114
|
44 self.devHandle = stlink2.open()
|
|
45 if self.devHandle.Configuration != 1:
|
|
46 self.devHandle.Configuration = 1
|
|
47 self.devHandle.claimInterface(0)
|
|
48 def close(self):
|
|
49 pass
|
113
|
50 def getCurrentMode(self):
|
114
|
51 cmd = bytearray(16)
|
|
52 cmd[0] = GET_CURRENT_MODE
|
|
53 reply = self.send_recv(cmd, 2) # Expect 2 bytes back
|
|
54 return reply[0]
|
113
|
55 CurrentMode = property(getCurrentMode)
|
114
|
56 @property
|
|
57 def CurrentModeString(self):
|
|
58 modes = {DFU_MODE: 'dfu', MASS_MODE: 'massmode', DEBUG_MODE:'debug'}
|
|
59 return modes[self.CurrentMode]
|
|
60 def exitDfuMode(self):
|
|
61 cmd = bytearray(16)
|
|
62 cmd[0] = DFU_COMMAND
|
|
63 cmd[1] = DFU_EXIT
|
|
64 self.send_recv(cmd)
|
|
65 def enterSwdMode(self):
|
|
66 cmd = bytearray(16)
|
|
67 cmd[0] = DEBUG_COMMAND
|
|
68 cmd[1] = DEBUG_ENTER
|
|
69 cmd[2] = DEBUG_ENTER_SWD
|
|
70 self.send_recv(cmd)
|
|
71 def exitDebugMode(self):
|
|
72 cmd = bytearray(16)
|
|
73 cmd[0] = DEBUG_COMMAND
|
|
74 cmd[1] = DEBUG_EXIT
|
|
75 self.send_recv(cmd)
|
|
76
|
|
77 def getVersion(self):
|
|
78 cmd = bytearray(16)
|
|
79 cmd[0] = GET_VERSION
|
|
80 data = self.send_recv(cmd, 6) # Expect 6 bytes back
|
|
81 # Parse 6 bytes into various versions:
|
|
82 b0, b1, b2, b3, b4, b5 = data
|
|
83 stlink_v = b0 >> 4
|
|
84 jtag_v = ((b0 & 0xf) << 2) | (b1 >> 6)
|
|
85 swim_v = b1 & 0x3f
|
|
86 vid = (b3 << 8) | b2
|
|
87 pid = (b5 << 8) | b4
|
|
88
|
|
89 return 'stlink={0} jtag={1} swim={2} vid:pid={3:04X}:{4:04X}'.format(\
|
|
90 stlink_v, jtag_v, swim_v, vid, pid)
|
|
91 Version = property(getVersion)
|
|
92
|
|
93 @property
|
|
94 def ChipId(self):
|
|
95 return self.read_debug32(0xE0042000)
|
|
96 @property
|
|
97 def CpuId(self):
|
|
98 u32 = self.read_debug32(CM3_REG_CPUID)
|
|
99 implementer_id = (u32 >> 24) & 0x7f
|
|
100 variant = (u32 >> 20) & 0xf
|
|
101 part = (u32 >> 4) & 0xfff
|
|
102 revision = u32 & 0xf
|
|
103 return implementer_id, variant, part, revision
|
|
104
|
|
105 def status(self):
|
|
106 cmd = bytearray(16)
|
|
107 cmd[0] = DEBUG_COMMAND
|
|
108 cmd[1] = DEBUG_GETSTATUS
|
|
109 reply = self.send_recv(cmd, 2)
|
|
110 return reply[0]
|
113
|
111
|
114
|
112 def step(self):
|
|
113 cmd = bytearray(16)
|
|
114 cmd[0] = DEBUG_COMMAND
|
|
115 cmd[1] = DEBUG_STEPCORE
|
|
116 self.send_recv(cmd, 2)
|
|
117 def run(self):
|
|
118 cmd = bytearray(16)
|
|
119 cmd[0] = DEBUG_COMMAND
|
|
120 cmd[1] = DEBUG_RUNCORE
|
|
121 self.send_recv(cmd, 2)
|
|
122
|
|
123 # Helper 1 functions:
|
|
124 def read_debug32(self, address):
|
|
125 cmd = bytearray(16)
|
|
126 cmd[0] = DEBUG_COMMAND
|
|
127 cmd[1] = JTAG_READDEBUG_32BIT
|
|
128 cmd[2:6] = struct.pack('<I', address) # pack into u32 little endian
|
|
129 reply = self.send_recv(cmd, 8)
|
|
130 return struct.unpack('<I', reply[4:8])[0]
|
|
131
|
|
132 # Helper 2 functions:
|
|
133 def send_recv(self, tx, rxsize=0):
|
|
134 """ Helper function that transmits and receives data. """
|
|
135 # TODO: we could use here the non-blocking libusb api.
|
|
136 tx = bytes(tx)
|
|
137 self.devHandle.bulkWrite(2, tx) # write to endpoint 2
|
|
138 if rxsize > 0:
|
|
139 return self.devHandle.bulkRead(1, rxsize) # read from endpoint 1
|
|
140
|
|
141 knownChipIds = {0x1: 'x'}
|
|
142
|
|
143 if __name__ == '__main__':
|
|
144 # Test program
|
|
145 sl = STLink()
|
|
146 sl.open()
|
|
147 print('version:', sl.Version)
|
|
148 print('mode before doing anything:', sl.CurrentModeString)
|
|
149 if sl.CurrentMode == DFU_MODE:
|
|
150 sl.exitDfuMode()
|
|
151 sl.enterSwdMode()
|
|
152 print('mode after entering swd mode:', sl.CurrentModeString)
|
|
153
|
|
154 i = sl.ChipId
|
|
155 if i in knownChipIds:
|
|
156 print('chip id: 0x{0:X} -> {1}'.format(i, knownChipIds[i]))
|
|
157 else:
|
|
158 print('chip id: 0x{0:X}'.format(i))
|
|
159 print('cpu: {0}'.format(sl.CpuId))
|
|
160
|
|
161 print('status: {0}'.format(sl.status()))
|
|
162
|
|
163 time.sleep(2.2)
|
|
164
|
|
165 sl.exitDebugMode()
|
|
166 print('mode at end:', sl.CurrentModeString)
|
|
167
|
|
168 sl.close()
|
|
169
|