|
|
@@ -8,7 +8,13 @@ from pathlib import Path
|
|
|
import cocotb
|
|
|
from cocotb.clock import Clock
|
|
|
from cocotb.queue import Queue
|
|
|
-from cocotb.triggers import ClockCycles, ReadOnly, RisingEdge, with_timeout
|
|
|
+from cocotb.triggers import (
|
|
|
+ ClockCycles,
|
|
|
+ ReadOnly,
|
|
|
+ RisingEdge,
|
|
|
+ SimTimeoutError,
|
|
|
+ with_timeout,
|
|
|
+)
|
|
|
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
|
@@ -19,17 +25,22 @@ import prod2u_p4_protocol as protocol # noqa: E402
|
|
|
|
|
|
UART_CLKS_PER_BIT = 4
|
|
|
FRAME_TIMEOUT_US = 5000
|
|
|
+TEMPERATURE_BYTE_TIMEOUT_US = 200
|
|
|
QUIET_CYCLES = 128
|
|
|
+TEMPERATURE_FRAME_LEN = 35
|
|
|
|
|
|
GOLDEN_DISABLE = bytes.fromhex("A55A02040500000000000000000426000000C4AE")
|
|
|
GOLDEN_GET_STATUS = bytes.fromhex("A55A02040500000000000000000424000000AC43")
|
|
|
+GOLDEN_GET_TEMPERATURES = bytes.fromhex(
|
|
|
+ "A55A020405000000000000000004280000009E0C"
|
|
|
+)
|
|
|
GOLDEN_GET_MOTOR_STATUS = bytes.fromhex(
|
|
|
"A55A0204110000000000000000083700000000000000347F"
|
|
|
)
|
|
|
GOLDEN_MARK_CLOSED = bytes.fromhex(
|
|
|
"A55A0204110000000000000000083600010000000000477D"
|
|
|
)
|
|
|
-GOLDEN_TEMPERATURE_REQUEST = bytes.fromhex(
|
|
|
+GOLDEN_LEGACY_TEMPERATURE_REQUEST = bytes.fromhex(
|
|
|
"A55A020405000000000000000004290000002A7A"
|
|
|
)
|
|
|
GOLDEN_X_JOG_65535 = bytes.fromhex(
|
|
|
@@ -99,8 +110,8 @@ async def step_monitor(dut, observed: dict[str, int | bool]) -> None:
|
|
|
previous_y = y_value
|
|
|
|
|
|
|
|
|
-async def next_uart_byte(queue: Queue) -> int:
|
|
|
- return await with_timeout(queue.get(), FRAME_TIMEOUT_US, "us")
|
|
|
+async def next_uart_byte(queue: Queue, timeout_us: int = FRAME_TIMEOUT_US) -> int:
|
|
|
+ return await with_timeout(queue.get(), timeout_us, "us")
|
|
|
|
|
|
|
|
|
async def read_uart_frame(queue: Queue) -> protocol.P4Frame:
|
|
|
@@ -113,6 +124,32 @@ async def read_uart_frame(queue: Queue) -> protocol.P4Frame:
|
|
|
return protocol.decode_frame(bytes(raw))
|
|
|
|
|
|
|
|
|
+async def read_temperature_frame(dut, queue: Queue) -> bytes:
|
|
|
+ raw = bytearray()
|
|
|
+ try:
|
|
|
+ for _ in range(TEMPERATURE_FRAME_LEN):
|
|
|
+ raw.append(
|
|
|
+ await next_uart_byte(queue, TEMPERATURE_BYTE_TIMEOUT_US)
|
|
|
+ )
|
|
|
+ except SimTimeoutError as exc:
|
|
|
+ raise AssertionError(
|
|
|
+ "temperature UART timeout "
|
|
|
+ f"raw={raw.hex()} "
|
|
|
+ f"read_state={int(dut.u_dut.u_temperature_read.state.value)} "
|
|
|
+ f"i2c_state={int(dut.u_dut.u_temperature_i2c.state.value)} "
|
|
|
+ f"i2c_error={int(dut.u_dut.u_temperature_i2c.response_error.value)} "
|
|
|
+ f"scl={dut.temp_scl.value} sda={dut.temp_sda.value} "
|
|
|
+ f"scl_low={int(dut.u_dut.temperature_scl_drive_low.value)} "
|
|
|
+ f"sda_low={int(dut.u_dut.temperature_sda_drive_low.value)} "
|
|
|
+ f"read_done={int(dut.u_dut.temperature_read_done.value)} "
|
|
|
+ f"bus_error={int(dut.u_dut.temperature_bus_error.value)} "
|
|
|
+ f"scan_active={int(dut.u_ct7428_slave.scan_active.value)} "
|
|
|
+ f"scan_count={int(dut.u_ct7428_slave.scan_count.value)} "
|
|
|
+ f"transaction_error={int(dut.u_ct7428_slave.transaction_error.value)}"
|
|
|
+ ) from exc
|
|
|
+ return protocol.decode_temperature_response(bytes(raw))
|
|
|
+
|
|
|
+
|
|
|
async def send_and_read_ack(dut, queue: Queue, request: bytes) -> protocol.HostAck:
|
|
|
await send_uart_frame(dut, request)
|
|
|
frame = await read_uart_frame(queue)
|
|
|
@@ -120,6 +157,11 @@ async def send_and_read_ack(dut, queue: Queue, request: bytes) -> protocol.HostA
|
|
|
return protocol.decode_host_ack(frame)
|
|
|
|
|
|
|
|
|
+async def send_and_read_temperatures(dut, queue: Queue, request: bytes) -> bytes:
|
|
|
+ await send_uart_frame(dut, request)
|
|
|
+ return await read_temperature_frame(dut, queue)
|
|
|
+
|
|
|
+
|
|
|
async def assert_uart_quiet(dut, queue: Queue, label: str) -> None:
|
|
|
await ClockCycles(dut.sys_clk, QUIET_CYCLES)
|
|
|
assert queue.qsize() == 0, f"unexpected follow-up bytes after {label}"
|
|
|
@@ -132,6 +174,7 @@ def assert_request_goldens() -> None:
|
|
|
assert protocol.build_host_command(command=protocol.CommandId.DISABLE) == (
|
|
|
GOLDEN_DISABLE
|
|
|
)
|
|
|
+ assert protocol.build_get_temperatures() == GOLDEN_GET_TEMPERATURES
|
|
|
assert protocol.build_get_motor_status() == GOLDEN_GET_MOTOR_STATUS
|
|
|
assert protocol.build_mark_closed(axis=0, close_dir=1) == GOLDEN_MARK_CLOSED
|
|
|
assert protocol.build_motor_jog(
|
|
|
@@ -177,12 +220,72 @@ async def ack_only_uart_contract(dut) -> None:
|
|
|
await ClockCycles(dut.sys_clk, 128)
|
|
|
assert received.qsize() == 0
|
|
|
|
|
|
+ early_temperatures = await send_and_read_temperatures(
|
|
|
+ dut,
|
|
|
+ received,
|
|
|
+ GOLDEN_GET_TEMPERATURES,
|
|
|
+ )
|
|
|
+ assert early_temperatures == b"\xff" * 27
|
|
|
+ assert int(dut.u_ct7428_slave.scan_count.value) == 0
|
|
|
+ await assert_uart_quiet(dut, received, "early temperature response")
|
|
|
+
|
|
|
+ await with_timeout(
|
|
|
+ RisingEdge(dut.temperature_powerup_ready_debug),
|
|
|
+ FRAME_TIMEOUT_US,
|
|
|
+ "us",
|
|
|
+ )
|
|
|
+
|
|
|
+ temperatures = await send_and_read_temperatures(
|
|
|
+ dut,
|
|
|
+ received,
|
|
|
+ GOLDEN_GET_TEMPERATURES,
|
|
|
+ )
|
|
|
+ assert temperatures == bytes(range(27))
|
|
|
+ assert int(dut.u_ct7428_slave.scan_count.value) == 1
|
|
|
+ assert int(dut.u_ct7428_slave.transaction_error.value) == 0
|
|
|
+ assert int(dut.u_ct7428_slave.address_49_count.value) == 9
|
|
|
+ assert int(dut.u_ct7428_slave.address_48_count.value) == 9
|
|
|
+ assert int(dut.u_ct7428_slave.address_4b_count.value) == 9
|
|
|
+ assert int(dut.u_ct7428_slave.read_ack_count.value) == 27
|
|
|
+ assert int(dut.u_ct7428_slave.read_nack_count.value) == 27
|
|
|
+
|
|
|
+ dut.mixed_nack.value = 1
|
|
|
+ dut.nack_slot.value = 10
|
|
|
+ mixed_temperatures = await send_and_read_temperatures(
|
|
|
+ dut,
|
|
|
+ received,
|
|
|
+ GOLDEN_GET_TEMPERATURES,
|
|
|
+ )
|
|
|
+ expected_mixed = bytearray(range(27))
|
|
|
+ expected_mixed[10] = 0xFF
|
|
|
+ assert mixed_temperatures == bytes(expected_mixed)
|
|
|
+ assert int(dut.u_ct7428_slave.scan_count.value) == 2
|
|
|
+ assert int(dut.u_ct7428_slave.transaction_error.value) == 0
|
|
|
+ assert int(dut.u_ct7428_slave.address_49_count.value) == 9
|
|
|
+ assert int(dut.u_ct7428_slave.address_48_count.value) == 9
|
|
|
+ assert int(dut.u_ct7428_slave.address_4b_count.value) == 9
|
|
|
+ assert int(dut.u_ct7428_slave.read_ack_count.value) == 26
|
|
|
+ assert int(dut.u_ct7428_slave.read_nack_count.value) == 26
|
|
|
+ dut.mixed_nack.value = 0
|
|
|
+
|
|
|
+ dut.force_sda_low.value = 1
|
|
|
+ bus_error_ack = await send_and_read_ack(
|
|
|
+ dut,
|
|
|
+ received,
|
|
|
+ GOLDEN_GET_TEMPERATURES,
|
|
|
+ )
|
|
|
+ assert bus_error_ack.ok is False
|
|
|
+ assert bus_error_ack.command == protocol.CommandId.GET_TEMPERATURES
|
|
|
+ assert bus_error_ack.status == protocol.StatusCode.TEMPERATURE_BUS_ERROR
|
|
|
+ dut.force_sda_low.value = 0
|
|
|
+ await assert_uart_quiet(dut, received, "temperature bus error ACK")
|
|
|
+
|
|
|
status_ack = await send_and_read_ack(dut, received, GOLDEN_GET_STATUS)
|
|
|
assert status_ack.ok is True
|
|
|
assert status_ack.status == protocol.StatusCode.OK
|
|
|
|
|
|
for request, command in (
|
|
|
- (GOLDEN_TEMPERATURE_REQUEST, 0x29),
|
|
|
+ (GOLDEN_LEGACY_TEMPERATURE_REQUEST, 0x29),
|
|
|
(GOLDEN_MARK_CLOSED, protocol.MotionOpcode.MARK_CLOSED),
|
|
|
):
|
|
|
ack = await send_and_read_ack(dut, received, request)
|