Files
hexapod-robot/2026-current/dongle/firmware/app.c
T

207 lines
8.9 KiB
C
Raw Normal View History

/* 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, 22-byte). Lets
* us find the AT2401C RX-enable pin: packets only arrive when the LNA/antenna
* path is on. */
static uint8_t rxmode;
static uint16_t rx_count;
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(0x11, 22); /* RX_PW_P0 */
nrf_write(0x07, 0x70); /* clear status flags */
nrf_cmd(0xE2); /* FLUSH_RX */
rx_count = 0;
CE_HI();
}
static void rx_poll(void)
{
if (nrf_read(0x07) & 0x40) { /* RX_DR */
uint8_t buf[22];
CSN_LO(); spi_xfer(0x61); for (int i = 0; i < 22; i++) buf[i] = spi_xfer(0xFF); CSN_HI();
nrf_write(0x07, 0x40); /* clear RX_DR */
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) 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");
}
/* 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);
}
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 == '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();
delay_ms(1);
if (++t >= 1000) {
t = 0;
if (rxmode) { uart_puts("RX pkts="); hex2(rx_count >> 8); hex2(rx_count & 0xFF);
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"); }
}
}
}