forked from sebastianhodapp/ESPbootloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uploader.py
352 lines (288 loc) · 10.8 KB
/
uploader.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/usr/bin/env python
# Copyright (C) 2015 Peter Magnusson
# For NodeMCU version 0.9.4 build 2014-12-30 and newer.
# Modified by @aug2uag limit to upload only
import os
import serial
import sys
import argparse
import time
import logging
log = logging.getLogger(__name__)
progressNotes = []
save_lua = \
r"""
function recv_block(d)
if string.byte(d, 1) == 1 then
size = string.byte(d, 2)
uart.write(0,'\006')
if size > 0 then
file.write(string.sub(d, 3, 3+size-1))
else
file.close()
uart.on('data')
uart.setup(0,9600,8,0,1,1)
end
else
uart.write(0, '\021' .. d)
uart.setup(0,9600,8,0,1,1)
uart.on('data')
end
end
function recv_name(d) d = string.gsub(d, '\000', '') file.remove(d) file.open(d, 'w') uart.on('data', 130, recv_block, 0) uart.write(0, '\006') end
function recv() uart.setup(0,9600,8,0,1,0) uart.on('data', '\000', recv_name, 0) uart.write(0, 'C') end
"""
CHUNK_END = '\v'
CHUNK_REPLY = '\v'
from serial.tools.miniterm import Miniterm, console, NEWLINE_CONVERISON_MAP
class MyMiniterm(Miniterm):
def __init__(self, serial):
self.serial = serial
self.echo = False
self.convert_outgoing = 2
self.repr_mode = 1
self.newline = NEWLINE_CONVERISON_MAP[self.convert_outgoing]
self.dtr_state = True
self.rts_state = True
self.break_state = False
class Uploader:
BAUD = 9600
PORT = '/dev/ttyUSB0'
TIMEOUT = 5
def expect(self, exp='> ', timeout=TIMEOUT):
t = self._port.timeout
# Checking for new data every 100us is fast enough
lt = 0.0001
if self._port.timeout != lt:
self._port.timeout = lt
end = time.time() + timeout
# Finish as soon as either exp matches or we run out of time (work like dump, but faster on success)
data = ''
while not data.endswith(exp) and time.time() <= end:
data += self._port.read()
self._port.timeout = t
log.debug('expect return: %s', data)
return data
def write(self, output, binary=False):
if not binary:
log.debug('write: %s', output)
else:
log.debug('write binary: %s' % ':'.join(x.encode('hex') for x in output))
self._port.write(output)
self._port.flush()
def writeln(self, output):
self.write(output + '\n')
def exchange(self, output):
self.writeln(output)
return self.expect()
def __init__(self, port = 0, baud = BAUD):
self._port = serial.Serial(port, Uploader.BAUD, timeout=Uploader.TIMEOUT)
# Keeps things working, if following conections are made:
## RTS = CH_PD (i.e reset)
## DTR = GPIO0
self._port.setRTS(False)
self._port.setDTR(False)
# Get in sync with LUA (this assumes that NodeMCU gets reset by the previous two lines)
self.exchange(';'); # Get a defined state
self.writeln('print("%sync%");');
self.expect('%sync%\r\n> ');
if baud != Uploader.BAUD:
log.info('Changing communication to %s baud', baud)
self.writeln('uart.setup(0,%s,8,0,1,1)' % baud)
# Wait for the string to be sent before switching baud
time.sleep(0.1)
self._port.setBaudrate(baud)
# Get in sync again
self.exchange('')
self.exchange('')
self.line_number = 0
def close(self):
self.writeln('uart.setup(0,%s,8,0,1,1)' % Uploader.BAUD)
self._port.close()
def prepare(self):
log.info('Preparing esp for transfer ..')
data = save_lua.replace('9600', '%d' % self._port.baudrate)
lines = data.replace('\r', '').split('\n')
for line in lines:
line = line.strip().replace(', ', ',').replace(' = ', '=')
if len(line) == 0:
continue
d = self.exchange(line)
if 'unexpected' in d or len(d) > len(save_lua)+10:
log.error('ESP not ready, try again momentarily.')
sys.exit()
return
def download_file(self, filename):
chunk_size=256
bytes_read = 0
data=""
while True:
d = self.exchange("file.open('" + filename + r"') print(file.seek('end', 0)) file.seek('set', %d) uart.write(0, file.read(%d))file.close()" % (bytes_read, chunk_size))
cmd, size, tmp_data = d.split('\n', 2)
data=data+tmp_data[0:chunk_size]
bytes_read=bytes_read+chunk_size
if bytes_read > int(size):
break
data = data[0:int(size)]
return data
def write_file(self, path, destination = '', verify = False):
filename = os.path.basename(path)
if not destination:
destination = filename
log.info('Transfering %s' %(filename))
self.writeln("recv()")
r = self.expect('C> ')
if not r.endswith('C> '):
log.error('Unexpected error, exiting now. Please try again momentarily')
sys.exit()
return
log.debug('sending destination filename "%s"', destination)
self.write(destination + '\x00', True)
if not self.got_ack():
log.error('Fatal error, exiting now')
sys.exit()
return
f = open( path, 'rt' ); content = f.read(); f.close()
log.debug('sending %d bytes in %s' % (len(content), filename))
pos = 0
chunk_size = 128
error = False
while pos < len(content):
rest = len(content) - pos
if rest > chunk_size:
rest = chunk_size
data = content[pos:pos+rest]
if not self.write_chunk(data):
d = self.expect()
log.error('Bad chunk response "%s" %s' % (d, ':'.join(x.encode('hex') for x in d)))
return
pos += chunk_size
log.debug('sending zero block')
#zero size block
self.write_chunk('')
if verify:
log.info('Verifying...')
data = self.download_file(destination)
if content != data:
log.error('Verification failed.')
def got_ack(self):
log.debug('waiting for ack')
r = self._port.read(1)
log.debug('ack read %s', r.encode('hex'))
return r == '\x06' #ACK
def write_chunk(self, chunk):
log.debug('writing %d bytes chunk' % len(chunk))
data = '\x01' + chr(len(chunk)) + chunk
if len(chunk) < 128:
padding = 128 - len(chunk)
log.debug('pad with %d characters' % padding)
data = data + (' ' * padding)
log.debug("packet size %d" % len(data))
self.write(data)
return self.got_ack()
def file_remove(self, path):
log.info('Remove '+path)
cmd = 'file.remove("%s")' % path
r = self.exchange(cmd)
log.info(r)
return r
def arg_auto_int(x):
return int(x, 0)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description = 'NodeMCU Lua file uploader', prog = 'nodemcu-uploader')
parser.add_argument(
'--verbose',
help = 'verbose output',
action = 'store_true',
default = False)
parser.add_argument(
'--port', '-p',
help = 'Serial port device',
default = Uploader.PORT)
parser.add_argument(
'--baud', '-b',
help = 'Serial port baudrate',
type = arg_auto_int,
default = Uploader.BAUD)
subparsers = parser.add_subparsers(
dest='operation',
help = 'Run nodemcu-uploader {command} -h for additional help')
upload_parser = subparsers.add_parser(
'upload',
help = 'Path to one or more files to be uploaded. Destination name will be the same as the file name.')
upload_parser.add_argument(
'--filename', '-f',
help = 'File to upload. You can specify this option multiple times.',
action='append')
upload_parser.add_argument(
'--destination', '-d',
help = 'Name to be used when saving in NodeMCU. You should specify one per file.',
action='append')
upload_parser.add_argument('filename', nargs='+', help = 'Lua file to upload. Use colon to give alternate destination.')
upload_parser.add_argument(
'--compile', '-c',
help = 'If file should be uploaded as compiled',
action='store_true',
default=False
)
upload_parser.add_argument(
'--verify', '-v',
help = 'To verify the uploaded data.',
action='store_true',
default=False
)
upload_parser.add_argument(
'--dofile', '-e',
help = 'If file should be run after upload.',
action='store_true',
default=False
)
upload_parser.add_argument(
'--terminal', '-t',
help = 'If miniterm should claim the port after all uploading is done.',
action='store_true',
default=False
)
upload_parser.add_argument(
'--restart', '-r',
help = 'If esp should be restarted',
action='store_true',
default=False
)
args = parser.parse_args()
formatter = logging.Formatter('%(message)s')
logging.basicConfig(level=logging.INFO, format='%(message)s')
if args.verbose:
log.setLevel(logging.DEBUG)
uploader = Uploader(args.port, args.baud)
if args.operation == 'upload':
sources = args.filename
destinations = []
for i in range(0, len(sources)):
sd = sources[i].split(':')
if len(sd) == 2:
destinations.append(sd[1])
sources[i]=sd[0]
else:
destinations.append(sd[0])
if len(destinations) == len(sources):
uploader.prepare()
for f, d in zip(sources, destinations):
if args.compile:
uploader.file_remove(os.path.splitext(d)[0]+'.lc')
uploader.write_file(f, d, args.verify)
if args.compile and d != 'init.lua':
uploader.file_compile(d)
uploader.file_remove(d)
if args.dofile:
uploader.file_do(os.path.splitext(d)[0]+'.lc')
elif args.dofile:
uploader.file_do(d)
else:
raise Exception('This custom script can only perform upload operation. For full operations please download the nodemcu-uploader.py script from Github.')
if args.terminal:
uploader.terminal()
if args.restart:
uploader.node_restart()
log.info('Success. Exit.')
uploader.close()