Skip to content

Commit

Permalink
Updated OTW script
Browse files Browse the repository at this point in the history
- Added argparse for argument parsing.
- The ExpressLink version is read before OTW and EOL is chosen based on version.
- Starting with v1.X.X of ExpressLink, the module sends an OK message upon entering OTW mode.
  The script has been updated to accomodate this change in behaviour.
  • Loading branch information
dhavalgujar committed Jun 9, 2022
1 parent b3cdc56 commit 86688cc
Showing 1 changed file with 67 additions and 30 deletions.
97 changes: 67 additions & 30 deletions tools/otw.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,31 @@
import os
import serial
import time
import re
import argparse

semver = {"major": '0', "minor": '0', "patch": '0'}

def get_parser():
parser = argparse.ArgumentParser(prog='OTW Update Script',
description='''This script can be used to
switch between firmware versions on Espressif ExpressLink modules/devkits.
The firmware upgrade/downgrade is carried out over the same
UART interface that accepts AT commands.
''')
parser.add_argument('port', help="UART Serial Port")
parser.add_argument('filepath', help="Firmware binary filepath")
parser.add_argument('--blocksize', '-b', type=int, help="Size of each block of the OTW firmware update (the default is 2048)", default=2048)
return parser

def read_semver(input_string):
global semver
regex = r'''(\d+(?:\.\d+)*)'''
version_string = re.findall(regex, input_string)[0]
semver = dict(zip(semver.keys(), version_string.split('.')))

def wait_for_ok_complete():
data = ser.readline();
print(data)
if not data:
print("Timeout reading over serial");
if data == str.encode("OK COMPLETE\n"):
Expand All @@ -17,43 +37,60 @@ def wait_for_ok_complete():

def wait_for_ok():
data = ser.readline();
if int(semver["major"]) > 0:
ok_string = "OK\r\n"
else:
ok_string = "OK\n"
if not data:
print("Timeout reading over serial");
if data == str.encode("OK\n"):
if data == str.encode(ok_string):
return "OK"
return "ERR"

def otw(port, filepath, blocksize):
filestat = os.stat(filepath)
print(f"File size {filestat.st_size}")

if __name__ == "__main__":
if (len(sys.argv) < 3):
print(f"Usage: {sys.argv[0]} <portname> <filename>")
sys.exit()
port = sys.argv[1]
filename = sys.argv[2]
global ser
ser = serial.Serial(port, 115200, timeout=60)

cmd = "AT+CONF? Version\n"
ser.write(str.encode(cmd))

cmd = "AT+OTW "+ str(filestat.st_size) + f",{blocksize}\n"
ser.write(str.encode(cmd))
data = ser.readline();
if data:
read_semver(data.decode())
else:
print("Timeout reading over serial");

stream = open(filepath, 'rb')
total_data_written = 0;

filestat = os.stat(filename)
print(f"File size {filestat.st_size}")
if int(semver["major"]) > 0:
ret = wait_for_ok()
if ret != "OK":
print("\nError in OTW update")
sys.exit()

ser = serial.Serial(port, 115200, timeout=60)
cmd = "AT+OTW "+ str(filestat.st_size) + ",2048\n"
ser.write(str.encode(cmd))
while True:
data = stream.read(blocksize);
if not data:
break;
ser.write(data)
total_data_written += len(data)
ret = wait_for_ok()
percent_str = 'Uploaded ' + str(round(total_data_written * 100 / filestat.st_size, 2)) + '%'
sys.stdout.write('\r%s' % percent_str)
if ret != "OK":
print("\nError in OTW update")
break

stream = open(filename, 'rb')
ret = wait_for_ok_complete()
print("\nDone...")

total_data_written = 0;
while True:
data = stream.read(2048);
if not data:
break;
ser.write(data)
total_data_written += len(data)
ret = wait_for_ok()
percent_str = 'Uploaded ' + str(round(total_data_written * 100 / filestat.st_size, 2)) + '%'
sys.stdout.write('\r%s' % percent_str)
if ret != "OK":
print("\nError in OTW update")
break

ret = wait_for_ok_complete()
print("\nDone...")
if __name__ == "__main__":
parser = get_parser()
args = parser.parse_args()
otw(args.port, args.filepath, args.blocksize)

0 comments on commit 86688cc

Please sign in to comment.