|
@@ -7,6 +7,7 @@ wire boundary: bytes are serialized as 8N1 bits at 217 system clocks/bit.
|
|
|
import cocotb
|
|
import cocotb
|
|
|
import cocotb.simulator as simulator
|
|
import cocotb.simulator as simulator
|
|
|
from cocotb.triggers import Event, FallingEdge, NextTimeStep, ReadOnly, ReadWrite, RisingEdge, SimTimeoutError, Timer, with_timeout
|
|
from cocotb.triggers import Event, FallingEdge, NextTimeStep, ReadOnly, ReadWrite, RisingEdge, SimTimeoutError, Timer, with_timeout
|
|
|
|
|
+from cocotb.utils import get_sim_time
|
|
|
|
|
|
|
|
|
|
|
|
|
CLKS_PER_BIT = 217
|
|
CLKS_PER_BIT = 217
|
|
@@ -16,6 +17,7 @@ TEMPERATURE_DATA = 0x82
|
|
|
MOTOR_STEPS = 450
|
|
MOTOR_STEPS = 450
|
|
|
MOTOR_COMPLETION_TIMEOUT_MS = 600
|
|
MOTOR_COMPLETION_TIMEOUT_MS = 600
|
|
|
TEMPERATURE_BYTE_TIMEOUT_MS = 100
|
|
TEMPERATURE_BYTE_TIMEOUT_MS = 100
|
|
|
|
|
+FRAME_ACCEPT_TIMEOUT_MS = 100
|
|
|
|
|
|
|
|
|
|
|
|
|
def crc16(data: bytes) -> int:
|
|
def crc16(data: bytes) -> int:
|
|
@@ -32,8 +34,22 @@ def frame(kind: int, payload: bytes) -> bytes:
|
|
|
return b"\xA5\x5A" + body + crc16(body).to_bytes(2, "little")
|
|
return b"\xA5\x5A" + body + crc16(body).to_bytes(2, "little")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+def _sim_time_ns() -> str:
|
|
|
|
|
+ try:
|
|
|
|
|
+ return f"{get_sim_time(unit='ns'):.0f}ns"
|
|
|
|
|
+ except Exception:
|
|
|
|
|
+ return "unknown"
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def checkpoint(stage: str, **fields) -> None:
|
|
|
|
|
+ """Emit a machine-searchable stage checkpoint with sim time."""
|
|
|
|
|
+ extra = " ".join(f"{key}={value}" for key, value in fields.items())
|
|
|
|
|
+ suffix = f" {extra}" if extra else ""
|
|
|
|
|
+ cocotb.log.info("checkpoint stage=%s sim_time=%s%s", stage, _sim_time_ns(), suffix)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
async def clocks(dut, count: int) -> None:
|
|
async def clocks(dut, count: int) -> None:
|
|
|
- await Timer(count * 40, units="ns")
|
|
|
|
|
|
|
+ await Timer(count * 40, unit="ns")
|
|
|
|
|
|
|
|
|
|
|
|
|
async def send_uart_byte(dut, value: int) -> None:
|
|
async def send_uart_byte(dut, value: int) -> None:
|
|
@@ -90,26 +106,44 @@ async def observe_motor_steps(dut, observed: dict[str, int | bool], completion:
|
|
|
|
|
|
|
|
async def receive_uart_byte(dut) -> int:
|
|
async def receive_uart_byte(dut) -> int:
|
|
|
await FallingEdge(dut.host_uart_tx)
|
|
await FallingEdge(dut.host_uart_tx)
|
|
|
- await Timer((CLKS_PER_BIT + CLKS_PER_BIT // 2) * 40, units="ns")
|
|
|
|
|
|
|
+ await Timer((CLKS_PER_BIT + CLKS_PER_BIT // 2) * 40, unit="ns")
|
|
|
value = 0
|
|
value = 0
|
|
|
for bit in range(8):
|
|
for bit in range(8):
|
|
|
value |= int(dut.host_uart_tx.value) << bit
|
|
value |= int(dut.host_uart_tx.value) << bit
|
|
|
- await Timer(CLKS_PER_BIT * 40, units="ns")
|
|
|
|
|
|
|
+ await Timer(CLKS_PER_BIT * 40, unit="ns")
|
|
|
assert int(dut.host_uart_tx.value) == 1
|
|
assert int(dut.host_uart_tx.value) == 1
|
|
|
return value
|
|
return value
|
|
|
|
|
|
|
|
|
|
|
|
|
-async def wait_for_stage(awaitable, timeout_ms: int, stage: str, byte_index: int = -1):
|
|
|
|
|
|
|
+async def wait_for_stage(awaitable, timeout_ms: int, stage: str, byte_index: int = -1, **fields):
|
|
|
"""Apply a bounded wait with a stable, machine-searchable failure message."""
|
|
"""Apply a bounded wait with a stable, machine-searchable failure message."""
|
|
|
try:
|
|
try:
|
|
|
return await with_timeout(awaitable, timeout_ms, "ms")
|
|
return await with_timeout(awaitable, timeout_ms, "ms")
|
|
|
except SimTimeoutError as exc:
|
|
except SimTimeoutError as exc:
|
|
|
suffix = "" if byte_index < 0 else f" byte_index={byte_index}"
|
|
suffix = "" if byte_index < 0 else f" byte_index={byte_index}"
|
|
|
|
|
+ extra = "".join(f" {key}={value}" for key, value in fields.items())
|
|
|
raise AssertionError(
|
|
raise AssertionError(
|
|
|
- f"timeout stage={stage} timeout_ms={timeout_ms}{suffix}"
|
|
|
|
|
|
|
+ f"timeout stage={stage} timeout_ms={timeout_ms} sim_time={_sim_time_ns()}"
|
|
|
|
|
+ f"{suffix}{extra}"
|
|
|
) from exc
|
|
) from exc
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+async def wait_level_or_edge(signal, *, want: int, timeout_ms: int, stage: str) -> None:
|
|
|
|
|
+ """Continue if signal already matches want; else wait for the matching edge."""
|
|
|
|
|
+ current = int(signal.value) if signal.value.is_resolvable else None
|
|
|
|
|
+ if current == want:
|
|
|
|
|
+ checkpoint(stage, already=want)
|
|
|
|
|
+ return
|
|
|
|
|
+ edge = RisingEdge(signal) if want else FallingEdge(signal)
|
|
|
|
|
+ await wait_for_stage(
|
|
|
|
|
+ edge,
|
|
|
|
|
+ timeout_ms,
|
|
|
|
|
+ stage,
|
|
|
|
|
+ signal_level=current,
|
|
|
|
|
+ want=want,
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
def stop_background_task(task) -> None:
|
|
def stop_background_task(task) -> None:
|
|
|
if task is None:
|
|
if task is None:
|
|
|
return
|
|
return
|
|
@@ -125,6 +159,7 @@ async def test_set_all_get_temperature_over_real_uart(dut):
|
|
|
observed = {"x": 0, "y": 0, "overlap": False}
|
|
observed = {"x": 0, "y": 0, "overlap": False}
|
|
|
motor_completion = Event()
|
|
motor_completion = Event()
|
|
|
try:
|
|
try:
|
|
|
|
|
+ checkpoint("test_start")
|
|
|
observer_task = cocotb.start_soon(
|
|
observer_task = cocotb.start_soon(
|
|
|
observe_motor_steps(dut, observed, motor_completion)
|
|
observe_motor_steps(dut, observed, motor_completion)
|
|
|
)
|
|
)
|
|
@@ -136,41 +171,60 @@ async def test_set_all_get_temperature_over_real_uart(dut):
|
|
|
await clocks(dut, 1024)
|
|
await clocks(dut, 1024)
|
|
|
# Limits already high => startup may finish during the wait above.
|
|
# Limits already high => startup may finish during the wait above.
|
|
|
# RisingEdge alone would hang forever if startup_done is already 1.
|
|
# RisingEdge alone would hang forever if startup_done is already 1.
|
|
|
- if int(dut.u_dut.startup_done.value) == 0:
|
|
|
|
|
- await wait_for_stage(
|
|
|
|
|
- RisingEdge(dut.u_dut.startup_done),
|
|
|
|
|
- MOTOR_COMPLETION_TIMEOUT_MS,
|
|
|
|
|
- "startup_done",
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ await wait_level_or_edge(
|
|
|
|
|
+ dut.u_dut.startup_done,
|
|
|
|
|
+ want=1,
|
|
|
|
|
+ timeout_ms=MOTOR_COMPLETION_TIMEOUT_MS,
|
|
|
|
|
+ stage="startup_done",
|
|
|
|
|
+ )
|
|
|
|
|
+ checkpoint("startup_done")
|
|
|
|
|
|
|
|
# X=01 open (DIR=0), Y=02 close (DIR=1); 450 steps each, overlapping.
|
|
# X=01 open (DIR=0), Y=02 close (DIR=1); 450 steps each, overlapping.
|
|
|
payload = bytes((10, 20, 30, 40, 0, 0, 0, 0, 0, 1, 2))
|
|
payload = bytes((10, 20, 30, 40, 0, 0, 0, 0, 0, 1, 2))
|
|
|
|
|
+ checkpoint("set_all_tx")
|
|
|
await send_frame(dut, frame(SET_ALL, payload))
|
|
await send_frame(dut, frame(SET_ALL, payload))
|
|
|
# Sample DIR while the jog is active (before completion).
|
|
# Sample DIR while the jog is active (before completion).
|
|
|
await clocks(dut, CLKS_PER_BIT * 8)
|
|
await clocks(dut, CLKS_PER_BIT * 8)
|
|
|
assert int(dut.x_dir.value) == 0, "SET_ALL X=01 must drive DIR=0 (open)"
|
|
assert int(dut.x_dir.value) == 0, "SET_ALL X=01 must drive DIR=0 (open)"
|
|
|
assert int(dut.y_dir.value) == 1, "SET_ALL Y=02 must drive DIR=1 (close)"
|
|
assert int(dut.y_dir.value) == 1, "SET_ALL Y=02 must drive DIR=1 (close)"
|
|
|
|
|
+ checkpoint("dir_sampled", x_dir=int(dut.x_dir.value), y_dir=int(dut.y_dir.value))
|
|
|
try:
|
|
try:
|
|
|
await with_timeout(motor_completion.wait(), MOTOR_COMPLETION_TIMEOUT_MS, "ms")
|
|
await with_timeout(motor_completion.wait(), MOTOR_COMPLETION_TIMEOUT_MS, "ms")
|
|
|
except SimTimeoutError as exc:
|
|
except SimTimeoutError as exc:
|
|
|
raise AssertionError(
|
|
raise AssertionError(
|
|
|
"timeout stage=motor_completion "
|
|
"timeout stage=motor_completion "
|
|
|
- f"timeout_ms={MOTOR_COMPLETION_TIMEOUT_MS} "
|
|
|
|
|
|
|
+ f"timeout_ms={MOTOR_COMPLETION_TIMEOUT_MS} sim_time={_sim_time_ns()} "
|
|
|
f"observed_x={observed['x']} observed_y={observed['y']}"
|
|
f"observed_x={observed['x']} observed_y={observed['y']}"
|
|
|
) from exc
|
|
) from exc
|
|
|
assert observed["x"] == MOTOR_STEPS
|
|
assert observed["x"] == MOTOR_STEPS
|
|
|
assert observed["y"] == MOTOR_STEPS
|
|
assert observed["y"] == MOTOR_STEPS
|
|
|
assert observed["overlap"] is True
|
|
assert observed["overlap"] is True
|
|
|
assert int(dut.motor_en_n.value) == 0
|
|
assert int(dut.motor_en_n.value) == 0
|
|
|
|
|
+ checkpoint(
|
|
|
|
|
+ "motor_completion",
|
|
|
|
|
+ observed_x=observed["x"],
|
|
|
|
|
+ observed_y=observed["y"],
|
|
|
|
|
+ overlap=observed["overlap"],
|
|
|
|
|
+ )
|
|
|
|
|
|
|
|
-
|
|
|
|
|
- await with_timeout(
|
|
|
|
|
- FallingEdge(dut.x_step),
|
|
|
|
|
- MOTOR_COMPLETION_TIMEOUT_MS,
|
|
|
|
|
- "ms",
|
|
|
|
|
|
|
+ # STEP may already be low after the last half-cycle; FallingEdge alone hangs.
|
|
|
|
|
+ await wait_level_or_edge(
|
|
|
|
|
+ dut.x_step,
|
|
|
|
|
+ want=0,
|
|
|
|
|
+ timeout_ms=MOTOR_COMPLETION_TIMEOUT_MS,
|
|
|
|
|
+ stage="x_step_idle",
|
|
|
|
|
+ )
|
|
|
|
|
+ # SET_ALL returns the controller to IDLE long before motors finish, so
|
|
|
|
|
+ # frame_accept_enable is often already 1; RisingEdge alone hangs forever.
|
|
|
|
|
+ await wait_level_or_edge(
|
|
|
|
|
+ dut.u_dut.frame_accept_enable,
|
|
|
|
|
+ want=1,
|
|
|
|
|
+ timeout_ms=FRAME_ACCEPT_TIMEOUT_MS,
|
|
|
|
|
+ stage="frame_accept_enable",
|
|
|
)
|
|
)
|
|
|
- await RisingEdge(dut.u_dut.frame_accept_enable)
|
|
|
|
|
|
|
+ checkpoint("frame_accept_enable")
|
|
|
await clocks(dut, 4)
|
|
await clocks(dut, 4)
|
|
|
|
|
+ checkpoint("get_temperature_tx")
|
|
|
await send_frame(dut, frame(GET_TEMPERATURE, b""))
|
|
await send_frame(dut, frame(GET_TEMPERATURE, b""))
|
|
|
response = bytearray()
|
|
response = bytearray()
|
|
|
for byte_index in range(33):
|
|
for byte_index in range(33):
|
|
@@ -187,6 +241,7 @@ async def test_set_all_get_temperature_over_real_uart(dut):
|
|
|
assert len(response) == 33
|
|
assert len(response) == 33
|
|
|
assert response[-2:] == crc16(response[2:-2]).to_bytes(2, "little")
|
|
assert response[-2:] == crc16(response[2:-2]).to_bytes(2, "little")
|
|
|
assert set(response[4:31]) == {0xFF}
|
|
assert set(response[4:31]) == {0xFF}
|
|
|
|
|
+ checkpoint("temperature_response_ok", bytes=len(response))
|
|
|
finally:
|
|
finally:
|
|
|
stop_background_task(observer_task)
|
|
stop_background_task(observer_task)
|
|
|
try:
|
|
try:
|