Enhance UART baud negotiation and acknowledgment

Added acknowledgment handling for GET_UART_BAUD command and improved baud negotiation logic.

Signed-off-by: Niel Nielsen <nieldk@gmail.com>
This commit is contained in:
Niel Nielsen authored and GitHub committed 2026-09-02 14:24:03 +02:00
1 parent 9573cb355c
commit a3b4767ff6
1 file changed
+70 -7
+70 -7
View File
@@ -59,6 +59,10 @@ static volatile int16_t s_fwd_inflight = 0;
// (the ESP's ack for a baud-set request). Consumed by bwm_fwd_negotiate_baud().
static volatile bool s_baud_ack = false;
// Set true by the parser on a SLAVE_RESP for BWM_CMD_GET_UART_BAUD - proof the
// ESP is alive and responding at the baud we just switched to (verify step).
static volatile bool s_getbaud_ack = false;
int bwm_fwd_writebuffer_sync(const uint8_t *data, size_t len) {
static uint8_t frame[BWM_TX_BUFSZ]; // single-threaded bare-metal: static OK
@@ -74,11 +78,11 @@ int bwm_fwd_writebuffer_sync(const uint8_t *data, size_t len) {
// disconnected ESP can't hard-hang us. A window >= 1 means single command
// replies never block - only sustained bursts hit the cap.
{
uint32_t spins = 0;
uint32_t t0 = GetTickCount();
while (s_fwd_inflight >= BWM_FC_WINDOW) {
bwm_pump();
if (++spins > BWM_FC_ACK_TIMEOUT_SPINS) {
s_fwd_inflight = 0; // best-effort: assume the pipe cleared
if (GetTickCountDelta(t0) > BWM_FC_ACK_TIMEOUT_MS) {
s_fwd_inflight = 0; // best-effort: assume the pipe cleared, never hard-hang
break;
}
}
@@ -244,6 +248,9 @@ static void bwm_feed_byte(bwm_parser_t *p, uint8_t byte) {
} else if ((p->is_bcast == false) && p->cmd == BWM_CMD_SET_UART_BAUD) {
// SLAVE_RESP ack for a baud-set request (see negotiate below)
s_baud_ack = true;
} else if ((p->is_bcast == false) && p->cmd == BWM_CMD_GET_UART_BAUD) {
// SLAVE_RESP for our GET_BAUD verify probe
s_getbaud_ack = true;
}
}
// valid non-DATA_FORWARD frames and CRC failures alike: just resync
@@ -330,6 +337,38 @@ uint32_t bwm_read_ng(uint8_t *data, size_t len) {
// ---------------------------------------------------------------------------
#define BWM_BAUD_ACK_WAIT_MS 300 // per-attempt wait for the ESP ack
#define BWM_BAUD_ATTEMPTS 3 // resend attempts before giving up
#define BWM_BAUD_VERIFY_MS 150 // per-probe wait for the GET_BAUD reply
#define BWM_BAUD_VERIFY_TRIES 5 // GET_BAUD probes before declaring the switch failed
// Probe the ESP at the CURRENT baud with GET_UART_BAUD; true only if it answers,
// i.e. it really is running at the baud we just switched to. Retried by the
// caller so a single lost probe on a good link does not force a needless revert.
static bool bwm_verify_baud(void) {
uint8_t f[2 + 2 + 2 + 2];
size_t i = 0;
f[i++] = BWM_HDR_HOST_CMD_1;
f[i++] = BWM_HDR_HOST_CMD_2;
f[i++] = (uint8_t)(BWM_CMD_GET_UART_BAUD & 0xFF);
f[i++] = (uint8_t)((BWM_CMD_GET_UART_BAUD >> 8) & 0xFF);
f[i++] = 0;
f[i++] = 0;
uint16_t crc = bwm_crc16(f, i, BWM_CRC16_INIT);
f[i++] = (uint8_t)(crc & 0xFF);
f[i++] = (uint8_t)((crc >> 8) & 0xFF);
s_getbaud_ack = false;
s_p.state = S_IDLE;
bwm_uart_write(f, i);
uint32_t t0 = GetTickCount();
while (GetTickCountDelta(t0) < BWM_BAUD_VERIFY_MS) {
bwm_pump();
if (s_getbaud_ack) {
return true;
}
}
return false;
}
bool bwm_fwd_negotiate_baud(uint32_t target) {
if (target == 0 || target == bwm_uart_get_baud()) {
@@ -362,15 +401,39 @@ bool bwm_fwd_negotiate_baud(uint32_t target) {
bwm_pump(); // parser sets s_baud_ack on the SLAVE_RESP
if (s_baud_ack) {
// ESP acked at the old baud and is committing to the new one.
// Switch our side and let both settle before any traffic.
// Switch our side and let it settle.
bwm_uart_set_baud(target);
SpinDelay(5);
// Fresh start on the faster link: clear framer + de-fifo + window.
// VERIFY the ESP is really at the new baud before trusting it. A
// lost or spurious ack must not leave the two ends at different
// bauds - that silently breaks every AT32<->ESP exchange (forward
// traffic, hw bwmwifi, ...). Retry so one dropped probe on a good
// link does not cause a needless fallback.
bool verified = false;
for (int v = 0; v < BWM_BAUD_VERIFY_TRIES; v++) {
if (bwm_verify_baud()) {
verified = true;
break;
}
}
s_p.state = S_IDLE; // fresh framer either way
s_fifo_head = 0;
s_fifo_tail = 0;
s_fwd_inflight = 0;
if (verified) {
return true;
}
// Could not confirm the ESP at the new baud: fall back so the
// ends stay in sync. 460800 is the always-safe boot default.
bwm_uart_set_baud(BWM_UART_BAUD);
s_p.state = S_IDLE;
s_fifo_head = 0;
s_fifo_tail = 0;
s_fwd_inflight = 0;
return true;
return false;
}
SpinDelay(1);
}