-
Notifications
You must be signed in to change notification settings - Fork 25
/
upload.py
167 lines (138 loc) · 3.69 KB
/
upload.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
from optparse import OptionParser
import serial
import xmodem
import os, sys, time
import logging
import pyprind
logging.basicConfig()
parser = OptionParser(usage="python %prog [options]")
parser.add_option("-f", dest="bin_path", help="path of bin to be upload")
parser.add_option("-c", dest="com_port", help="COM port, can be COM1, COM2, ..., COMx")
parser.add_option("--cm4", action="store_true", dest="cm4")
parser.add_option("--n9", action="store_true", dest="n9")
parser.add_option("--ldr", action="store_true", dest="ldr")
parser.add_option("-d", action="store_true", dest="debug")
(opt, args) = parser.parse_args()
if not opt.n9 and not opt.cm4 and not opt.ldr:
print >> sys.stderr, "\nError: Invalid parameter!! Please specify which target to program.\n"
parser.print_help()
sys.exit(-1)
pass
debug = opt.debug
if not opt.bin_path or not opt.com_port:
print >> sys.stderr, "\nError: Invalid parameter!! Please specify COM port and bin.\n"
parser.print_help()
sys.exit(-1)
if not os.path.exists(opt.bin_path):
print >> sys.stderr, "\nError: File [ %s ] not found !!!\n" % (opt.bin_path)
parser.print_help()
sys.exit(-1)
#s = serial.Serial(opt.com_port, 115200)
#s.rts = 0;
s = serial.Serial()
def resetIntoBootloader():
s.baudrate = 115200
s.port = opt.com_port
s.timeout = 1
#s.rts = 0;
s.open()
#init Com port to orginal state
s.setRTS(False)
s.setDTR(False)
time.sleep(0.05)
#Discharge RTS
s.setRTS(True)
time.sleep(0.05)
#Pull down only DTR
s.setDTR(True)
#time.sleep(0.01)
s.setRTS(False)
pass
print >> sys.stderr, "Please push the Reset button"
error_count = 0
c_count = 0
retry = 0
start_time = time.time()
resetIntoBootloader()
while 1:
c = s.read()
if debug:
print >> sys.stderr, "Read: "+c.encode('hex')
pass
if c == "C":
c_count = c_count +1
if c!=0 and c!="C":
error_count = error_count +1
if c_count>1:
print >> sys.stderr, "Reset button pushed, start uploading bootloader"
break
pass
if error_count>3:
print "Error - Not Reading the start flag"
retry = retry +1
error_count = 0
c_count = 0
start_time = time.time()
s.close()
resetIntoBootloader()
if time.time() - start_time > 3.0:
print "Error - Timeout"
retry = retry +1
error_count = 0
c_count = 0
start_time = time.time()
s.close()
resetIntoBootloader()
pass
if retry>3:
print "Exiting"
exit()
pass
statinfo = os.stat('./bootloader.bin')
bar = pyprind.ProgBar(statinfo.st_size/128+1)
def getc(size, timeout=1):
return s.read(size)
def putc(data, timeout=1):
bar.update()
return s.write(data)
def putc_user(data, timeout=1):
bar_user.update()
return s.write(data)
def pgupdate(read, total):
print "\r%d/%d bytes (%2.f%%) ..." % (read, total, read*100/total)
m = xmodem.XMODEM(getc, putc)
stream = open('./bootloader.bin', 'rb')
m.send(stream)
s.baudrate = 115200
print >> sys.stderr, "Bootloader uploaded, start uploading user bin"
time.sleep(1)
if opt.ldr:
s.write("1\r")
pass
if opt.n9:
s.write("3\r")
pass
if opt.cm4:
s.write("2\r")
pass
s.flush()
s.flushInput()
flag = 1
while flag:
c = s.read()
if c =='C':
flag = 0
pass
pass
s.flush()
s.flushInput()
statinfo_bin = os.stat(opt.bin_path)
bar_user = pyprind.ProgBar(statinfo_bin.st_size/128+2)
stream = open(opt.bin_path, 'rb')
m = xmodem.XMODEM(getc, putc_user)
m.send(stream)
print >> sys.stderr, "Program uploaded, starting the program"
time.sleep(1)
s.write("C\r")
s.flush()
s.flushInput()