Skip to content

Commit 86688cc

Browse files
committed
Updated OTW script
- 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.
1 parent b3cdc56 commit 86688cc

File tree

1 file changed

+67
-30
lines changed

1 file changed

+67
-30
lines changed

tools/otw.py

+67-30
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,31 @@
44
import os
55
import serial
66
import time
7+
import re
8+
import argparse
79

10+
semver = {"major": '0', "minor": '0', "patch": '0'}
11+
12+
def get_parser():
13+
parser = argparse.ArgumentParser(prog='OTW Update Script',
14+
description='''This script can be used to
15+
switch between firmware versions on Espressif ExpressLink modules/devkits.
16+
The firmware upgrade/downgrade is carried out over the same
17+
UART interface that accepts AT commands.
18+
''')
19+
parser.add_argument('port', help="UART Serial Port")
20+
parser.add_argument('filepath', help="Firmware binary filepath")
21+
parser.add_argument('--blocksize', '-b', type=int, help="Size of each block of the OTW firmware update (the default is 2048)", default=2048)
22+
return parser
23+
24+
def read_semver(input_string):
25+
global semver
26+
regex = r'''(\d+(?:\.\d+)*)'''
27+
version_string = re.findall(regex, input_string)[0]
28+
semver = dict(zip(semver.keys(), version_string.split('.')))
829

930
def wait_for_ok_complete():
1031
data = ser.readline();
11-
print(data)
1232
if not data:
1333
print("Timeout reading over serial");
1434
if data == str.encode("OK COMPLETE\n"):
@@ -17,43 +37,60 @@ def wait_for_ok_complete():
1737

1838
def wait_for_ok():
1939
data = ser.readline();
40+
if int(semver["major"]) > 0:
41+
ok_string = "OK\r\n"
42+
else:
43+
ok_string = "OK\n"
2044
if not data:
2145
print("Timeout reading over serial");
22-
if data == str.encode("OK\n"):
46+
if data == str.encode(ok_string):
2347
return "OK"
2448
return "ERR"
2549

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

27-
if __name__ == "__main__":
28-
if (len(sys.argv) < 3):
29-
print(f"Usage: {sys.argv[0]} <portname> <filename>")
30-
sys.exit()
31-
port = sys.argv[1]
32-
filename = sys.argv[2]
54+
global ser
55+
ser = serial.Serial(port, 115200, timeout=60)
56+
57+
cmd = "AT+CONF? Version\n"
58+
ser.write(str.encode(cmd))
3359

60+
cmd = "AT+OTW "+ str(filestat.st_size) + f",{blocksize}\n"
61+
ser.write(str.encode(cmd))
62+
data = ser.readline();
63+
if data:
64+
read_semver(data.decode())
65+
else:
66+
print("Timeout reading over serial");
67+
68+
stream = open(filepath, 'rb')
69+
total_data_written = 0;
3470

35-
filestat = os.stat(filename)
36-
print(f"File size {filestat.st_size}")
71+
if int(semver["major"]) > 0:
72+
ret = wait_for_ok()
73+
if ret != "OK":
74+
print("\nError in OTW update")
75+
sys.exit()
3776

38-
ser = serial.Serial(port, 115200, timeout=60)
39-
cmd = "AT+OTW "+ str(filestat.st_size) + ",2048\n"
40-
ser.write(str.encode(cmd))
77+
while True:
78+
data = stream.read(blocksize);
79+
if not data:
80+
break;
81+
ser.write(data)
82+
total_data_written += len(data)
83+
ret = wait_for_ok()
84+
percent_str = 'Uploaded ' + str(round(total_data_written * 100 / filestat.st_size, 2)) + '%'
85+
sys.stdout.write('\r%s' % percent_str)
86+
if ret != "OK":
87+
print("\nError in OTW update")
88+
break
4189

42-
stream = open(filename, 'rb')
90+
ret = wait_for_ok_complete()
91+
print("\nDone...")
4392

44-
total_data_written = 0;
45-
while True:
46-
data = stream.read(2048);
47-
if not data:
48-
break;
49-
ser.write(data)
50-
total_data_written += len(data)
51-
ret = wait_for_ok()
52-
percent_str = 'Uploaded ' + str(round(total_data_written * 100 / filestat.st_size, 2)) + '%'
53-
sys.stdout.write('\r%s' % percent_str)
54-
if ret != "OK":
55-
print("\nError in OTW update")
56-
break
57-
58-
ret = wait_for_ok_complete()
59-
print("\nDone...")
93+
if __name__ == "__main__":
94+
parser = get_parser()
95+
args = parser.parse_args()
96+
otw(args.port, args.filepath, args.blocksize)

0 commit comments

Comments
 (0)