annotate python/stm32.py @ 128:51cc127648e4

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