Przeglądaj źródła

fix(host): finish RTL GET_TEMPERATURE inside the 20s watchdog budget

Icarus spends tens of seconds on 27 real I2C NACK timeouts, so production UART GET_TEMPERATURE expires, reconnects, and poisons SET_ALL. Accelerate only the sim I2C bit time, keep UART at 217 clocks/bit, and drain PTY before hthd starts.
root 3 miesięcy temu
rodzic
commit
13876f1384

+ 4 - 0
tools/rtl_uart_cosim/prod2u_hthd_fast_uart_top.v

@@ -53,6 +53,10 @@ module prod2u_hthd_fast_uart_top (
 
 
     defparam u_dut.u_uart_rx.CLKS_PER_BIT = 8;
     defparam u_dut.u_uart_rx.CLKS_PER_BIT = 8;
     defparam u_dut.u_uart_tx.CLKS_PER_BIT = 8;
     defparam u_dut.u_uart_tx.CLKS_PER_BIT = 8;
+    defparam u_dut.u_temperature_i2c.HALF_PERIOD_CLKS = 2;
+    defparam u_dut.u_temperature_i2c.TIMEOUT_CLKS = 16;
+    defparam u_dut.u_temperature_i2c.impl.HALF_PERIOD_CLKS = 2;
+    defparam u_dut.u_temperature_i2c.impl.TIMEOUT_CLKS = 16;
 
 
     initial begin
     initial begin
         force u_dut.tick_500us = fast_tick;
         force u_dut.tick_500us = fast_tick;

+ 8 - 0
tools/rtl_uart_cosim/prod2u_hthd_prod_uart_top.v

@@ -53,6 +53,14 @@ module prod2u_hthd_prod_uart_top (
 
 
     defparam u_dut.u_uart_rx.CLKS_PER_BIT = 217;
     defparam u_dut.u_uart_rx.CLKS_PER_BIT = 217;
     defparam u_dut.u_uart_tx.CLKS_PER_BIT = 217;
     defparam u_dut.u_uart_tx.CLKS_PER_BIT = 217;
+    // Production UART stays 217 clocks/bit. Icarus wall-clock for 27 I2C
+    // TIMEOUT_CLKS=25000 reads exceeds hthd's 20s GET_TEMPERATURE budget
+    // (must stay < firmware watchdog 30s). Shorten only the sim I2C bit
+    // time / NACK timeout, same idea as the accelerated motor tick.
+    defparam u_dut.u_temperature_i2c.HALF_PERIOD_CLKS = 2;
+    defparam u_dut.u_temperature_i2c.TIMEOUT_CLKS = 16;
+    defparam u_dut.u_temperature_i2c.impl.HALF_PERIOD_CLKS = 2;
+    defparam u_dut.u_temperature_i2c.impl.TIMEOUT_CLKS = 16;
 
 
     initial begin
     initial begin
         force u_dut.tick_500us = fast_tick;
         force u_dut.tick_500us = fast_tick;

+ 18 - 1
tools/rtl_uart_cosim/run_hthd_pty_rtl.py

@@ -186,6 +186,20 @@ grpc:
     )
     )
 
 
 
 
+def _drain_fd(fd: int) -> None:
+    """Drop bytes already sitting on the PTY master before hthd opens the slave."""
+    while True:
+        ready, _, _ = select.select([fd], [], [], 0)
+        if not ready:
+            return
+        try:
+            chunk = os.read(fd, 4096)
+        except OSError:
+            return
+        if not chunk:
+            return
+
+
 def _set_raw_clocal(fd: int) -> None:
 def _set_raw_clocal(fd: int) -> None:
     """Binary 8N1-like PTY: no ICANON (or SET_ALL never leaves the line buffer)."""
     """Binary 8N1-like PTY: no ICANON (or SET_ALL never leaves the line buffer)."""
     attrs = termios.tcgetattr(fd)
     attrs = termios.tcgetattr(fd)
@@ -271,8 +285,10 @@ class UartTap:
         self._started.set()
         self._started.set()
 
 
     def holding(self) -> bool:
     def holding(self) -> bool:
+        # Rewrite MOTOR_STATUS before hthd starts so leftover 0x47 cannot
+        # sit in the PTY buffer and look like an immediate startup_complete.
         if not self._started.is_set():
         if not self._started.is_set():
-            return False
+            return True
         elapsed = time.monotonic() - self._start
         elapsed = time.monotonic() - self._start
         if elapsed >= self.max_hold_s:
         if elapsed >= self.max_hold_s:
             return False
             return False
@@ -678,6 +694,7 @@ def main() -> int:
         # baud) and at least one MOTOR_STATUS rewrite, so production UART
         # baud) and at least one MOTOR_STATUS rewrite, so production UART
         # proves no GET_TEMPERATURE during delayed startup.
         # proves no GET_TEMPERATURE during delayed startup.
         tap.mark_hthd_start()
         tap.mark_hthd_start()
+        _drain_fd(master)
         hthd = subprocess.Popen([str(hthd_bin), "--config", str(cfg)], cwd=str(HOST), env={**os.environ})
         hthd = subprocess.Popen([str(hthd_bin), "--config", str(cfg)], cwd=str(HOST), env={**os.environ})
         for _ in range(200):
         for _ in range(200):
             if Path(obs).exists() and Path(ctl).exists():
             if Path(obs).exists() and Path(ctl).exists():

+ 51 - 0
tools/rtl_uart_cosim/test_uart_tap.py

@@ -0,0 +1,51 @@
+#!/usr/bin/env python3
+"""Unit tests for the RTL UART delayed-startup tap (no Icarus)."""
+
+from __future__ import annotations
+
+import time
+
+from run_hthd_pty_rtl import UartTap, _clear_startup_complete, _crc16_ccitt_false
+
+
+def _motor(startup: bool) -> bytes:
+    payload = bytearray(31)
+    if startup:
+        payload[0] = 0x47
+    else:
+        payload[0] = 0x07
+    body = bytes([0x83, 31]) + bytes(payload)
+    crc = _crc16_ccitt_false(body)
+    return bytes([0xA5, 0x5A]) + body + bytes((crc & 0xFF, crc >> 8))
+
+
+def test_hold_before_mark_clears_startup():
+    tap = UartTap(hold_s=10.0, max_hold_s=20.0)
+    out = tap.rewrite_rtl_to_pty(_motor(True))
+    assert out[4] & 0x40 == 0, hex(out[4])
+    assert tap._cleared_motors == 1
+
+
+def test_hold_until_first_motor_after_mark():
+    tap = UartTap(hold_s=0.05, max_hold_s=1.0)
+    tap.mark_hthd_start()
+    time.sleep(0.08)
+    out = tap.rewrite_rtl_to_pty(_motor(True))
+    assert out[4] & 0x40 == 0, hex(out[4])
+    out2 = tap.rewrite_rtl_to_pty(_motor(True))
+    assert out2[4] & 0x40, hex(out2[4])
+
+
+def test_clear_startup_preserves_length():
+    raw = _motor(True)
+    cleared = _clear_startup_complete(raw)
+    assert len(cleared) == len(raw)
+    assert cleared[2] == 0x83
+    assert cleared[4] & 0x40 == 0
+
+
+if __name__ == "__main__":
+    test_hold_before_mark_clears_startup()
+    test_hold_until_first_motor_after_mark()
+    test_clear_startup_preserves_length()
+    print("PASS")