SDK docs menu

Guides

Exact times

A plain board.do(...)acts when its request reaches the board, while the firmware keeps running. For “trip the rail 5 ms after the firmware enables it” that isn't precise enough. Give the control a time instead. The simulation works it at that instant, so it lands at the same point of the firmware's run on every run, however busy the network or the server is.

At a time, after a delay, or on a trigger

from hardlabs_sdk import on_pin

board.do("U7.shutdown", at=2.5)                     # 2.5 s of simulated time after boot
board.do("U7.shutdown", after=0.010)                # 10 ms from now
board.do("U7.shutdown", when=on_pin("PA22", "falling", after=0.005))   # 5 ms after PA22 falls

Each returns a Scheduled action. refresh() says whether it has fired and when, wait() waits for it, and cancel() drops it. Board.scheduled lists them all. A button is released 0.15 s of simulated time after its press.

Pin names are the processor's: PA22, P0.13. An edge the firmware drives is caught to the instruction. A pin that only a model sets through the port, with nothing wired to it, is sampled every 100 µs, and the action's note says so.

Set it all up before the firmware runs

Start the board held and it waits at t = 0. Set every action in one call, then run it. Nothing waits on your test while it runs:

board.start("build/app.elf", held=True)
board.schedule([
    {"control": "U7.shutdown", "at": 2.5},
    {"control": "U7.clear-fault", "at": 3.0},
    {"serial": "rs485", "data": frame, "when": on_pin("PD8", "falling", after=0.001)},
])
board.run_until(4.0)
assert all(a.refresh().status == "fired" for a in board.scheduled())

Board.schedulesets all of them or, when one can't be taken, none. Each item takes what do or send_serial takes.

Hold the board between steps

For a test that reads the board at known instants, hold it. Simulated time then moves only when you run it, and each run stops at exactly its time:

def test_rail_trip_is_counted(hardlabs_board):
    board = hardlabs_board
    board.wait_for("app ready")
    with board.held():
        t = board.clock()
        trip = board.do("U7.shutdown", at=t + 0.005)
        board.run_until(t + 0.200)          # stops at exactly t + 200 ms
        assert trip.refresh().fired_at == pytest.approx(t + 0.005, abs=1e-9)
        assert board.read("U7.fault-injected")
        board.do("U7.clear-fault")
  • Board.run_until runs a held board and stops there. Board.wait on a held board does the same.
  • Actions scheduled before that time happen on the way, each at its own time.
  • Bytes your framework writes to a serial port wait in it while the board is held, and arrive when time runs.

Serial bytes at a time

Your framework's own port sends whenever its bytes get there. For a frame that has to reach the firmware at a known point of its run, use Board.send_serial. The bytes arrive one character time apart, at the port's baud rate, as down a wire:

board.send_serial("rs485", frame, when=on_pin("PD8", "falling", after=0.001))