|
|
@@ -6,8 +6,7 @@ wire boundary: bytes are serialized as 8N1 bits at 217 system clocks/bit.
|
|
|
|
|
|
import cocotb
|
|
|
import cocotb.simulator as simulator
|
|
|
-from cocotb.clock import Clock
|
|
|
-from cocotb.triggers import Event, NextTimeStep, ReadOnly, ReadWrite, RisingEdge, SimTimeoutError, with_timeout
|
|
|
+from cocotb.triggers import Event, FallingEdge, NextTimeStep, ReadOnly, ReadWrite, RisingEdge, SimTimeoutError, Timer, with_timeout
|
|
|
|
|
|
|
|
|
CLKS_PER_BIT = 217
|
|
|
@@ -34,8 +33,7 @@ def frame(kind: int, payload: bytes) -> bytes:
|
|
|
|
|
|
|
|
|
async def clocks(dut, count: int) -> None:
|
|
|
- for _ in range(count):
|
|
|
- await RisingEdge(dut.sys_clk)
|
|
|
+ await Timer(count * 40, units="ns")
|
|
|
|
|
|
|
|
|
async def send_uart_byte(dut, value: int) -> None:
|
|
|
@@ -56,38 +54,31 @@ async def send_frame(dut, packet: bytes) -> None:
|
|
|
|
|
|
|
|
|
async def observe_motor_steps(dut, observed: dict[str, int | bool], completion: Event) -> None:
|
|
|
- previous_x = 0
|
|
|
- previous_y = 0
|
|
|
while True:
|
|
|
- await RisingEdge(dut.sys_clk)
|
|
|
+ await RisingEdge(dut.x_step)
|
|
|
await ReadOnly()
|
|
|
if not dut.x_step.value.is_resolvable or not dut.y_step.value.is_resolvable:
|
|
|
- previous_x = 0
|
|
|
- previous_y = 0
|
|
|
continue
|
|
|
current_x = int(dut.x_step.value)
|
|
|
current_y = int(dut.y_step.value)
|
|
|
- if current_x and not previous_x:
|
|
|
+ if current_x and observed["x"] < MOTOR_STEPS:
|
|
|
observed["x"] = int(observed["x"]) + 1
|
|
|
- if current_y and not previous_y:
|
|
|
+ if current_y and observed["y"] < MOTOR_STEPS:
|
|
|
observed["y"] = int(observed["y"]) + 1
|
|
|
if current_x and current_y:
|
|
|
observed["overlap"] = True
|
|
|
- previous_x = current_x
|
|
|
- previous_y = current_y
|
|
|
if observed["x"] == MOTOR_STEPS and observed["y"] == MOTOR_STEPS:
|
|
|
completion.set()
|
|
|
return
|
|
|
|
|
|
|
|
|
async def receive_uart_byte(dut) -> int:
|
|
|
- while int(dut.host_uart_tx.value) != 0:
|
|
|
- await RisingEdge(dut.sys_clk)
|
|
|
- await clocks(dut, CLKS_PER_BIT + CLKS_PER_BIT // 2)
|
|
|
+ await FallingEdge(dut.host_uart_tx)
|
|
|
+ await Timer((CLKS_PER_BIT + CLKS_PER_BIT // 2) * 40, units="ns")
|
|
|
value = 0
|
|
|
for bit in range(8):
|
|
|
value |= int(dut.host_uart_tx.value) << bit
|
|
|
- await clocks(dut, CLKS_PER_BIT)
|
|
|
+ await Timer(CLKS_PER_BIT * 40, units="ns")
|
|
|
assert int(dut.host_uart_tx.value) == 1
|
|
|
return value
|
|
|
|
|
|
@@ -114,12 +105,10 @@ def stop_background_task(task) -> None:
|
|
|
|
|
|
@cocotb.test()
|
|
|
async def test_set_all_get_temperature_over_real_uart(dut):
|
|
|
- clock_task = None
|
|
|
observer_task = None
|
|
|
observed = {"x": 0, "y": 0, "overlap": False}
|
|
|
motor_completion = Event()
|
|
|
try:
|
|
|
- clock_task = cocotb.start_soon(Clock(dut.sys_clk, 40, unit="ns").start())
|
|
|
observer_task = cocotb.start_soon(
|
|
|
observe_motor_steps(dut, observed, motor_completion)
|
|
|
)
|
|
|
@@ -129,8 +118,7 @@ async def test_set_all_get_temperature_over_real_uart(dut):
|
|
|
dut.temp_scl.value = 1
|
|
|
dut.temp_sda.value = 1
|
|
|
await clocks(dut, 1100)
|
|
|
- while not int(dut.u_dut.startup_done.value):
|
|
|
- await RisingEdge(dut.sys_clk)
|
|
|
+ await RisingEdge(dut.u_dut.startup_done)
|
|
|
|
|
|
payload = bytes(range(8)) + bytes((10, 20, 30, 40, 1, 2))
|
|
|
await send_frame(dut, frame(SET_ALL, payload))
|
|
|
@@ -147,13 +135,12 @@ async def test_set_all_get_temperature_over_real_uart(dut):
|
|
|
assert observed["overlap"] is True
|
|
|
assert int(dut.motor_en_n.value) == 0
|
|
|
|
|
|
- while True:
|
|
|
- await RisingEdge(dut.sys_clk)
|
|
|
- await ReadOnly()
|
|
|
- if int(dut.x_step.value) == 0 and int(dut.y_step.value) == 0:
|
|
|
- break
|
|
|
- while not int(dut.u_dut.frame_accept_enable.value):
|
|
|
- await RisingEdge(dut.sys_clk)
|
|
|
+ await with_timeout(
|
|
|
+ FallingEdge(dut.x_step),
|
|
|
+ MOTOR_COMPLETION_TIMEOUT_MS,
|
|
|
+ "ms",
|
|
|
+ )
|
|
|
+ await RisingEdge(dut.u_dut.frame_accept_enable)
|
|
|
await clocks(dut, 4)
|
|
|
await send_frame(dut, frame(GET_TEMPERATURE, b""))
|
|
|
response = bytearray()
|
|
|
@@ -173,7 +160,6 @@ async def test_set_all_get_temperature_over_real_uart(dut):
|
|
|
assert set(response[4:31]) == {0xFF}
|
|
|
finally:
|
|
|
stop_background_task(observer_task)
|
|
|
- stop_background_task(clock_task)
|
|
|
try:
|
|
|
simulator.stop_simulator()
|
|
|
except Exception:
|