forked from microbuilder/LPC810_CodeBase
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathflushmagic.py
More file actions
executable file
·120 lines (104 loc) · 2.92 KB
/
flushmagic.py
File metadata and controls
executable file
·120 lines (104 loc) · 2.92 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/python
# from
# http://midibel.com/flushmagic.py
#
#
# linux comand line usage example:
#
# ./flushmagic.py /dev/ttyUSB0 blinky.bin
#
#
#
# 2013-Jul-25 Lars Ole Belhage, [email protected]
# A simple script to program a LPC810 via ISP serial interface
# It also calculates the signature and that checks code-read-protection is off
#
# sleep time implemented to give the LPC some time to go into ISP mode
# ChrisMicro, Sep.2014
import sys
import serial
import time
tty = sys.argv[1] if len(sys.argv) > 1 else "/dev/ttyUSB0"
flsh = sys.argv[2] if len(sys.argv) > 2 else ""
ser = serial.Serial(tty, baudrate=115200, bytesize=8, parity='N', stopbits=1, timeout=10, xonxoff=0, rtscts=0)
# sleep time implemented to give the LPC some time to go into ISP mode
# ChrisMicro, Sep.2014
print "wait for ISP mode"
time.sleep(1)
print "start programming"
def sendCmd(cmd):
ser.write(cmd)
ser.flush()
ok = ser.readline().rstrip()
if ok != "0":
print "Error returned from cmd: " + cmd + " code=" + ok
quit()
ser.write("?")
ser.flush()
ser.timeout = 1
j = ser.readline().rstrip()
ser.timeout = 3
if j == "Synchronized":
ser.write("Synchronized\r\n")
ser.flush()
ser.readline().rstrip()
ser.readline().rstrip()
ser.write("12\r\n")
ser.flush()
ser.readline().rstrip()
ser.readline().rstrip()
ser.write("A 0\r\n")
ser.flush()
ser.readline().rstrip()
ser.readline().rstrip()
else:
ser.write("\r\n")
ser.flush()
ok = ser.readline().rstrip()
if ok != "0" and ok != "1":
print "LPC8xx not in initial ISP-mode..."
quit()
sendCmd("J\r\n")
print "PartID: " + ser.readline().rstrip()
sendCmd("K\r\n")
print "BootPromVersion: " + ser.readline().rstrip() + "." + ser.readline().rstrip()
sendCmd("N\r\n")
print "UID: " + ser.readline().rstrip() + "-" + ser.readline().rstrip() + "-" + ser.readline().rstrip() + "-" + ser.readline().rstrip()
if flsh == "":
quit()
f = open(flsh, "rb")
data = bytearray(f.read())
f.close()
dlen = len(data)
rlen = ((dlen + 63) / 64) * 64
data += bytearray(rlen - dlen)
sig = 0
for v in range(0, 7):
sig += (data[4*v+3] << 24) | (data[4*v+2] << 16) | (data[4*v+1] << 8) | (data[4*v+0])
sig ^= 0xffffffff
sig += 1
data[4*7+0] = (sig >> 0) & 0xff
data[4*7+1] = (sig >> 8) & 0xff
data[4*7+2] = (sig >> 16) & 0xff
data[4*7+3] = (sig >> 24) & 0xff
if rlen >= 0x2ff:
magic = (data[0x2fc+3] << 24) | (data[0x2fc+2] << 16) | (data[0x2fc+1] << 8) | (data[0x2fc+0])
if (magic == 0x12345678 or
magic == 0x87654321 or
magic == 0x43218765 or
magic == 0x4E697370):
print "Error: CRAP-Magic word hit: " + str(magic)
quit()
sect = str(dlen / 1024)
sendCmd("U 23130\r\n")
sendCmd("P 0 " + sect + "\r\n")
sendCmd("E 0 " + sect + "\r\n")
b = 0
while b < dlen:
# print "Write block: " + str(b) + "/" + str(dlen)
sendCmd("P 0 " + sect + "\r\n")
sendCmd("W " + str(0x10000000) + " 64\r\n")
ser.write(data[b : b+64])
sendCmd("C " + str(b) + " " + str(0x10000000) + " 64\r\n")
b += 64
print str(dlen) + " bytes was written"