| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- """Production-profile full-top integration over real Host and BMC UART bits."""
- from __future__ import annotations
- import cocotb
- from cocotb.triggers import ReadOnly, RisingEdge, Timer, with_timeout
- from thermal_6400_uart import (
- BMC_CLKS_PER_BIT,
- CLOCK_NS,
- GET_SNAPSHOT,
- GET_SNAPSHOT_ACK,
- HOST_CLKS_PER_BIT,
- SET_STIMULUS,
- SET_TARGET,
- SET_TARGET_ACK,
- SNAPSHOT_DATA,
- bring_auto,
- check_pwm_timing,
- encode_frame,
- get_snapshot,
- host_transaction,
- pulse_tick_1s,
- receive_frame,
- send_bmc_snapshot,
- send_uart,
- wait_for,
- )
- def duties(snapshot: dict[str, object], name: str) -> list[int]:
- value = snapshot[name]
- assert isinstance(value, list)
- return [int(item) for item in value]
- @cocotb.test()
- async def test_control_dual_host_sensor_safety_and_snapshot(dut):
- pwm_timing = cocotb.start_soon(check_pwm_timing(dut))
- await bring_auto(dut, verify_boot=True)
- # GET is one locked two-frame transaction. Traffic on the alternate Host
- # after ACK must not change port_sel, unlock snapshot banks, or commit a
- # command before the 0x42 DATA frame's final physical stop bit retires.
- get_state = {"ack": False, "done": False}
- async def receive_locked_get() -> tuple[int, bytes]:
- ack = await receive_frame(dut.host_ttl_tx)
- assert ack == (GET_SNAPSHOT_ACK, b"")
- get_state["ack"] = True
- data = await receive_frame(dut.host_ttl_tx)
- get_state["done"] = True
- return data
- target_before_locked_get = int(dut.current_target_mon.value)
- get_task = cocotb.start_soon(receive_locked_get())
- await send_uart(dut.host_ttl_rx, encode_frame(GET_SNAPSHOT), HOST_CLKS_PER_BIT)
- while not get_state["ack"]:
- await RisingEdge(dut.sys_clk)
- assert int(dut.host_locked_mon.value) == 1
- assert int(dut.host_port_sel_mon.value) == 1
- assert int(dut.u_dut.snapshot_locked.value) == 1
- alternate = cocotb.start_soon(
- send_uart(dut.host_rs232_rx, encode_frame(SET_TARGET, b"\x46"), HOST_CLKS_PER_BIT)
- )
- while not get_state["done"]:
- await RisingEdge(dut.sys_clk)
- await ReadOnly()
- assert int(dut.host_locked_mon.value) == 1
- assert int(dut.host_port_sel_mon.value) == 1
- assert int(dut.u_dut.snapshot_locked.value) == 1
- data_kind, data_payload = await with_timeout(get_task, 5, "ms")
- assert data_kind == SNAPSHOT_DATA and len(data_payload) == 193
- # receive_frame completes at the center of the final stop bit; lock must
- # still be present until uart_tx byte_done at the end of that stop bit.
- assert int(dut.host_locked_mon.value) == 1
- assert int(dut.u_dut.snapshot_locked.value) == 1
- await alternate
- await wait_for(
- dut,
- lambda: int(dut.host_locked_mon.value) == 0
- and int(dut.u_dut.snapshot_locked.value) == 0,
- cycles=100,
- label="GET final-stop unlock",
- )
- assert int(dut.current_target_mon.value) == target_before_locked_get
- # Production must silently reject the identification-only command.
- rejected_payload = bytes([2, *([20] * 32)])
- await send_uart(
- dut.host_rs232_rx,
- encode_frame(SET_STIMULUS, rejected_payload),
- HOST_CLKS_PER_BIT,
- )
- await Timer(HOST_CLKS_PER_BIT * CLOCK_NS * 20, units="ns")
- assert int(dut.host_rs232_tx.value) == 1
- assert int(dut.host_ttl_tx.value) == 1
- assert int(dut.stimulus_active_mon.value) == 0
- # Exercise simultaneous Host and BMC receive frontends. ACK is routed
- # only to RS-232 while the BMC commits a complete fifth snapshot.
- old_control_epoch = int(dut.control_epoch_mon.value)
- bmc_task = cocotb.start_soon(send_bmc_snapshot(dut, [61] * 24))
- ack_task = cocotb.start_soon(receive_frame(dut.host_rs232_tx))
- await send_uart(dut.host_rs232_rx, encode_frame(SET_TARGET, b"\x41"), HOST_CLKS_PER_BIT)
- ack_kind, ack_payload = await with_timeout(ack_task, 20, "ms")
- assert (ack_kind, ack_payload) == (SET_TARGET_ACK, b"")
- assert int(dut.host_ttl_tx.value) == 1
- await bmc_task
- # A target change deliberately restarts CT/preheat qualification. The
- # concurrent BMC frame enters WAIT_BMC_STABLE; three further new epochs
- # are required before AUTO resumes.
- for _ in range(3):
- await send_bmc_snapshot(dut, [61] * 24)
- await wait_for(
- dut,
- lambda: int(dut.control_state_mon.value) == 4
- and int(dut.control_epoch_mon.value) > old_control_epoch,
- cycles=1000,
- label="target/BMC control commit",
- )
- await Timer(HOST_CLKS_PER_BIT * CLOCK_NS * 2, units="ns")
- snapshot = await get_snapshot(dut, "ttl")
- assert snapshot["control_state"] == 4
- assert snapshot["current_target"] == 65
- assert snapshot["host_target_seen"] == 1
- assert int(snapshot["bmc_epoch"]) >= 5
- assert int(snapshot["ct_epoch"]) >= 1
- assert snapshot["bmc_valid_mask"] == (1 << 24) - 1
- assert snapshot["ct_valid_mask"] == (1 << 27) - 1
- assert duties(snapshot, "bmc_raw") == [0x3D00] * 24
- assert duties(snapshot, "ct_raw") == [0x3C00] * 27
- ptc = duties(snapshot, "ptc_applied")
- assert len(ptc) == 24 and all(0 <= value <= 100 for value in ptc) and any(ptc)
- assert duties(snapshot, "front_applied") == [30] * 4
- assert duties(snapshot, "rear_applied") == [30] * 4
- # A 74 C sample reduces only the matching PTC and raises both fans for
- # that lane to the contract's 80% protection duty. The public safety bit
- # remains telemetry and must not turn the soft action into a full cutoff.
- old_control_epoch = int(dut.control_epoch_mon.value)
- await send_bmc_snapshot(dut, [74, *([60] * 23)])
- await wait_for(
- dut,
- lambda: int(dut.control_epoch_mon.value) > old_control_epoch,
- cycles=1000,
- label="74 C soft-limit commit",
- )
- soft = await get_snapshot(dut, "rs232")
- soft_ptc = duties(soft, "ptc_applied")
- assert int(soft["safety_override_mask"]) & 1
- assert ptc[0] > 1 and soft_ptc[0] == ptc[0] - 1
- assert duties(soft, "front_applied") == [80, 30, 30, 30]
- assert duties(soft, "rear_applied") == [80, 30, 30, 30]
- # At 76 C the matching PTC is fully off, but the lane is not yet hard
- # faulted and therefore remains at 80% rather than 100% airflow.
- old_control_epoch = int(dut.control_epoch_mon.value)
- await send_bmc_snapshot(dut, [76, *([60] * 23)])
- await wait_for(
- dut,
- lambda: int(dut.control_epoch_mon.value) > old_control_epoch,
- cycles=1000,
- label="76 C pretrip commit",
- )
- pretrip = await get_snapshot(dut, "ttl")
- assert int(pretrip["safety_override_mask"]) & 1
- assert not (int(pretrip["lane_fault_mask"]) & 1)
- assert duties(pretrip, "ptc_applied")[0] == 0
- assert duties(pretrip, "front_applied") == [80, 30, 30, 30]
- assert duties(pretrip, "rear_applied") == [80, 30, 30, 30]
- # One invalid DIMM is quarantined without corrupting the other 23 values.
- await send_bmc_snapshot(dut, [None, *([60] * 23)])
- await wait_for(
- dut,
- lambda: int(dut.control_state_mon.value) == 5 and bool(int(dut.safety_mask_mon.value) & 1),
- cycles=1000,
- label="single-channel invalid override",
- )
- invalid = await get_snapshot(dut, "rs232")
- assert int(invalid["bmc_valid_mask"]) == ((1 << 24) - 2)
- assert int(invalid["safety_override_mask"]) & 1
- assert duties(invalid, "ptc_applied")[0] == 0
- # A 77 C sample latches the full L0 hard override and both lane fans.
- await send_bmc_snapshot(dut, [77, *([60] * 23)])
- await wait_for(
- dut,
- lambda: bool(int(dut.lane_fault_mask_mon.value) & 1),
- cycles=1000,
- label="lane hard limit",
- )
- hard = await get_snapshot(dut, "ttl")
- assert int(hard["lane_fault_mask"]) & 1
- assert duties(hard, "ptc_applied")[:6] == [0] * 6
- assert duties(hard, "front_applied")[0] == 100
- assert duties(hard, "rear_applied")[0] == 100
- # Repeating a legal target while the lane hard fault is active must ACK
- # but cannot reduce its applied airflow or its physical connector request.
- old_control_epoch = int(dut.control_epoch_mon.value)
- kind, payload = await host_transaction(dut, "rs232", SET_TARGET, b"\x41")
- assert (kind, payload) == (SET_TARGET_ACK, b"")
- await wait_for(
- dut,
- lambda: int(dut.control_epoch_mon.value) > old_control_epoch,
- cycles=1000,
- label="hard-fault repeated target safe commit",
- )
- hard_after_target = await get_snapshot(dut, "ttl")
- assert int(hard_after_target["lane_fault_mask"]) & 1
- assert duties(hard_after_target, "front_applied")[0] == 100
- assert duties(hard_after_target, "rear_applied")[0] == 100
- for _ in range(2):
- await wait_for(dut, lambda: int(dut.pwm_boundary_mon.value) == 1,
- cycles=600, label="hard-fault physical fan boundary")
- await Timer(CLOCK_NS, units="ns")
- assert not (int(dut.front_fan_pwm_out.value) & 0x1)
- assert int(dut.rear_fan_pwm_out.value) & 0x1
- # Requalify acquisition while retaining the hot lane, so the stale test
- # below starts from an admitted runtime state rather than startup gating.
- for _ in range(4):
- await send_bmc_snapshot(dut, [77, *([60] * 23)])
- await wait_for(
- dut,
- lambda: int(dut.control_state_mon.value) == 5,
- cycles=1000,
- label="hard-fault runtime requalification",
- )
- # Three explicit age ticks without a new BMC epoch create global stale.
- await pulse_tick_1s(dut, 3)
- await wait_for(
- dut,
- lambda: int(dut.global_fault_mon.value) == 1,
- cycles=200,
- label="global BMC stale",
- )
- stale = await get_snapshot(dut, "rs232")
- assert stale["global_fault"] == 1
- assert duties(stale, "ptc_applied") == [0] * 24
- assert duties(stale, "front_applied") == [100] * 4
- assert duties(stale, "rear_applied") == [100] * 4
- # A legal target during global stale also ACKs without weakening the
- # already-applied all-fan safety request.
- old_control_epoch = int(dut.control_epoch_mon.value)
- kind, payload = await host_transaction(dut, "ttl", SET_TARGET, b"\x41")
- assert (kind, payload) == (SET_TARGET_ACK, b"")
- await wait_for(
- dut,
- lambda: int(dut.control_epoch_mon.value) > old_control_epoch,
- cycles=1000,
- label="stale target safe commit",
- )
- stale_after_target = await get_snapshot(dut, "rs232")
- assert stale_after_target["global_fault"] == 1
- assert duties(stale_after_target, "front_applied") == [100] * 4
- assert duties(stale_after_target, "rear_applied") == [100] * 4
- for _ in range(2):
- await wait_for(dut, lambda: int(dut.pwm_boundary_mon.value) == 1,
- cycles=600, label="stale physical fan boundary")
- await Timer(CLOCK_NS, units="ns")
- assert int(dut.front_fan_pwm_out.value) == 0x0
- assert int(dut.rear_fan_pwm_out.value) == 0xF
- # Three complete safe epochs retain the clamp through the release commit;
- # the following epoch resumes only the normal slew-limited ramp.
- for _ in range(3):
- await send_bmc_snapshot(dut, [60] * 24)
- await wait_for(
- dut,
- lambda: int(dut.control_epoch_mon.value) >= 1,
- cycles=1000,
- label="recovery control pass",
- )
- await wait_for(
- dut,
- lambda: int(dut.global_fault_mon.value) == 0
- and int(dut.lane_fault_mask_mon.value) == 0
- and int(dut.control_state_mon.value) == 4,
- cycles=1000,
- label="three-epoch recovery",
- )
- released = await get_snapshot(dut, "ttl")
- assert duties(released, "ptc_applied") == [0] * 24
- await send_bmc_snapshot(dut, [60] * 24)
- await wait_for(
- dut,
- lambda: int(dut.control_epoch_mon.value) > int(released["control_epoch"]),
- cycles=1000,
- label="post-recovery limited ramp",
- )
- ramped = await get_snapshot(dut, "rs232")
- assert all(0 <= value <= 5 for value in duties(ramped, "ptc_applied"))
- assert any(duties(ramped, "ptc_applied"))
- kind, payload = await host_transaction(dut, "ttl", SET_TARGET, b"\x00")
- assert (kind, payload) == (SET_TARGET_ACK, b"")
- stopped = await get_snapshot(dut, "rs232")
- assert stopped["control_state"] == 6
- assert duties(stopped, "ptc_applied") == [0] * 24
- await pwm_timing
|