Files
hardware/firmware/test/wave.c
T
Claude d18fcbcca7 Firmware: learn the far end's baud rate (auto-baud)
As DCE the board now works out the computer's baud rate from what it
types, like a Hayes modem, so the host's speed setting doesn't have to
match the terminal's.

- PIO (autobaud.pio) times each low/high stretch on the RX pin while the
  UART keeps the pin.
- src/core/autobaud.c decodes those timings at every standard rate
  (110-230400) and picks the one they fit: bit length vs the shortest
  stretch, edges on bit boundaries, framing/parity, then the slowest rate
  whose edges score near the best.
- The characters it learned from ("AT") are decoded and passed on; the
  UART switches only once the line is idle.
- Re-learns on 3 framing errors in 2 s, AUTOBAUD_RESTART, or a new peer.
- Baud mode setting (saved, settings byte 7): auto (learn as DCE, host's
  as DTE), host, learn. Vendor requests 8 SET_BAUD_MODE and
  9 AUTOBAUD_RESTART; GET_INFO grows to 14 bytes with flags
  BAUD_LOCKED/BAUD_LEARNING and the baud mode.
- Tests: detector suite (every rate, 5 formats, jitter, rate error,
  glitches, mid-character starts), 5 new core tests, and the emulator
  test drives "AT\r" at 9600 and "ATI3\r" at 115200 onto GP1 with PIO
  stepped per CPU cycle.
- Docs: firmware README, PROTOCOL.md, SPEC.md 5.7 and 6.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013PRsUCpxazyipPn5UGGkF8
2026-10-18 01:18:45 +00:00

38 lines
1.3 KiB
C

/*
* SPDX-License-Identifier: AGPL-3.0-only
* Copyright (C) 2026 Neo-Desktop
*/
#include "wave.h"
#include <math.h>
static uint32_t rng = 12345;
double wave_rnd(void) {
rng = rng * 1103515245u + 12345u;
return (double)((rng >> 8) & 0xFFFFFF) / (double)0x1000000;
}
void wave_make(wave_t *w, uint32_t hz, uint32_t baud, double err, const uart_format_t *f,
const uint8_t *msg, size_t len, double gap_bits, double jitter) {
static bool lv[WAVE_MAX * 2];
size_t n = 0;
for (size_t i = 0; i < len; i++) {
int ones = 0;
lv[n++] = false;
for (int d = 0; d < f->data_bits; d++) { bool b = (msg[i] >> d) & 1; ones += b; lv[n++] = b; }
if (f->parity) lv[n++] = f->parity == 1 ? !(ones & 1) : (ones & 1);
for (int s = 0; s < f->stop_bits; s++) lv[n++] = true;
for (int g = 0; g < (int)gap_bits; g++) lv[n++] = true;
}
double bit = (double)hz / (baud * (1.0 + err));
double last = (wave_rnd() * 2 - 1) * jitter * bit; // the first start bit's falling edge
w->n = 0;
for (size_t k = 1; k < n && w->n < WAVE_MAX; k++) {
if (lv[k] == lv[k - 1]) continue;
double e = (double)k * bit + (wave_rnd() * 2 - 1) * jitter * bit;
w->high[w->n] = lv[k - 1];
w->ticks[w->n] = (uint32_t)llround(e - last);
w->n++;
last = e;
}
}