test_prod2u_door_temp_uart_e2e.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. """Real UART-bit e2e scenario for the refactored top.
  2. This test is intentionally kept independent of the Python protocol model at the
  3. wire boundary: bytes are serialized as 8N1 bits at 217 system clocks/bit.
  4. """
  5. import cocotb
  6. from cocotb.clock import Clock
  7. from cocotb.triggers import RisingEdge, SimTimeoutError, Timer, with_timeout
  8. CLKS_PER_BIT = 217
  9. SET_ALL = 0x01
  10. GET_TEMPERATURE = 0x02
  11. TEMPERATURE_DATA = 0x82
  12. def crc16(data: bytes) -> int:
  13. crc = 0xFFFF
  14. for value in data:
  15. crc ^= value << 8
  16. for _ in range(8):
  17. crc = ((crc << 1) ^ 0x1021) & 0xFFFF if crc & 0x8000 else (crc << 1) & 0xFFFF
  18. return crc
  19. def frame(kind: int, payload: bytes) -> bytes:
  20. body = bytes((kind, len(payload))) + payload
  21. return b"\xA5\x5A" + body + crc16(body).to_bytes(2, "little")
  22. async def clocks(dut, count: int) -> None:
  23. for _ in range(count):
  24. await RisingEdge(dut.sys_clk)
  25. async def send_uart_byte(dut, value: int) -> None:
  26. dut.host_uart_rx.value = 0
  27. await clocks(dut, CLKS_PER_BIT)
  28. for bit in range(8):
  29. dut.host_uart_rx.value = (value >> bit) & 1
  30. await clocks(dut, CLKS_PER_BIT)
  31. dut.host_uart_rx.value = 1
  32. await clocks(dut, CLKS_PER_BIT)
  33. async def send_frame(dut, packet: bytes) -> None:
  34. for value in packet:
  35. await send_uart_byte(dut, value)
  36. async def observe_motor_steps(dut, observed: dict[str, int | bool]) -> None:
  37. previous_x = 0
  38. previous_y = 0
  39. while True:
  40. await RisingEdge(dut.sys_clk)
  41. if not dut.x_step.value.is_resolvable or not dut.y_step.value.is_resolvable:
  42. previous_x = 0
  43. previous_y = 0
  44. continue
  45. current_x = int(dut.x_step.value)
  46. current_y = int(dut.y_step.value)
  47. if current_x and not previous_x:
  48. observed["x"] = int(observed["x"]) + 1
  49. if current_y and not previous_y:
  50. observed["y"] = int(observed["y"]) + 1
  51. if current_x and current_y:
  52. observed["overlap"] = True
  53. previous_x = current_x
  54. previous_y = current_y
  55. async def receive_uart_byte(dut) -> int:
  56. while int(dut.host_uart_tx.value) != 0:
  57. await RisingEdge(dut.sys_clk)
  58. await clocks(dut, CLKS_PER_BIT + CLKS_PER_BIT // 2)
  59. value = 0
  60. for bit in range(8):
  61. value |= int(dut.host_uart_tx.value) << bit
  62. await clocks(dut, CLKS_PER_BIT)
  63. assert int(dut.host_uart_tx.value) == 1
  64. return value
  65. @cocotb.test()
  66. async def test_set_all_get_temperature_over_real_uart(dut):
  67. cocotb.start_soon(Clock(dut.sys_clk, 40, units="ns").start())
  68. observed = {"x": 0, "y": 0, "overlap": False}
  69. cocotb.start_soon(observe_motor_steps(dut, observed))
  70. dut.host_uart_rx.value = 1
  71. dut.temp_scl.value = 1
  72. dut.temp_sda.value = 1
  73. await clocks(dut, 1100)
  74. payload = bytes(range(24)) + bytes((0x80, 0x90, 0xA0, 0xB0, 0, 1))
  75. await send_frame(dut, frame(SET_ALL, payload))
  76. await Timer(5, units="ms")
  77. assert observed["x"] == 450
  78. assert observed["y"] == 450
  79. assert observed["overlap"] is False
  80. await send_frame(dut, frame(GET_TEMPERATURE, b""))
  81. try:
  82. response = bytes(
  83. [await with_timeout(receive_uart_byte(dut), 100, "ms") for _ in range(33)]
  84. )
  85. except SimTimeoutError as exc:
  86. raise AssertionError("timed out waiting for the 33-byte temperature response") from exc
  87. assert response[:4] == bytes((0xA5, 0x5A, TEMPERATURE_DATA, 27))
  88. assert len(response) == 33
  89. assert response[-2:] == crc16(response[2:-2]).to_bytes(2, "little")
  90. assert set(response[4:31]) == {0xFF}