2026 dongle: own firmware for the USB nRF24 stick, wire-free USB flashing

Reverse-engineered the AliExpress "USB_24RF Ver3.1" stick (STM32F030F4P6 +
Si24R1 + AT2401C PA + CH330N) and replaced its closed AT-command firmware so it
can be the PC-side radio for the hexapod.

- Pin map found over SWD and confirmed at runtime: USART1 PA9/PA10 (CH330N),
  SPI1 PA5/6/7, nRF CSN PA3 / CE PA4 / IRQ PA2. LED + AT2401C control are among
  the 5 free pins, still to map.
- Two-stage firmware so wire-free flashing can't be broken by an app update:
  - safeboot (4 KB, pages 0-3): permanent recovery bootloader, fixed-baud flash
    protocol over USB, only it survives every update;
  - app (12 KB, pages 4+): the radio firmware, the only part updates rewrite.
  The app updater never erases safeboot; recovery works even with a dead app.
- tools/dongle_flash.py: one-time install via the STM32 ROM bootloader (the only
  time the BOOT0 wire is used). tools/safeboot_flash.py: wire-free app updates.
- dongle/README.md documents the hardware, pin map, RDP1 (factory fw can't be
  dumped), the memory map and the flashing workflow.
- README: add dongle/ to the layout and the PC-radio plan.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
mangavalkandClaude Opus 4.8 committed 2026-11-14 23:14:16 +01:00
1 parent 0f9655d646
commit 97f580b6a5
12 files changed
+885 -10

No files matched your search

+161
View File
@@ -0,0 +1,161 @@
"""Flash a .bin to the dongle's STM32F030 over the ROM UART bootloader (COM16).
First run unlocks (readout-unprotect = mass erase + drop RDP), then writes & verifies.
usage: dongle_flash.py firmware.bin [--port COM16]"""
import sys, time, serial
ACK, NACK = 0x79, 0x1F
FLASH = 0x08000000
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)
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)
def _wait_ack(self, what, tmo=3.0):
self.s.timeout = tmo
b = self.s.read(1)
self.s.timeout = 1.0
if b == bytes([ACK]):
return True
if b == bytes([NACK]):
raise RuntimeError("%s: NACK" % what)
raise RuntimeError("%s: no answer (%r)" % (what, b))
def _probe_get(self):
"""Already synced? A GET (0x00) ACKs and returns the command list."""
self.s.reset_input_buffer()
self.s.write(bytes([0x00, 0xFF])); time.sleep(0.15)
b = self.s.read(1)
if b == bytes([ACK]):
n = self.s.read(1)[0]; self.s.read(n + 1); self.s.read(1)
return True
return b == bytes([NACK])
def sync(self):
# autobaud (works once per power-up)
self.s.reset_input_buffer()
self.s.write(b"\x7f"); time.sleep(0.15)
b = self.s.read(1)
if b in (bytes([ACK]), bytes([NACK])):
return True
# already synced this power cycle?
if self._probe_get():
return True
raise RuntimeError("no bootloader sync (BOOT0 high? on USB power? replug it)")
def cmd(self, c):
self.s.write(bytes([c, c ^ 0xFF]))
return self._wait_ack("cmd 0x%02X" % c)
def get(self):
self.cmd(0x00)
n = self.s.read(1)[0]
data = self.s.read(n + 1)
self.s.read(1)
return data
def readable(self):
self.s.write(bytes([0x11, 0xEE]))
b = self.s.read(1)
if b == bytes([ACK]):
# send a dummy address so the bootloader doesn't hang mid-command
self._addr(FLASH); self.s.write(bytes([0, 0xFF])); self.s.read(1)
self.s.read(1)
return True
return False
def _addr(self, addr):
a = addr.to_bytes(4, "big")
self.s.write(a + bytes([a[0] ^ a[1] ^ a[2] ^ a[3]]))
self._wait_ack("address 0x%08X" % addr)
def unprotect(self):
"""Readout-unprotect: mass-erase + drop RDP to 0. Erases factory firmware."""
self.s.write(bytes([0x92, 0x6D]))
self._wait_ack("readout-unprotect start")
self._wait_ack("readout-unprotect erase", tmo=6.0)
time.sleep(1.0) # chip resets; BOOT0 still high -> bootloader again
self.reopen(); self.sync()
def mass_erase(self):
self.s.write(bytes([0x44, 0xBB]))
self._wait_ack("ext-erase start")
self.s.write(bytes([0xFF, 0xFF, 0x00])) # 0xFFFF = global mass erase, cksum 0
self._wait_ack("mass erase", tmo=8.0)
def write(self, addr, data):
self.cmd(0x31)
self._addr(addr)
n = len(data) - 1
cs = n
for b in data:
cs ^= b
self.s.write(bytes([n]) + data + bytes([cs]))
self._wait_ack("write @0x%08X" % addr, tmo=3.0)
def read(self, addr, n):
self.cmd(0x11)
self._addr(addr)
self.s.write(bytes([n - 1, (n - 1) ^ 0xFF]))
self._wait_ack("read @0x%08X" % addr)
return self.s.read(n)
def go(self, addr):
self.cmd(0x21)
self._addr(addr)
def main():
args = [a for a in sys.argv[1:] if not a.startswith("--")]
port = "COM16"
if "--port" in sys.argv:
port = sys.argv[sys.argv.index("--port") + 1]
data = open(args[0], "rb").read()
if len(data) % 4:
data += b"\xFF" * (4 - len(data) % 4)
print("firmware %s: %d bytes" % (args[0], len(data)))
bl = BL(port)
bl.sync()
time.sleep(0.1)
bl.s.reset_input_buffer()
print("sync ok")
# 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:
bl.mass_erase()
print("mass-erased.")
except RuntimeError:
print("locked -> readout-unprotect (ERASES factory firmware)...")
bl.unprotect()
print("unlocked and erased.")
print("writing...")
for off in range(0, len(data), 256):
chunk = data[off:off + 256]
bl.write(FLASH + off, chunk)
print("verifying...")
bad = 0
for off in range(0, len(data), 256):
chunk = data[off:off + 256]
rb = bl.read(FLASH + off, len(chunk))
if rb != chunk:
bad += 1
print(" mismatch at +0x%X" % off)
if bad:
print("VERIFY FAILED (%d blocks)" % bad); return 1
print("verify OK. starting firmware...")
bl.go(FLASH)
return 0
if __name__ == "__main__":
sys.exit(main())