Mercurial > lcfOS
comparison python/stlink.py @ 117:f2b37d78082d
Flash write improvements
author | Windel Bouwman |
---|---|
date | Sat, 12 Jan 2013 09:38:35 +0100 |
parents | 90b03bc018cf |
children | f51791638cae |
comparison
equal
deleted
inserted
replaced
116:90b03bc018cf | 117:f2b37d78082d |
---|---|
8 | 8 |
9 def checkDevice(device): | 9 def checkDevice(device): |
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 = 0, 1, 2 | 12 DFU_MODE, MASS_MODE, DEBUG_MODE = 0, 1, 2 |
13 | |
14 CORE_RUNNING = 0x80 | |
15 CORE_HALTED = 0x81 | |
16 | |
13 # Commands: | 17 # Commands: |
14 GET_VERSION = 0xf1 | 18 GET_VERSION = 0xf1 |
15 DEBUG_COMMAND = 0xf2 | 19 DEBUG_COMMAND = 0xf2 |
16 DFU_COMMAND = 0xf3 | 20 DFU_COMMAND = 0xf3 |
17 GET_CURRENT_MODE = 0xf5 | 21 GET_CURRENT_MODE = 0xf5 |
27 DEBUG_RESETSYS = 0x03 | 31 DEBUG_RESETSYS = 0x03 |
28 DEBUG_READREG = 0x5 | 32 DEBUG_READREG = 0x5 |
29 DEBUG_WRITEREG = 0x6 | 33 DEBUG_WRITEREG = 0x6 |
30 DEBUG_READMEM_32BIT = 0x7 | 34 DEBUG_READMEM_32BIT = 0x7 |
31 DEBUG_WRITEMEM_32BIT = 0x8 | 35 DEBUG_WRITEMEM_32BIT = 0x8 |
36 DEBUG_RUNCORE = 0x9 | |
37 DEBUG_STEPCORE = 0xa | |
32 | 38 |
33 JTAG_WRITEDEBUG_32BIT = 0x35 | 39 JTAG_WRITEDEBUG_32BIT = 0x35 |
34 JTAG_READDEBUG_32BIT = 0x36 | 40 JTAG_READDEBUG_32BIT = 0x36 |
35 | 41 |
36 # cortex M3 | 42 # cortex M3 |
49 FLASH_F4_SR = FLASH_F4_REGS_ADDR + 0x0c | 55 FLASH_F4_SR = FLASH_F4_REGS_ADDR + 0x0c |
50 FLASH_F4_CR = FLASH_F4_REGS_ADDR + 0x10 | 56 FLASH_F4_CR = FLASH_F4_REGS_ADDR + 0x10 |
51 | 57 |
52 FLASH_F4_CR_START = 16 | 58 FLASH_F4_CR_START = 16 |
53 FLASH_F4_CR_LOCK = 31 | 59 FLASH_F4_CR_LOCK = 31 |
60 FLASH_CR_PG = 0 | |
54 FLASH_F4_CR_SER = 1 | 61 FLASH_F4_CR_SER = 1 |
55 FLASH_CR_MER = 2 | 62 FLASH_CR_MER = 2 |
56 FLASH_F4_CR_SNB = 3 | 63 FLASH_F4_CR_SNB = 3 |
57 FLASH_F4_CR_SNB_MASK = 0x38 | 64 FLASH_F4_CR_SNB_MASK = 0x38 |
58 FLASH_F4_SR_BSY = 16 | 65 FLASH_F4_SR_BSY = 16 |
129 self.exitDfuMode() | 136 self.exitDfuMode() |
130 if self.CurrentMode != DEBUG_MODE: | 137 if self.CurrentMode != DEBUG_MODE: |
131 self.enterSwdMode() | 138 self.enterSwdMode() |
132 self.reset() | 139 self.reset() |
133 def close(self): | 140 def close(self): |
141 # TODO | |
134 pass | 142 pass |
135 | 143 |
136 # modes: | 144 # modes: |
137 def getCurrentMode(self): | 145 def getCurrentMode(self): |
138 cmd = bytearray(16) | 146 cmd = bytearray(16) |
183 variant = (u32 >> 20) & 0xf | 191 variant = (u32 >> 20) & 0xf |
184 part = (u32 >> 4) & 0xfff | 192 part = (u32 >> 4) & 0xfff |
185 revision = u32 & 0xf | 193 revision = u32 & 0xf |
186 return implementer_id, variant, part, revision | 194 return implementer_id, variant, part, revision |
187 | 195 |
188 def status(self): | 196 def getStatus(self): |
189 cmd = bytearray(16) | 197 cmd = bytearray(16) |
190 cmd[0:2] = DEBUG_COMMAND, DEBUG_GETSTATUS | 198 cmd[0:2] = DEBUG_COMMAND, DEBUG_GETSTATUS |
191 reply = self.send_recv(cmd, 2) | 199 reply = self.send_recv(cmd, 2) |
192 return reply[0] | 200 return reply[0] |
201 Status = property(getStatus) | |
202 @property | |
203 def StatusString(self): | |
204 s = self.Status | |
205 statii = {CORE_RUNNING: 'CORE RUNNING', CORE_HALTED: 'CORE HALTED'} | |
206 if s in statii: | |
207 return statii[s] | |
208 | |
193 def reset(self): | 209 def reset(self): |
194 cmd = bytearray(16) | 210 cmd = bytearray(16) |
195 cmd[0:2] = DEBUG_COMMAND, DEBUG_RESETSYS | 211 cmd[0:2] = DEBUG_COMMAND, DEBUG_RESETSYS |
196 self.send_recv(cmd, 2) | 212 self.send_recv(cmd, 2) |
197 | 213 |
233 self.eraseFlashSector(sector) | 249 self.eraseFlashSector(sector) |
234 | 250 |
235 # program pages: | 251 # program pages: |
236 self.initFlashLoader() | 252 self.initFlashLoader() |
237 self.unlockFlashIf() | 253 self.unlockFlashIf() |
238 # TODO | 254 self.writeFlashCrPsiz(2) |
255 self.setFlashCrPg() | |
256 | |
257 offset = 0 | |
258 while offset < len(content): | |
259 size = len(content) - offset | |
260 if size > 0x8000: | |
261 size = 0x8000 | |
262 | |
263 self.runFlashLoader(address + offset, content[offset:offset + size]) | |
264 offset += size | |
239 | 265 |
240 self.lockFlash() | 266 self.lockFlash() |
241 | 267 |
242 # verfify program: | 268 # verfify program: |
243 self.verifyFlash(address, content) | 269 self.verifyFlash(address, content) |
289 print('done! {0} bytes/second'.format(size/(t2-t1))) | 315 print('done! {0} bytes/second'.format(size/(t2-t1))) |
290 return image | 316 return image |
291 def initFlashLoader(self): | 317 def initFlashLoader(self): |
292 # TODO: support other loader code. | 318 # TODO: support other loader code. |
293 self.write_mem32(STM32_SRAM_BASE, loader_code_stm32f4) | 319 self.write_mem32(STM32_SRAM_BASE, loader_code_stm32f4) |
294 self.bufAddress = STM32_SRAM_BASE + len(loader_code_stm32f4) | 320 def runFlashLoader(self, targetaddress, buf): |
321 bufAddress = STM32_SRAM_BASE + len(loader_code_stm32f4) | |
322 print('running flash loader for {0} bytes'.format(len(buf))) | |
323 self.write_buffer_to_sram(buf) | |
324 count = int(len(buf) / 4) | |
325 if len(buf) % 4 != 0: count += 1 | |
326 self.write_reg(0, bufAddress) | |
327 self.write_reg(1, targetaddress) | |
328 self.write_reg(2, count) | |
329 self.write_reg(15, STM32_SRAM_BASE) # pc | |
330 | |
331 self.run() # Let loose cpu! | |
332 | |
333 while self.Status == CORE_RUNNING: | |
334 pass | |
335 | |
336 r2 = self.read_reg(2) | |
337 if r2 != 0: | |
338 raise STLinkException("write error! {0}".format(r2)) | |
339 def write_buffer_to_sram(self, buf): | |
340 bufAddress = STM32_SRAM_BASE + len(loader_code_stm32f4) | |
341 chunk = len(buf) >> 2 | |
342 rem = len(buf) & 0x3 | |
343 if chunk > 0: | |
344 self.write_mem32(bufAddress, buf[:chunk]) | |
345 if rem > 0: | |
346 self.write_mem8(bufAddress + chunk, buf[chunk:chunk+rem]) | |
347 | |
295 def readFlashSr(self): | 348 def readFlashSr(self): |
296 return self.read_debug32(FLASH_F4_SR) | 349 return self.read_debug32(FLASH_F4_SR) |
297 def readFlashCr(self): | 350 def readFlashCr(self): |
298 return self.read_debug32(FLASH_F4_CR) | 351 return self.read_debug32(FLASH_F4_CR) |
299 def writeFlashCrSnb(self, sector): | 352 def writeFlashCrSnb(self, sector): |
303 x |= 1 << FLASH_F4_CR_SER | 356 x |= 1 << FLASH_F4_CR_SER |
304 self.write_debug32(FLASH_F4_CR, x) | 357 self.write_debug32(FLASH_F4_CR, x) |
305 def setFlashCrMer(self): | 358 def setFlashCrMer(self): |
306 x = self.readFlashCr() | 359 x = self.readFlashCr() |
307 x |= 1 << FLASH_CR_MER | 360 x |= 1 << FLASH_CR_MER |
361 self.write_debug32(FLASH_F4_CR, x) | |
362 def setFlashCrPg(self): | |
363 x = self.readFlashCr() | |
364 x |= 1 << FLASH_CR_PG | |
365 self.write_debug32(FLASH_F4_CR, x) | |
366 def writeFlashCrPsiz(self, n): | |
367 x = self.readFlashCr() | |
368 x &= (0x3 << 8) | |
369 x |= n << 8 | |
370 print('psiz', n) | |
308 self.write_debug32(FLASH_F4_CR, x) | 371 self.write_debug32(FLASH_F4_CR, x) |
309 def clearFlashCrMer(self): | 372 def clearFlashCrMer(self): |
310 x = self.readFlashCr() | 373 x = self.readFlashCr() |
311 x &= ~(1 << FLASH_CR_MER) | 374 x &= ~(1 << FLASH_CR_MER) |
312 self.write_debug32(FLASH_F4_CR, x) | 375 self.write_debug32(FLASH_F4_CR, x) |
406 print('chip id: 0x{0:X} -> {1}'.format(i, knownChipIds[i])) | 469 print('chip id: 0x{0:X} -> {1}'.format(i, knownChipIds[i])) |
407 else: | 470 else: |
408 print('chip id: 0x{0:X}'.format(i)) | 471 print('chip id: 0x{0:X}'.format(i)) |
409 print('cpu: {0}'.format(sl.CpuId)) | 472 print('cpu: {0}'.format(sl.CpuId)) |
410 | 473 |
411 print('status: {0}'.format(sl.status())) | 474 print('status: {0}'.format(sl.StatusString)) |
412 | 475 |
413 #time.sleep(2.2) | 476 #time.sleep(2.2) |
414 | 477 |
415 # test registers: | 478 # test registers: |
416 sl.write_reg(3, 0x1337) | 479 sl.write_reg(3, 0x1337) |
417 sl.write_reg(2, 0x1332) | 480 sl.write_reg(2, 0x1332) |
481 sl.write_reg(6, 0x12345) | |
418 assert sl.read_reg(3) == 0x1337 | 482 assert sl.read_reg(3) == 0x1337 |
419 assert sl.read_reg(2) == 0x1332 | 483 assert sl.read_reg(2) == 0x1332 |
484 assert sl.read_reg(6) == 0x12345 | |
420 | 485 |
421 sl.exitDebugMode() | 486 sl.exitDebugMode() |
422 print('mode at end:', sl.CurrentModeString) | 487 print('mode at end:', sl.CurrentModeString) |
423 | 488 |
424 sl.close() | 489 sl.close() |