Dongle (app):
- bridge mode: stands in for the robot's radio (ch 96 / 250k / HXPD1,
auto-ack, dynamic payloads, ack payloads). Received packets go to the PC
as A5-framed 'R' frames; the PC's 'A' frames become the ack payload of
the next packet, like robot/Comms.cpp; dropped after ~0.5 s without a
refresh. 'S' status once a second.
- USART RX through circular DMA into a ring buffer, overrun stall
disabled, error flags cleared: a burst (e.g. SignalRGB probing the port)
used to deafen the app until a replug.
- RX test mode ('x') accepts protocol v4's dynamic-length packets.
- safeboot_flash.py --knock: keeps knocking across a replug to catch
safeboot's cold-boot window when the app doesn't answer.
Unity:
- SimRemote sources: Keyboard, or the real remote via the dongle
(DongleLink.cs: kernel32 serial, auto-finds the CH340, all port I/O on
one background thread). Packets go into the firmware byte for byte, the
robot's telemetry goes back on the ack, so the remote's display shows
the sim. Tested end to end with the real remote.
- Dropped the USB joystick and robot-serial-harness inputs and the old
Serial/Serial2/Serial3/HandleHoystick scripts (and their components in
Main.unity): no USB links to remote or robot any more.
- Test panel: dongle status; no sideways scroll bar.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
357 lines
16 KiB
C
357 lines
16 KiB
C
/* 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_HI() BSRR(GPIOA) = (1u << 4)
|
|
#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 void nrf_write_buf(uint8_t reg, const uint8_t *d, int n)
|
|
{
|
|
CSN_LO(); spi_xfer(0x20 | (reg & 0x1F)); while (n--) spi_xfer(*d++); CSN_HI();
|
|
}
|
|
static void nrf_cmd(uint8_t c) { CSN_LO(); spi_xfer(c); CSN_HI(); }
|
|
|
|
/* Receive on the hexapod protocol (ch 96, 250 kbps, addr HXPD1). Protocol v4
|
|
* uses dynamic payload lengths (the remote's CmdPacket is 26 bytes, with ack
|
|
* payloads), so this does too and accepts any length. Auto-ack is on, so the
|
|
* remote's link % also shows whether our acks get back out (TX path). Lets us
|
|
* find the AT2401C enable pins: packets only arrive when the LNA path is on. */
|
|
static uint8_t rxmode;
|
|
static uint16_t rx_count;
|
|
static uint8_t rx_len;
|
|
static void rx_setup(void)
|
|
{
|
|
static const uint8_t addr[5] = { 0x48, 0x58, 0x50, 0x44, 0x31 }; /* "HXPD1" */
|
|
CE_LO();
|
|
nrf_write(0x00, 0x0F); /* CONFIG: PWR_UP, PRIM_RX, EN_CRC, CRCO=16 */
|
|
nrf_write(0x01, 0x01); /* EN_AA pipe0 */
|
|
nrf_write(0x02, 0x01); /* EN_RXADDR pipe0 */
|
|
nrf_write(0x03, 0x03); /* SETUP_AW = 5 bytes */
|
|
nrf_write(0x05, 96); /* RF_CH */
|
|
nrf_write(0x06, 0x26); /* RF_SETUP: 250 kbps, max power */
|
|
nrf_write_buf(0x0A, addr, 5); /* RX_ADDR_P0 */
|
|
nrf_write(0x1D, 0x04); /* FEATURE: EN_DPL */
|
|
nrf_write(0x1C, 0x01); /* DYNPD: pipe0 */
|
|
nrf_write(0x07, 0x70); /* clear status flags */
|
|
nrf_cmd(0xE2); /* FLUSH_RX */
|
|
rx_count = 0;
|
|
rx_len = 0;
|
|
CE_HI();
|
|
}
|
|
static void rx_poll(void)
|
|
{
|
|
while (nrf_read(0x07) & 0x40) { /* RX_DR */
|
|
CSN_LO(); spi_xfer(0x60); uint8_t n = spi_xfer(0xFF); CSN_HI(); /* R_RX_PL_WID */
|
|
if (n > 32) { nrf_cmd(0xE2); nrf_write(0x07, 0x40); break; } /* corrupt: flush */
|
|
CSN_LO(); spi_xfer(0x61); for (int i = 0; i < n; i++) spi_xfer(0xFF); CSN_HI();
|
|
nrf_write(0x07, 0x40); /* clear RX_DR */
|
|
rx_len = n;
|
|
rx_count++;
|
|
}
|
|
}
|
|
|
|
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));
|
|
}
|
|
}
|
|
|
|
/* Every usable GPIO on the TSSOP-20 except PA9/PA10 (our serial). Sweep each
|
|
* HIGH ~3 s so the user can watch the LED. Disturbs the radio; re-init after. */
|
|
static const struct { uint32_t port; uint8_t pin; const char *name; } ALLPINS[] = {
|
|
{ GPIOA, 0, "PA0" }, { GPIOA, 1, "PA1" }, { GPIOA, 2, "PA2 IRQ" },
|
|
{ GPIOA, 3, "PA3 CSN" }, { GPIOA, 4, "PA4 CE" }, { GPIOA, 5, "PA5 SCK" },
|
|
{ GPIOA, 6, "PA6 MISO" }, { GPIOA, 7, "PA7 MOSI" }, { GPIOB, 1, "PB1" },
|
|
{ GPIOF, 0, "PF0" }, { GPIOF, 1, "PF1" }, { GPIOA, 13, "PA13 SWDIO" },
|
|
{ GPIOA, 14, "PA14 SWCLK" },
|
|
};
|
|
static void sweep_all(void)
|
|
{
|
|
uart_puts("sweeping ALL pins, 3 s each (radio will be disturbed):\r\n");
|
|
for (unsigned i = 0; i < sizeof(ALLPINS) / sizeof(ALLPINS[0]); i++) {
|
|
uint32_t p = ALLPINS[i].port; uint8_t n = ALLPINS[i].pin;
|
|
MODER(p) = (MODER(p) & ~(3u << (n * 2))) | (1u << (n * 2)); /* output */
|
|
BSRR(p) = (1u << n); /* HIGH */
|
|
uart_puts(">>> "); uart_puts(ALLPINS[i].name); uart_puts(" HIGH\r\n");
|
|
delay_ms(3000);
|
|
BSRR(p) = (1u << (n + 16)); /* LOW */
|
|
delay_ms(500);
|
|
}
|
|
uart_puts("sweep done; restoring radio\r\n");
|
|
spi_init();
|
|
nrf_write(0, 0x0E);
|
|
}
|
|
|
|
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) x=rx test b=reflash; bridge: frame A5 'L'\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");
|
|
}
|
|
/* Pin finder: sit in RX on a WiFi-busy channel and report the carrier-detect
|
|
* (RPD) bit. Toggling the real RXEN pin switches the AT2401C LNA on, which lifts
|
|
* ambient 2.4 GHz above the -64 dBm detect threshold -> RPD flips to 1. */
|
|
static uint8_t finder;
|
|
static uint8_t finder_ch = 62; /* 2462 MHz ~ WiFi ch 11 */
|
|
static void finder_rx(void)
|
|
{
|
|
CE_LO();
|
|
nrf_write(0x05, finder_ch); /* RF_CH */
|
|
nrf_write(0x06, 0x0F); /* RF_SETUP: keep it simple */
|
|
nrf_write(0x00, 0x0F); /* CONFIG: PWR_UP, PRIM_RX, CRC16 */
|
|
delay_ms(2);
|
|
CE_HI(); /* enter RX */
|
|
delay_ms(2);
|
|
}
|
|
|
|
/* ---- USART1 RX through circular DMA ------------------------------------
|
|
* The bridge moves ~100 frames/s each way at 115200 baud. Polled single-byte
|
|
* RX loses bytes while we block on a TX frame or an SPI read, and this M0 app
|
|
* runs without interrupts (no vector relocation), so DMA channel 3 (USART1_RX)
|
|
* writes every byte into a ring we drain at leisure. */
|
|
#define DMA_CCR3 REG(0x40020030)
|
|
#define DMA_CNDTR3 REG(0x40020034)
|
|
#define DMA_CPAR3 REG(0x40020038)
|
|
#define DMA_CMAR3 REG(0x4002003C)
|
|
#define USART1_CR3 REG(0x40013808)
|
|
#define RXRING 256u
|
|
static volatile uint8_t rxring[RXRING];
|
|
static uint16_t rxtail;
|
|
static void uart_dma_init(void)
|
|
{
|
|
RCC_AHBENR |= 1u; /* DMA1 */
|
|
while (!(USART1_ISR & (1u << 6))) {} /* let any TX finish */
|
|
USART1_CR1 &= ~1u; /* UE off: CR3 OVRDIS needs it */
|
|
USART1_CR3 = (1u << 6) | (1u << 12); /* DMAR, OVRDIS (never stall on overrun) */
|
|
USART1_CR1 |= 1u;
|
|
DMA_CCR3 = 0;
|
|
DMA_CPAR3 = 0x40013824u; /* USART1_RDR */
|
|
DMA_CMAR3 = (uint32_t)rxring;
|
|
DMA_CNDTR3 = RXRING;
|
|
DMA_CCR3 = (1u << 7) | (1u << 5) | 1u; /* MINC, CIRC, EN; 8-bit, periph -> mem */
|
|
}
|
|
static int rx_getc(void)
|
|
{
|
|
uint16_t head = (uint16_t)(RXRING - DMA_CNDTR3) % RXRING;
|
|
if (head == rxtail) return -1;
|
|
uint8_t c = rxring[rxtail];
|
|
rxtail = (uint16_t)((rxtail + 1) % RXRING);
|
|
return c;
|
|
}
|
|
|
|
/* ---- Bridge: the dongle stands in for the robot's radio ------------------
|
|
* Framing both ways: A5 type len payload[len] sum, sum = type+len+payload (mod 256).
|
|
* host -> dongle 'L' listen (enter bridge) 'X' leave
|
|
* 'A' ack payload (<= 32 B): goes back on the auto-ack of the
|
|
* next packet, like the robot's Comms::loadAckPayload.
|
|
* Dropped if the host stops refreshing it for 500 ms, so
|
|
* the remote doesn't see stale telemetry as live.
|
|
* dongle -> host 'R' a received packet (the remote's CmdPacket)
|
|
* 'S' once a second: packets received (u16 LE), acks loaded (u16 LE)
|
|
* Plain text never contains A5, so the host can skip anything between frames. */
|
|
static uint8_t bridge;
|
|
static uint8_t ack_buf[32], ack_len;
|
|
static uint32_t ack_ms;
|
|
static uint16_t br_rx, br_acks;
|
|
static void frame_send(uint8_t type, const uint8_t *d, uint8_t n)
|
|
{
|
|
uint8_t sum = (uint8_t)(type + n);
|
|
uart_putc((char)0xA5); uart_putc((char)type); uart_putc((char)n);
|
|
for (uint8_t i = 0; i < n; i++) { uart_putc((char)d[i]); sum = (uint8_t)(sum + d[i]); }
|
|
uart_putc((char)sum);
|
|
}
|
|
static void ack_load(void)
|
|
{
|
|
nrf_cmd(0xE1); /* FLUSH_TX: only the newest one */
|
|
if (!ack_len) return;
|
|
CSN_LO(); spi_xfer(0xA8); for (uint8_t i = 0; i < ack_len; i++) spi_xfer(ack_buf[i]); CSN_HI(); /* W_ACK_PAYLOAD pipe 0 */
|
|
br_acks++;
|
|
}
|
|
static void bridge_start(void)
|
|
{
|
|
rx_setup(); /* ch96 / 250k / HXPD1 / auto-ack / dynamic length */
|
|
CE_LO();
|
|
nrf_write(0x1D, 0x06); /* FEATURE: EN_DPL | EN_ACK_PAY */
|
|
nrf_write(0x1C, 0x01); /* DYNPD pipe 0 */
|
|
nrf_cmd(0xE1);
|
|
ack_len = 0; br_rx = br_acks = 0;
|
|
rxmode = 0; finder = 0; bridge = 1;
|
|
BSRR(GPIOA) = 1u; /* LED on while bridging */
|
|
CE_HI();
|
|
}
|
|
static void bridge_stop(void)
|
|
{
|
|
bridge = 0; ack_len = 0;
|
|
CE_LO(); nrf_write(0, 0x0E); nrf_cmd(0xE1);
|
|
BSRR(GPIOA) = 1u << 16;
|
|
}
|
|
static void bridge_poll(uint32_t now)
|
|
{
|
|
if (ack_len && now - ack_ms > 500) { ack_len = 0; nrf_cmd(0xE1); } /* host went quiet */
|
|
while (nrf_read(0x07) & 0x40) { /* RX_DR */
|
|
uint8_t buf[32];
|
|
CSN_LO(); spi_xfer(0x60); uint8_t n = spi_xfer(0xFF); CSN_HI(); /* R_RX_PL_WID */
|
|
if (n > 32) { nrf_cmd(0xE2); nrf_write(0x07, 0x40); break; }
|
|
CSN_LO(); spi_xfer(0x61); for (uint8_t i = 0; i < n; i++) buf[i] = spi_xfer(0xFF); CSN_HI();
|
|
nrf_write(0x07, 0x40);
|
|
ack_load(); /* the ack for the NEXT packet */
|
|
frame_send('R', buf, n);
|
|
br_rx++;
|
|
if ((br_rx & 31) == 0) BSRR(GPIOA) = (IDR(GPIOA) & 1u) ? (1u << 16) : 1u; /* LED flicker */
|
|
}
|
|
}
|
|
/* Host frame parser; returns 1 while inside a frame (so its bytes aren't
|
|
* taken as single-letter commands). */
|
|
static uint8_t fr_state, fr_type, fr_len, fr_got, fr_sum, fr_buf[32];
|
|
static int frame_rx(int c, uint32_t now)
|
|
{
|
|
/* if-chain, not switch: a switch becomes a jump table needing libgcc */
|
|
if (fr_state == 0) {
|
|
if (c != 0xA5) return 0;
|
|
fr_state = 1;
|
|
} else if (fr_state == 1) {
|
|
fr_type = (uint8_t)c;
|
|
fr_state = 2;
|
|
} else if (fr_state == 2) {
|
|
fr_len = (uint8_t)c; fr_got = 0; fr_sum = (uint8_t)(fr_type + fr_len);
|
|
fr_state = (fr_len > 32) ? 0 : (fr_len ? 3 : 4);
|
|
} else if (fr_state == 3) {
|
|
fr_buf[fr_got++] = (uint8_t)c; fr_sum = (uint8_t)(fr_sum + c);
|
|
if (fr_got == fr_len) fr_state = 4;
|
|
} else {
|
|
fr_state = 0;
|
|
if ((uint8_t)c != fr_sum) return 1; /* corrupt: drop */
|
|
if (fr_type == 'L') bridge_start();
|
|
else if (fr_type == 'X') bridge_stop();
|
|
else if (fr_type == 'A' && bridge) {
|
|
for (uint8_t i = 0; i < fr_len; i++) ack_buf[i] = fr_buf[i];
|
|
ack_len = fr_len; ack_ms = now;
|
|
ack_load();
|
|
}
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
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();
|
|
uart_dma_init();
|
|
spi_init();
|
|
free_init();
|
|
nrf_write(0, 0x0E);
|
|
delay_ms(5);
|
|
banner();
|
|
|
|
uint32_t now = 0, t = 0;
|
|
systick_setup();
|
|
for (;;) {
|
|
uint32_t ctrl = STK_CTRL; /* one read: reading clears COUNTFLAG */
|
|
if (ctrl == 0) systick_setup(); /* delay_ms() (sweep, finder) stops it */
|
|
else if (ctrl & 0x10000) { now++; t++; } /* 1 ms ticks, polled */
|
|
if (bridge) bridge_poll(now);
|
|
int c = rx_getc();
|
|
if (c >= 0 && frame_rx(c, now)) c = -1; /* consumed by a host frame */
|
|
if (c < 0) {}
|
|
else if (bridge && c != 'b' && c != 'B') {} /* bridging: only frames (and reflash) */
|
|
else if (c == 'v' || c == 'V') banner();
|
|
else if (c == 'r' || c == 'R') regdump();
|
|
else if (c == 'b' || c == 'B') reflash();
|
|
else if (c == 'x' || c == 'X') { rxmode = !rxmode; finder = 0; if (rxmode) rx_setup(); else { CE_LO(); nrf_write(0, 0x0E); } uart_puts(rxmode ? "RX mode on (ch96/250k/HXPD1)\r\n" : "RX off\r\n"); }
|
|
else if (c == 'a' || c == 'A') sweep_all();
|
|
else if (c == 'f' || c == 'F') { finder = !finder; if (finder) finder_rx(); else { CE_LO(); nrf_write(0, 0x0E); } uart_puts(finder ? "finder RX on\r\n" : "finder off\r\n"); }
|
|
else if (c == 'n' || c == 'N') { finder_ch = (finder_ch == 62) ? 37 : (finder_ch == 37 ? 12 : 62); if (finder) finder_rx(); uart_puts("ch="); hex2(finder_ch); uart_puts("\r\n"); }
|
|
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" : " LOW");
|
|
if (finder) { delay_ms(2); uart_puts(" RPD="); hex2(nrf_read(9)); }
|
|
uart_puts("\r\n");
|
|
}
|
|
if (rxmode) rx_poll();
|
|
if (t >= 1000) {
|
|
t = 0;
|
|
if (bridge) {
|
|
uint8_t s[4] = { (uint8_t)br_rx, (uint8_t)(br_rx >> 8), (uint8_t)br_acks, (uint8_t)(br_acks >> 8) };
|
|
frame_send('S', s, 4);
|
|
br_rx = br_acks = 0;
|
|
}
|
|
else if (rxmode) { uart_puts("RX pkts="); hex2(rx_count >> 8); hex2(rx_count & 0xFF);
|
|
uart_puts(" len="); hex2(rx_len);
|
|
uart_puts(" pins="); for (int i = 0; i < NFREE; i++) uart_putc(fst[i] ? '1' : '0');
|
|
uart_puts(" (0=PA0LED,1=PA1,2=PB1,3=PF0,4=PF1)\r\n"); rx_count = 0; }
|
|
else if (finder) { uart_puts("finder ch=0x"); hex2(finder_ch); uart_puts(" RPD="); hex2(nrf_read(9));
|
|
uart_puts(" pins="); for (int i = 0; i < NFREE; i++) uart_putc(fst[i] ? '1' : '0'); uart_puts("\r\n"); }
|
|
else { uart_puts("hb STATUS=0x"); hex2(nrf_read(7)); uart_puts("\r\n"); }
|
|
}
|
|
}
|
|
}
|