-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathefm32isp.py
executable file
·191 lines (163 loc) · 5.37 KB
/
efm32isp.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env python2
from xmodem import XMODEM
import sys,os,os.path
import time
import serial
from docopt import docopt
RESP_ERR = "unexpected response!"
def ERR(msg,ecode=0):
sys.stderr.write(msg + os.linesep)
if( ecode!= 0):
exit(ecode)
def INFO(msg):
sys.stdout.write(msg + os.linesep)
def CHK(bval, msg, ecode=0):
if not bval:
ERR(msg,ecode)
def get_response(ser):
answer = ""
resp=ser.read()
while resp != "":
answer += resp
resp = ser.read()
return answer
def handle_init(resp):
lines = resp.split("\r\n")
while '' in lines:
lines.remove('')
CHK( "ChipID" in lines[0], RESP_ERR,3)
version,ignore,chipid = lines[0].split(" ")
INFO( "Bootloader version: '%s' ChipID: '%s'" % (version,chipid) )
return (version,chipid)
def upload(ser,path,flashsize,bootloadersize,shouldverify=True,destructive=False):
run = True
while run:
try:
f = open(path,"rb")
except IOError:
CHK( False, "'%s': can't open file" % path,5)
# upload command
if destructive:
ser.write('d')
else:
ser.write('u')
def ser_write(msg,timeout=1):
ser.setWriteTimeout(timeout)
return ser.write(msg)
def ser_read(size,timeout=1):
ser.setTimeout(timeout)
return ser.read(size)
modem = XMODEM(ser_read, ser_write, pad='\xff')
modem.send(f)
f.close()
ser.setTimeout(0)
ser.setWriteTimeout(0)
if shouldverify:
run = not verify(ser,path,flashsize,bootloadersize,destructive)
if run: #verify failed
input_ok = False
while not input_ok:
tmp = raw_input( "Verify failed! Retry? [Y|n]" )
if len(tmp) == 0 or tmp.upper() == 'Y':
input_ok = True
elif tmp.upper() == 'N':
input_ok = True
CHK( False, "Verify failed! Uploaded programm may be inconsistent!", 6)
else:
run = False
# reset command
ser.write('r')
def verify(ser,path,flashsize,bootloadersize,destructive=False):
try:
f = open(path,"rb")
except IOError:
CHK( False, "'%s': can't open file" % path,5)
data = f.read()
f.close()
modem = XMODEM(None,None)
if destructive:
ser.write('v')
#no prefixed bytes, since uploading a bootloader
bootloadersize = 0x0000
else:
ser.write('c')
lines = []
resp=""
while len(lines)<3:
resp+=get_response(ser)
lines = resp.split('\r\n')
CHK( lines[1].startswith("CRC:" ), RESP_ERR, 3 )
testcrc=lines[1][9:]
crc = int(modem.calc_crc(data))
# rest of the flash is 0xFF
for i in xrange( flashsize-len(data)-bootloadersize ):
crc = modem.calc_crc( '\xFF',crc )
crc = hex(crc)[2:].upper()
#extend to 4 chars
crc = (4-len(crc))*"0"+crc
print "CRC",crc,testcrc
if testcrc == crc:
INFO("Verify OK!")
return testcrc == crc
def main(args):
"""
Usage:
efm32isp [(--verify|--noverify)] [options] <binfile>
Options:
-h --help Prints this help message
-d Destructive upload, overwrites the bootloader
-p <port>, --port=<port> Sets the UART port, any valid pyserial string is
possible [default: /dev/ttyUSB0].
-b <port>, --baud=<baud> Sets the UART baud rate [default: 115200].
-f <size>, --flashs=<size> Sets the programmflash size of the MCU (needed for
verify) [default: 0x100000]
-s <size>, --boots=<size> Sets the size reserved for the bootloader (needed
non destructive verify) [default: 0x3000]
"""
argp = docopt(main.__doc__,version="efm32isp 2014-03-28")
try:
ser = serial.Serial(argp["--port"], argp["--baud"], timeout=0, parity=serial.PARITY_NONE)
if not ser.isOpen():
ser.open()
except serial.serialutil.SerialException as ex:
ERR("Couldn't open serial port '" + argp["--port"] + "'" + os.linesep + str(ex),1)
if not ser.isOpen():
ERR("Couldn't open serial port '" + argp["--port"] + "'",1)
sys.stdout.write("Put the chip into bootloader mode!\n")
sys.stdout.write("Waiting for bootloader to respond ")
sys.stdout.flush()
resp = ""
tries = 10
while resp == "":
#trigger auto baud rate configuration
ser.write("U")
time.sleep(1.0/10)
resp = get_response(ser)
for i in range(5):
sys.stdout.write(".")
sys.stdout.flush()
time.sleep(1.0/10)
tries -= 1
if tries < 0:
INFO(" ERROR")
ERR("Bootloader not responding!",2)
INFO("") #newline
handle_init(resp)
if argp["--verify"]:
# only a verify
verify(
ser,
argp["<binfile>"],
int(argp["--flashs"],16),
int(argp["--boots"],16),
argp["-d"])
else:
upload(
ser,
argp["<binfile>"],
int(argp["--flashs"],16),
int(argp["--boots"],16),
not argp["--noverify"],
argp["-d"])
if __name__ == "__main__":
main(sys.argv)