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:
1 parent
0f9655d646
commit
97f580b6a5
12 files changed
+885
-10
No files matched your search
@@ -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"); }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
/* app: flash pages 4-15 (12 KB) after safeboot. RAM starts at +4 so the magic
|
||||
* word at 0x20000000 (used to signal safeboot) is left alone. */
|
||||
MEMORY
|
||||
{
|
||||
FLASH (rx) : ORIGIN = 0x08001000, LENGTH = 12K
|
||||
RAM (rwx) : ORIGIN = 0x20000004, LENGTH = 4092
|
||||
}
|
||||
_estack = 0x20001000;
|
||||
|
||||
SECTIONS
|
||||
{
|
||||
.isr_vector : { KEEP(*(.isr_vector)) } > FLASH
|
||||
.text : { *(.text*) *(.rodata*) . = ALIGN(4); } > FLASH
|
||||
_sidata = LOADADDR(.data);
|
||||
.data : { . = ALIGN(4); _sdata = .; *(.data*) . = ALIGN(4); _edata = .; } > RAM AT > FLASH
|
||||
.bss : { . = ALIGN(4); _sbss = .; *(.bss*) *(COMMON) . = ALIGN(4); _ebss = .; } > RAM
|
||||
/DISCARD/ : { *(.ARM.exidx*) *(.ARM.extab*) }
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/* Startup for the stage-2 app. Polling only, so no IRQ vectors are needed
|
||||
* beyond SP/Reset; safeboot jumps here directly (no M0 vector relocation). */
|
||||
#include <stdint.h>
|
||||
|
||||
extern uint32_t _sidata, _sdata, _edata, _sbss, _ebss, _estack;
|
||||
int main(void);
|
||||
|
||||
void Reset_Handler(void)
|
||||
{
|
||||
uint32_t *src = &_sidata, *dst = &_sdata;
|
||||
while (dst < &_edata) *dst++ = *src++;
|
||||
for (dst = &_sbss; dst < &_ebss;) *dst++ = 0;
|
||||
main();
|
||||
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,
|
||||
};
|
||||
@@ -0,0 +1,36 @@
|
||||
# Build the dongle firmware: safeboot.bin (stage 1), app.bin (stage 2), and
|
||||
# dongle_combined.bin (safeboot padded to 4 KB + app) for the one-time install.
|
||||
# Uses the ARM toolchain PlatformIO installed (pio pkg install -g -p ststm32).
|
||||
$ErrorActionPreference = "Stop"
|
||||
$bin = "$env:USERPROFILE\.platformio\packages\toolchain-gccarmnoneeabi\bin"
|
||||
$gcc = (Get-ChildItem "$bin\arm-none-eabi-gcc*.exe" | Select-Object -First 1).FullName
|
||||
$objcopy = (Get-ChildItem "$bin\arm-none-eabi-objcopy*.exe" | Select-Object -First 1).FullName
|
||||
$size = (Get-ChildItem "$bin\arm-none-eabi-size*.exe" | Select-Object -First 1).FullName
|
||||
$cf = "-mcpu=cortex-m0 -mthumb -Os -ffreestanding -nostdlib -ffunction-sections -fdata-sections -Wall -Wextra"
|
||||
$here = $PSScriptRoot
|
||||
Set-Location $here
|
||||
$out = "$here\build"; New-Item -ItemType Directory -Force $out | Out-Null
|
||||
|
||||
$lf = @("-mcpu=cortex-m0", "-mthumb", "-nostdlib", "-Wl,--gc-sections")
|
||||
# stage 1
|
||||
& $gcc $cf.Split(' ') -c safeboot.c -o "$out\safeboot.o"
|
||||
& $gcc $lf -T safeboot.ld "$out\safeboot.o" -o "$out\safeboot.elf"
|
||||
& $objcopy -O binary "$out\safeboot.elf" "$out\safeboot.bin"
|
||||
# stage 2
|
||||
& $gcc $cf.Split(' ') -c app.c -o "$out\app.o"
|
||||
& $gcc $cf.Split(' ') -c app_startup.c -o "$out\app_startup.o"
|
||||
& $gcc $lf -T app.ld "$out\app.o" "$out\app_startup.o" -o "$out\app.elf"
|
||||
& $objcopy -O binary "$out\app.elf" "$out\app.bin"
|
||||
|
||||
& $size "$out\safeboot.elf" "$out\app.elf"
|
||||
|
||||
# combined image: safeboot in [0,4KB), app at 4KB
|
||||
$sb = [System.IO.File]::ReadAllBytes("$out\safeboot.bin")
|
||||
$ap = [System.IO.File]::ReadAllBytes("$out\app.bin")
|
||||
if ($sb.Length -gt 4096) { throw "safeboot is $($sb.Length) bytes, over the 4 KB reserved" }
|
||||
$img = New-Object byte[] (4096 + $ap.Length)
|
||||
for ($i = 0; $i -lt $img.Length; $i++) { $img[$i] = 0xFF }
|
||||
[Array]::Copy($sb, 0, $img, 0, $sb.Length)
|
||||
[Array]::Copy($ap, 0, $img, 4096, $ap.Length)
|
||||
[System.IO.File]::WriteAllBytes("$out\dongle_combined.bin", $img)
|
||||
Write-Host "safeboot $($sb.Length) B, app $($ap.Length) B, combined $($img.Length) B -> build\dongle_combined.bin"
|
||||
@@ -0,0 +1,81 @@
|
||||
/* Shared register defs + helpers for the hexapod dongle (STM32F030F4 @ 8 MHz).
|
||||
* Magic word at 0x20000000 survives a soft reset: the app sets it to ask
|
||||
* safeboot to enter flash mode. Both linker scripts keep RAM clear of it
|
||||
* (RAM ORIGIN 0x20000004). */
|
||||
#ifndef DONGLE_COMMON_H
|
||||
#define DONGLE_COMMON_H
|
||||
#include <stdint.h>
|
||||
|
||||
#define REG(a) (*(volatile uint32_t *)(a))
|
||||
#define REG8(a) (*(volatile uint8_t *)(a))
|
||||
|
||||
#define MAGIC_ADDR 0x20000000u
|
||||
#define MAGIC_FLASH 0xB00710ADu /* "BOOT LOAD": app -> safeboot flash */
|
||||
|
||||
#define APP_BASE 0x08001000u /* stage 2 starts here (page 4) */
|
||||
#define APP_PAGE_LO 4
|
||||
#define APP_PAGE_HI 15 /* 16 KB / 1 KB pages = 0..15 */
|
||||
#define PAGE_SIZE 1024u
|
||||
|
||||
/* RCC */
|
||||
#define RCC_AHBENR REG(0x40021014)
|
||||
#define RCC_APB2ENR REG(0x40021018)
|
||||
#define RCC_APB2RSTR REG(0x4002100C)
|
||||
#define RCC_AHBRSTR REG(0x40021028)
|
||||
/* GPIO */
|
||||
#define GPIOA 0x48000000u
|
||||
#define GPIOB 0x48000400u
|
||||
#define GPIOF 0x48001400u
|
||||
#define MODER(p) REG((p)+0x00)
|
||||
#define OTYPER(p) REG((p)+0x04)
|
||||
#define PUPDR(p) REG((p)+0x0C)
|
||||
#define IDR(p) REG((p)+0x10)
|
||||
#define BSRR(p) REG((p)+0x18)
|
||||
#define AFRH(p) REG((p)+0x24)
|
||||
/* USART1 */
|
||||
#define USART1_CR1 REG(0x40013800)
|
||||
#define USART1_BRR REG(0x4001380C)
|
||||
#define USART1_ISR REG(0x4001381C)
|
||||
#define USART1_RDR REG(0x40013824)
|
||||
#define USART1_TDR REG(0x40013828)
|
||||
/* SysTick */
|
||||
#define STK_CTRL REG(0xE000E010)
|
||||
#define STK_LOAD REG(0xE000E014)
|
||||
#define STK_VAL REG(0xE000E018)
|
||||
/* FLASH */
|
||||
#define FLASH_KEYR REG(0x40022004)
|
||||
#define FLASH_SR REG(0x4002200C)
|
||||
#define FLASH_CR REG(0x40022010)
|
||||
#define FLASH_AR REG(0x40022014)
|
||||
|
||||
static inline void uart_init8n1(void) /* USART1 PA9/PA10, 115200 8N1, 8 MHz */
|
||||
{
|
||||
RCC_AHBENR |= (1u << 17); /* GPIOA */
|
||||
RCC_APB2ENR |= (1u << 14); /* USART1 */
|
||||
MODER(GPIOA) = (MODER(GPIOA) & ~((3u << 18) | (3u << 20)))
|
||||
| (2u << 18) | (2u << 20);
|
||||
AFRH(GPIOA) = (AFRH(GPIOA) & ~((0xFu << 4) | (0xFu << 8)))
|
||||
| (1u << 4) | (1u << 8);
|
||||
USART1_BRR = 69;
|
||||
USART1_CR1 = (1u << 0) | (1u << 2) | (1u << 3);
|
||||
}
|
||||
static inline void uart_putc(char c)
|
||||
{
|
||||
while (!(USART1_ISR & (1u << 7))) {}
|
||||
USART1_TDR = (uint8_t)c;
|
||||
}
|
||||
static inline void uart_puts(const char *s) { while (*s) uart_putc(*s++); }
|
||||
static inline int uart_getc(void) /* non-blocking, -1 if none */
|
||||
{
|
||||
if (USART1_ISR & (1u << 5)) return (uint8_t)USART1_RDR;
|
||||
return -1;
|
||||
}
|
||||
static inline uint8_t uart_getc_block(void)
|
||||
{
|
||||
while (!(USART1_ISR & (1u << 5))) {}
|
||||
return (uint8_t)USART1_RDR;
|
||||
}
|
||||
static inline void systick_setup(void) { STK_LOAD = 8000 - 1; STK_VAL = 0; STK_CTRL = 5; }
|
||||
static inline void delay_ms(uint32_t ms) { systick_setup(); while (ms--) { while (!(STK_CTRL & 0x10000)) {} } STK_CTRL = 0; }
|
||||
|
||||
#endif
|
||||
@@ -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;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
/* safeboot: first 4 KB of flash. RAM starts at +4 so the magic word at
|
||||
* 0x20000000 is never touched by .data/.bss. */
|
||||
MEMORY
|
||||
{
|
||||
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 4K
|
||||
RAM (rwx) : ORIGIN = 0x20000004, LENGTH = 4092
|
||||
}
|
||||
_estack = 0x20001000;
|
||||
|
||||
SECTIONS
|
||||
{
|
||||
.isr_vector : { KEEP(*(.isr_vector)) } > FLASH
|
||||
.text : { *(.text*) *(.rodata*) . = ALIGN(4); } > FLASH
|
||||
_sidata = LOADADDR(.data);
|
||||
.data : { . = ALIGN(4); _sdata = .; *(.data*) . = ALIGN(4); _edata = .; } > RAM AT > FLASH
|
||||
.bss : { . = ALIGN(4); _sbss = .; *(.bss*) *(COMMON) . = ALIGN(4); _ebss = .; } > RAM
|
||||
/DISCARD/ : { *(.ARM.exidx*) *(.ARM.extab*) }
|
||||
}
|
||||
Reference in new issue
Block a user