-
Notifications
You must be signed in to change notification settings - Fork 2
/
sqn.py
145 lines (136 loc) · 3.83 KB
/
sqn.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
import time
import os
import binascii
u = None
pins = {
# pins = ( TXD, RXD, RTS, CTS )
'GPy' : ( 'P5', 'P98', 'P7', 'P99'),
'FiPy' : ('P20', 'P18', 'P19', 'P17'),
'PyGo1': ('P20', 'P18', 'P19', 'P17'),
}
def init(br=None):
global u
if not br:
if u:
# already initialized
return True
else:
# not initialized and no br specified, so, we guess
return init(921600) or init(115200)
else:
s = os.uname().sysname
p=pins[s]
print('init:', br, s, p)
u = UART(1, baudrate=br, pins=p, timeout_chars=10)
try:
at(verbose=True)
print('init(', br, ') succeeded')
return True
except Exception as e:
print('init(', br, ') failed', e)
try:
at()
print('init(', br, ') succeeded')
return True
except:
print('init(', br, ') failed', e)
return False
def help(verbose=False):
init()
r = at('AT+CLAC', do_return=True)
if verbose:
for cmd in r:
at(cmd + '=?')
else:
for cmd in r:
print(cmd, end=' ')
def at(cmd='', pretty=True, verbose=False, do_return=False):
init()
if do_return:
retval = []
else:
retval = False
if not cmd:
cmd='AT'
verbose = True
if verbose:
print('[', cmd, ']', sep='', end=':')
u.write((cmd + '\r\n').encode())
time.sleep_ms(1000)
r = u.read()
# print("[", len(r), "]<", binascii.hexlify(r), ">", sep='', end='')
try:
for line in r.decode().split('\r\n'):
if len(line) == 0:
continue
else:
if do_return:
retval += [line]
if verbose:
print(line)
else:
if line == 'OK':
retval = True
if verbose:
print(line)
elif line == 'ERROR':
retval = False
if verbose:
print(line)
else:
print(line)
return retval
except:
print("[", len(r), "]<", binascii.hexlify(r), ">", sep='', end='')
print()
def smod():
init()
r = at('AT+SMOD?', do_return=True)
print(r, end=' ')
m = int(r[0])
if m == 0:
print('FFH (bootloader)')
elif m == 1:
print('mTools')
elif m == 2:
print('FFF (application)')
elif m == 3:
print('dunno')
elif m == 4:
print('upgrade???')
else:
print('?')
if __name__ == '__main__':
s = os.uname().sysname
if s == 'PyGo1':
init(115200)
at()
else:
# init(921600) or init(115200)
init()
at('AT', verbose=True)
smod()
if False:
# which mode are we in right now
at('AT+SMOD?') # 2
at('AT+BMOD?') # FFF
# which mode is configured for next boot
at('AT+SMSWBOOT?')
# change mode for next boot
at('AT+SMSWBOOT=?', verbose=True)
# +SMSWBOOT: mode[,reboot]
# mode: 0=FFH, 1=FFF, 2=UPDATER, 3=RECOVERY
# reboot: 0=Do not reboot, 1=reboot
at('AT+SMSWBOOT=0', verbose=True) # FFH
at('AT+SMSWBOOT=1', verbose=True) # FFF
at('AT+SMSWBOOT=2', verbose=True) # UPDATER
at('AT+SMSWBOOT=3', verbose=True) # RECOVERY
at('AT^RESET', verbose=True)
at()
help(True)
help(verbose=True)
at('AT+BIND=?')
at('AT+BIND?')
at("ATI1")
# UE:5.0.0.0d;att:5.1.0.0d
# LR5.1.1.0-39529