2024-10-25 06:24:14 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
# A helper utility to allow uploading of files to NodeMCU versions which use
|
|
|
|
# the 'console' module, rather than having the console multiplexed via the
|
|
|
|
# 'uart' module.
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import serial
|
|
|
|
import sys
|
|
|
|
import atexit
|
|
|
|
|
|
|
|
STX = 0x02
|
|
|
|
ETX = 0x03
|
|
|
|
DLE = 0x10
|
|
|
|
|
|
|
|
# The loader we send to NodeMCU so that we may upload a (binary) file safely.
|
|
|
|
# Uses STX/ETX/DLE framing and escaping.
|
2024-10-27 04:34:15 +01:00
|
|
|
# The CDC-ACM console gets overwhelmed unless we throttle the send by using
|
|
|
|
# an ack scheme. We use a fake prompt for simplicity's sake for that.
|
2024-10-25 06:24:14 +02:00
|
|
|
loader = b'''
|
|
|
|
(function()
|
|
|
|
local function transmission_receiver(chunk_cb)
|
|
|
|
local inframe = false
|
|
|
|
local escaped = false
|
|
|
|
local done = false
|
2024-10-27 04:34:15 +01:00
|
|
|
local len = 0
|
2024-10-25 06:24:14 +02:00
|
|
|
local STX = 2
|
|
|
|
local ETX = 3
|
|
|
|
local DLE = 16
|
|
|
|
local function dispatch(data, i, j)
|
|
|
|
if (j - i) < 0 then return end
|
|
|
|
chunk_cb(data:sub(i, j))
|
|
|
|
end
|
|
|
|
return function(data)
|
|
|
|
if done then return end
|
2024-10-27 04:34:15 +01:00
|
|
|
len = len + #data
|
|
|
|
while len >= @BLOCKSIZE@ do
|
|
|
|
len = len - @BLOCKSIZE@
|
|
|
|
console.write("> ")
|
|
|
|
end
|
2024-10-25 06:24:14 +02:00
|
|
|
local from
|
|
|
|
local to
|
|
|
|
for i = 1, #data
|
|
|
|
do
|
|
|
|
local b = data:byte(i)
|
|
|
|
if inframe
|
|
|
|
then
|
|
|
|
if not from then from = i end -- first valid byte
|
2024-10-27 04:34:15 +01:00
|
|
|
if escaped then escaped = false else
|
2024-10-25 06:24:14 +02:00
|
|
|
if b == DLE
|
|
|
|
then
|
|
|
|
escaped = true
|
|
|
|
dispatch(data, from, i-1)
|
|
|
|
from = nil
|
|
|
|
elseif b == ETX
|
|
|
|
then
|
|
|
|
done = true
|
|
|
|
to = i-1
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else -- look for an (unescaped) STX to sync frame start
|
|
|
|
if b == DLE then escaped = true
|
|
|
|
elseif b == STX and not escaped then inframe = true
|
2024-10-27 04:34:15 +01:00
|
|
|
else escaped = false end
|
2024-10-25 06:24:14 +02:00
|
|
|
end
|
|
|
|
-- else ignore byte outside of framing
|
|
|
|
end
|
|
|
|
if from then dispatch(data, from, to or #data) end
|
|
|
|
if done then chunk_cb(nil) end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function file_saver(name)
|
|
|
|
local f = io.open(name, "w")
|
|
|
|
return function(chunk)
|
2024-10-27 04:34:15 +01:00
|
|
|
if chunk then f:write(chunk) else
|
2024-10-25 06:24:14 +02:00
|
|
|
f:close()
|
|
|
|
console.on("data", 0, nil)
|
|
|
|
console.mode(console.INTERACTIVE)
|
2024-10-27 04:34:15 +01:00
|
|
|
console.write("done")
|
2024-10-25 06:24:14 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-10-27 04:34:15 +01:00
|
|
|
console.on("data", 0, transmission_receiver(file_saver(
|
|
|
|
"@FILENAME@")))
|
2024-10-25 06:24:14 +02:00
|
|
|
console.mode(console.NONINTERACTIVE)
|
2024-10-27 04:34:15 +01:00
|
|
|
console.write("ready")
|
2024-10-25 06:24:14 +02:00
|
|
|
end)()
|
|
|
|
'''
|
|
|
|
|
|
|
|
def parse_args():
|
|
|
|
"""Parse command-line arguments."""
|
|
|
|
parser = argparse.ArgumentParser(description="NodeMCU file uploader.")
|
|
|
|
parser.add_argument("file", help="File to read data from.")
|
|
|
|
parser.add_argument("name", nargs="?", help="Name to upload file as.")
|
|
|
|
parser.add_argument("-p", "--port", default="/dev/ttyUSB0", help="Serial port (default: /dev/ttyUSB0).")
|
|
|
|
parser.add_argument("-b", "--bitrate", type=int, default=115200, help="Bitrate (default: 115200).")
|
2024-10-27 04:34:15 +01:00
|
|
|
parser.add_argument("-s", "--blocksize", type=int, default=80, help="Block size of file data, tweak for speed/reliability of upload (default: 80)")
|
2024-10-25 06:24:14 +02:00
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
def load_file(filename):
|
|
|
|
"""Open a file and read its contents into memory."""
|
|
|
|
try:
|
|
|
|
with open(filename, "r") as f:
|
|
|
|
data = f.read()
|
|
|
|
return data
|
|
|
|
except IOError as e:
|
|
|
|
print(f"Error reading file {filename}: {e}")
|
|
|
|
sys.exit(1)
|
|
|
|
|
2024-10-27 04:34:15 +01:00
|
|
|
def xprint(msg):
|
|
|
|
print(msg, end='', flush=True)
|
|
|
|
|
|
|
|
def wait_prompt(ser, ignore):
|
2024-10-25 06:24:14 +02:00
|
|
|
"""Wait until we see the '> ' prompt, or the serial times out"""
|
|
|
|
buf = bytearray()
|
|
|
|
b = ser.read()
|
2024-10-27 04:34:15 +01:00
|
|
|
timeout = 5
|
|
|
|
while timeout > 0:
|
|
|
|
if b == b'':
|
|
|
|
timeout -= 1
|
|
|
|
xprint('!')
|
|
|
|
else:
|
|
|
|
buf.extend(b)
|
|
|
|
if not ignore and buf.find(b'Lua error:') != -1:
|
|
|
|
xprint(buf.decode())
|
|
|
|
line = ser.readline()
|
|
|
|
while line != b'':
|
|
|
|
xprint(line.decode())
|
|
|
|
line = ser.readline()
|
|
|
|
sys.exit(1)
|
2024-10-25 06:24:14 +02:00
|
|
|
if buf.find(b'> ') != -1:
|
|
|
|
return True
|
|
|
|
b = ser.read()
|
2024-10-27 04:34:15 +01:00
|
|
|
xprint(buf.decode())
|
|
|
|
return False
|
|
|
|
|
|
|
|
def wait_line_match(ser, match, timeout):
|
|
|
|
"""Wait until the 'match' string is found within a line, or times out"""
|
|
|
|
line = ser.readline()
|
|
|
|
while timeout > 0:
|
|
|
|
if line.find(match) != -1:
|
|
|
|
return True
|
|
|
|
elif line == b'':
|
|
|
|
timeout -= 1
|
|
|
|
xprint('!')
|
2024-10-25 06:24:14 +02:00
|
|
|
return False
|
|
|
|
|
|
|
|
def sync(ser):
|
|
|
|
"""Get ourselves to a clean prompt so we can understand the output"""
|
|
|
|
ser.write(b'\x03\x03\n')
|
2024-10-27 04:34:15 +01:00
|
|
|
if not wait_prompt(ser, True):
|
|
|
|
return False
|
2024-10-25 06:24:14 +02:00
|
|
|
ser.write(b"print('sync')\n")
|
2024-10-27 04:34:15 +01:00
|
|
|
return wait_line_match(ser, b'sync', 5) and wait_prompt(ser, True)
|
2024-10-25 06:24:14 +02:00
|
|
|
|
|
|
|
def cleanup():
|
|
|
|
"""Cleanup function to send final data and close the serial port."""
|
|
|
|
if ser:
|
|
|
|
# Ensure we don't leave the console in a weird state if we get
|
|
|
|
# interrupted.
|
|
|
|
ser.write(ETX)
|
|
|
|
ser.write(ETX)
|
|
|
|
ser.write(b"\n")
|
|
|
|
ser.readline()
|
|
|
|
ser.close()
|
|
|
|
|
|
|
|
def line_interactive_send(ser, data):
|
|
|
|
"""Send one line at a time, waiting for the prompt before sending next"""
|
|
|
|
for line in data.split(b'\n'):
|
|
|
|
ser.write(line)
|
|
|
|
ser.write(b'\n')
|
2024-10-27 04:34:15 +01:00
|
|
|
if not wait_prompt(ser, False):
|
|
|
|
return False
|
|
|
|
xprint('.')
|
|
|
|
return True
|
|
|
|
|
|
|
|
def chunk_data(data, size):
|
|
|
|
"""Split a data block into chunks"""
|
|
|
|
return (data[0+i:size+i] for i in range(0, len(data), size))
|
|
|
|
|
|
|
|
def chunk_interactive_send(ser, data, size):
|
|
|
|
"""Send the data chunked into blocks, waiting for an ack in between"""
|
|
|
|
n=0
|
|
|
|
for chunk in chunk_data(data, size):
|
|
|
|
ser.write(chunk)
|
|
|
|
if len(chunk) == size and not wait_prompt(ser, False):
|
|
|
|
print(f"failed after sending {n} blocks")
|
|
|
|
return False
|
|
|
|
xprint('.')
|
|
|
|
n += 1
|
|
|
|
print(f" ok, sent {n} blocks")
|
|
|
|
return True
|
2024-10-25 06:24:14 +02:00
|
|
|
|
|
|
|
def transmission(data):
|
|
|
|
"""Perform STX/ETX/DLE framing and escaping of the data"""
|
|
|
|
out = bytearray()
|
|
|
|
out.append(STX)
|
|
|
|
for b in data:
|
|
|
|
if b == STX or b == ETX or b == DLE:
|
|
|
|
out.append(DLE)
|
|
|
|
out.append(ord(b))
|
|
|
|
out.append(ETX)
|
|
|
|
return bytes(out)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
args = parse_args()
|
|
|
|
|
|
|
|
upload_name = args.name if args.name else args.file
|
|
|
|
|
|
|
|
file_data = load_file(args.file)
|
2024-10-27 04:34:15 +01:00
|
|
|
print(f"Loaded {len(file_data)} bytes of file contents")
|
|
|
|
|
|
|
|
blocksize = bytes(str(args.blocksize).encode())
|
2024-10-25 06:24:14 +02:00
|
|
|
|
|
|
|
try:
|
2024-10-27 04:34:15 +01:00
|
|
|
ser = serial.Serial(port=args.port, baudrate=args.bitrate, timeout=1)
|
2024-10-25 06:24:14 +02:00
|
|
|
except serial.SerialException as e:
|
|
|
|
print(f"Error opening serial port {args.port}: {e}")
|
|
|
|
sys.exit(1)
|
|
|
|
|
2024-10-27 04:34:15 +01:00
|
|
|
print("Synchronising serial...", end='')
|
2024-10-25 06:24:14 +02:00
|
|
|
if not sync(ser):
|
2024-10-27 04:34:15 +01:00
|
|
|
print("\nNodeMCU not responding\n")
|
2024-10-25 06:24:14 +02:00
|
|
|
sys.exit(1)
|
|
|
|
|
2024-10-27 04:34:15 +01:00
|
|
|
print(f' ok\nUploading "{args.file}" as "{upload_name}"')
|
2024-10-25 06:24:14 +02:00
|
|
|
|
|
|
|
atexit.register(cleanup)
|
|
|
|
|
2024-10-27 04:34:15 +01:00
|
|
|
xprint("Sending loader")
|
|
|
|
ok = line_interactive_send(
|
|
|
|
ser, loader.replace(
|
|
|
|
b"@FILENAME@", upload_name.encode()).replace(
|
|
|
|
b"@BLOCKSIZE@", blocksize))
|
|
|
|
|
|
|
|
if ok:
|
|
|
|
xprint(" ok\nWaiting for go-ahead...")
|
|
|
|
ok = wait_line_match(ser, b"ready", 5)
|
2024-10-25 06:24:14 +02:00
|
|
|
|
2024-10-27 04:34:15 +01:00
|
|
|
if ok:
|
|
|
|
xprint(f" ok\nSending file contents (using blocksize {args.blocksize})")
|
|
|
|
ok = chunk_interactive_send(
|
|
|
|
ser, transmission(file_data), int(blocksize))
|
|
|
|
if ok:
|
|
|
|
xprint("Waiting for final ack...")
|
|
|
|
ok = wait_line_match(ser, b"done", 5)
|
|
|
|
ser.write(b"\n")
|
|
|
|
|
|
|
|
if not ok or not wait_prompt(ser, False):
|
|
|
|
print("transmission timed out")
|
|
|
|
sys.exit(1)
|
2024-10-25 06:24:14 +02:00
|
|
|
|
|
|
|
ser.close()
|
|
|
|
ser = None
|
2024-10-27 04:34:15 +01:00
|
|
|
print(" ok\nUpload complete.")
|