annotate python/stlink.py @ 124:d38729d35c4d stm32f4discovery flash with python

New implementation of hexfile viewer
author Windel Bouwman
date Sat, 12 Jan 2013 15:43:10 +0100
parents 9f1094b1587a
children 51cc127648e4
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
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
7 ST_VID, STLINK2_PID = 0x0483, 0x3748
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
8
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
9 def checkDevice(device):
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
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
12 DFU_MODE, MASS_MODE, DEBUG_MODE = 0, 1, 2
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
13
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
14 CORE_RUNNING = 0x80
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
15 CORE_HALTED = 0x81
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
16
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
17 # Commands:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
18 GET_VERSION = 0xf1
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
19 DEBUG_COMMAND = 0xf2
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
20 DFU_COMMAND = 0xf3
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
21 GET_CURRENT_MODE = 0xf5
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
22
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
23 # dfu commands:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
24 DFU_EXIT = 0x7
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
25
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
26 # debug commands:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
27 DEBUG_ENTER = 0x20
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
28 DEBUG_EXIT = 0x21
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
29 DEBUG_ENTER_SWD = 0xa3
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
30 DEBUG_GETSTATUS = 0x01
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
31 DEBUG_RESETSYS = 0x03
124
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 123
diff changeset
32 DEBUG_READALLREGS = 0x04
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
33 DEBUG_READREG = 0x5
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
34 DEBUG_WRITEREG = 0x6
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
35 DEBUG_READMEM_32BIT = 0x7
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
36 DEBUG_WRITEMEM_32BIT = 0x8
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
37 DEBUG_RUNCORE = 0x9
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
38 DEBUG_STEPCORE = 0xa
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
39
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
40 JTAG_WRITEDEBUG_32BIT = 0x35
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
41 JTAG_READDEBUG_32BIT = 0x36
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
42
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
43 # cortex M3
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
44 CM3_REG_CPUID = 0xE000ED00
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
45
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
46 # F4 specifics:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
47 STM32_FLASH_BASE = 0x08000000
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
48 STM32_SRAM_BASE = 0x20000000
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
49
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
50 FLASH_KEY1 = 0x45670123
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
51 FLASH_KEY2 = 0xcdef89ab
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
52 # flash registers:
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
53 FLASH_F4_REGS_ADDR = 0x40023c00
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
54
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
55 FLASH_F4_KEYR = FLASH_F4_REGS_ADDR + +0x04
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
56 FLASH_F4_SR = FLASH_F4_REGS_ADDR + 0x0c
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
57 FLASH_F4_CR = FLASH_F4_REGS_ADDR + 0x10
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
58
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
59 FLASH_F4_CR_START = 16
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
60 FLASH_F4_CR_LOCK = 31
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
61 FLASH_CR_PG = 0
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
62 FLASH_F4_CR_SER = 1
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
63 FLASH_CR_MER = 2
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
64 FLASH_F4_CR_SNB = 3
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
65 FLASH_F4_CR_SNB_MASK = 0x38
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
66 FLASH_F4_SR_BSY = 16
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
67
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
68 def calculate_F4_sector(address):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
69 """
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
70 from 0x8000000 to 0x80FFFFF
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
71 4 sectors of 0x4000 (16 kB)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
72 1 sector of 0x10000 (64 kB)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
73 7 of 0x20000 (128 kB)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
74 """
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
75 sectorsizes = [0x4000] * 4 + [0x10000] + [0x20000] * 7
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
76 sectorstarts = []
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
77 a = STM32_FLASH_BASE
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
78 for sectorsize in sectorsizes:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
79 sectorstarts.append(a)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
80 a += sectorsize
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
81 # linear search:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
82 sec = 0
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
83 while sec < len(sectorsizes) and address >= sectorstarts[sec]:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
84 sec += 1
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
85 sec -= 1 # one back.
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
86 return sec, sectorsizes[sec]
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
87
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
88 def calcSectors(address, size):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
89 off = 0
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
90 sectors = []
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
91 while off < size:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
92 sectornum, sectorsize = calculate_F4_sector(address + off)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
93 sectors.append((sectornum, sectorsize))
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
94 off += sectorsize
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
95 return sectors
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
96
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
97 class STLink:
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
98 """ ST link interface code """
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
99 def __init__(self, stlink2=None):
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
100 if not stlink2:
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
101 context = UsbContext()
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
102 stlink2s = list(filter(checkDevice, context.DeviceList))
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
103 if not stlink2s:
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
104 raise STLinkException('Could not find an ST link')
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
105 if len(stlink2s) > 1:
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
106 print('More then one stlink2 found, picking first one')
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
107 stlink2 = stlink2s[0]
124
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 123
diff changeset
108 assert checkDevice(stlink2)
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
109 self.stlink2 = stlink2
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
110 def open(self):
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
111 self.devHandle = self.stlink2.open()
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
112 if self.devHandle.Configuration != 1:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
113 self.devHandle.Configuration = 1
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
114 self.devHandle.claimInterface(0)
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
115
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
116 # First initialization:
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
117 if self.CurrentMode == DFU_MODE:
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
118 self.exitDfuMode()
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
119 if self.CurrentMode != DEBUG_MODE:
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
120 self.enterSwdMode()
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
121 self.reset()
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
122 def close(self):
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
123 # TODO
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
124 pass
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
125
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
126 # modes:
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
127 def getCurrentMode(self):
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
128 cmd = bytearray(16)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
129 cmd[0] = GET_CURRENT_MODE
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
130 reply = self.send_recv(cmd, 2) # Expect 2 bytes back
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
131 return reply[0]
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
132 CurrentMode = property(getCurrentMode)
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
133 @property
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
134 def CurrentModeString(self):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
135 modes = {DFU_MODE: 'dfu', MASS_MODE: 'massmode', DEBUG_MODE:'debug'}
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
136 return modes[self.CurrentMode]
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
137 def exitDfuMode(self):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
138 cmd = bytearray(16)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
139 cmd[0:2] = DFU_COMMAND, DFU_EXIT
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
140 self.send_recv(cmd)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
141 def enterSwdMode(self):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
142 cmd = bytearray(16)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
143 cmd[0:3] = DEBUG_COMMAND, DEBUG_ENTER, DEBUG_ENTER_SWD
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
144 self.send_recv(cmd)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
145 def exitDebugMode(self):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
146 cmd = bytearray(16)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
147 cmd[0:2] = DEBUG_COMMAND, DEBUG_EXIT
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
148 self.send_recv(cmd)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
149
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
150 def getVersion(self):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
151 cmd = bytearray(16)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
152 cmd[0] = GET_VERSION
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
153 data = self.send_recv(cmd, 6) # Expect 6 bytes back
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
154 # Parse 6 bytes into various versions:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
155 b0, b1, b2, b3, b4, b5 = data
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
156 stlink_v = b0 >> 4
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
157 jtag_v = ((b0 & 0xf) << 2) | (b1 >> 6)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
158 swim_v = b1 & 0x3f
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
159 vid = (b3 << 8) | b2
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
160 pid = (b5 << 8) | b4
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
161 return 'stlink={0} jtag={1} swim={2} vid:pid={3:04X}:{4:04X}'.format(\
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
162 stlink_v, jtag_v, swim_v, vid, pid)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
163 Version = property(getVersion)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
164
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
165 @property
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
166 def ChipId(self):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
167 return self.read_debug32(0xE0042000)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
168 @property
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
169 def CpuId(self):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
170 u32 = self.read_debug32(CM3_REG_CPUID)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
171 implementer_id = (u32 >> 24) & 0x7f
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
172 variant = (u32 >> 20) & 0xf
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
173 part = (u32 >> 4) & 0xfff
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
174 revision = u32 & 0xf
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
175 return implementer_id, variant, part, revision
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
176
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
177 def getStatus(self):
114
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, DEBUG_GETSTATUS
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
180 reply = self.send_recv(cmd, 2)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
181 return reply[0]
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
182 Status = property(getStatus)
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
183 @property
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
184 def StatusString(self):
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
185 s = self.Status
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
186 statii = {CORE_RUNNING: 'CORE RUNNING', CORE_HALTED: 'CORE HALTED'}
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
187 if s in statii:
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
188 return statii[s]
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
189
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
190 def reset(self):
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
191 cmd = bytearray(16)
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
192 cmd[0:2] = DEBUG_COMMAND, DEBUG_RESETSYS
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
193 self.send_recv(cmd, 2)
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
194
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
195 # debug commands:
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
196 def step(self):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
197 cmd = bytearray(16)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
198 cmd[0:2] = DEBUG_COMMAND, DEBUG_STEPCORE
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
199 self.send_recv(cmd, 2)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
200 def run(self):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
201 cmd = bytearray(16)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
202 cmd[0:2] = DEBUG_COMMAND, DEBUG_RUNCORE
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
203 self.send_recv(cmd, 2)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
204
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
205 # flashing commands:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
206 def writeFlash(self, address, content):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
207 # TODO:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
208 flashsize = 0x100000 # fixed 1 MB for now..
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
209 print('WARNING: using 1 MB as flash size')
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
210 pagesize = 0x4000 # fixed for now!
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
211
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
212 # Check address range:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
213 if address < STM32_FLASH_BASE:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
214 raise STLinkException('Flashing below flash start')
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
215 if address + len(content) > STM32_FLASH_BASE + flashsize:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
216 raise STLinkException('Flashing above flash size')
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
217 if address & 1 == 1:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
218 raise STLinkException('Unaligned flash')
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
219 if len(content) & 1 == 1:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
220 print('unaligned length, padding with zero')
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
221 content += bytes([0])
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
222 if address & (pagesize - 1) != 0:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
223 raise STLinkException('Address not aligned with pagesize')
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
224
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
225 # erase required space
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
226 sectors = calcSectors(address, len(content))
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
227 print('erasing {0} sectors'.format(len(sectors)))
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
228 for sector, secsize in sectors:
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
229 print('erasing sector {0} of {1} bytes'.format(sector, secsize))
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
230 self.eraseFlashSector(sector)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
231
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
232 # program pages:
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
233 self.unlockFlashIf()
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
234 self.writeFlashCrPsiz(2) # writes are 32 bits aligned
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
235 self.setFlashCrPg()
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
236
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
237 print('writing {0} bytes'.format(len(content)), end='')
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
238 offset = 0
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
239 t1 = time.time()
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
240 while offset < len(content):
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
241 size = len(content) - offset
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
242 if size > 0x8000:
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
243 size = 0x8000
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
244
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
245 chunk = content[offset:offset + size]
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
246 while len(chunk) % 4 != 0:
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
247 print('padding chunk')
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
248 chunk = chunk + bytes([0])
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
249
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
250 # Use simple mem32 writes:
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
251 self.write_mem32(address + offset, chunk)
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
252
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
253 offset += size
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
254 print('.', end='', flush=True)
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
255 t2 = time.time()
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
256 print('Done!')
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
257 print('Speed: {0} bytes/second'.format(len(content)/(t2-t1)))
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
258
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
259 self.lockFlash()
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
260
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
261 # verfify program:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
262 self.verifyFlash(address, content)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
263 def eraseFlashSector(self, sector):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
264 self.waitFlashBusy()
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
265 self.unlockFlashIf()
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
266 self.writeFlashCrSnb(sector)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
267 self.setFlashCrStart()
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
268 self.waitFlashBusy()
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
269 self.lockFlash()
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
270 def eraseFlash(self):
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
271 self.waitFlashBusy()
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
272 self.unlockFlashIf()
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
273 self.setFlashCrMer()
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
274 self.setFlashCrStart()
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
275 self.waitFlashBusy()
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
276 self.clearFlashCrMer()
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
277 self.lockFlash()
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
278 def verifyFlash(self, address, content):
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
279 device_content = self.readFlash(address, len(content))
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
280 ok = content == device_content
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
281 print('Verify:', ok)
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
282 def readFlash(self, address, size):
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
283 print('Reading {1} bytes from 0x{0:X}'.format(address, size), end='')
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
284 offset = 0
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
285 tmp_size = 0x1800
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
286 t1 = time.time()
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
287 image = bytes()
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
288 while offset < size:
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
289 # Correct for last page:
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
290 if offset + tmp_size > size:
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
291 tmp_size = size - offset
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
292
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
293 # align size to 4 bytes:
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
294 aligned_size = tmp_size
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
295 while aligned_size % 4 != 0:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
296 aligned_size += 1
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
297
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
298 mem = self.read_mem32(address + offset, aligned_size)
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
299 image += mem[:tmp_size]
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
300
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
301 # indicate progress:
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
302 print('.', end='', flush=True)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
303
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
304 # increase for next piece:
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
305 offset += tmp_size
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
306 t2 = time.time()
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
307 assert size == len(image)
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
308 print('Done!')
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
309 print('Speed: {0} bytes/second'.format(size/(t2-t1)))
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
310 return image
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
311 def readFlashSr(self):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
312 return self.read_debug32(FLASH_F4_SR)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
313 def readFlashCr(self):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
314 return self.read_debug32(FLASH_F4_CR)
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
315 def writeFlashCr(self, x):
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
316 self.write_debug32(FLASH_F4_CR, x)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
317 def writeFlashCrSnb(self, sector):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
318 x = self.readFlashCr()
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
319 x &= ~FLASH_F4_CR_SNB_MASK
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
320 x |= sector << FLASH_F4_CR_SNB
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
321 x |= 1 << FLASH_F4_CR_SER
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
322 self.writeFlashCr(x)
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
323 def setFlashCrMer(self):
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
324 x = self.readFlashCr()
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
325 x |= 1 << FLASH_CR_MER
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
326 self.writeFlashCr(x)
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
327 def setFlashCrPg(self):
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
328 x = self.readFlashCr()
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
329 x |= 1 << FLASH_CR_PG
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
330 self.writeFlashCr(x)
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
331 def writeFlashCrPsiz(self, n):
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
332 x = self.readFlashCr()
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
333 x &= (0x3 << 8)
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
334 x |= n << 8
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
335 self.writeFlashCr(x)
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
336 def clearFlashCrMer(self):
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
337 x = self.readFlashCr()
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
338 x &= ~(1 << FLASH_CR_MER)
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
339 self.writeFlashCr(x)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
340 def setFlashCrStart(self):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
341 x = self.readFlashCr()
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
342 x |= 1 << FLASH_F4_CR_START
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
343 self.writeFlashCr(x)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
344 def isFlashBusy(self):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
345 mask = 1 << FLASH_F4_SR_BSY
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
346 sr = self.readFlashSr()
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
347 # Check for error bits:
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
348 errorbits = {}
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
349 errorbits[7] = 'Programming sequence error'
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
350 errorbits[6] = 'Programming parallelism error'
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
351 errorbits[5] = 'Programming alignment error'
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
352 errorbits[4] = 'Write protection error'
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
353 errorbits[1] = 'Operation error'
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
354 #errorbits[0] = 'End of operation'
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
355 for bit, msg in errorbits.items():
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
356 if sr & (1 << bit) == (1 << bit):
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
357 raise STLinkException(msg)
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
358
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
359 return sr & mask == mask
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
360 def waitFlashBusy(self):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
361 """ block until flash operation completes. """
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
362 while self.isFlashBusy():
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
363 pass
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
364 def isFlashLocked(self):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
365 cr = self.readFlashCr()
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
366 mask = 1 << FLASH_F4_CR_LOCK
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
367 return cr & mask == mask
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
368 def unlockFlashIf(self):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
369 if self.isFlashLocked():
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
370 self.write_debug32(FLASH_F4_KEYR, FLASH_KEY1)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
371 self.write_debug32(FLASH_F4_KEYR, FLASH_KEY2)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
372 if self.isFlashLocked():
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
373 raise STLinkException('Failed to unlock')
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
374 def lockFlash(self):
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
375 x = self.readFlashCr() | (1 << FLASH_F4_CR_LOCK)
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
376 self.write_debug32(FLASH_F4_CR, x)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
377
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
378 # Helper 1 functions:
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
379 def write_debug32(self, address, value):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
380 cmd = bytearray(16)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
381 cmd[0:2] = DEBUG_COMMAND, JTAG_WRITEDEBUG_32BIT
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
382 cmd[2:10] = struct.pack('<II', address, value)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
383 self.send_recv(cmd, 2)
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
384 def read_debug32(self, address):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
385 cmd = bytearray(16)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
386 cmd[0:2] = DEBUG_COMMAND, JTAG_READDEBUG_32BIT
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
387 cmd[2:6] = struct.pack('<I', address) # pack into u32 little endian
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
388 reply = self.send_recv(cmd, 8)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
389 return struct.unpack('<I', reply[4:8])[0]
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
390 def write_reg(self, reg, value):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
391 cmd = bytearray(16)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
392 cmd[0:3] = DEBUG_COMMAND, DEBUG_WRITEREG, reg
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
393 cmd[3:7] = struct.pack('<I', value)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
394 r = self.send_recv(cmd, 2)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
395 def read_reg(self, reg):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
396 cmd = bytearray(16)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
397 cmd[0:3] = DEBUG_COMMAND, DEBUG_READREG, reg
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
398 reply = self.send_recv(cmd, 4)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
399 return struct.unpack('<I', reply)[0]
124
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 123
diff changeset
400 def read_all_regs(self):
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 123
diff changeset
401 cmd = bytearray(16)
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 123
diff changeset
402 cmd[0:2] = DEBUG_COMMAND, DEBUG_READALLREGS
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 123
diff changeset
403 reply = self.send_recv(cmd, 84)
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 123
diff changeset
404 fmt = '<' + 'I' * 21 # unpack 21 register values
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 123
diff changeset
405 return list(struct.unpack(fmt, reply))
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
406 def write_mem32(self, address, content):
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
407 assert len(content) % 4 == 0
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
408 cmd = bytearray(16)
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
409 cmd[0:2] = DEBUG_COMMAND, DEBUG_WRITEMEM_32BIT
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
410 cmd[2:8] = struct.pack('<IH', address, len(content))
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
411 self.send_recv(cmd)
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
412 self.send_recv(content)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
413 def read_mem32(self, address, length):
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
414 assert length % 4 == 0
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
415 cmd = bytearray(16)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
416 cmd[0:2] = DEBUG_COMMAND, DEBUG_READMEM_32BIT
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
417 cmd[2:8] = struct.pack('<IH', address, length)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
418 reply = self.send_recv(cmd, length) # expect memory back!
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
419 return reply
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
420
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
421 # Helper 2 functions:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
422 def send_recv(self, tx, rxsize=0):
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
423 """ Helper function that transmits and receives data in bulk mode. """
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
424 # TODO: we could use here the non-blocking libusb api.
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
425 tx = bytes(tx)
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
426 #assert len(tx) == 16
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
427 self.devHandle.bulkWrite(2, tx) # write to endpoint 2
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
428 if rxsize > 0:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
429 return self.devHandle.bulkRead(1, rxsize) # read from endpoint 1
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
430
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
431 knownChipIds = {0x1: 'x'}
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
432
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
433 if __name__ == '__main__':
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
434 # Test program
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
435 sl = STLink()
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
436 sl.open()
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
437 print('version:', sl.Version)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
438 print('mode before doing anything:', sl.CurrentModeString)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
439 if sl.CurrentMode == DFU_MODE:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
440 sl.exitDfuMode()
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
441 sl.enterSwdMode()
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
442 print('mode after entering swd mode:', sl.CurrentModeString)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
443
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
444 i = sl.ChipId
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
445 if i in knownChipIds:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
446 print('chip id: 0x{0:X} -> {1}'.format(i, knownChipIds[i]))
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
447 else:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
448 print('chip id: 0x{0:X}'.format(i))
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
449 print('cpu: {0}'.format(sl.CpuId))
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
450
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
451 print('status: {0}'.format(sl.StatusString))
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
452
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
453 # test registers:
124
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 123
diff changeset
454 sl.write_reg(0, 0xdeadbeef)
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 123
diff changeset
455 sl.write_reg(1, 0xcafebabe)
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 123
diff changeset
456 sl.write_reg(2, 0xc0ffee)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
457 sl.write_reg(3, 0x1337)
124
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 123
diff changeset
458 sl.write_reg(5, 0x1332)
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
459 sl.write_reg(6, 0x12345)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 114
diff changeset
460 assert sl.read_reg(3) == 0x1337
124
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 123
diff changeset
461 assert sl.read_reg(5) == 0x1332
117
f2b37d78082d Flash write improvements
Windel Bouwman
parents: 116
diff changeset
462 assert sl.read_reg(6) == 0x12345
124
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 123
diff changeset
463 regs = sl.read_all_regs()
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 123
diff changeset
464 for i in range(len(regs)):
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 123
diff changeset
465 print('R{0}=0x{1:X}'.format(i, regs[i]))
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
466
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
467 sl.exitDebugMode()
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
468 print('mode at end:', sl.CurrentModeString)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
469
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
470 sl.close()
119
f51791638cae Simplified flashing
Windel Bouwman
parents: 117
diff changeset
471 print('Test succes!')
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
472