annotate python/stm32.py @ 266:649884d9dc61 Working chain 1

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