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

+112
View File
@@ -0,0 +1,112 @@
/* Hexapod dongle - stage 2 "app" (bring-up / radio firmware).
*
* Links at 0x08001000 (after safeboot). Uses polling only (no interrupts), so
* no Cortex-M0 vector relocation is needed - safeboot jumps straight to it.
*
* This v0 brings up the hardware and helps map the last unknown pins:
* - USART1 PA9/PA10 115200 8N1 (USB serial via CH330N)
* - SPI1 PA5/6/7, nRF24 CSN PA3 CE PA4 IRQ PA2
* - free pins PA0 PA1 PB1 PF0 PF1 (LED + AT2401C control, still to identify)
* Serial commands: v=info r=nRF regs 0-4=toggle a free pin b=reflash (safeboot)
*/
#include "common.h"
#define CSN_HI() BSRR(GPIOA) = (1u << 3)
#define CSN_LO() BSRR(GPIOA) = (1u << (3 + 16))
#define CE_LO() BSRR(GPIOA) = (1u << (4 + 16))
static void spi_init(void)
{
RCC_AHBENR |= (1u << 17); /* GPIOA */
RCC_APB2ENR |= (1u << 12); /* SPI1 */
MODER(GPIOA) = (MODER(GPIOA) & ~((3u << 10) | (3u << 12) | (3u << 14)))
| (2u << 10) | (2u << 12) | (2u << 14); /* PA5/6/7 AF0 */
MODER(GPIOA) = (MODER(GPIOA) & ~((3u << 6) | (3u << 8)))
| (1u << 6) | (1u << 8); /* PA3/PA4 out */
MODER(GPIOA) &= ~(3u << 4); /* PA2 in */
PUPDR(GPIOA) = (PUPDR(GPIOA) & ~(3u << 4)) | (1u << 4); /* PA2 pull-up */
CSN_HI(); CE_LO();
REG(0x40013000) = (1u << 2) | (1u << 9) | (1u << 8) | (2u << 3); /* CR1 */
REG(0x40013004) = (7u << 8) | (1u << 12); /* CR2 8-bit */
REG(0x40013000) |= (1u << 6); /* SPE */
}
static uint8_t spi_xfer(uint8_t b)
{
REG8(0x4001300C) = b;
while (!(REG(0x40013008) & (1u << 0))) {}
return REG8(0x4001300C);
}
static uint8_t nrf_read(uint8_t reg)
{
CSN_LO(); spi_xfer(reg & 0x1F); uint8_t v = spi_xfer(0xFF); CSN_HI();
return v;
}
static void nrf_write(uint8_t reg, uint8_t val)
{
CSN_LO(); spi_xfer(0x20 | (reg & 0x1F)); spi_xfer(val); CSN_HI();
}
static const struct { uint32_t port; uint8_t pin; const char *name; } FREE[] = {
{ GPIOA, 0, "PA0" }, { GPIOA, 1, "PA1" }, { GPIOB, 1, "PB1" },
{ GPIOF, 0, "PF0" }, { GPIOF, 1, "PF1" },
};
#define NFREE 5
static uint8_t fst[NFREE];
static void free_init(void)
{
RCC_AHBENR |= (1u << 17) | (1u << 18) | (1u << 22);
for (int i = 0; i < NFREE; i++) {
MODER(FREE[i].port) = (MODER(FREE[i].port) & ~(3u << (FREE[i].pin * 2)))
| (1u << (FREE[i].pin * 2));
BSRR(FREE[i].port) = (1u << (FREE[i].pin + 16));
}
}
static void hex2(uint8_t b) { const char *h = "0123456789ABCDEF"; uart_putc(h[b >> 4]); uart_putc(h[b & 0xF]); }
static void banner(void)
{
uart_puts("\r\nhexapod dongle app v0 (@0x08001000)\r\n");
uart_puts("nRF CONFIG=0x"); hex2(nrf_read(0)); uart_puts(" STATUS=0x"); hex2(nrf_read(7));
uart_puts(" RF_CH=0x"); hex2(nrf_read(5)); uart_puts(" RF_SETUP=0x"); hex2(nrf_read(6));
uart_puts("\r\ncmds: v r 0-4(PA0/PA1/PB1/PF0/PF1) b=reflash\r\n");
}
static void regdump(void)
{
static const uint8_t rg[] = {0,1,2,3,4,5,6,7,8,9,0x17};
for (unsigned i = 0; i < sizeof(rg); i++) { uart_puts(" R"); hex2(rg[i]); uart_putc('='); hex2(nrf_read(rg[i])); }
uart_puts("\r\n");
}
static void reflash(void) /* ask safeboot to take over, no wire needed */
{
uart_puts("resetting into safeboot flash mode...\r\n");
while (!(USART1_ISR & (1u << 6))) {} /* TC */
REG(MAGIC_ADDR) = MAGIC_FLASH;
REG(0xE000ED0C) = 0x05FA0004; /* SCB AIRCR SYSRESETREQ */
for (;;) {}
}
int main(void)
{
uart_init8n1();
spi_init();
free_init();
nrf_write(0, 0x0E);
delay_ms(5);
banner();
uint32_t t = 0;
for (;;) {
int c = uart_getc();
if (c == 'v' || c == 'V') banner();
else if (c == 'r' || c == 'R') regdump();
else if (c == 'b' || c == 'B') reflash();
else if (c >= '0' && c <= '4') {
int i = c - '0';
fst[i] = !fst[i];
BSRR(FREE[i].port) = fst[i] ? (1u << FREE[i].pin) : (1u << (FREE[i].pin + 16));
uart_puts("pin "); uart_puts(FREE[i].name); uart_puts(fst[i] ? " HIGH\r\n" : " LOW\r\n");
}
delay_ms(1);
if (++t >= 2000) { t = 0; uart_puts("hb STATUS=0x"); hex2(nrf_read(7)); uart_puts("\r\n"); }
}
}