2026 dongle: pre-erase link gate + race SignalRGB for the COM port

- dongle_flash.py aborts before erasing unless the ROM-bootloader link tests
  clean (8/8 GETs), so a flaky software-jump link can't wipe a working stick.
- open_racing() retries opening the port to win the race against SignalRGB,
  which grabs CH340 ports on enumeration.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
mangavalkandClaude Opus 4.8 committed 2026-11-14 23:21:18 +01:00
1 parent 97f580b6a5
commit 5ae1a014ff
1 file changed
+34 -4
+34 -4
View File
@@ -7,16 +7,28 @@ ACK, NACK = 0x79, 0x1F
FLASH = 0x08000000
def open_racing(port, open_timeout=20.0):
"""Keep retrying to open the port - wins the race against programs (e.g.
SignalRGB) that also grab CH340 COM ports on enumeration."""
t = time.time()
while True:
try:
return serial.Serial(port, 115200, parity=serial.PARITY_EVEN,
stopbits=1, bytesize=8, timeout=1.0)
except serial.SerialException:
if time.time() - t > open_timeout:
raise
time.sleep(0.02)
class BL:
def __init__(self, port):
self.port = port
self.s = serial.Serial(port, 115200, parity=serial.PARITY_EVEN,
stopbits=1, bytesize=8, timeout=1.0)
self.s = open_racing(port)
def reopen(self):
self.s.close(); time.sleep(0.3)
self.s = serial.Serial(self.port, 115200, parity=serial.PARITY_EVEN,
stopbits=1, bytesize=8, timeout=1.0)
self.s = open_racing(self.port)
def _wait_ack(self, what, tmo=3.0):
self.s.timeout = tmo
@@ -128,6 +140,24 @@ def main():
bl.s.reset_input_buffer()
print("sync ok")
# Safety gate: the ROM bootloader auto-baud can come back garbled after a
# software jump. Prove the link is solid (several clean GETs) BEFORE erasing,
# so a flaky link aborts without wiping a working firmware.
good = 0
for _ in range(8):
try:
d = bl.get()
if len(d) >= 2 and d[1:].count(0x44) and d[1:].count(0x31):
good += 1
except Exception:
pass
time.sleep(0.02)
if good < 8:
print("link not reliable (%d/8 clean) - aborting BEFORE erase." % good)
print("use the BOOT0 wire + a power-cycle for a clean ROM-bootloader link.")
return 2
print("link solid (%d/8)." % good)
# Erase. On an unlocked chip a mass-erase is enough; if it's still locked
# (factory RDP1) the erase NACKs, so fall back to readout-unprotect.
try: