From d2bd7435429580037fd1b21b8cf767730e5e74eb Mon Sep 17 00:00:00 2001 From: Niel Nielsen Date: Wed, 26 Aug 2026 19:48:27 +0200 Subject: [PATCH] Refactor bwm_forward.c for improved clarity and structure Signed-off-by: Niel Nielsen --- armsrc/bwm_forward.c | 99 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 79 insertions(+), 20 deletions(-) diff --git a/armsrc/bwm_forward.c b/armsrc/bwm_forward.c index 5d0343b20..09b077593 100644 --- a/armsrc/bwm_forward.c +++ b/armsrc/bwm_forward.c @@ -14,13 +14,15 @@ #include "bwm_forward.h" #include "bwm_uart_at32.h" -#include "pm3_cmd.h" +#include "pm3_cmd.h" // PM3_CMD_DATA_SIZE, PM3_* return codes #include "string.h" #ifndef MIN #define MIN(a, b) (((a) < (b)) ? (a) : (b)) #endif +// CRC-16/CCITT-FALSE, byte-identical to the BWM firmware's crc16_ccitt() +// (poly 0x1021, init 0xFFFF, MSB-first, no reflection, no xorout). static uint16_t bwm_crc16(const uint8_t *data, size_t len, uint16_t crc) { for (size_t i = 0; i < len; i++) { crc ^= (uint16_t)data[i] << 8; @@ -35,18 +37,51 @@ static uint16_t bwm_crc16(const uint8_t *data, size_t len, uint16_t crc) { return crc; } -#define BWM_TX_OVERHEAD (2 + 2 + 2 + 2) -#define BWM_TX_MAX_PAYLOAD (PM3_CMD_DATA_SIZE + 64) +// --------------------------------------------------------------------------- +// TX: wrap one reply frame into a SEND_FORWARD_DATA app_com frame. +// A full NG/OLD frame is <= PM3_CMD_DATA_SIZE + a small header/postamble, well +// under the BWM 4096-byte payload cap, so a single frame always suffices. +// --------------------------------------------------------------------------- +#define BWM_TX_OVERHEAD (2 + 2 + 2 + 2) // hdr + cmd + len + crc +#define BWM_TX_MAX_PAYLOAD (PM3_CMD_DATA_SIZE + 64) // NG/OLD frame ceiling #define BWM_TX_BUFSZ (BWM_TX_OVERHEAD + BWM_TX_MAX_PAYLOAD) +static void bwm_pump(void); // fwd decl: TX gate pumps RX to collect credit grants + +// --- Flow control (credit window) ------------------------------------------ +// s_fc_granted: cumulative # of frames the ESP has authorized (updated from +// BWM_CMD_FLOW_CREDIT frames). s_fc_sent: cumulative # the ARM has sent. +// Sending is allowed while (int16_t)(granted - sent) > 0; the signed diff is +// wrap-safe. Grants are absolute/cumulative, so a lost credit frame self-corrects. +static volatile uint16_t s_fc_granted = BWM_FC_INITIAL_CREDIT; +static uint16_t s_fc_sent = 0; + +static inline bool bwm_fc_may_send(void) { + return (int16_t)(s_fc_granted - s_fc_sent) > 0; +} + int bwm_fwd_writebuffer_sync(const uint8_t *data, size_t len) { - static uint8_t frame[BWM_TX_BUFSZ]; + static uint8_t frame[BWM_TX_BUFSZ]; // single-threaded bare-metal: static OK if (len > BWM_TX_MAX_PAYLOAD) { - len = BWM_TX_MAX_PAYLOAD; + len = BWM_TX_MAX_PAYLOAD; // defensive; should never trigger } size_t idx = 0; + // Flow control: block until the ESP has granted credit for another frame. + // bwm_pump() drains the IRQ-filled RX ring, so grants are collected even + // while we sit inside a tight download loop (the reply_old firehose). The + // spin cap is a safety valve so a dead/disconnected ESP can't hard-hang us. + { + uint32_t spins = 0; + while (bwm_fc_may_send() == false) { + bwm_pump(); + if (++spins > BWM_FC_STALL_SPINS) { + break; // best-effort: proceed even without a fresh grant + } + } + } + frame[idx++] = BWM_HDR_HOST_CMD_1; frame[idx++] = BWM_HDR_HOST_CMD_2; frame[idx++] = (uint8_t)(BWM_CMD_SEND_FORWARD_DATA & 0xFF); @@ -61,10 +96,18 @@ int bwm_fwd_writebuffer_sync(const uint8_t *data, size_t len) { frame[idx++] = (uint8_t)(crc & 0xFF); frame[idx++] = (uint8_t)((crc >> 8) & 0xFF); - return bwm_uart_write(frame, idx); + int wr = bwm_uart_write(frame, idx); + s_fc_sent++; // one forward frame consumed a credit + return wr; } -#define BWM_DEFIFO_SZ 2048 +// --------------------------------------------------------------------------- +// RX: persistent app_com de-framer. Feeds raw FPC bytes through a state machine +// and pushes the payloads of valid DATA_FORWARD (0xD2 0xD3 / cmd 8089) frames +// into a byte FIFO that bwm_read_ng() drains. Non-DATA_FORWARD frames (slave +// responses, forwarded logs, cmd-error reports) are validated and discarded. +// --------------------------------------------------------------------------- +#define BWM_DEFIFO_SZ 2048 // >= one full NG frame's payload #define BWM_RXFRAME_MAX (PM3_CMD_DATA_SIZE + 64) typedef enum { @@ -74,32 +117,33 @@ typedef enum { typedef struct { bwm_state_t state; uint8_t hdr1; - bool is_bcast; + bool is_bcast; // header pair is 0xD2 0xD3 uint16_t cmd; uint16_t len; - uint16_t got; - uint16_t crc_calc; + uint16_t got; // payload bytes received + uint16_t crc_calc; // running CRC over hdr..payload uint16_t crc_recv; uint8_t payload[BWM_RXFRAME_MAX]; } bwm_parser_t; static bwm_parser_t s_p = { .state = S_IDLE }; +// De-framed payload ring static uint8_t s_fifo[BWM_DEFIFO_SZ]; -static volatile uint16_t s_fifo_head = 0; -static volatile uint16_t s_fifo_tail = 0; +static volatile uint16_t s_fifo_head = 0; // write +static volatile uint16_t s_fifo_tail = 0; // read static uint16_t fifo_count(void) { return (uint16_t)((s_fifo_head - s_fifo_tail) & (BWM_DEFIFO_SZ - 1)); } -static inline void fifo_push(uint8_t b) { +static void fifo_push(uint8_t b) { uint16_t next = (uint16_t)((s_fifo_head + 1) & (BWM_DEFIFO_SZ - 1)); - if (next != s_fifo_tail) { + if (next != s_fifo_tail) { // drop on overflow rather than corrupt s_fifo[s_fifo_head] = b; s_fifo_head = next; } } -static inline uint8_t fifo_pop(void) { +static uint8_t fifo_pop(void) { uint8_t b = s_fifo[s_fifo_tail]; s_fifo_tail = (uint16_t)((s_fifo_tail + 1) & (BWM_DEFIFO_SZ - 1)); return b; @@ -109,6 +153,8 @@ static void bwm_reset_frame(bwm_parser_t *p) { p->state = S_IDLE; } +// Update running CRC one byte at a time (mirrors the streaming update in the +// BWM firmware parser). static void crc_step(bwm_parser_t *p, uint8_t byte) { p->crc_calc = bwm_crc16(&byte, 1, p->crc_calc); } @@ -121,12 +167,14 @@ static void bwm_feed_byte(bwm_parser_t *p, uint8_t byte) { } else if (byte == BWM_HDR_SLAVE_RESP_1) { p->hdr1 = byte; p->is_bcast = false; p->state = S_HDR2; } + // any other byte: stay idle (resync) break; case S_HDR2: { bool ok = (p->is_bcast && byte == BWM_HDR_SLAVE_BCAST_2) || (!p->is_bcast && byte == BWM_HDR_SLAVE_RESP_2); if (!ok) { + // header mismatch: reset and re-examine this byte as a potential SOF p->state = S_IDLE; bwm_feed_byte(p, byte); return; @@ -144,7 +192,7 @@ static void bwm_feed_byte(bwm_parser_t *p, uint8_t byte) { p->len |= (uint16_t)byte << 8; crc_step(p, byte); p->got = 0; - if (p->len > BWM_RXFRAME_MAX) { + if (p->len > BWM_RXFRAME_MAX) { // oversized -> drop frame bwm_reset_frame(p); } else { p->state = (p->len == 0) ? S_CRC_LO : S_PAYLOAD; @@ -162,12 +210,17 @@ static void bwm_feed_byte(bwm_parser_t *p, uint8_t byte) { case S_CRC_LO: p->crc_recv = byte; p->state = S_CRC_HI; break; case S_CRC_HI: p->crc_recv |= (uint16_t)byte << 8; - if (p->crc_recv == p->crc_calc && - p->is_bcast && p->cmd == BWM_CMD_DATA_FORWARD) { - for (uint16_t i = 0; i < p->len; i++) { - fifo_push(p->payload[i]); + if (p->crc_recv == p->crc_calc && p->is_bcast) { + if (p->cmd == BWM_CMD_DATA_FORWARD) { + for (uint16_t i = 0; i < p->len; i++) { + fifo_push(p->payload[i]); + } + } else if (p->cmd == BWM_CMD_FLOW_CREDIT && p->len >= 2) { + // absolute cumulative grant from the ESP + s_fc_granted = (uint16_t)(p->payload[0] | ((uint16_t)p->payload[1] << 8)); } } + // valid non-DATA_FORWARD frames and CRC failures alike: just resync bwm_reset_frame(p); break; @@ -177,6 +230,7 @@ static void bwm_feed_byte(bwm_parser_t *p, uint8_t byte) { } } +// Pull whatever raw framed bytes are waiting and run them through the parser. static void bwm_pump(void) { uint8_t scratch[64]; uint16_t avail = bwm_uart_rx_available(); @@ -196,6 +250,8 @@ uint16_t bwm_fwd_rxdata_available(void) { if (fifo_count() > 0) { return fifo_count(); } + // No de-framed payload yet, but raw frame bytes may be waiting; pump once so + // receive_ng()'s gate reflects real forward data. bwm_pump(); return fifo_count(); } @@ -204,6 +260,9 @@ uint32_t bwm_read_ng(uint8_t *data, size_t len) { if (len == 0) { return 0; } + + // Same bounded-retry budget shape as bwm_uart_read(); USART_SLOW_LINK (set + // for the BWM/BLE link) widens it so a slow round-trip doesn't time out. uint32_t tryconstant = 0; #ifdef USART_SLOW_LINK tryconstant = 50000;