-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbulk_upload_firmware.py
executable file
·259 lines (205 loc) · 8.67 KB
/
bulk_upload_firmware.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/usr/bin/env python3
# bulk_upload_firmware.py
# This module is used to automatically install firmware when devices are
# connected to the computer's USB ports.
import sys, os, re, time, argparse
import esptool
# Detect newly connected serial ports of interest
serial_port_pattern = {
'darwin': r'tty\.wchusbserial.*|tty\.usbserial.*|tty\.usbmodem.*',
'linux': r'ttyUSB.*|ttyACM.*|ttyS[0-9]',
}
# Detect devices with know names
device_id_patterns = [
{ 'name': 'BLINX', 'patterns': ['BLX([0-9][0-9]*)'] },
{ 'name': '', 'patterns': ['()'] }, # catch all
]
# Id generation either by extracting the device id from serial port name or
# generating a fresh one.
id_counter = 0
def generate_id(template, start):
global id_counter
m = re.match('^([^#]*)(##*)([^#]*)$', template)
if m:
g = m.groups()
if len(g) == 3:
i = id_counter
id_counter += 1
return g[0] + str(i+start).zfill(len(g[1])) + g[2]
return template
def device_id_from_port(port, template, start):
for i in range(len(device_id_patterns)):
name = device_id_patterns[i]['name']
for pattern in device_id_patterns[i]['patterns']:
m = re.match('^.*('+pattern+')$', port)
if m:
g = m.groups()
if len(g) == 2:
if name == '':
return generate_id(template, start)
else:
return name + g[1]
return None
# Detect newly connected serial ports of interest
def search_for_serial_ports():
dir = '/dev'
pattern = serial_port_pattern.get(sys.platform)
ports = set()
for name in os.listdir(dir):
if re.fullmatch(pattern, name):
ports.add(dir + '/' + name)
return ports
def observe_serial_ports_for_changes(notify):
connected = set()
while True:
ports = search_for_serial_ports()
for port in connected - ports:
notify(port, False)
for port in ports - connected:
notify(port, True)
connected = ports
time.sleep(0.1)
# Detect newly connected devices and upload appropriate firmware
def get_device(port):
try:
return esptool.cmds.detect_chip(port=port)
except:
return None
def get_chip(dev):
try:
return dev.get_chip_description().split(' ')[0]
except:
return None
def get_mac(dev):
try:
return ':'.join(map(lambda x: "%02x" % x, dev.read_mac()))
except:
return None
def detect_device_and_upload_firmware(port, firmware_dir, device_id, start, ident):
dev = get_device(port)
if dev:
chip = get_chip(dev)
if chip:
mac = get_mac(dev)
if mac:
upload(port, firmware_dir, device_id, start, ident, dev, chip, mac)
def upload(port, firmware_dir, device_id, start, ident, dev, chip, mac):
chip_dir = firmware_dir + '/' + chip
if not os.path.exists(chip_dir) or not os.path.isdir(chip_dir):
print('*** ERROR: no directory ' + chip_dir)
return
firmware_files = list(os.listdir(chip_dir))
bin_files = list(filter(lambda name: name.endswith('.bin'), firmware_files))
common_dir = firmware_dir + '/common'
run_file = None # file to run at end of upload
if len(bin_files) == 0:
print('*** ERROR: no .bin files in ' + chip_dir)
elif len(bin_files) >= 2:
print('*** ERROR: 2 or more .bin files in ' + chip_dir)
else:
single_device = not ('#' in device_id)
if not single_device:
device_id = device_id_from_port(port, device_id, start)
bin_file = chip_dir + '/' + bin_files[0]
print('###############################################################################')
print('###################################### ' + device_id)
print('###################################### ' + mac)
print('###################################### ' + chip)
print('###################################### ' + bin_file)
if ident:
print('###################################### ' + ident)
print('###############################################################################')
# return
baud = '460800'
baud = '115200' # play it safe (seems to cause problems on Windows running under Parallels desktop)
esptool_run(['--port', port, '--baud', baud, 'erase_flash'])
esptool_run(['--port', port, '--baud', baud, 'write_flash', '-z', '0x0', bin_file])
dev._port.close() # release serial port so ampy can use it
delay = '0.3'
baud = '115200'
def generate_ident(file):
filename = 'idents/' + device_id + '.' + file
f = open(filename, 'w')
# TODO: Add other configuration variables
f.write('id = ' + repr(device_id) + '\n')
f.write('mac = ' + repr(mac) + '\n')
f.write('chip = ' + repr(chip) + '\n')
if ident:
f.write(ident.replace('\\n','\n') + '\n')
f.close()
return filename
def upload_dir(dir):
nonlocal run_file
def rmdir(remote_path):
print('ampy rmdir', remote_path)
ampy_run(['--port', port, '--baud', baud, '--delay', delay, 'rmdir', remote_path])
def mkdir(remote_path):
print('ampy mkdir', remote_path)
ampy_run(['--port', port, '--baud', baud, '--delay', delay, 'mkdir', remote_path])
def put(local_path, remote_path):
print('ampy put', local_path, remote_path)
ampy_run(['--port', port, '--baud', baud, '--delay', delay, 'put', local_path, remote_path])
if dir != '': dir = dir + '/'
files = list(os.listdir(common_dir + '/' + dir))
for file in files:
local_path = common_dir + '/' + dir + file
remote_path = dir + file
if os.path.isdir(local_path):
rmdir(remote_path)
mkdir(remote_path)
upload_dir(remote_path)
elif file.endswith('.py') or file.endswith('.mpy'):
if file.endswith('_test.py'):
run_file = local_path
else:
is_ident = file.endswith('_ident.py')
print(local_path, ident)
if is_ident: local_path = generate_ident(file)
put(local_path, remote_path)
upload_dir('')
if run_file:
ampy_run(['--port', port, '--baud', baud, '--delay', delay, 'run', '--no-output', run_file])
print('##################################### done with ' + device_id)
if single_device:
sys.exit(0) # quit after this device done
def esptool_run(args):
command = 'esptool ' + ' '.join(args)
print(command)
esptool.main(args)
def ampy_run(args):
command = 'ampy ' + ' '.join(args)
print(command)
os.system(command)
# Start uploading firmware automatically when devices are connected
import threading
def main(port, firmware_dir, device_id, start, ident, bulk):
def serial_port_notification(port, connection):
def handle_notification():
if connection:
print('##################################### connected: ' + port)
detect_device_and_upload_firmware(port, firmware_dir, device_id, start, ident)
else:
print('##################################### disconnected: ' + port)
if bulk:
t = threading.Thread(target=handle_notification)
t.start()
else:
handle_notification() # don't do it concurrently
if port is None:
observe_serial_ports_for_changes(serial_port_notification)
else:
detect_device_and_upload_firmware(port, firmware_dir, device_id, start, ident)
def cli():
parser = argparse.ArgumentParser(
prog = 'bulk_upload_firmware',
description = 'Upload firmware to Espressif based microcontroller boards allowing\nupload to be done for multiple devices as soon as they are connected to\na USB port, with sequentially allocated unique device ids (--bulk flag).')
parser.add_argument('--port', default=None)
parser.add_argument('--dir', default='firmware/default')
parser.add_argument('--id', default='DEV#')
parser.add_argument('--start', type=int, default=0)
parser.add_argument('--ident', default='')
parser.add_argument('--bulk', action='store_true')
args = parser.parse_args()
main(args.port, args.dir, args.id, args.start, args.ident, args.bulk)
if __name__ == '__main__':
cli()