Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/embeddedci/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog — `embeddedci`

## Unreleased

- USB console: a firmware log line printed right after the prompt (`[sig] SPI1 link at ... for
gateware 58`, on every `status` up to firmware 3.10.12) no longer makes the serial transport
time out waiting for the prompt. A prompt followed only by complete `[tag] ...` lines ends the
reply.

## 3.0.3

- The preset of the default pins is now `Wiring.preset("embeddedci-default")` (also the default of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
DAP_LEAVE = b"\x00\x00" # zero-length frame — leaves the console DAP passthrough
UART_READY = "uart ready" # console prints "uart ready (press Ctrl-] to exit)"
CTRL_RBRACKET = b"\x1d" # Ctrl-] — leaves the console UART proxy
# The prompt followed only by complete firmware log lines ("[sig] ...\r\n"): see _read_until_prompt.
_PROMPT_THEN_LOG = re.compile(r"(?:^|\n)>[ \t]*(?P<log>(?:\[[A-Za-z0-9_.:-]+\][^\r\n]*\r?\n)+)\Z")
_CLEAR_LINE = b"\x08" * 128 # backspaces to clear any partial input line
_RAW_READ_TIMEOUT = 0.1 # poll interval while bridging raw CMSIS-DAP bytes
UNKNOWN_COMMAND = "unknown command"
Expand Down Expand Up @@ -429,7 +431,11 @@ def _write_line(self, line: str) -> None:
self._port.flush()

def _read_until_prompt(self, deadline: float) -> str:
"""Accumulate output until the trailing ``"> "`` prompt or timeout."""
"""Accumulate output until the trailing ``"> "`` prompt or timeout.

Firmware log lines (``[sig] SPI1 link at ...``) are printed from other tasks and can land
right after the prompt, so the reply no longer ends with it. A prompt followed only by
complete ``[tag] ...`` lines ends the reply too; those lines are dropped."""
buf = bytearray()
while time.monotonic() < deadline:
chunk = self._port.read(256)
Expand All @@ -438,6 +444,9 @@ def _read_until_prompt(self, deadline: float) -> str:
text = buf.decode("utf-8", errors="replace")
if text.rstrip(" \t").endswith(">"):
return text
trailing = _PROMPT_THEN_LOG.search(text)
if trailing:
return text[:trailing.start("log")]
raise TransportTimeout(
f"timed out waiting for console prompt; got: "
f"{buf.decode('utf-8', errors='replace')!r}"
Expand Down
32 changes: 32 additions & 0 deletions packages/embeddedci/tests/test_serial_text_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,35 @@ def test_a_silent_console_is_a_timeout():
t._port._process = lambda line: None # the pod stops answering
with pytest.raises(TransportTimeout):
t.ping()


class LogAfterPromptPort(TextConsolePort):
"""Firmware up to 3.10.12 printed its SPI1 link rate from the hw task on every `status`; the
line could land after the prompt, so the reply ended "> [sig] ...\\r\\n" with no new prompt."""

LOG = "[sig] SPI1 link at 3906250 Hz (/64) for gateware 58\r\n"

def _reply(self, text: str) -> None:
super()._reply(text)
self._in.extend(self.LOG.encode())


def test_a_log_line_after_the_prompt_ends_the_reply():
t = SerialTransport(port=LogAfterPromptPort(), timeout=1)
start = time.monotonic()
st = t.status()
assert time.monotonic() - start < 0.5
assert st["device"] == "benchpod"
assert t.status()["device"] == "benchpod" # the next command is not thrown off either


def test_a_log_line_inside_the_reply_is_not_a_prompt():
from embeddedci.benchpod.transport.serial import _PROMPT_THEN_LOG

# Real status output: the log line arrives mid-reply, before the prompt. No early return.
mid = "status\r\n device : benchpod\r\n[sig] SPI1 link at 3906250 Hz (/64) for gateware 58\r\n"
assert _PROMPT_THEN_LOG.search(mid) is None
# A partial log line after the prompt: wait for the rest.
assert _PROMPT_THEN_LOG.search("x\r\n> [sig] SPI1 li") is None
m = _PROMPT_THEN_LOG.search("x\r\n> [sig] SPI1 link\r\n[c3] up\r\n")
assert m and "x\r\n> [sig] SPI1 link\r\n[c3] up\r\n"[:m.start("log")] == "x\r\n> "