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

+154
View File
@@ -0,0 +1,154 @@
/* Hexapod dongle - stage 1 "safeboot" (permanent recovery bootloader).
*
* Lives in the first 4 KB of flash (pages 0-3) and runs first on every reset.
* It speaks a tiny fixed-baud (115200 8N1) flashing protocol over USB and can
* always erase/rewrite the application (pages 4-15). It is flashed ONCE and the
* host flasher never erases it, so no application update can remove the
* wire-free recovery path. See ../README.md.
*
* Entry decisions:
* - soft reset with MAGIC_FLASH set -> enter flash mode immediately
* - else print banner, open a knock window; host 'S' -> flash mode
* - else if the app looks valid -> jump to it
* - else (no valid app) -> stay in flash mode (recovery)
*
* Flash-mode protocol (host -> safeboot, each replies one byte 'K' ok / 'E' err
* unless noted):
* 'P' ping -> 'K'
* 'E' erase app region (pages 4-15)
* 'W' a0 a1 a2 a3 n d[n] write n bytes (n even, <=128) at little-endian addr
* 'R' a0 a1 a2 a3 n read n bytes at addr -> sends n bytes (no K)
* 'G' 'K', then boot the app (or reset)
* 'V' -> prints "SBV1\n" (identify)
*/
#include "common.h"
#define KNOCK_MS 1500
/* ---- startup (custom: read the magic word before touching RAM) ---- */
extern uint32_t _sidata, _sdata, _edata, _sbss, _ebss, _estack;
int sb_main(int forced);
void Reset_Handler(void)
{
uint32_t m = REG(MAGIC_ADDR);
REG(MAGIC_ADDR) = 0;
uint32_t *src = &_sidata, *dst = &_sdata;
while (dst < &_edata) *dst++ = *src++;
for (dst = &_sbss; dst < &_ebss;) *dst++ = 0;
sb_main(m == MAGIC_FLASH);
for (;;) {}
}
void Default_Handler(void) { for (;;) {} }
__attribute__((section(".isr_vector"), used))
void (*const g_vectors[])(void) = {
(void (*)(void))&_estack, Reset_Handler, Default_Handler, Default_Handler,
0, 0, 0, 0, 0, 0, 0, Default_Handler, 0, 0, Default_Handler, Default_Handler,
};
/* ---- flash programming ---- */
static void flash_unlock(void)
{
if (FLASH_CR & (1u << 7)) { FLASH_KEYR = 0x45670123; FLASH_KEYR = 0xCDEF89AB; }
}
static int flash_wait(void)
{
while (FLASH_SR & (1u << 0)) {} /* BSY */
uint32_t sr = FLASH_SR;
FLASH_SR = (1u << 5) | (1u << 2) | (1u << 4); /* clear EOP/PGERR/WRPRT */
return !(sr & ((1u << 2) | (1u << 4))); /* ok if no error bits */
}
static int flash_erase_page(uint32_t addr)
{
flash_wait();
FLASH_CR |= (1u << 1); /* PER */
FLASH_AR = addr;
FLASH_CR |= (1u << 6); /* STRT */
int ok = flash_wait();
FLASH_CR &= ~(1u << 1);
return ok;
}
static int flash_prog_hw(uint32_t addr, uint16_t hw)
{
flash_wait();
FLASH_CR |= (1u << 0); /* PG */
*(volatile uint16_t *)addr = hw;
int ok = flash_wait();
FLASH_CR &= ~(1u << 0);
return ok && (*(volatile uint16_t *)addr == hw);
}
static int erase_app(void)
{
flash_unlock();
for (int p = APP_PAGE_LO; p <= APP_PAGE_HI; p++)
if (!flash_erase_page(0x08000000u + p * PAGE_SIZE)) return 0;
return 1;
}
static uint32_t rd4(void)
{
uint32_t v = uart_getc_block();
v |= (uint32_t)uart_getc_block() << 8;
v |= (uint32_t)uart_getc_block() << 16;
v |= (uint32_t)uart_getc_block() << 24;
return v;
}
static void boot_app(void)
{
uint32_t sp = *(volatile uint32_t *)APP_BASE;
uint32_t pc = *(volatile uint32_t *)(APP_BASE + 4);
if (sp >= 0x20000000u && sp <= 0x20001000u) {
__asm volatile("msr msp, %0" : : "r"(sp));
((void (*)(void))pc)();
}
}
static void flash_loop(void)
{
flash_unlock();
for (;;) {
int c = uart_getc_block();
if (c == 'P') uart_putc('K');
else if (c == 'V') uart_puts("SBV1\r\n");
else if (c == 'E') uart_putc(erase_app() ? 'K' : 'E');
else if (c == 'W') {
uint32_t a = rd4();
uint8_t n = uart_getc_block();
uint8_t buf[128];
for (uint8_t i = 0; i < n; i++) buf[i] = uart_getc_block();
int ok = (n <= 128) && !(n & 1) && a >= APP_BASE && (a + n) <= 0x08004000u;
for (uint8_t i = 0; ok && i < n; i += 2)
ok = flash_prog_hw(a + i, (uint16_t)(buf[i] | (buf[i + 1] << 8)));
uart_putc(ok ? 'K' : 'E');
}
else if (c == 'R') {
uint32_t a = rd4();
uint8_t n = uart_getc_block();
for (uint8_t i = 0; i < n; i++) uart_putc(*(volatile uint8_t *)(a + i));
}
else if (c == 'G') { uart_putc('K'); boot_app(); /* if it returns, stay */ }
}
}
int sb_main(int forced)
{
uart_init8n1();
if (forced) { uart_puts("SBF\r\n"); flash_loop(); }
uart_puts("SB1\r\n"); /* cold boot: offer a knock window */
systick_setup();
uint32_t ms = 0;
while (ms < KNOCK_MS) {
int c = uart_getc();
if (c == 'S') { uart_putc('K'); flash_loop(); }
if (STK_CTRL & 0x10000) ms++;
}
STK_CTRL = 0;
boot_app(); /* valid app -> never returns */
uart_puts("SBR\r\n"); /* no app -> recovery flash mode */
flash_loop();
return 0;
}