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