-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiflash.py
414 lines (339 loc) · 13.2 KB
/
multiflash.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
#
# SPDX-License-Identifier: MIT
#
import json
import logging
import os
import re
import shutil
import subprocess
import time
from contextlib import contextmanager
from datetime import datetime, timedelta
from pathlib import Path
from textwrap import dedent
from typing import NamedTuple, Optional
from threading import Thread
import psutil
import serial
import serial.tools.list_ports
logging.basicConfig(format="%(asctime)s %(threadName)s %(levelname)s %(message)s",
level=logging.INFO)
DESIRED_CPY_VERSION = "8.0.5"
SOURCE_CONTENT = Path(__file__).parent / "content"
EMPTY_FS_FILES = {
".fseventsd",
".fseventsd/no_log",
".metadata_never_index",
".Trashes",
"code.py",
"lib",
"boot_out.txt",
}
EMPTY_FS_IGNORE = {"fseventsd/no_log"}
COPY_IGNORE = [".DS_Store", "__pycache__", "*.pyc", "._*", ".*"]
seen_devices = dict()
in_progress = set()
most_recent_devices = []
done_devices = set()
DONE_SCRIPT = """
from adafruit_circuitplayground import cp
cp.pixels.brightness = 0.0625
cp.pixels[:] = [0x22cc28 for n in range(10)]
"""
class DeviceInfo(NamedTuple):
serial_no: str
tty_device: Optional[str]
mount_point: str
last_seen_at: datetime
def __ne__(self, other):
return not (self.serial_no, self.tty_device, self.mount_point) == (
other.serial_no,
other.tty_device,
other.mount_point,
)
def __repr__(self):
return f"DeviceInfo({self.serial_no} {self.tty_device}, {self.mount_point})"
def find_devices():
usb_data = json.loads(
subprocess.run(
"/usr/sbin/system_profiler -json SPUSBDataType SPStorageDataType".split(), capture_output=True
).stdout
).get("SPUSBDataType", [])
def recurse(data):
if type(data) is list:
for dev in data:
yield from recurse(dev)
return
if "_items" in data:
yield from recurse(data["_items"])
yield {k: v for k, v in data.items() if k != "_items"}
yield from recurse(usb_data)
def find_mount_point(item: dict) -> Optional[DeviceInfo]:
mount_point = None
try:
media = item.get("Media")
if not media:
return
volumes = media[0].get("volumes", [])
if volumes and "mount_point" in volumes[0]:
mount_point = media[0]["volumes"][0]["mount_point"]
if not mount_point:
parts = psutil.disk_partitions()
for part in parts:
device_path = "/dev/" + media[0]["bsd_name"]
if part.device == device_path:
mount_point = part.mountpoint
break
if not mount_point:
logging.debug("No disk device found for %s", item)
return
serial_no = item["serial_num"]
return DeviceInfo(serial_no=serial_no, tty_device=None, mount_point=mount_point, last_seen_at=datetime.now())
except (TypeError, KeyError, UnboundLocalError) as exc:
logging.exception("Error handling discovered device", exc_info=exc)
def find_serial_port(usb_device: Optional[DeviceInfo]) -> Optional[DeviceInfo]:
if not usb_device:
return None
for serial_port in serial.tools.list_ports.comports():
if not serial_port.serial_number:
continue
if serial_port.serial_number != usb_device.serial_no:
continue
tty_path = serial_port.device.replace("cu", "tty")
with_tty = DeviceInfo(
tty_device=tty_path,
serial_no=usb_device.serial_no,
mount_point=usb_device.mount_point,
last_seen_at=usb_device.last_seen_at,
)
return with_tty
def discover_devices(once=False, specific_serial_no=None, fetch=True):
while True:
if fetch:
most_recent_devices.clear()
most_recent_devices.extend(find_devices())
for item in most_recent_devices:
if item.get("vendor_id") != "0x239a":
continue
device = find_serial_port(find_mount_point(item))
if not device:
continue
if specific_serial_no and device.serial_no == specific_serial_no:
yield device
continue
if (
device.serial_no in seen_devices
and seen_devices[device.serial_no] == device
and (datetime.now() - device.last_seen_at) <= timedelta(seconds=120)
):
logging.warning("Ignoring %s", device.serial_no)
continue
seen_devices[device.serial_no] = device
if device.serial_no in done_devices and device.serial_no not in str(most_recent_devices):
run_script(device, script=DONE_SCRIPT, description="rerun done script")
if device and device.serial_no not in in_progress and device not in done_devices:
yield device
time.sleep(0.1)
if once:
return
def bootloader_flash(device: DeviceInfo):
# Install CircuitPython
logging.info("Installing firmware.uf2 to %s (%s)", device.mount_point, device.serial_no)
(Path(device.mount_point) / "firmware.uf2").write_bytes(
Path("firmware.uf2").read_bytes())
wait_for_device(device)
logging.info("Done bootloader for %s", device.mount_point)
def wait_for_device(device, timeout=60, require_mount=False, reason=""):
started = datetime.now()
logging.info("Waiting for %s %s", device.serial_no, f" ({reason})" if reason else "")
orig_device = device
while True:
if datetime.now() - started > timedelta(seconds=timeout):
raise TimeoutError("Device not seen after %d", timeout)
devices = list(discover_devices(once=True, specific_serial_no=orig_device.serial_no))
ok = False
device = None
if len(devices):
device = devices[0]
ok = True
if require_mount and not device.mount_point:
ok = False
if not device.tty_device:
ok = False
if ok:
return device
time.sleep(0.2)
def get_circuitpython_version(device):
boot_contents = (Path(device.mount_point) / "boot_out.txt").read_text(encoding="utf-8")
# Adafruit CircuitPython 7.2.5 on 2022-04-06; Adafruit Circuit Playground Bluefruit with nRF52840
# Board ID:circuitplayground_bluefruit
version = re.search(r"^Adafruit CircuitPython (\S+) on.*", boot_contents)
# board_id = re.match(r'^Board ID:(\S+)')
return version.group(1)
def content_flash(device: DeviceInfo):
# Start by erasing filesystem
try:
# Check filesystem is clean
mount_point = Path(device.mount_point)
fs_contents = set(str(path.relative_to(mount_point)) for path in sorted(mount_point.rglob("*")))
extras = fs_contents - EMPTY_FS_FILES - EMPTY_FS_IGNORE
missing = EMPTY_FS_FILES - fs_contents - EMPTY_FS_FILES
if extras or missing:
logging.info("Filesystem differences: extra=%s, missing%s", extras, missing)
device = erase_filesystem(device)
wait_for_device(device)
# Check boot version
cpy_version = get_circuitpython_version(device)
if cpy_version < DESIRED_CPY_VERSION:
logging.warning("Circuit Python version is %s", cpy_version)
logging.info("Upgrading")
with serial.Serial(device.tty_device, timeout=5, exclusive=True) as serial_port:
serial_port: serial.Serial
acquire_repl(serial_port)
logging.info("Entering bootloader to upgrade to %s", DESIRED_CPY_VERSION)
serial_port.write(
b"import microcontroller\r" b"microcontroller.on_next_reset(microcontroller.RunMode.BOOTLOADER)\r"
)
logging.info("Waiting for device to restart")
device = wait_for_device(device)
else:
logging.info("Running %s", cpy_version)
logging.info("Copying content to board")
copy_content(device)
os.sync()
logging.info("Done copying to %s", device)
done_devices.add(device.serial_no)
run_script(
device,
script=DONE_SCRIPT,
description="Set LEDs",
)
except (serial.SerialException, OSError) as exc:
logging.error("Exception: %s", str(exc), exc_info=exc)
def copy_content(device):
for src in Path(SOURCE_CONTENT).rglob("*"):
if src.stem == '.DS_Store':
continue
dst = device.mount_point / src.relative_to(SOURCE_CONTENT)
if src.is_dir():
logging.debug("- mkdir %s", dst)
dst.mkdir(0o755, exist_ok=True)
continue
logging.debug("- %s", dst)
shutil.copy(src=src, dst=dst)
shutil.copymode(src=src, dst=dst)
for dotfiles in dst.glob("._*"):
dotfiles.unlink()
def run_script(device, script, serial_port: Optional[serial.Serial] = None, timeout=15, description: str = ""):
logging.info("Obtaining REPL")
script = dedent(script).replace("\n", "\r").encode("utf-8")
def get_port(retry=True, retries=10, interval=1):
for attempt in range(retries if retry else 1):
try:
if serial_port:
return serial_port
return serial.Serial(device.tty_device, timeout=timeout, exclusive=True)
except serial.serialutil.SerialException as exc:
logging.error("Serial error: %s", exc)
time.sleep(interval)
with get_port() as serial_port:
serial_port: serial.Serial
acquire_repl(serial_port)
boot_out = Path(device.mount_point) / "boot_out.txt"
if boot_out.exists():
logging.info("BOOT: %s", boot_out.read_text())
logging.info("Running script %s", description)
for line in script.splitlines():
logging.debug("Script: %s", line.decode('utf-8'))
serial_port.write([5])
log_serial_output(serial_port.readline())
serial_port.write(script)
serial_port.write([13])
serial_port.flush()
time.sleep(0.1)
serial_port.write([4])
serial_port.flush()
with temporary_timeout(serial_port, 2):
try:
while serial_port.in_waiting:
log_serial_output(serial_port.readline(serial_port.in_waiting))
time.sleep(0.1)
except OSError:
pass
def log_serial_output(loggable: bytes):
for line in loggable.decode('utf-8').splitlines():
logging.debug(" << %s", line)
def erase_filesystem(device):
try:
run_script(
device,
script="""
import storage
storage.erase_filesystem()
""",
description="erase filesystem",
)
time.sleep(1)
except serial.SerialException:
pass
device = wait_for_device(device)
while device.mount_point is None:
time.sleep(0.5)
device = wait_for_device(device)
return device
@contextmanager
def temporary_timeout(port: serial.Serial, timeout: int):
cur_timeout = port.timeout
port.timeout = timeout
yield
port.timeout = cur_timeout
def acquire_repl(serial_port):
while True:
serial_port.write(b'\r')
with temporary_timeout(serial_port, 1):
output = serial_port.read_until(b">>> ")
if b">>>" in output:
log_serial_output(output)
logging.info("Found REPL")
return True
else:
logging.info("Sending Ctrl-C")
serial_port.write(b"\003\r\r") # Ctrl-C LF
if b">>>" in serial_port.read_until(b">>> "):
time.sleep(0.1)
return True
def main():
tasks_flash = []
tasks_boot = []
for device in discover_devices():
if device.serial_no in in_progress:
continue
logging.info("Discovered new device %s", device)
in_progress.add(device.serial_no)
if "BOOT" in device.mount_point:
logging.info("Run boot flash on device %s", device)
task = Thread(target=bootloader_flash, args=(device,), daemon=True, name=device.serial_no)
tasks_boot.append(task)
task.start()
else:
logging.info("Run content flash on device %s", device)
task = Thread(target=content_flash, args=(device,), daemon=True, name=device.serial_no)
tasks_flash.append(task)
task.start()
for source, tasks in (('content', tasks_flash), ('boot', tasks_boot)):
for task in list(tasks):
if not task.is_alive():
tasks.remove(task)
logging.info("Task done %s", task)
if task.name in in_progress:
in_progress.remove(task.name)
if source == 'content':
done_devices.add(task.name)
logging.info("Device %s done %s, len=%d", task.name, source,
len(tasks))
task.join()
time.sleep(0.25)
if __name__ == "__main__":
main()