annotate python/stlink.py @ 132:205578c96a79

Moved hexview to seperate class
author Windel Bouwman
date Sun, 20 Jan 2013 17:45:45 +0100
parents 654093a9a1e3
children 14e739ed03ab
rev   line source
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
1 import struct, time
129
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 128
diff changeset
2 from usb import UsbContext, UsbDevice
128
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents: 124
diff changeset
3 from devices import Interface, STLinkException, registerInterface
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
4
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
5 ST_VID, STLINK2_PID = 0x0483, 0x3748
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
6
113
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 return device.VendorId == ST_VID and device.ProductId == STLINK2_PID
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
9
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
10 DFU_MODE, MASS_MODE, DEBUG_MODE = 0, 1, 2
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
11
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
12 CORE_RUNNING = 0x80
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
13 CORE_HALTED = 0x81
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
14
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
15 # Commands:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
16 GET_VERSION = 0xf1
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
17 DEBUG_COMMAND = 0xf2
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
18 DFU_COMMAND = 0xf3
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
19 GET_CURRENT_MODE = 0xf5
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
20
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
21 # dfu commands:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
22 DFU_EXIT = 0x7
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
23
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
24 # debug commands:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
25 DEBUG_ENTER = 0x20
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
26 DEBUG_EXIT = 0x21
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
27 DEBUG_ENTER_SWD = 0xa3
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
28 DEBUG_GETSTATUS = 0x01
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
29 DEBUG_RESETSYS = 0x03
124
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 123
diff changeset
30 DEBUG_READALLREGS = 0x04
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
31 DEBUG_READREG = 0x5
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
32 DEBUG_WRITEREG = 0x6
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
33 DEBUG_READMEM_32BIT = 0x7
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
34 DEBUG_WRITEMEM_32BIT = 0x8
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
35 DEBUG_RUNCORE = 0x9
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
36 DEBUG_STEPCORE = 0xa
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
37
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
38 JTAG_WRITEDEBUG_32BIT = 0x35
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
39 JTAG_READDEBUG_32BIT = 0x36
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
40
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
41 # cortex M3
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
42 CM3_REG_CPUID = 0xE000ED00
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
43
128
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents: 124
diff changeset
44 @registerInterface((ST_VID, STLINK2_PID))
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents: 124
diff changeset
45 class STLink2(Interface):
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents: 124
diff changeset
46 """ STlink2 interface implementation. """
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
47 def __init__(self, stlink2=None):
129
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 128
diff changeset
48 self.devHandle = None
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
49 if not stlink2:
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
50 context = UsbContext()
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
51 stlink2s = list(filter(checkDevice, context.DeviceList))
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
52 if not stlink2s:
128
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents: 124
diff changeset
53 raise STLinkException('Could not find an ST link 2 interface')
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
54 if len(stlink2s) > 1:
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
55 print('More then one stlink2 found, picking first one')
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
56 stlink2 = stlink2s[0]
129
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 128
diff changeset
57 assert isinstance(stlink2, UsbDevice) # Nifty type checking
124
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 123
diff changeset
58 assert checkDevice(stlink2)
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
59 self.stlink2 = stlink2
130
654093a9a1e3 Added icons, improved device explorer
Windel Bouwman
parents: 129
diff changeset
60 def __del__(self):
654093a9a1e3 Added icons, improved device explorer
Windel Bouwman
parents: 129
diff changeset
61 if self.IsOpen:
654093a9a1e3 Added icons, improved device explorer
Windel Bouwman
parents: 129
diff changeset
62 if self.CurrentMode == DEBUG_MODE:
654093a9a1e3 Added icons, improved device explorer
Windel Bouwman
parents: 129
diff changeset
63 self.exitDebugMode()
654093a9a1e3 Added icons, improved device explorer
Windel Bouwman
parents: 129
diff changeset
64 self.close()
129
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 128
diff changeset
65 def __str__(self):
130
654093a9a1e3 Added icons, improved device explorer
Windel Bouwman
parents: 129
diff changeset
66 if self.IsOpen:
129
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 128
diff changeset
67 return 'STlink2 device version {0}'.format(self.Version)
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 128
diff changeset
68 else:
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 128
diff changeset
69 return 'STlink2 device'
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
70 def open(self):
130
654093a9a1e3 Added icons, improved device explorer
Windel Bouwman
parents: 129
diff changeset
71 if self.IsOpen:
129
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 128
diff changeset
72 return
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
73 self.devHandle = self.stlink2.open()
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
74 if self.devHandle.Configuration != 1:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
75 self.devHandle.Configuration = 1
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
76 self.devHandle.claimInterface(0)
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
77
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
78 # First initialization:
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
79 if self.CurrentMode == DFU_MODE:
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
80 self.exitDfuMode()
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
81 if self.CurrentMode != DEBUG_MODE:
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
82 self.enterSwdMode()
132
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents: 130
diff changeset
83 #self.reset()
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
84 def close(self):
130
654093a9a1e3 Added icons, improved device explorer
Windel Bouwman
parents: 129
diff changeset
85 if self.IsOpen:
654093a9a1e3 Added icons, improved device explorer
Windel Bouwman
parents: 129
diff changeset
86 self.devHandle.close()
654093a9a1e3 Added icons, improved device explorer
Windel Bouwman
parents: 129
diff changeset
87 self.devHandle = None
654093a9a1e3 Added icons, improved device explorer
Windel Bouwman
parents: 129
diff changeset
88 @property
654093a9a1e3 Added icons, improved device explorer
Windel Bouwman
parents: 129
diff changeset
89 def IsOpen(self):
654093a9a1e3 Added icons, improved device explorer
Windel Bouwman
parents: 129
diff changeset
90 return self.devHandle != None
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
91 # modes:
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
92 def getCurrentMode(self):
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
93 cmd = bytearray(16)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
94 cmd[0] = GET_CURRENT_MODE
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
95 reply = self.send_recv(cmd, 2) # Expect 2 bytes back
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
96 return reply[0]
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
97 CurrentMode = property(getCurrentMode)
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
98 @property
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
99 def CurrentModeString(self):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
100 modes = {DFU_MODE: 'dfu', MASS_MODE: 'massmode', DEBUG_MODE:'debug'}
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
101 return modes[self.CurrentMode]
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
102 def exitDfuMode(self):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
103 cmd = bytearray(16)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
104 cmd[0:2] = DFU_COMMAND, DFU_EXIT
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
105 self.send_recv(cmd)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
106 def enterSwdMode(self):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
107 cmd = bytearray(16)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
108 cmd[0:3] = DEBUG_COMMAND, DEBUG_ENTER, DEBUG_ENTER_SWD
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
109 self.send_recv(cmd)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
110 def exitDebugMode(self):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
111 cmd = bytearray(16)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
112 cmd[0:2] = DEBUG_COMMAND, DEBUG_EXIT
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
113 self.send_recv(cmd)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
115 def getVersion(self):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
116 cmd = bytearray(16)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
117 cmd[0] = GET_VERSION
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
118 data = self.send_recv(cmd, 6) # Expect 6 bytes back
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
119 # Parse 6 bytes into various versions:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
120 b0, b1, b2, b3, b4, b5 = data
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
121 stlink_v = b0 >> 4
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
122 jtag_v = ((b0 & 0xf) << 2) | (b1 >> 6)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
123 swim_v = b1 & 0x3f
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
124 vid = (b3 << 8) | b2
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
125 pid = (b5 << 8) | b4
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
126 return 'stlink={0} jtag={1} swim={2} vid:pid={3:04X}:{4:04X}'.format(\
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
127 stlink_v, jtag_v, swim_v, vid, pid)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
128 Version = property(getVersion)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
129
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
130 @property
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
131 def ChipId(self):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
132 return self.read_debug32(0xE0042000)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
133 @property
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
134 def CpuId(self):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
135 u32 = self.read_debug32(CM3_REG_CPUID)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
136 implementer_id = (u32 >> 24) & 0x7f
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
137 variant = (u32 >> 20) & 0xf
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
138 part = (u32 >> 4) & 0xfff
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
139 revision = u32 & 0xf
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
140 return implementer_id, variant, part, revision
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
141
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
142 def getStatus(self):
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
143 cmd = bytearray(16)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
144 cmd[0:2] = DEBUG_COMMAND, DEBUG_GETSTATUS
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
145 reply = self.send_recv(cmd, 2)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
146 return reply[0]
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
147 Status = property(getStatus)
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
148 @property
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
149 def StatusString(self):
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
150 s = self.Status
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
151 statii = {CORE_RUNNING: 'CORE RUNNING', CORE_HALTED: 'CORE HALTED'}
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
152 if s in statii:
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
153 return statii[s]
132
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents: 130
diff changeset
154 return 'Unknown status'
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
155
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
156 def reset(self):
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
157 cmd = bytearray(16)
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
158 cmd[0:2] = DEBUG_COMMAND, DEBUG_RESETSYS
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
159 self.send_recv(cmd, 2)
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
160
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
161 # debug commands:
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
162 def step(self):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
163 cmd = bytearray(16)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
164 cmd[0:2] = DEBUG_COMMAND, DEBUG_STEPCORE
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
165 self.send_recv(cmd, 2)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
166 def run(self):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
167 cmd = bytearray(16)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
168 cmd[0:2] = DEBUG_COMMAND, DEBUG_RUNCORE
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
169 self.send_recv(cmd, 2)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
170
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
171 # Helper 1 functions:
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
172 def write_debug32(self, address, value):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
173 cmd = bytearray(16)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
174 cmd[0:2] = DEBUG_COMMAND, JTAG_WRITEDEBUG_32BIT
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
175 cmd[2:10] = struct.pack('<II', address, value)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
176 self.send_recv(cmd, 2)
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
177 def read_debug32(self, address):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
178 cmd = bytearray(16)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
179 cmd[0:2] = DEBUG_COMMAND, JTAG_READDEBUG_32BIT
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
180 cmd[2:6] = struct.pack('<I', address) # pack into u32 little endian
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
181 reply = self.send_recv(cmd, 8)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
182 return struct.unpack('<I', reply[4:8])[0]
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
183 def write_reg(self, reg, value):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
184 cmd = bytearray(16)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
185 cmd[0:3] = DEBUG_COMMAND, DEBUG_WRITEREG, reg
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
186 cmd[3:7] = struct.pack('<I', value)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
187 r = self.send_recv(cmd, 2)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
188 def read_reg(self, reg):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
189 cmd = bytearray(16)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
190 cmd[0:3] = DEBUG_COMMAND, DEBUG_READREG, reg
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
191 reply = self.send_recv(cmd, 4)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
192 return struct.unpack('<I', reply)[0]
124
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 123
diff changeset
193 def read_all_regs(self):
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 123
diff changeset
194 cmd = bytearray(16)
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 123
diff changeset
195 cmd[0:2] = DEBUG_COMMAND, DEBUG_READALLREGS
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 123
diff changeset
196 reply = self.send_recv(cmd, 84)
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 123
diff changeset
197 fmt = '<' + 'I' * 21 # unpack 21 register values
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 123
diff changeset
198 return list(struct.unpack(fmt, reply))
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
199 def write_mem32(self, address, content):
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
200 assert len(content) % 4 == 0
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
201 cmd = bytearray(16)
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
202 cmd[0:2] = DEBUG_COMMAND, DEBUG_WRITEMEM_32BIT
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
203 cmd[2:8] = struct.pack('<IH', address, len(content))
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
204 self.send_recv(cmd)
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
205 self.send_recv(content)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
206 def read_mem32(self, address, length):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
207 assert length % 4 == 0
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
208 cmd = bytearray(16)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
209 cmd[0:2] = DEBUG_COMMAND, DEBUG_READMEM_32BIT
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
210 cmd[2:8] = struct.pack('<IH', address, length)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
211 reply = self.send_recv(cmd, length) # expect memory back!
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
212 return reply
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
213
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
214 # Helper 2 functions:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
215 def send_recv(self, tx, rxsize=0):
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
216 """ Helper function that transmits and receives data in bulk mode. """
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
217 # TODO: we could use here the non-blocking libusb api.
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
218 tx = bytes(tx)
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
219 #assert len(tx) == 16
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
220 self.devHandle.bulkWrite(2, tx) # write to endpoint 2
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
221 if rxsize > 0:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
222 return self.devHandle.bulkRead(1, rxsize) # read from endpoint 1
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
223
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
224 if __name__ == '__main__':
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
225 # Test program
128
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents: 124
diff changeset
226 sl = STLink2()
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
227 sl.open()
132
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents: 130
diff changeset
228 sl.reset()
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
229 print('version:', sl.Version)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
230 print('mode before doing anything:', sl.CurrentModeString)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
231 if sl.CurrentMode == DFU_MODE:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
232 sl.exitDfuMode()
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
233 sl.enterSwdMode()
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
234 print('mode after entering swd mode:', sl.CurrentModeString)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
235
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
236 i = sl.ChipId
129
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 128
diff changeset
237 print('chip id: 0x{0:X}'.format(i))
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
238 print('cpu: {0}'.format(sl.CpuId))
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
239
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
240 print('status: {0}'.format(sl.StatusString))
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
241
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
242 # test registers:
124
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 123
diff changeset
243 sl.write_reg(0, 0xdeadbeef)
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 123
diff changeset
244 sl.write_reg(1, 0xcafebabe)
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 123
diff changeset
245 sl.write_reg(2, 0xc0ffee)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
246 sl.write_reg(3, 0x1337)
124
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 123
diff changeset
247 sl.write_reg(5, 0x1332)
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
248 sl.write_reg(6, 0x12345)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
249 assert sl.read_reg(3) == 0x1337
124
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 123
diff changeset
250 assert sl.read_reg(5) == 0x1332
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
251 assert sl.read_reg(6) == 0x12345
124
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 123
diff changeset
252 regs = sl.read_all_regs()
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 123
diff changeset
253 for i in range(len(regs)):
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 123
diff changeset
254 print('R{0}=0x{1:X}'.format(i, regs[i]))
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
255
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
256 sl.exitDebugMode()
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
257 print('mode at end:', sl.CurrentModeString)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
258
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
259 sl.close()
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
260 print('Test succes!')
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
261