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>
91 lines
3.1 KiB
C
91 lines
3.1 KiB
C
/* 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_ICR REG(0x40013820)
|
|
#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++); }
|
|
/* Clear overrun / noise / framing / parity errors. While ORE is set the USART
|
|
* stops moving new bytes into RDR, so one burst the app didn't read in time
|
|
* (e.g. SignalRGB probing the COM port) used to deafen it until a replug. */
|
|
static inline void uart_clear_errors(void)
|
|
{
|
|
if (USART1_ISR & 0x0Fu) USART1_ICR = 0x0Fu; /* ORECF NCF FECF PECF */
|
|
}
|
|
static inline int uart_getc(void) /* non-blocking, -1 if none */
|
|
{
|
|
uart_clear_errors();
|
|
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))) uart_clear_errors();
|
|
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
|