Skip to content

Commit eb2869c

Browse files
author
Daniel Campora
committed
sqnsupgrade: Make it compatible with UART upgrade on a PC.
1 parent bb736c6 commit eb2869c

File tree

3 files changed

+101
-55
lines changed

3 files changed

+101
-55
lines changed

lib/sqnsupgrade/sqnsupgrade.py

Lines changed: 63 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@
44
import stp
55
import time
66
import os
7-
from machine import UART
8-
from machine import SD
97

8+
sysname = os.uname().sysname
9+
10+
if 'FiPy' in sysname or 'GPy' in sysname:
11+
from machine import UART
12+
from machine import SD
13+
else: # this is a computer
14+
import serial
1015

1116
FFF_FMT = "<4sIIIIIIIHHIHHIHHHH"
1217
FFF_SLIM_FMT = "<4sIIIIIIIHHHH"
@@ -26,9 +31,14 @@ def read_rsp(s, size=None, timeout=-1):
2631
timeout = 20000
2732
elif timeout is None:
2833
timeout = 0
29-
while not s.any() and timeout > 0:
30-
time.sleep_ms(1)
31-
timeout -= 1
34+
if 'FiPy' in sysname or 'GPy' in sysname:
35+
while not s.any() and timeout > 0:
36+
time.sleep_ms(1)
37+
timeout -= 1
38+
else:
39+
while s.in_waiting <= 0 and timeout > 0:
40+
time.sleep(0.001)
41+
timeout -= 1
3242

3343
if size is not None:
3444
rsp = s.read(size)
@@ -47,62 +57,77 @@ def print_pretty_response(rsp):
4757

4858

4959
def wait_for_modem(s, send=True, expected=b'OK'):
60+
rsp = b''
5061
while True:
5162
if send:
52-
s.write("AT\r\n")
53-
rsp = read_rsp(s, timeout=50)
63+
s.write(b"AT\r\n")
64+
r = read_rsp(s, size=(len(expected) + 4), timeout=50)
65+
if r:
66+
rsp += r
5467
if expected in rsp:
5568
print()
5669
break
5770
else:
5871
print('.', end='', flush=True)
5972
time.sleep(0.5)
6073

61-
def run(file_path, baudrate):
74+
def run(file_path, baudrate, port=None):
75+
global sysname
76+
6277
abort = False
78+
s = None
6379

6480
print('<<< Welcome to the SQN3330 firmware updater >>>')
6581

66-
if '/sd' in file_path:
67-
sd = SD()
68-
time.sleep(0.5)
69-
os.mount(sd, '/sd')
70-
time.sleep(0.5)
82+
if 'FiPy' in sysname or 'GPy' in sysname:
83+
if '/sd' in file_path and not 'sd' in os.listdir('/'):
84+
sd = SD()
85+
time.sleep(0.5)
86+
os.mount(sd, '/sd')
87+
time.sleep(0.5)
7188

72-
blobsize = os.stat(file_path)[6]
73-
blob = open(file_path, "rb")
89+
if 'GPy' in sysname:
90+
pins = ('P5', 'P98', 'P7', 'P99')
91+
else:
92+
pins = ('P20', 'P18', 'P19', 'P17')
7493

75-
if 'FiPy' in os.uname().sysname:
76-
pins = ('P20', 'P18', 'P19', 'P17')
94+
s = UART(1, baudrate=baudrate, pins=pins, timeout_chars=100)
95+
s.read()
7796
else:
78-
pins = ('P5', 'P98', 'P7', 'P99')
97+
if port is None:
98+
raise ValueError('serial port not specified')
99+
s = serial.Serial(port, baudrate=921600, bytesize=serial.EIGHTBITS, timeout=0.1)
100+
s.reset_input_buffer()
101+
s.reset_output_buffer()
79102

80-
s = UART(1, baudrate=baudrate, pins=pins, timeout_chars=100)
81-
s.read()
103+
blobsize = os.stat(file_path)[6]
104+
blob = open(file_path, "rb")
82105

83106
# disable echo
84-
s.write("ATE0\r\n")
85-
response = read_rsp(s)
107+
s.write(b"ATE0\r\n")
108+
response = read_rsp(s, size=6)
86109

110+
s.read(100)
87111
print('Entering recovery mode')
88-
s.write("AT+SMSWBOOT=3,0\r\n")
89-
response = read_rsp(s)
112+
s.write(b"AT+SMSWBOOT=3,0\r\n")
113+
response = read_rsp(s, size=6)
90114
if b'OK' in response:
91115
print('Resetting.', end='', flush=True)
92-
s.write('AT^RESET\r\n')
116+
s.write(b'AT^RESET\r\n')
93117
wait_for_modem(s, send=False, expected=b'+SHUTDOWN')
94118
time.sleep(2)
95119
wait_for_modem(s)
96-
s.write("AT\r\n")
97-
s.write("AT\r\n")
120+
s.write(b"AT\r\n")
121+
s.write(b"AT\r\n")
98122
else:
99123
raise OSError('AT+SMSWBOOT=3,0 failed!')
100124

101125
time.sleep(1)
102126
s.read()
103127

104128
print('Starting STP (DO NOT DISCONNECT POWER!!!)')
105-
s.write('AT+SMSTPU=\"ON_THE_FLY\"\r\n')
129+
s.read(100)
130+
s.write(b'AT+SMSTPU=\"ON_THE_FLY\"\r\n')
106131
response = read_rsp(s, size=4)
107132
if response != b'OK\r\n' and response != b'\r\nOK' and response != b'\nOK':
108133
raise OSError("Invalid answer '%s' from the device" % response)
@@ -118,36 +143,36 @@ def run(file_path, baudrate):
118143

119144
time.sleep(1.5)
120145
s.read()
121-
s.write("AT+SMSWBOOT=1,0\r\n")
122-
response = read_rsp(s)
146+
s.write(b"AT+SMSWBOOT=1,0\r\n")
147+
response = read_rsp(s, size=6)
123148

124149
print('Resetting (DO NOT DISCONNECT POWER!!!).', end='', flush=True)
125150
time.sleep(1.5)
126-
s.write("AT^RESET\r\n")
151+
s.write(b"AT^RESET\r\n")
127152
wait_for_modem(s, send=False, expected=b'+SHUTDOWN')
128153
time.sleep(2)
129154
wait_for_modem(s, send=False, expected=b'+SYSSTART')
130155

131156
if not abort:
132157
time.sleep(0.5)
133158
print('Deploying the upgrade (DO NOT DISCONNECT POWER!!!)...')
134-
s.write("AT+SMUPGRADE\r\n")
135-
response = read_rsp(s, timeout=120000)
159+
s.write(b"AT+SMUPGRADE\r\n")
160+
response = read_rsp(s, size=6, timeout=120000)
136161

137162
print('Resetting (DO NOT DISCONNECT POWER!!!).', end='', flush=True)
138163
time.sleep(1.5)
139-
s.write("AT^RESET\r\n")
164+
s.write(b"AT^RESET\r\n")
140165
wait_for_modem(s, send=False, expected=b'+SHUTDOWN')
141166
time.sleep(2)
142167
wait_for_modem(s, send=False, expected=b'+SYSSTART')
143-
s.write("AT\r\n")
144-
s.write("AT\r\n")
168+
s.write(b"AT\r\n")
169+
s.write(b"AT\r\n")
145170
time.sleep(0.5)
146171
s.read()
147172
print('Upgrade completed!')
148173
print("Here's the current firmware version:")
149174
time.sleep(0.5)
150175
s.read()
151-
s.write("ATI1\r\n")
152-
response = read_rsp(s)
176+
s.write(b"ATI1\r\n")
177+
response = read_rsp(s, size=100)
153178
print_pretty_response(response)

lib/sqnsupgrade/stp.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import struct
44
import time
55
import os
6-
from machine import UART
76

7+
sysname = os.uname().sysname
88

99
# CRC-16(CCIT)
1010
def crc16(s):
@@ -69,18 +69,12 @@ def __str__(self):
6969
return self.s
7070

7171
class SerialDev(object):
72-
def __init__(self, serial, baud, timeout=None):
72+
def __init__(self, serial, baud, timeout=90000): # 90 seconds timeout
7373
self.serial = serial
74-
75-
if 'FiPy' in os.uname().sysname:
76-
pins = ('P20', 'P18', 'P19', 'P17')
77-
else:
78-
pins = ('P5', 'P98', 'P7', 'P99')
79-
80-
self.serial.init(baudrate=baud, pins=pins, timeout_chars=100)
81-
self.timeout = 90000 # 90 seconds
74+
self.timeout = timeout
8275

8376
def read(self, n):
77+
global sysname
8478
_n = n
8579
t = self.timeout
8680
r = b''
@@ -91,8 +85,11 @@ def read(self, n):
9185
if len(r) == n:
9286
break
9387
_n -= len(c)
94-
time.sleep_ms(5)
95-
t -= 5
88+
if 'FiPy' in sysname or 'GPy' in sysname:
89+
time.sleep_ms(2)
90+
else:
91+
time.sleep(0.002)
92+
t -= 2
9693
return r
9794

9895
def write(self, s):
@@ -250,6 +247,8 @@ def open_session(self):
250247

251248

252249
def send_data(self, blobfile, filesize, trials=4):
250+
global sysname
251+
253252
class Trial:
254253
def __init__(self, trials):
255254
self.trials = trials
@@ -267,7 +266,10 @@ def need_retry(self, c, *a, **k):
267266
downloaded = 0
268267

269268
while True:
270-
data = blobfile.read(1536)
269+
if 'FiPy' in sysname or 'GPy' in sysname:
270+
data = blobfile.read(1536)
271+
else:
272+
data = blobfile.read(512)
271273
size = len(data)
272274
if size:
273275
while size:
@@ -365,10 +367,10 @@ def start(elf, elfsize, serial, baud=3686400, retry=None, debug=None, AT=True):
365367
flush_data = dev.read(256)
366368
dev.serial.timeout = 1
367369

368-
ok_msg = "OK\r\n"
370+
ok_msg = b"OK\r\n"
369371
print("Starting AT negociation")
370372

371-
dev.write("AT\n")
373+
dev.write(b"AT\n")
372374
data = dev.read(len(ok_msg))
373375
if data != ok_msg:
374376
print("Remote didn't answered to AT command")
@@ -377,7 +379,7 @@ def start(elf, elfsize, serial, baud=3686400, retry=None, debug=None, AT=True):
377379
return
378380

379381
print("Setting baudrate %d"%baud)
380-
dev.write("AT+IPR=%d\n"%baud)
382+
dev.write(b"AT+IPR=%d\n"%baud)
381383
data = dev.read(len(ok_msg))
382384
if data != ok_msg:
383385
print("Remote failed to set baudrate")
@@ -388,7 +390,7 @@ def start(elf, elfsize, serial, baud=3686400, retry=None, debug=None, AT=True):
388390

389391
time.sleep(0.1)
390392
print("Starting STP")
391-
dev.write("AT+STP\n")
393+
dev.write(b"AT+STP\n")
392394
data = dev.read(len(ok_msg))
393395
if data != ok_msg:
394396
print("Remote failed to start STP")

lib/sqnsupgrade/uartmirror.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from machine import UART
2+
import time
3+
import os
4+
5+
os.dupterm(None)
6+
7+
repl_u = UART(0, baudrate=921600, timeout_chars=10)
8+
9+
if 'FiPy' in os.uname().sysname:
10+
lte_u = UART(1, baudrate=921600, pins=('P20', 'P18', 'P19', 'P17'), timeout_chars=10)
11+
else:
12+
lte_u = UART(1, baudrate=921600, pins=('P5', 'P98', 'P7', 'P99'), timeout_chars=10)
13+
14+
while True:
15+
if repl_u.any():
16+
lte_u.write(repl_u.read())
17+
if lte_u.any():
18+
repl_u.write(lte_u.read())
19+
time.sleep_ms(2)

0 commit comments

Comments
 (0)