forked from ExpressLRS/ExpressLRS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fw.py
86 lines (63 loc) · 2.02 KB
/
fw.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import pylink
import sys
fw = open(sys.argv[1],"rb")
data = fw.read()
fw.close()
#ADDR = 0x8008000
ADDR = 0x8000000
print("file_size = ")
print(len(data))
size = len(data)
jlink = pylink.JLink()
jlink.open()
jlink.set_tif(pylink.enums.JLinkInterfaces.SWD)
jlink.connect('Cortex-M4')
jlink.coresight_configure()
jlink.reset()
tmp = [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]
aligned_size = (size)&0xFFFFFFF8;
print(aligned_size)
for x in range(size):
if (x >= aligned_size):
tmp[x - aligned_size] = data[x]
x = 0
pages = size // 4096
pages+=1
paddr = 0;
print("Pages = ")
print(pages)
for (z) in range(pages):
print("erase addr =")
print(ADDR+paddr)
jlink.memory_write32(0x40020018, [0x8C9DAEBF])
jlink.memory_write32(0x40020018, [0x13141516])
jlink.memory_write32(0x40020000, [0x22])
jlink.memory_write32(0x40020018, [0x8C9DAEBF])
jlink.memory_write32(0x40020018, [0])
jlink.memory_write32(ADDR+paddr, [0xFFFFFFFF])
paddr+=4096
z+=1
sr = [0]
while (sr[0] & 0x1) == 0:
sr = jlink.memory_read32(0x40020008, 1)
jlink.memory_write32(0x40020008, [0x1])
jlink.memory_write32(0x40020018, [0x8C9DAEBF])
jlink.memory_write32(0x40020018, [0x13141516])
jlink.memory_write32(0x40020000, [0x24])
jlink.memory_write32(0x40020018, [0x8C9DAEBF])
jlink.memory_write32(0x40020018, [0])
while x < size:
if (x < aligned_size):
jlink.memory_write32(0x4002000C, [data[x+3] << 24 | data[x+2] << 16 | data[x+1] << 8 | data[x+0]])
jlink.memory_write32(0x40020010, [data[x+7] << 24 | data[x+6] << 16 | data[x+5] << 8 | data[x+4]])
else:
jlink.memory_write32(0x4002000C, [tmp[3] << 24 | tmp[2] << 16 | tmp[1] << 8 | tmp[0]])
jlink.memory_write32(0x40020010, [tmp[7] << 24 | tmp[6] << 16 | tmp[5] << 8 | tmp[4]])
jlink.memory_write32(ADDR+x, [0xFFFFFFFF])
sr = [0]
while (sr[0] & 0x1) == 0:
sr = jlink.memory_read32(0x40020008, 1)
jlink.memory_write32(0x40020008, [0x1])
x+=8
jlink.reset()
jlink.close()