test_6400_v1_control_integration.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. """Production-profile full-top integration over real Host and BMC UART bits."""
  2. from __future__ import annotations
  3. import cocotb
  4. from cocotb.triggers import Timer, with_timeout
  5. from thermal_6400_uart import (
  6. BMC_CLKS_PER_BIT,
  7. CLOCK_NS,
  8. HOST_CLKS_PER_BIT,
  9. SET_STIMULUS,
  10. SET_TARGET,
  11. SET_TARGET_ACK,
  12. bring_auto,
  13. check_pwm_timing,
  14. encode_frame,
  15. get_snapshot,
  16. host_transaction,
  17. pulse_tick_1s,
  18. receive_frame,
  19. send_bmc_snapshot,
  20. send_uart,
  21. wait_for,
  22. )
  23. def duties(snapshot: dict[str, object], name: str) -> list[int]:
  24. value = snapshot[name]
  25. assert isinstance(value, list)
  26. return [int(item) for item in value]
  27. @cocotb.test()
  28. async def test_control_dual_host_sensor_safety_and_snapshot(dut):
  29. pwm_timing = cocotb.start_soon(check_pwm_timing(dut))
  30. await bring_auto(dut)
  31. # Production must silently reject the identification-only command.
  32. rejected_payload = bytes([2, *([20] * 32)])
  33. await send_uart(
  34. dut.host_rs232_rx,
  35. encode_frame(SET_STIMULUS, rejected_payload),
  36. HOST_CLKS_PER_BIT,
  37. )
  38. await Timer(HOST_CLKS_PER_BIT * CLOCK_NS * 20, units="ns")
  39. assert int(dut.host_rs232_tx.value) == 1
  40. assert int(dut.host_ttl_tx.value) == 1
  41. assert int(dut.stimulus_active_mon.value) == 0
  42. # Exercise simultaneous Host and BMC receive frontends. ACK is routed
  43. # only to RS-232 while the BMC commits a complete fifth snapshot.
  44. old_control_epoch = int(dut.control_epoch_mon.value)
  45. bmc_task = cocotb.start_soon(send_bmc_snapshot(dut, [61] * 24))
  46. ack_task = cocotb.start_soon(receive_frame(dut.host_rs232_tx))
  47. await send_uart(dut.host_rs232_rx, encode_frame(SET_TARGET, b"\x41"), HOST_CLKS_PER_BIT)
  48. ack_kind, ack_payload = await with_timeout(ack_task, 20, "ms")
  49. assert (ack_kind, ack_payload) == (SET_TARGET_ACK, b"")
  50. assert int(dut.host_ttl_tx.value) == 1
  51. await bmc_task
  52. # A target change deliberately restarts CT/preheat qualification. The
  53. # concurrent BMC frame enters WAIT_BMC_STABLE; three further new epochs
  54. # are required before AUTO resumes.
  55. for _ in range(3):
  56. await send_bmc_snapshot(dut, [61] * 24)
  57. await wait_for(
  58. dut,
  59. lambda: int(dut.control_state_mon.value) == 4
  60. and int(dut.control_epoch_mon.value) > old_control_epoch,
  61. cycles=1000,
  62. label="target/BMC control commit",
  63. )
  64. await Timer(HOST_CLKS_PER_BIT * CLOCK_NS * 2, units="ns")
  65. snapshot = await get_snapshot(dut, "ttl")
  66. assert snapshot["control_state"] == 4
  67. assert snapshot["current_target"] == 65
  68. assert snapshot["host_target_seen"] == 1
  69. assert int(snapshot["bmc_epoch"]) >= 5
  70. assert int(snapshot["ct_epoch"]) >= 1
  71. assert snapshot["bmc_valid_mask"] == (1 << 24) - 1
  72. assert snapshot["ct_valid_mask"] == (1 << 27) - 1
  73. assert duties(snapshot, "bmc_raw") == [0x3D00] * 24
  74. assert duties(snapshot, "ct_raw") == [0x3C00] * 27
  75. ptc = duties(snapshot, "ptc_applied")
  76. assert len(ptc) == 24 and all(0 <= value <= 100 for value in ptc) and any(ptc)
  77. assert duties(snapshot, "front_applied") == [30] * 4
  78. assert duties(snapshot, "rear_applied") == [30] * 4
  79. # A 74 C sample reduces only the matching PTC and raises both fans for
  80. # that lane to the contract's 80% protection duty. The public safety bit
  81. # remains telemetry and must not turn the soft action into a full cutoff.
  82. old_control_epoch = int(dut.control_epoch_mon.value)
  83. await send_bmc_snapshot(dut, [74, *([60] * 23)])
  84. await wait_for(
  85. dut,
  86. lambda: int(dut.control_epoch_mon.value) > old_control_epoch,
  87. cycles=1000,
  88. label="74 C soft-limit commit",
  89. )
  90. soft = await get_snapshot(dut, "rs232")
  91. soft_ptc = duties(soft, "ptc_applied")
  92. assert int(soft["safety_override_mask"]) & 1
  93. assert ptc[0] > 1 and soft_ptc[0] == ptc[0] - 1
  94. assert duties(soft, "front_applied") == [80, 30, 30, 30]
  95. assert duties(soft, "rear_applied") == [80, 30, 30, 30]
  96. # At 76 C the matching PTC is fully off, but the lane is not yet hard
  97. # faulted and therefore remains at 80% rather than 100% airflow.
  98. old_control_epoch = int(dut.control_epoch_mon.value)
  99. await send_bmc_snapshot(dut, [76, *([60] * 23)])
  100. await wait_for(
  101. dut,
  102. lambda: int(dut.control_epoch_mon.value) > old_control_epoch,
  103. cycles=1000,
  104. label="76 C pretrip commit",
  105. )
  106. pretrip = await get_snapshot(dut, "ttl")
  107. assert int(pretrip["safety_override_mask"]) & 1
  108. assert not (int(pretrip["lane_fault_mask"]) & 1)
  109. assert duties(pretrip, "ptc_applied")[0] == 0
  110. assert duties(pretrip, "front_applied") == [80, 30, 30, 30]
  111. assert duties(pretrip, "rear_applied") == [80, 30, 30, 30]
  112. # One invalid DIMM is quarantined without corrupting the other 23 values.
  113. await send_bmc_snapshot(dut, [None, *([60] * 23)])
  114. await wait_for(
  115. dut,
  116. lambda: int(dut.control_state_mon.value) == 5 and bool(int(dut.safety_mask_mon.value) & 1),
  117. cycles=1000,
  118. label="single-channel invalid override",
  119. )
  120. invalid = await get_snapshot(dut, "rs232")
  121. assert int(invalid["bmc_valid_mask"]) == ((1 << 24) - 2)
  122. assert int(invalid["safety_override_mask"]) & 1
  123. assert duties(invalid, "ptc_applied")[0] == 0
  124. # A 77 C sample latches the full L0 hard override and both lane fans.
  125. await send_bmc_snapshot(dut, [77, *([60] * 23)])
  126. await wait_for(
  127. dut,
  128. lambda: bool(int(dut.lane_fault_mask_mon.value) & 1),
  129. cycles=1000,
  130. label="lane hard limit",
  131. )
  132. hard = await get_snapshot(dut, "ttl")
  133. assert int(hard["lane_fault_mask"]) & 1
  134. assert duties(hard, "ptc_applied")[:6] == [0] * 6
  135. assert duties(hard, "front_applied")[0] == 100
  136. assert duties(hard, "rear_applied")[0] == 100
  137. # Three explicit age ticks without a new BMC epoch create global stale.
  138. await pulse_tick_1s(dut, 3)
  139. await wait_for(
  140. dut,
  141. lambda: int(dut.global_fault_mon.value) == 1,
  142. cycles=200,
  143. label="global BMC stale",
  144. )
  145. stale = await get_snapshot(dut, "rs232")
  146. assert stale["global_fault"] == 1
  147. assert duties(stale, "ptc_applied") == [0] * 24
  148. assert duties(stale, "front_applied") == [100] * 4
  149. assert duties(stale, "rear_applied") == [100] * 4
  150. # Three complete safe epochs retain the clamp through the release commit;
  151. # the following epoch resumes only the normal slew-limited ramp.
  152. for _ in range(3):
  153. await send_bmc_snapshot(dut, [60] * 24)
  154. await wait_for(
  155. dut,
  156. lambda: int(dut.control_epoch_mon.value) >= 1,
  157. cycles=1000,
  158. label="recovery control pass",
  159. )
  160. await wait_for(
  161. dut,
  162. lambda: int(dut.global_fault_mon.value) == 0
  163. and int(dut.lane_fault_mask_mon.value) == 0
  164. and int(dut.control_state_mon.value) == 4,
  165. cycles=1000,
  166. label="three-epoch recovery",
  167. )
  168. released = await get_snapshot(dut, "ttl")
  169. assert duties(released, "ptc_applied") == [0] * 24
  170. await send_bmc_snapshot(dut, [60] * 24)
  171. await wait_for(
  172. dut,
  173. lambda: int(dut.control_epoch_mon.value) > int(released["control_epoch"]),
  174. cycles=1000,
  175. label="post-recovery limited ramp",
  176. )
  177. ramped = await get_snapshot(dut, "rs232")
  178. assert all(0 <= value <= 5 for value in duties(ramped, "ptc_applied"))
  179. assert any(duties(ramped, "ptc_applied"))
  180. kind, payload = await host_transaction(dut, "ttl", SET_TARGET, b"\x00")
  181. assert (kind, payload) == (SET_TARGET_ACK, b"")
  182. stopped = await get_snapshot(dut, "rs232")
  183. assert stopped["control_state"] == 6
  184. assert duties(stopped, "ptc_applied") == [0] * 24
  185. await pwm_timing