128
|
1 import time
|
256
|
2 import logging
|
128
|
3 from devices import Device, registerDevice, STLinkException, Interface
|
138
|
4 import stlink
|
128
|
5
|
|
6 # F4 specifics:
|
|
7 STM32_FLASH_BASE = 0x08000000
|
|
8 STM32_SRAM_BASE = 0x20000000
|
|
9
|
|
10 # flash registers:
|
|
11 FLASH_F4_REGS_ADDR = 0x40023c00
|
|
12 FLASH_F4_KEYR = FLASH_F4_REGS_ADDR + 0x04
|
|
13 FLASH_F4_SR = FLASH_F4_REGS_ADDR + 0x0c
|
|
14 FLASH_F4_CR = FLASH_F4_REGS_ADDR + 0x10
|
|
15
|
|
16 FLASH_F4_CR_START = 16
|
|
17 FLASH_F4_CR_LOCK = 31
|
|
18 FLASH_CR_PG = 0
|
|
19 FLASH_F4_CR_SER = 1
|
|
20 FLASH_CR_MER = 2
|
|
21 FLASH_F4_CR_SNB = 3
|
|
22 FLASH_F4_CR_SNB_MASK = 0x38
|
|
23 FLASH_F4_SR_BSY = 16
|
|
24
|
|
25 class Stm32F4(Device):
|
260
|
26 """
|
130
|
27 Implementation of the specifics of the STM32F4xx device series.
|
260
|
28 """
|
|
29 def __init__(self, iface):
|
129
|
30 super().__init__(iface)
|
255
|
31 self.logger = logging.getLogger('stm32')
|
246
|
32
|
260
|
33 def __str__(self):
|
130
|
34 return 'STM32F4 device size=0x{1:X} id=0x{0:X}'.format(\
|
|
35 self.UID, self.FlashSize)
|
246
|
36
|
260
|
37 def calculate_F4_sector(self, address):
|
130
|
38 sectorstarts = []
|
|
39 a = STM32_FLASH_BASE
|
|
40 for sectorsize in self.sectorsizes:
|
|
41 sectorstarts.append(a)
|
|
42 a += sectorsize
|
|
43 # linear search:
|
|
44 sec = 0
|
|
45 while sec < len(self.sectorsizes) and address >= sectorstarts[sec]:
|
|
46 sec += 1
|
|
47 sec -= 1 # one back.
|
|
48 return sec, self.sectorsizes[sec]
|
|
49
|
260
|
50 def calcSectors(self, address, size):
|
130
|
51 off = 0
|
|
52 sectors = []
|
|
53 while off < size:
|
|
54 sectornum, sectorsize = self.calculate_F4_sector(address + off)
|
|
55 sectors.append((sectornum, sectorsize))
|
|
56 off += sectorsize
|
|
57 return sectors
|
260
|
58
|
|
59 # Device registers:
|
|
60 @property
|
|
61 def UID(self):
|
130
|
62 uid_base = 0x1FFF7A10
|
|
63 uid1 = self.iface.read_debug32(uid_base)
|
|
64 uid2 = self.iface.read_debug32(uid_base + 0x4)
|
|
65 uid3 = self.iface.read_debug32(uid_base + 0x8)
|
|
66 return (uid3 << 64) | (uid2 << 32) | uid1
|
246
|
67
|
260
|
68 @property
|
|
69 def FlashSize(self):
|
130
|
70 f_id = self.iface.read_debug32(0x1FFF7A22)
|
|
71 f_id = f_id >> 16
|
|
72 return f_id * 1024
|
246
|
73
|
260
|
74 @property
|
|
75 def Running(self):
|
138
|
76 return self.iface.Status == stlink.CORE_RUNNING
|
246
|
77
|
260
|
78 # flashing commands:
|
|
79 def writeFlash(self, address, content):
|
130
|
80 flashsize = self.FlashSize
|
|
81 pagesize = min(self.sectorsizes)
|
128
|
82
|
|
83 # Check address range:
|
|
84 if address < STM32_FLASH_BASE:
|
|
85 raise STLinkException('Flashing below flash start')
|
|
86 if address + len(content) > STM32_FLASH_BASE + flashsize:
|
|
87 raise STLinkException('Flashing above flash size')
|
|
88 if address & 1 == 1:
|
|
89 raise STLinkException('Unaligned flash')
|
|
90 if len(content) & 1 == 1:
|
255
|
91 self.logger.warning('unaligned length, padding with zero')
|
128
|
92 content += bytes([0])
|
|
93 if address & (pagesize - 1) != 0:
|
|
94 raise STLinkException('Address not aligned with pagesize')
|
|
95 # erase required space
|
130
|
96 sectors = self.calcSectors(address, len(content))
|
255
|
97 self.logger.info('erasing {0} sectors'.format(len(sectors)))
|
128
|
98 for sector, secsize in sectors:
|
255
|
99 self.logger.info('erasing sector {0} of {1} bytes'.format(sector, secsize))
|
128
|
100 self.eraseFlashSector(sector)
|
|
101 # program pages:
|
|
102 self.unlockFlashIf()
|
|
103 self.writeFlashCrPsiz(2) # writes are 32 bits aligned
|
|
104 self.setFlashCrPg()
|
256
|
105 self.logger.info('writing {0} bytes'.format(len(content)))
|
128
|
106 offset = 0
|
|
107 t1 = time.time()
|
|
108 while offset < len(content):
|
|
109 size = len(content) - offset
|
|
110 if size > 0x8000:
|
|
111 size = 0x8000
|
|
112 chunk = content[offset:offset + size]
|
|
113 while len(chunk) % 4 != 0:
|
|
114 chunk = chunk + bytes([0])
|
|
115 # Use simple mem32 writes:
|
|
116 self.iface.write_mem32(address + offset, chunk)
|
|
117 offset += size
|
260
|
118 self.logger.info('{}%'.format(offset*100/len(content)))
|
256
|
119 self.logger.info('Done!')
|
128
|
120 self.lockFlash()
|
|
121 # verfify program:
|
|
122 self.verifyFlash(address, content)
|
260
|
123
|
|
124 def eraseFlashSector(self, sector):
|
|
125 self.waitFlashBusy()
|
|
126 self.unlockFlashIf()
|
|
127 self.writeFlashCrSnb(sector)
|
|
128 self.setFlashCrStart()
|
|
129 self.waitFlashBusy()
|
|
130 self.lockFlash()
|
|
131
|
|
132 def eraseFlash(self):
|
128
|
133 self.waitFlashBusy()
|
|
134 self.unlockFlashIf()
|
|
135 self.setFlashCrMer()
|
|
136 self.setFlashCrStart()
|
|
137 self.waitFlashBusy()
|
|
138 self.clearFlashCrMer()
|
|
139 self.lockFlash()
|
256
|
140
|
260
|
141 def verifyFlash(self, address, content):
|
|
142 device_content = self.readFlash(address, len(content))
|
|
143 ok = content == device_content
|
|
144 if ok:
|
|
145 self.logger.info('Verify: OK')
|
|
146 else:
|
|
147 self.logger.warning('Verify: Mismatch')
|
256
|
148
|
260
|
149 def readFlash(self, address, size):
|
256
|
150 self.logger.info('Reading {1} bytes from 0x{0:X}'.format(address, size))
|
128
|
151 offset = 0
|
|
152 tmp_size = 0x1800
|
|
153 image = bytes()
|
|
154 while offset < size:
|
|
155 # Correct for last page:
|
|
156 if offset + tmp_size > size:
|
|
157 tmp_size = size - offset
|
|
158
|
|
159 # align size to 4 bytes:
|
|
160 aligned_size = tmp_size
|
|
161 while aligned_size % 4 != 0:
|
|
162 aligned_size += 1
|
|
163
|
|
164 mem = self.iface.read_mem32(address + offset, aligned_size)
|
|
165 image += mem[:tmp_size]
|
|
166
|
|
167 # indicate progress:
|
260
|
168 self.logger.info('{}%'.format(100*len(image) / size))
|
128
|
169
|
|
170 # increase for next piece:
|
|
171 offset += tmp_size
|
|
172 assert size == len(image)
|
256
|
173 self.logger.info('Done!')
|
128
|
174 return image
|
|
175
|
260
|
176 def waitFlashBusy(self):
|
246
|
177 """ block until flash operation completes. """
|
|
178 while self.isFlashBusy():
|
|
179 time.sleep(0.01)
|
260
|
180
|
|
181 def isFlashLocked(self):
|
|
182 mask = 1 << FLASH_F4_CR_LOCK
|
|
183 return self.Cr & mask == mask
|
|
184
|
|
185 def unlockFlashIf(self):
|
130
|
186 FLASH_KEY1, FLASH_KEY2 = 0x45670123, 0xcdef89ab
|
128
|
187 if self.isFlashLocked():
|
|
188 self.iface.write_debug32(FLASH_F4_KEYR, FLASH_KEY1)
|
|
189 self.iface.write_debug32(FLASH_F4_KEYR, FLASH_KEY2)
|
|
190 if self.isFlashLocked():
|
|
191 raise STLinkException('Failed to unlock')
|
260
|
192
|
|
193 def lockFlash(self):
|
|
194 self.Cr = self.Cr | (1 << FLASH_F4_CR_LOCK)
|
|
195
|
|
196 def readFlashSr(self):
|
|
197 return self.iface.read_debug32(FLASH_F4_SR)
|
|
198
|
|
199 def readFlashCr(self):
|
|
200 return self.iface.read_debug32(FLASH_F4_CR)
|
|
201
|
|
202 def writeFlashCr(self, x):
|
|
203 self.iface.write_debug32(FLASH_F4_CR, x)
|
|
204
|
|
205 Cr = property(readFlashCr, writeFlashCr)
|
|
206
|
|
207 def writeFlashCrSnb(self, sector):
|
|
208 x = self.Cr
|
128
|
209 x &= ~FLASH_F4_CR_SNB_MASK
|
|
210 x |= sector << FLASH_F4_CR_SNB
|
|
211 x |= 1 << FLASH_F4_CR_SER
|
260
|
212 self.Cr = x
|
|
213
|
|
214 def setFlashCrMer(self):
|
|
215 self.Cr = self.Cr | (1 << FLASH_CR_MER)
|
|
216
|
|
217 def setFlashCrPg(self):
|
|
218 self.Cr = self.Cr | (1 << FLASH_CR_PG)
|
|
219
|
|
220 def writeFlashCrPsiz(self, n):
|
|
221 x = self.Cr
|
|
222 x &= (0x3 << 8)
|
|
223 x |= n << 8
|
|
224 self.Cr = x
|
|
225
|
|
226 def clearFlashCrMer(self):
|
|
227 x = self.Cr
|
|
228 x &= ~(1 << FLASH_CR_MER)
|
|
229 self.Cr = x
|
|
230
|
|
231 def setFlashCrStart(self):
|
|
232 self.Cr = self.Cr | (1 << FLASH_F4_CR_START)
|
|
233
|
|
234 def isFlashBusy(self):
|
128
|
235 mask = 1 << FLASH_F4_SR_BSY
|
|
236 sr = self.readFlashSr()
|
|
237 # Check for error bits:
|
|
238 errorbits = {}
|
|
239 errorbits[7] = 'Programming sequence error'
|
|
240 errorbits[6] = 'Programming parallelism error'
|
|
241 errorbits[5] = 'Programming alignment error'
|
|
242 errorbits[4] = 'Write protection error'
|
|
243 errorbits[1] = 'Operation error'
|
|
244 #errorbits[0] = 'End of operation'
|
|
245 for bit, msg in errorbits.items():
|
|
246 if sr & (1 << bit) == (1 << bit):
|
|
247 raise STLinkException(msg)
|
|
248 return sr & mask == mask
|
|
249
|
131
|
250 @registerDevice(0x10016413)
|
|
251 class Stm32F40x(Stm32F4):
|
|
252 """ STM32F40x and STM32F41x device series """
|
|
253 def __init__(self, iface):
|
|
254 super().__init__(iface)
|
|
255 # Assert the proper size for this device:
|
|
256 assert self.FlashSize == 0x100000
|
|
257 """
|
|
258 from 0x8000000 to 0x80FFFFF
|
|
259 4 sectors of 0x4000 (16 kB)
|
|
260 1 sector of 0x10000 (64 kB)
|
|
261 7 of 0x20000 (128 kB)
|
|
262 """
|
|
263 self.sectorsizes = [0x4000] * 4 + [0x10000] + [0x20000] * 7
|
|
264
|