forked from pfalcon/PySWD
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathSTM32.py
More file actions
63 lines (50 loc) · 1.71 KB
/
STM32.py
File metadata and controls
63 lines (50 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from SWDCommon import *
flash_base = 0x40023c00
FLASH_PECR = flash_base+0x04
FLASH_PEKEYR = flash_base+0x0C
FLASH_PRGKEYR = flash_base+0x10
FLASH_SR = flash_base+0x18
class STM32:
def __init__ (self, debugPort):
self.ahb = MEM_AP(debugPort, 0)
def halt (self):
# halt the processor core
self.ahb.writeWord(0xE000EDF0, 0xA05F0003)
def unhalt (self):
# unhalt the processor core
self.ahb.writeWord(0xE000EDF0, 0xA05F0000)
def sysReset (self):
# restart the processor and peripherals
self.ahb.writeWord(0xE000ED0C, 0x05FA0004)
def flashUnlock (self):
""" unlock data EEPROM """
self.ahb.writeWord(FLASH_PEKEYR, 0x89ABCDEF)
self.ahb.writeWord(FLASH_PEKEYR, 0x02030405)
def flashLock(self):
""" Lock data EEPROM """
self.ahb.writeWord(FLASH_PECR, 0x1)
def programUnlock(self):
""" unlock program memory """
self.ahb.writeWord(FLASH_PRGKEYR, 0x8C9DAEBF)
self.ahb.writeWord(FLASH_PRGKEYR, 0x13141516)
def programLock(self):
""" lock program memory """
self.ahb.writeWord(FLASH_PECR, 0x2)
def flashErase (self, prog, addr):
""" erase a page of FLASH """
PROG = 1<<3
DATA = 1<<4
ERASE = 1<<9
# start the mass erase
v = ERASE | (PROG if prog else 0)
self.ahb.writeWord(FLASH_PECR, v)
# check the BSY flag
while (self.ahb.readWord(FLASH_SR) & 1) == 1:
print "waiting for erase completion..."
time.sleep(0.01)
self.ahb.writeWord(addr, 0x0)
self.ahb.writeWord(FLASH_PECR, 0x0)
def flashProgram (self):
pass
def flashProgramEnd (self):
pass