annotate python/stm32.py @ 251:6ed3d3a82a63

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