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