annotate python/stlink.py @ 114:f42268da614f

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