Mercurial > lcfOS
comparison python/stlink.py @ 115:92b2bf0da1ec
Added erase and verify
author | Windel Bouwman |
---|---|
date | Sun, 06 Jan 2013 18:13:17 +0100 |
parents | f42268da614f |
children | 90b03bc018cf |
comparison
equal
deleted
inserted
replaced
114:f42268da614f | 115:92b2bf0da1ec |
---|---|
2 from usb import UsbContext | 2 from usb import UsbContext |
3 | 3 |
4 class STLinkException(Exception): | 4 class STLinkException(Exception): |
5 pass | 5 pass |
6 | 6 |
7 ST_VID, STLINK2_PID = 0x0483, 0x3748 | |
8 | |
7 def checkDevice(device): | 9 def checkDevice(device): |
8 ST_VID=0x0483 | |
9 STLINK2_PID=0x3748 | |
10 return device.VendorId == ST_VID and device.ProductId == STLINK2_PID | 10 return device.VendorId == ST_VID and device.ProductId == STLINK2_PID |
11 | 11 |
12 DFU_MODE, MASS_MODE, DEBUG_MODE = range(3) | 12 DFU_MODE, MASS_MODE, DEBUG_MODE = 0, 1, 2 |
13 # Commands: | 13 # Commands: |
14 GET_VERSION = 0xf1 | 14 GET_VERSION = 0xf1 |
15 DEBUG_COMMAND = 0xf2 | 15 DEBUG_COMMAND = 0xf2 |
16 DFU_COMMAND = 0xf3 | 16 DFU_COMMAND = 0xf3 |
17 GET_CURRENT_MODE = 0xf5 | 17 GET_CURRENT_MODE = 0xf5 |
22 # debug commands: | 22 # debug commands: |
23 DEBUG_ENTER = 0x20 | 23 DEBUG_ENTER = 0x20 |
24 DEBUG_EXIT = 0x21 | 24 DEBUG_EXIT = 0x21 |
25 DEBUG_ENTER_SWD = 0xa3 | 25 DEBUG_ENTER_SWD = 0xa3 |
26 DEBUG_GETSTATUS = 0x01 | 26 DEBUG_GETSTATUS = 0x01 |
27 | 27 DEBUG_READREG = 0x5 |
28 DEBUG_WRITEREG = 0x6 | |
29 DEBUG_READMEM_32BIT = 0x7 | |
30 DEBUG_WRITEMEM_32BIT = 0x8 | |
31 | |
32 JTAG_WRITEDEBUG_32BIT = 0x35 | |
28 JTAG_READDEBUG_32BIT = 0x36 | 33 JTAG_READDEBUG_32BIT = 0x36 |
29 | 34 |
30 # cortex M3 | 35 # cortex M3 |
31 CM3_REG_CPUID = 0xE000ED00 | 36 CM3_REG_CPUID = 0xE000ED00 |
37 | |
38 # F4 specifics: | |
39 STM32_FLASH_BASE = 0x08000000 | |
40 STM32_SRAM_BASE = 0x20000000 | |
41 | |
42 # flash registers: | |
43 FLASH_F4_REGS_ADDR = 0x400223c0 | |
44 | |
45 FLASH_F4_SR = FLASH_F4_REGS_ADDR + 0x0c | |
46 FLASH_F4_CR = FLASH_F4_REGS_ADDR + 0x10 | |
47 | |
48 FLASH_F4_CR_START = 16 | |
49 FLASH_F4_CR_LOCK = 31 | |
50 FLASH_F4_CR_SER = 1 | |
51 FLASH_F4_CR_SNB = 3 | |
52 FLASH_F4_CR_SNB_MASK = 0x38 | |
53 FLASH_F4_SR_BSY = 16 | |
54 | |
55 def calculate_F4_sector(address): | |
56 """ | |
57 from 0x8000000 to 0x80FFFFF | |
58 4 sectors of 0x4000 (16 kB) | |
59 1 sector of 0x10000 (64 kB) | |
60 7 of 0x20000 (128 kB) | |
61 """ | |
62 sectorsizes = [0x4000] * 4 + [0x10000] + [0x20000] * 7 | |
63 sectorstarts = [] | |
64 a = STM32_FLASH_BASE | |
65 for sectorsize in sectorsizes: | |
66 sectorstarts.append(a) | |
67 a += sectorsize | |
68 # linear search: | |
69 sec = 0 | |
70 while sec < len(sectorsizes) and address >= sectorstarts[sec]: | |
71 sec += 1 | |
72 sec -= 1 # one back. | |
73 return sec, sectorsizes[sec] | |
74 | |
75 def calcSectors(address, size): | |
76 off = 0 | |
77 sectors = [] | |
78 while off < size: | |
79 sectornum, sectorsize = calculate_F4_sector(address + off) | |
80 #print('num: {0} size: {1:X} offset: {2}'.format(sectornum, sectorsize, off)) | |
81 sectors.append((sectornum, sectorsize)) | |
82 off += sectorsize | |
83 return sectors | |
32 | 84 |
33 class STLink: | 85 class STLink: |
34 def __init__(self): | 86 def __init__(self): |
35 self.context = UsbContext() | 87 self.context = UsbContext() |
36 def open(self): | 88 def open(self): |
45 if self.devHandle.Configuration != 1: | 97 if self.devHandle.Configuration != 1: |
46 self.devHandle.Configuration = 1 | 98 self.devHandle.Configuration = 1 |
47 self.devHandle.claimInterface(0) | 99 self.devHandle.claimInterface(0) |
48 def close(self): | 100 def close(self): |
49 pass | 101 pass |
102 | |
103 # modes: | |
50 def getCurrentMode(self): | 104 def getCurrentMode(self): |
51 cmd = bytearray(16) | 105 cmd = bytearray(16) |
52 cmd[0] = GET_CURRENT_MODE | 106 cmd[0] = GET_CURRENT_MODE |
53 reply = self.send_recv(cmd, 2) # Expect 2 bytes back | 107 reply = self.send_recv(cmd, 2) # Expect 2 bytes back |
54 return reply[0] | 108 return reply[0] |
57 def CurrentModeString(self): | 111 def CurrentModeString(self): |
58 modes = {DFU_MODE: 'dfu', MASS_MODE: 'massmode', DEBUG_MODE:'debug'} | 112 modes = {DFU_MODE: 'dfu', MASS_MODE: 'massmode', DEBUG_MODE:'debug'} |
59 return modes[self.CurrentMode] | 113 return modes[self.CurrentMode] |
60 def exitDfuMode(self): | 114 def exitDfuMode(self): |
61 cmd = bytearray(16) | 115 cmd = bytearray(16) |
62 cmd[0] = DFU_COMMAND | 116 cmd[0:2] = DFU_COMMAND, DFU_EXIT |
63 cmd[1] = DFU_EXIT | |
64 self.send_recv(cmd) | 117 self.send_recv(cmd) |
65 def enterSwdMode(self): | 118 def enterSwdMode(self): |
66 cmd = bytearray(16) | 119 cmd = bytearray(16) |
67 cmd[0] = DEBUG_COMMAND | 120 cmd[0:3] = DEBUG_COMMAND, DEBUG_ENTER, DEBUG_ENTER_SWD |
68 cmd[1] = DEBUG_ENTER | |
69 cmd[2] = DEBUG_ENTER_SWD | |
70 self.send_recv(cmd) | 121 self.send_recv(cmd) |
71 def exitDebugMode(self): | 122 def exitDebugMode(self): |
72 cmd = bytearray(16) | 123 cmd = bytearray(16) |
73 cmd[0] = DEBUG_COMMAND | 124 cmd[0:2] = DEBUG_COMMAND, DEBUG_EXIT |
74 cmd[1] = DEBUG_EXIT | |
75 self.send_recv(cmd) | 125 self.send_recv(cmd) |
76 | 126 |
77 def getVersion(self): | 127 def getVersion(self): |
78 cmd = bytearray(16) | 128 cmd = bytearray(16) |
79 cmd[0] = GET_VERSION | 129 cmd[0] = GET_VERSION |
102 revision = u32 & 0xf | 152 revision = u32 & 0xf |
103 return implementer_id, variant, part, revision | 153 return implementer_id, variant, part, revision |
104 | 154 |
105 def status(self): | 155 def status(self): |
106 cmd = bytearray(16) | 156 cmd = bytearray(16) |
107 cmd[0] = DEBUG_COMMAND | 157 cmd[0:2] = DEBUG_COMMAND, DEBUG_GETSTATUS |
108 cmd[1] = DEBUG_GETSTATUS | |
109 reply = self.send_recv(cmd, 2) | 158 reply = self.send_recv(cmd, 2) |
110 return reply[0] | 159 return reply[0] |
111 | 160 |
161 # debug commands: | |
112 def step(self): | 162 def step(self): |
113 cmd = bytearray(16) | 163 cmd = bytearray(16) |
114 cmd[0] = DEBUG_COMMAND | 164 cmd[0:2] = DEBUG_COMMAND, DEBUG_STEPCORE |
115 cmd[1] = DEBUG_STEPCORE | |
116 self.send_recv(cmd, 2) | 165 self.send_recv(cmd, 2) |
117 def run(self): | 166 def run(self): |
118 cmd = bytearray(16) | 167 cmd = bytearray(16) |
119 cmd[0] = DEBUG_COMMAND | 168 cmd[0:2] = DEBUG_COMMAND, DEBUG_RUNCORE |
120 cmd[1] = DEBUG_RUNCORE | |
121 self.send_recv(cmd, 2) | 169 self.send_recv(cmd, 2) |
170 | |
171 # flashing commands: | |
172 def writeFlash(self, address, content): | |
173 # TODO: | |
174 flashsize = 0x100000 # fixed 1 MB for now.. | |
175 print('WARNING: using 1 MB as flash size') | |
176 pagesize = 0x4000 # fixed for now! | |
177 | |
178 # Check address range: | |
179 if address < STM32_FLASH_BASE: | |
180 raise STLinkException('Flashing below flash start') | |
181 if address + len(content) > STM32_FLASH_BASE + flashsize: | |
182 raise STLinkException('Flashing above flash size') | |
183 if address & 1 == 1: | |
184 raise STLinkException('Unaligned flash') | |
185 if len(content) & 1 == 1: | |
186 print('unaligned length, padding with zero') | |
187 content += bytes([0]) | |
188 if address & (pagesize - 1) != 0: | |
189 raise STLinkException('Address not aligned with pagesize') | |
122 | 190 |
191 # erase required space | |
192 sectors = calcSectors(address, len(content)) | |
193 print('erasing {0} sectors'.format(len(sectors))) | |
194 for sector, secsize in sectors: | |
195 print('erasing sector {0} of size {1}'.format(sector, secsize)) | |
196 self.eraseFlashSector(sector) | |
197 | |
198 # program pages: | |
199 # TODO | |
200 | |
201 # verfify program: | |
202 self.verifyFlash(address, content) | |
203 def eraseFlashSector(self, sector): | |
204 self.waitFlashBusy() | |
205 self.unlockFlashIf() | |
206 self.writeFlashCrSnb(sector) | |
207 self.setFlashCrStart() | |
208 self.waitFlashBusy() | |
209 self.lockFlash() | |
210 def verifyFlash(self, address, content): | |
211 print('verifying', address, len(content)) | |
212 offset = 0 | |
213 cmp_size = 0x1800 | |
214 t1 = time.time() | |
215 while offset < len(content): | |
216 # Correct for last page: | |
217 if offset + cmp_size > len(content): | |
218 cmp_size = len(content) - offset | |
219 | |
220 # align size to 4 bytes: | |
221 aligned_size = cmp_size | |
222 while aligned_size % 4 != 0: | |
223 aligned_size += 1 | |
224 | |
225 mem = self.read_mem32(address + offset, aligned_size) | |
226 ok = mem[:cmp_size] == content[offset:offset+cmp_size] | |
227 | |
228 # indicate progress: | |
229 okc = '.' if ok else 'x' | |
230 print(okc, end='', flush=True) | |
231 | |
232 # increase for next piece: | |
233 offset += cmp_size | |
234 t2 = time.time() | |
235 print('done! {0} bytes/second'.format(len(content)/(t2-t1))) | |
236 return content == mem | |
237 def readFlashSr(self): | |
238 return self.read_debug32(FLASH_F4_SR) | |
239 def readFlashCr(self): | |
240 return self.read_debug32(FLASH_F4_CR) | |
241 def writeFlashCrSnb(self, sector): | |
242 x = self.readFlashCr() | |
243 x &= ~FLASH_F4_CR_SNB_MASK | |
244 x |= sector << FLASH_F4_CR_SNB | |
245 x |= 1 << FLASH_F4_CR_SER | |
246 self.write_debug32(FLASH_F4_CR, x) | |
247 def setFlashCrStart(self): | |
248 x = self.readFlashCr() | |
249 x |= 1 << FLASH_F4_CR_START | |
250 self.write_debug32(FLASH_F4_CR, x) | |
251 def isFlashBusy(self): | |
252 mask = 1 << FLASH_F4_SR_BSY | |
253 sr = self.readFlashSr() | |
254 return sr & mask == mask | |
255 def waitFlashBusy(self): | |
256 """ block until flash operation completes. """ | |
257 while self.isFlashBusy(): | |
258 pass | |
259 def isFlashLocked(self): | |
260 cr = self.readFlashCr() | |
261 mask = 1 << FLASH_F4_CR_LOCK | |
262 return cr & mask == mask | |
263 def unlockFlashIf(self): | |
264 if self.isFlashLocked(): | |
265 self.write_debug32(FLASH_F4_KEYR, FLASH_KEY1) | |
266 self.write_debug32(FLASH_F4_KEYR, FLASH_KEY2) | |
267 if self.isFlashLocked(): | |
268 raise STLinkException('Failed to unlock') | |
269 | |
270 def lockFlash(self): | |
271 # TODO | |
272 pass | |
273 | |
123 # Helper 1 functions: | 274 # Helper 1 functions: |
275 def write_debug32(self, address, value): | |
276 cmd = bytearray(16) | |
277 cmd[0:2] = DEBUG_COMMAND, JTAG_WRITEDEBUG_32BIT | |
278 cmd[2:6] = struct.pack('<I', address) | |
279 cmd[6:10] = struct.pack('<I', value) | |
280 self.send_recv(cmd, 2) | |
124 def read_debug32(self, address): | 281 def read_debug32(self, address): |
125 cmd = bytearray(16) | 282 cmd = bytearray(16) |
126 cmd[0] = DEBUG_COMMAND | 283 cmd[0:2] = DEBUG_COMMAND, JTAG_READDEBUG_32BIT |
127 cmd[1] = JTAG_READDEBUG_32BIT | |
128 cmd[2:6] = struct.pack('<I', address) # pack into u32 little endian | 284 cmd[2:6] = struct.pack('<I', address) # pack into u32 little endian |
129 reply = self.send_recv(cmd, 8) | 285 reply = self.send_recv(cmd, 8) |
130 return struct.unpack('<I', reply[4:8])[0] | 286 return struct.unpack('<I', reply[4:8])[0] |
287 def write_reg(self, reg, value): | |
288 cmd = bytearray(16) | |
289 cmd[0:3] = DEBUG_COMMAND, DEBUG_WRITEREG, reg | |
290 cmd[3:7] = struct.pack('<I', value) | |
291 r = self.send_recv(cmd, 2) | |
292 def read_reg(self, reg): | |
293 cmd = bytearray(16) | |
294 cmd[0:3] = DEBUG_COMMAND, DEBUG_READREG, reg | |
295 reply = self.send_recv(cmd, 4) | |
296 return struct.unpack('<I', reply)[0] | |
297 def read_mem32(self, address, length): | |
298 assert length % 4 == 0 | |
299 cmd = bytearray(16) | |
300 cmd[0:2] = DEBUG_COMMAND, DEBUG_READMEM_32BIT | |
301 cmd[2:6] = struct.pack('<I', address) | |
302 cmd[6:8] = struct.pack('<H', length) # uint16 | |
303 reply = self.send_recv(cmd, length) # expect memory back! | |
304 return reply | |
131 | 305 |
132 # Helper 2 functions: | 306 # Helper 2 functions: |
133 def send_recv(self, tx, rxsize=0): | 307 def send_recv(self, tx, rxsize=0): |
134 """ Helper function that transmits and receives data. """ | 308 """ Helper function that transmits and receives data in bulk mode. """ |
135 # TODO: we could use here the non-blocking libusb api. | 309 # TODO: we could use here the non-blocking libusb api. |
136 tx = bytes(tx) | 310 tx = bytes(tx) |
311 assert len(tx) == 16 | |
137 self.devHandle.bulkWrite(2, tx) # write to endpoint 2 | 312 self.devHandle.bulkWrite(2, tx) # write to endpoint 2 |
138 if rxsize > 0: | 313 if rxsize > 0: |
139 return self.devHandle.bulkRead(1, rxsize) # read from endpoint 1 | 314 return self.devHandle.bulkRead(1, rxsize) # read from endpoint 1 |
140 | 315 |
141 knownChipIds = {0x1: 'x'} | 316 knownChipIds = {0x1: 'x'} |
158 print('chip id: 0x{0:X}'.format(i)) | 333 print('chip id: 0x{0:X}'.format(i)) |
159 print('cpu: {0}'.format(sl.CpuId)) | 334 print('cpu: {0}'.format(sl.CpuId)) |
160 | 335 |
161 print('status: {0}'.format(sl.status())) | 336 print('status: {0}'.format(sl.status())) |
162 | 337 |
163 time.sleep(2.2) | 338 #time.sleep(2.2) |
339 | |
340 # test registers: | |
341 sl.write_reg(3, 0x1337) | |
342 sl.write_reg(1, 0x1332) | |
343 assert sl.read_reg(3) == 0x1337 | |
344 assert sl.read_reg(1) == 0x1332 | |
164 | 345 |
165 sl.exitDebugMode() | 346 sl.exitDebugMode() |
166 print('mode at end:', sl.CurrentModeString) | 347 print('mode at end:', sl.CurrentModeString) |
167 | 348 |
168 sl.close() | 349 sl.close() |