From 00381a4d0c848d9b3123825da5283155b7676d6f Mon Sep 17 00:00:00 2001 From: Edward Viaene Date: Mon, 23 Nov 2026 17:13:01 -0600 Subject: [PATCH 1/2] SDK: wiring profile debug_port picks SWD or JTAG for flash Pins passed in the call win, then the profile's debug_port, then the automatic rule. Co-Authored-By: Claude Sonnet 5.5 --- packages/embeddedci/CHANGELOG.md | 6 + packages/embeddedci/README.md | 6 +- .../src/embeddedci/benchpod/client.py | 49 ++++---- .../src/embeddedci/benchpod/flash.py | 41 ++++++- .../src/embeddedci/benchpod/wiring.py | 7 ++ packages/embeddedci/tests/api_surface.json | 1 + packages/embeddedci/tests/test_jtag.py | 114 +++++++++++++++++- 7 files changed, 194 insertions(+), 30 deletions(-) diff --git a/packages/embeddedci/CHANGELOG.md b/packages/embeddedci/CHANGELOG.md index eb7d7fe..8943518 100644 --- a/packages/embeddedci/CHANGELOG.md +++ b/packages/embeddedci/CHANGELOG.md @@ -13,6 +13,12 @@ SWD flashing is unchanged. either no SWD pins are, or the target is JTAG only (`esp32`, `esp32s2`, `gd32vf103`, `fe310`, `k210`, names starting with `riscv`); otherwise SWD. A pod without the `jtag` feature raises `UnsupportedFeatureError` before anything is armed. +- **`debug_port` in the wiring profile** (`"swd"`, `"jtag"` or `None`, the default: automatic) picks the + port for a board wired for both. `bp.flash()` decides in this order: pins passed in the call + win (JTAG pins only: JTAG; SWD pins only: SWD; both kinds or neither: go on), then the profile's + `debug_port` (an incomplete pin set raises, naming the unset profile fields, e.g. "debug_port is + jtag but jtag_tdo is not set"), then the automatic rule above. `flash` takes no new argument. + Passing only JTAG pins now means JTAG even when SWD is wired and the target is an ARM part. - `Transport.dap_start()` takes `tck`, `tms`, `tdi`, `tdo`, `trst` and `port`; `swclk` and `swdio` are optional when the JTAG pins are given. - The bundled feature table is revision 16. diff --git a/packages/embeddedci/README.md b/packages/embeddedci/README.md index 6b1d8ff..c107382 100644 --- a/packages/embeddedci/README.md +++ b/packages/embeddedci/README.md @@ -799,6 +799,7 @@ print(wiring.describe()) # a pin table: bias resistor and | `i2c_sda`, `i2c_scl`, `i2c_addr` | not wired, not wired, not set | the I2C bus (emulated sensors default to it; put I2C on LA1-LA6, which have pull-ups) | | `swd_swclk`, `swd_swdio`, `swd_nreset`, `swd_target` | not wired, not wired, `False`, `""` (auto-detect) | flashing | | `jtag_tck`, `jtag_tms`, `jtag_tdi`, `jtag_tdo`, `jtag_trst` | not wired | JTAG flashing (firmware feature `jtag`); `jtag_trst` is optional. `jtag_tck` may share a channel with `swd_swclk` and `jtag_tms` with `swd_swdio` (the same pins on an ARM part); every other overlap is an error | +| `debug_port` | `None` (automatic) | `"swd"` or `"jtag"`: which port `flash` uses when the call passes no pins of either kind. Automatic: JTAG only when SWD is not wired or the target is JTAG only. An incomplete pin set for the chosen port is an error naming the unset fields | | `spi_sclk`, `spi_mosi`, `spi_miso`, `spi_cs` | not wired | decoder channels | | `signals` | `[]` | named signals: `Signal(name, la, direction="input"/"output"/"open_drain"/"bidir", active_low=False, description="")` | | `dut_dirs` | `{}` | the DUT direction of a role or signal where its default is wrong: `{"uart_rx": "bidir", "READY": "dut_input"}` (role key or signal name to `dut_input`, `dut_output` or `bidir`); see [pin directions](#gpio-on-the-la-pins) | @@ -996,7 +997,10 @@ assert result.ok, result.stderr # also: returncode, stdout, target_unreachab JTAG when all four JTAG pins are known and either no SWD pins are, or the target is JTAG only: `esp32`, `esp32s2`, `gd32vf103`, `fe310`, `k210` and any name starting with `riscv` (the OpenOCD config's file name, so `target/esp32.cfg` counts). Otherwise it uses SWD. With both wired, an ARM - target keeps SWD (faster). JTAG needs the pod's `jtag` feature (firmware with features table + target keeps SWD (faster) unless the profile's `debug_port` says `"jtag"`. The order: pins passed + in the call win (JTAG pins only: JTAG, the rest from the profile; SWD pins only: SWD; both kinds + or neither: go on), then the profile's `debug_port` (`"swd"` or `"jtag"`; an incomplete pin set + raises, e.g. "debug_port is jtag but jtag_tdo is not set"), then the automatic rule above. JTAG needs the pod's `jtag` feature (firmware with features table revision 16, gateware v61 or later, not the emulation FPGA image) and raises `UnsupportedFeatureError` before anything is armed without it. A bare `"esp32"` target (no `target/` path) still flashes over the chip's UART bootloader. An uploaded build flashed by diff --git a/packages/embeddedci/src/embeddedci/benchpod/client.py b/packages/embeddedci/src/embeddedci/benchpod/client.py index 1313434..0c7f0a1 100644 --- a/packages/embeddedci/src/embeddedci/benchpod/client.py +++ b/packages/embeddedci/src/embeddedci/benchpod/client.py @@ -1876,9 +1876,12 @@ def flash( ``swclk``/``swdio`` are LA pins (``benchpod.PIN1``..``PIN14`` or 1-14). ``tck``, ``tms``, ``tdi``, ``tdo`` and ``trst`` are the JTAG pins (``trst`` is optional). The port is chosen - for you: JTAG when the pins for it are known (given, else the wiring profile's ``jtag_*``) - and either no SWD pins are or the target is JTAG only (:data:`~embeddedci.benchpod.flash. - JTAG_ONLY_TARGETS`: ``esp32``, ``esp32s2``, the RISC-V names); otherwise SWD. JTAG needs + for you (:func:`~embeddedci.benchpod.flash.select_port`): JTAG pins passed in the call (and + no SWD pins) mean JTAG, SWD pins passed (and no JTAG pins) mean SWD; else the wiring + profile's ``debug_port`` (``"swd"``/``"jtag"``) decides; else JTAG when the pins for it are + known (given, else the profile's ``jtag_*``) and either no SWD pins are or the target is + JTAG only (:data:`~embeddedci.benchpod.flash.JTAG_ONLY_TARGETS`: ``esp32``, ``esp32s2``, + the RISC-V names), otherwise SWD. JTAG needs the ``jtag`` firmware feature (:class:`UnsupportedFeatureError` before anything is armed). ``nreset`` is a flag, not a pin: pass ``True`` when the target's reset line is wired to the pod's reset @@ -2065,28 +2068,32 @@ def _flash_pins(self, swclk: Any, swdio: Any, tck: Any, tms: Any, tdi: Any, tdo: """The pins and port of a flash: ``swclk``, ``swdio``, ``tck``, ``tms``, ``tdi``, ``tdo``, ``trst`` (None: not used) and ``port`` (``"swd"``/``"jtag"``, :func:`flash.select_port`). A pin the caller passes beats the wiring profile's.""" - def optional(given: Sequence[Any], keys: Sequence[str]) -> List[Optional[int]]: - """The pins in ``given`` (else the profile's ``keys``); all None when none is set.""" - if all(g is None for g in given) and all(getattr(self.wiring, k) is None for k in keys): - return [None] * len(keys) - return [self._wired(g, k) for g, k in zip(given, keys)] - swd_keys = ("swd_swclk", "swd_swdio") jtag_keys = ("jtag_tck", "jtag_tms", "jtag_tdi", "jtag_tdo") - jtag_given = (tck, tms, tdi, tdo) - jtag_set = [g is not None or getattr(self.wiring, k) is not None - for g, k in zip(jtag_given, jtag_keys)] - if any(jtag_set) and not all(jtag_set): - missing = ", ".join(k for k, ok in zip(jtag_keys, jtag_set) if not ok) - raise ValueError(f"JTAG needs all of tck, tms, tdi and tdo: {missing} is not set (pass it or " - "set it in the wiring profile)") - swd = optional((swclk, swdio), swd_keys) - jtag = optional(jtag_given, jtag_keys) - have_swd, have_jtag = swd[0] is not None, jtag[0] is not None - if not have_swd and not have_jtag: + jtag_given = (tck, tms, tdi, tdo, trst) + swd_given = (swclk, swdio) + + def unset(given: Sequence[Any], keys: Sequence[str]) -> List[str]: + return [k for g, k in zip(given, keys) if g is None and getattr(self.wiring, k) is None] + + swd_missing, jtag_missing = unset(swd_given, swd_keys), unset(jtag_given, jtag_keys) + debug_port = getattr(self.wiring, "debug_port", None) + if (debug_port is None and len(swd_missing) == 2 and len(jtag_missing) == 4 + and all(g is None for g in swd_given + jtag_given)): # name the pin like the SWD-only flash always did self._wired(swclk, "swd_swclk") - port = _flash.select_port(swd=have_swd, jtag=have_jtag, target=target) + port = _flash.select_port( + swd=not swd_missing, jtag=not jtag_missing, target=target, + swd_given=any(g is not None for g in swd_given), + jtag_given=any(g is not None for g in jtag_given), debug_port=debug_port, + swd_missing=swd_missing, jtag_missing=jtag_missing) + swd: List[Optional[int]] = [None, None] + if port == "swd" or len(swd_missing) < 2 or any(g is not None for g in swd_given): + swd = [self._wired(g, k) for g, k in zip(swd_given, swd_keys)] + jtag: List[Optional[int]] = [None] * 4 + if port == "jtag": + jtag = [self._wired(g, k) for g, k in zip(jtag_given[:4], jtag_keys)] + have_swd = swd[0] is not None out: Dict[str, Any] = {"swclk": swd[0], "swdio": swd[1], "tck": None, "tms": None, "tdi": None, "tdo": None, "trst": None, "port": port} if have_swd and swd[0] == swd[1]: diff --git a/packages/embeddedci/src/embeddedci/benchpod/flash.py b/packages/embeddedci/src/embeddedci/benchpod/flash.py index 812b1de..24047af 100644 --- a/packages/embeddedci/src/embeddedci/benchpod/flash.py +++ b/packages/embeddedci/src/embeddedci/benchpod/flash.py @@ -186,10 +186,43 @@ def target_is_jtag_only(target: str) -> bool: return name in JTAG_ONLY_TARGETS or name.startswith(JTAG_ONLY_PREFIXES) -def select_port(*, swd: bool, jtag: bool, target: str = "") -> str: - """``"jtag"`` or ``"swd"`` for a flash. JTAG when all four JTAG pins are available (``jtag``) - and either no SWD pins are (``swd`` False) or the target is JTAG only; otherwise SWD. With - neither, raises :class:`ValueError`.""" +def select_port(*, swd: bool, jtag: bool, target: str = "", swd_given: bool = False, + jtag_given: bool = False, debug_port: Optional[str] = None, + swd_missing: Sequence[str] = (), jtag_missing: Sequence[str] = ()) -> str: + """``"jtag"`` or ``"swd"`` for a flash (the one place the rule lives). + + ``swd`` / ``jtag``: all the pins of that port are known (given, else the wiring profile's). + ``swd_given`` / ``jtag_given``: the caller passed at least one pin of that port. + ``debug_port``: the wiring profile's ``debug_port`` (None: automatic). + ``swd_missing`` / ``jtag_missing``: the profile fields still unset, for the error messages. + + In order: + + 1. Pins in the call win: JTAG pins only -> JTAG (the rest from the profile, else + :class:`ValueError`); SWD pins only -> SWD. Both kinds or neither: go on. + 2. The profile's ``debug_port``, when set: its port, or :class:`ValueError` naming the unset + profile fields. + 3. Automatic: JTAG when all four JTAG pins are known and either no SWD pins are or the target + is JTAG only; otherwise SWD. With neither, :class:`ValueError`. + """ + if jtag_given and not swd_given: + if not jtag: + raise ValueError("JTAG needs all of tck, tms, tdi and tdo: " + ", ".join(jtag_missing) + + " is not set (pass it or set it in the wiring profile)") + return "jtag" + if swd_given and not jtag_given: + return "swd" + if debug_port is not None: + if debug_port not in ("swd", "jtag"): + raise ValueError(f"debug_port must be 'swd' or 'jtag', got {debug_port!r}") + have, missing = (jtag, jtag_missing) if debug_port == "jtag" else (swd, swd_missing) + if not have: + raise ValueError(f"debug_port is {debug_port} but " + ", ".join(missing) + + " is not set (set it in the wiring profile or pass the pin)") + return debug_port + if 0 < len(jtag_missing) < 4: # some JTAG pins wired, not all + raise ValueError("JTAG needs all of tck, tms, tdi and tdo: " + ", ".join(jtag_missing) + + " is not set (pass it or set it in the wiring profile)") if not swd and not jtag: raise ValueError("flash needs SWD pins (swclk, swdio) or JTAG pins (tck, tms, tdi, tdo): " "pass them or set them in the wiring profile") diff --git a/packages/embeddedci/src/embeddedci/benchpod/wiring.py b/packages/embeddedci/src/embeddedci/benchpod/wiring.py index 3a2399a..a1a57cf 100644 --- a/packages/embeddedci/src/embeddedci/benchpod/wiring.py +++ b/packages/embeddedci/src/embeddedci/benchpod/wiring.py @@ -49,6 +49,8 @@ JTAG_SHARED_ROLES: Tuple[Tuple[str, str], ...] = (("swd_swclk", "jtag_tck"), ("swd_swdio", "jtag_tms")) #: The four pins JTAG needs (``jtag_trst`` is optional). JTAG_REQUIRED_ROLES: Tuple[str, ...] = ("jtag_tck", "jtag_tms", "jtag_tdi", "jtag_tdo") +#: Valid :attr:`Wiring.debug_port` values (None: automatic). +DEBUG_PORTS: Tuple[str, ...] = ("swd", "jtag") #: Valid :attr:`Signal.direction` values. SIGNAL_DIRECTIONS: Tuple[str, ...] = ("input", "output", "open_drain", "bidir") #: Valid :attr:`Wiring.dut_dirs` values: which way the DUT's signal goes. @@ -153,6 +155,9 @@ class Wiring: jtag_tdi: Optional[int] = None jtag_tdo: Optional[int] = None jtag_trst: Optional[int] = None + #: Which debug port ``flash`` uses when the call passes no pins of either kind: ``"swd"``, + #: ``"jtag"`` or None (automatic: JTAG only when SWD is not wired or the target is JTAG only). + debug_port: Optional[str] = None spi_sclk: Optional[int] = None spi_mosi: Optional[int] = None spi_miso: Optional[int] = None @@ -445,6 +450,8 @@ def _errors(self) -> List[str]: addr = _i2c_address(self.i2c_addr) if self.i2c_addr is not None and (addr is None or not 0x03 <= addr <= 0x77): errs.append(f"i2c_addr must be a 7-bit address 0x03..0x77, got {self.i2c_addr!r}") + if self.debug_port is not None and self.debug_port not in DEBUG_PORTS: + errs.append(f"debug_port must be 'swd' or 'jtag' (or None: automatic), got {self.debug_port!r}") if not isinstance(self.swd_nreset, bool): errs.append(f"swd_nreset must be true or false, got {self.swd_nreset!r}") if not isinstance(self.swd_target, str) or len(self.swd_target) > 128: diff --git a/packages/embeddedci/tests/api_surface.json b/packages/embeddedci/tests/api_surface.json index 01d8e85..7f2cf20 100644 --- a/packages/embeddedci/tests/api_surface.json +++ b/packages/embeddedci/tests/api_surface.json @@ -1550,6 +1550,7 @@ "jtag_tdi", "jtag_tdo", "jtag_trst", + "debug_port", "spi_sclk", "spi_mosi", "spi_miso", diff --git a/packages/embeddedci/tests/test_jtag.py b/packages/embeddedci/tests/test_jtag.py index 243c697..be5ba57 100644 --- a/packages/embeddedci/tests/test_jtag.py +++ b/packages/embeddedci/tests/test_jtag.py @@ -227,10 +227,9 @@ def test_both_wired_uses_swd_for_arm_and_jtag_for_jtag_only_targets(fake_pod): def test_explicit_pins_override_the_wiring(fake_pod): bp = fake_pod.make({"swd_swclk": 11, "swd_swdio": 12}) - # JTAG pins passed, SWD wired, ARM target: SWD still wins by the rule - fake_pod.flash(bp, target="target/stm32f4x.cfg", tck=1, tms=2, tdi=3, tdo=4) - assert "tck" not in fake_pod.t.calls[0] - # ... but a JTAG-only target picks JTAG with the passed pins + # JTAG pins passed, no SWD pins passed: JTAG, even with SWD wired and an ARM target + assert fake_pod.flash(bp, target="target/stm32f4x.cfg", tck=1, tms=2, tdi=3, tdo=4) == "jtag" + assert fake_pod.t.calls[0]["tck"] == 1 fake_pod.flash(bp, target="target/riscv.cfg", tck=1, tms=2, tdi=3, tdo=4, trst=9) call = fake_pod.t.calls[1] assert (call["tck"], call["tms"], call["tdi"], call["tdo"], call["trst"]) == (1, 2, 3, 4, 9) @@ -240,6 +239,113 @@ def test_explicit_pins_override_the_wiring(fake_pod): assert fake_pod.t.calls[2]["args"][:2] == (8, 9) +# -- the profile's debug_port and pins passed in the call ------------------------------ + +BOTH = {"swd_swclk": 11, "swd_swdio": 12, "jtag_tck": 11, "jtag_tms": 12, "jtag_tdi": 5, "jtag_tdo": 6} +ARM = "target/stm32f4x.cfg" + + +def test_debug_port_field_validation_and_round_trip(): + assert Wiring().debug_port is None and Wiring().to_dict()["debug_port"] is None + for value in ("swd", "jtag"): + w = Wiring(debug_port=value) + assert Wiring.from_dict(w.to_dict()) == w + assert Wiring.from_dict({"version": 2, "debug_port": None}).debug_port is None + for bad in ("SWD", "usb", "", 1, True): + with pytest.raises(ValueError, match="debug_port must be 'swd' or 'jtag'"): + Wiring(debug_port=bad) + + +def test_profile_debug_port_jtag_beats_automatic_swd(fake_pod): + bp = fake_pod.make({**BOTH, "debug_port": "jtag"}) + assert fake_pod.flash(bp, target=ARM) == "jtag" + call = fake_pod.t.calls[0] + # both pin sets known: both are sent, shared TCK/TMS + assert call["args"][:2] == (11, 12) and call["port"] == "jtag" and call["tck"] == 11 + + +def test_profile_debug_port_swd_beats_jtag_only_target(fake_pod): + bp = fake_pod.make({**BOTH, "debug_port": "swd"}) + assert fake_pod.flash(bp, target="target/esp32.cfg") == "swd" + assert "tck" not in fake_pod.t.calls[0] + + +def test_profile_debug_port_swd_ignores_partial_jtag_pins(fake_pod): + bp = fake_pod.make({"swd_swclk": 11, "swd_swdio": 12, "jtag_tdi": 5, "debug_port": "swd"}) + assert fake_pod.flash(bp, target=ARM) == "swd" + + +def test_debug_port_jtag_with_incomplete_pins_names_the_profile_field(fake_pod): + bp = fake_pod.make({"swd_swclk": 11, "swd_swdio": 12, "jtag_tck": 3, "jtag_tms": 4, + "jtag_tdi": 5, "debug_port": "jtag"}) + with pytest.raises(ValueError, match="debug_port is jtag but jtag_tdo is not set"): + bp.flash(target=ARM, file="f") + partial = fake_pod.make({"swd_swclk": 11, "jtag_tdo": 6, "debug_port": "jtag"}) + with pytest.raises(ValueError, match="debug_port is jtag but jtag_tck, jtag_tms, jtag_tdi is not set"): + partial.flash(target=ARM, file="f") + assert fake_pod.t.calls == [] + + +def test_debug_port_swd_with_incomplete_pins_names_the_profile_field(fake_pod): + bp = fake_pod.make({**JTAG, "swd_swclk": 11, "debug_port": "swd"}) + with pytest.raises(ValueError, match="debug_port is swd but swd_swdio is not set"): + bp.flash(target=ARM, file="f") + bp = fake_pod.make({**JTAG, "debug_port": "swd"}) + with pytest.raises(ValueError, match="debug_port is swd but swd_swclk, swd_swdio is not set"): + bp.flash(target=ARM, file="f") + + +def test_jtag_pin_in_the_call_beats_debug_port_swd(fake_pod): + bp = fake_pod.make({**BOTH, "debug_port": "swd"}) + assert fake_pod.flash(bp, target=ARM, tdo=9) == "jtag" # the rest from the profile + call = fake_pod.t.calls[0] + assert (call["tck"], call["tms"], call["tdi"], call["tdo"]) == (11, 12, 5, 9) + assert fake_pod.flash(bp, target=ARM, trst=7) == "jtag" # trst alone counts as a JTAG pin + assert fake_pod.t.calls[1]["trst"] == 7 + + +def test_swd_pin_in_the_call_beats_debug_port_jtag_and_jtag_only_target(fake_pod): + bp = fake_pod.make({**BOTH, "debug_port": "jtag"}) + assert fake_pod.flash(bp, target=ARM, swclk=8) == "swd" + assert fake_pod.t.calls[0]["args"][:2] == (8, 12) + assert fake_pod.flash(bp, target="target/esp32.cfg", swdio=9) == "swd" + assert "tck" not in fake_pod.t.calls[1] + + +def test_jtag_pin_in_the_call_with_incomplete_pins_is_the_missing_pins_error(fake_pod): + bp = fake_pod.make({"swd_swclk": 11, "swd_swdio": 12, "debug_port": "swd"}) + with pytest.raises(ValueError, match="all of tck, tms, tdi and tdo: jtag_tms, jtag_tdi, jtag_tdo"): + bp.flash(target=ARM, file="f", tck=3) + + +def test_both_kinds_of_pins_in_the_call_fall_through_to_the_profile(fake_pod): + bp = fake_pod.make({**BOTH, "debug_port": "jtag"}) + assert fake_pod.flash(bp, target=ARM, swclk=8, swdio=9, tck=1, tms=2, tdi=3, tdo=4) == "jtag" + bp = fake_pod.make({**BOTH, "debug_port": "swd"}) + assert fake_pod.flash(bp, target="target/esp32.cfg", swclk=11, swdio=12, + tck=11, tms=12, tdi=3, tdo=4) == "swd" + bp = fake_pod.make(BOTH) # no debug_port: automatic + assert fake_pod.flash(bp, target=ARM, swclk=11, swdio=12, tck=11, tms=12, tdi=3, tdo=4) == "swd" + + +def test_no_debug_port_keeps_the_automatic_rule(fake_pod): + bp = fake_pod.make(BOTH) + assert fake_pod.flash(bp, target=ARM) == "swd" + assert fake_pod.flash(bp, target="target/esp32.cfg") == "jtag" + + +@pytest.mark.parametrize("kw, want", [ + (dict(swd=True, jtag=True, jtag_given=True), "jtag"), + (dict(swd=True, jtag=True, swd_given=True, target="target/esp32.cfg"), "swd"), + (dict(swd=True, jtag=True, swd_given=True, jtag_given=True, debug_port="jtag"), "jtag"), + (dict(swd=True, jtag=True, debug_port="jtag"), "jtag"), + (dict(swd=True, jtag=True, debug_port="swd", target="target/esp32.cfg"), "swd"), + (dict(swd=True, jtag=True), "swd"), +]) +def test_select_port_order(kw, want): + assert flashmod.select_port(**kw) == want + + def test_jtag_without_the_feature_is_refused_before_arming(fake_pod): bp = fake_pod.make(JTAG, jtag=False) with pytest.raises(UnsupportedFeatureError) as e: From 995df2bb05a09b1d355331e10d9c193bd8562003 Mon Sep 17 00:00:00 2001 From: Edward Viaene Date: Mon, 23 Nov 2026 17:14:54 -0600 Subject: [PATCH 2/2] MCP: set_wiring and wiring carry debug_port; flash description mentions it Co-Authored-By: Claude Sonnet 5.5 --- packages/embeddedci-mcp/CHANGELOG.md | 4 +++ packages/embeddedci-mcp/README.md | 2 +- .../src/embeddedci_mcp/models.py | 3 ++ .../src/embeddedci_mcp/server.py | 6 ++-- .../src/embeddedci_mcp/summaries.py | 1 + .../embeddedci-mcp/tests/test_wiring_tools.py | 36 +++++++++++++++++++ .../embeddedci-mcp/tests/tools_surface.json | 2 +- 7 files changed, 50 insertions(+), 4 deletions(-) diff --git a/packages/embeddedci-mcp/CHANGELOG.md b/packages/embeddedci-mcp/CHANGELOG.md index 22bfbf5..9fc7452 100644 --- a/packages/embeddedci-mcp/CHANGELOG.md +++ b/packages/embeddedci-mcp/CHANGELOG.md @@ -9,6 +9,10 @@ For BenchPod firmware with the `jtag` feature; JTAG flashing needs embeddedci 4. There is no transport argument: JTAG is used when the four JTAG pins are known and either no SWD pins are, or the target is JTAG only (`esp32`, `esp32s2`, `gd32vf103`, `fe310`, `k210`, names starting with `riscv`); otherwise SWD. `set_wiring` and `wiring` carry the new `jtag_*` fields. +- **`debug_port` in the wiring profile** (`swd`, `jtag` or null: automatic) picks the port for a board + wired for both. `flash` decides in this order: JTAG pins passed in the call (and no SWD pins) mean + JTAG, SWD pins passed mean SWD; else the profile's `debug_port`; else the automatic rule above. + `set_wiring` takes the field and `wiring` shows it. `flash` has no new argument. Needs embeddedci 4.0.4. ## 4.0.3 diff --git a/packages/embeddedci-mcp/README.md b/packages/embeddedci-mcp/README.md index c13c77f..aec4c1c 100644 --- a/packages/embeddedci-mcp/README.md +++ b/packages/embeddedci-mcp/README.md @@ -233,7 +233,7 @@ description, ranges and enums, so an agent sees more than this table. | Tool | Purpose | Parameters | | --- | --- | --- | -| `flash` | Program the DUT over SWD or JTAG through the pod's CMSIS-DAP probe (JTAG is picked when the wiring has `jtag_tck`, `jtag_tms`, `jtag_tdi` and `jtag_tdo` and there are no SWD pins, or the target is JTAG only: `esp32`, `esp32s2`, `gd32vf103`, `fe310`, `k210`, `riscv*`; needs the pod's `jtag` feature): a local `file` (OpenOCD runs next to the server), or a build uploaded to embeddedci.com (`build` id or `latest`, `repo`, `ref`; embeddedci.com runs OpenOCD on a cloud connection, LAN/USB downloads it). With `target` `esp32` (or `esp32s3`, `esp32c3`, ...) the pod flashes an ESP32-family target over its UART ROM bootloader instead: `file` is a `.bin` or an ESP-IDF `flasher_args.json`, TXD0/RXD0 on `rx`/`tx` (default: the profile's `uart_rx`/`uart_tx`), IO0 on `boot`, EN on the pod's reset pin (`nreset`), a `.bin` at `load_address`. | `swclk`, `swdio`, `tck`, `tms`, `tdi`, `tdo`, `trst`, `target`, `file`, `nreset`, `load_address`, `target_power`, `verify=true`, `reset=true`, `connect_under_reset`, `extra_configs`, `extra_args`, `timeout_s=300`, `connect_attempts=5`, `build`, `repo`, `ref`, `artifact`, `server_side`, `rx`, `tx`, `boot` | +| `flash` | Program the DUT over SWD or JTAG through the pod's CMSIS-DAP probe (JTAG is picked when the wiring has `jtag_tck`, `jtag_tms`, `jtag_tdi` and `jtag_tdo` and there are no SWD pins, or the target is JTAG only: `esp32`, `esp32s2`, `gd32vf103`, `fe310`, `k210`, `riscv*`; the wiring profile's `debug_port` (`swd` or `jtag`) picks for a board wired for both, and JTAG or SWD pins passed in the call beat it; needs the pod's `jtag` feature): a local `file` (OpenOCD runs next to the server), or a build uploaded to embeddedci.com (`build` id or `latest`, `repo`, `ref`; embeddedci.com runs OpenOCD on a cloud connection, LAN/USB downloads it). With `target` `esp32` (or `esp32s3`, `esp32c3`, ...) the pod flashes an ESP32-family target over its UART ROM bootloader instead: `file` is a `.bin` or an ESP-IDF `flasher_args.json`, TXD0/RXD0 on `rx`/`tx` (default: the profile's `uart_rx`/`uart_tx`), IO0 on `boot`, EN on the pod's reset pin (`nreset`), a `.bin` at `load_address`. | `swclk`, `swdio`, `tck`, `tms`, `tdi`, `tdo`, `trst`, `target`, `file`, `nreset`, `load_address`, `target_power`, `verify=true`, `reset=true`, `connect_under_reset`, `extra_configs`, `extra_args`, `timeout_s=300`, `connect_attempts=5`, `build`, `repo`, `ref`, `artifact`, `server_side`, `rx`, `tx`, `boot` | | `usb_flash` | Flash the board on the pod's USB host port, the way a computer would (pod firmware 4.0.3+, LAN or cloud). The loader comes from the file and the board that mounted: a UF2 or drag-and-drop drive (Pico, Adafruit, DAPLink), DFU or the STM32 ROM loader, Teensy, Arduino AVR and SAMD, ESP32; `reason` in the result says why. `.hex` and `.dfu` files are converted to a binary with their address. A Pico (RP2040, RP2350) in BOOTSEL gets a `.bin` or `.hex` through PICOBOOT (every page read back); `verify` reads a drive back (`FAIL.TXT`, `CURRENT.UF2`) and the result's `note` says what it found. A drive flash sets the file's date from this computer's clock. Starts host mode when it is off. | **`file`**, `address`, `name`, `leave=true`, `mode`, `port`, `timeout_s`, `verify=false` | | `usb_check` | Check the board's own USB firmware on the pod's host port (pod firmware 4.0.3+, LAN or cloud): `enumerate` (power-on to pull-up, descriptor and mounted over `count` power cycles), `power` (current against `bMaxPower`), `suspend` (suspended current against 2.5 mA, resume, still answers), `reset` (port resets and the time to enumerate again), `replug` (VBUS dropouts of `off_ms`). The board's VBUS must come from the internal 5 V (efuse 1) rail, switched on (`power_on`). Returns when the check ends: `passed` is the verdict, `result` the numbers (uA, ms); a failing board is a normal result. Starts host mode when it is off. | **`test`**, `count`, `off_ms`, `timeout_ms`, `duration_ms`, `settle_ms`, `port`, `timeout_s` | | `usb_hid` | The HID interfaces of the board on the pod's host port and its input reports (keyboard keys, mouse, gamepad; pod firmware 4.0.3+, `usb_host` first): `list` the interfaces with their report descriptors, `start` a capture into a ring of 64 on the pod, `read` reports from it, `stop` it. The capture ends when the board detaches or host mode ends. | `action=list`, `count=1`, `timeout_s=2`, `instance` | diff --git a/packages/embeddedci-mcp/src/embeddedci_mcp/models.py b/packages/embeddedci-mcp/src/embeddedci_mcp/models.py index ecc7082..3835a6b 100644 --- a/packages/embeddedci-mcp/src/embeddedci_mcp/models.py +++ b/packages/embeddedci-mcp/src/embeddedci_mcp/models.py @@ -211,6 +211,9 @@ class WiringResult(BaseModel): i2c_address: Optional[int] = Field(description="The profile's I2C address (i2c_addr) as a 7-bit integer; null when unset.") swd_nreset: bool swd_target: str + debug_port: Optional[str] = Field(None, description=( + "The profile's debug_port: 'swd' or 'jtag' (what flash uses when the call passes no pins); " + "null: automatic.")) pins: List[WiringPin] = Field(description="LA1-LA14 with what is wired to each and its bias resistor.") signals: List[WiringSignal] = Field(default_factory=list) warnings: List[str] = Field(default_factory=list, diff --git a/packages/embeddedci-mcp/src/embeddedci_mcp/server.py b/packages/embeddedci-mcp/src/embeddedci_mcp/server.py index efde94f..c48fbb6 100644 --- a/packages/embeddedci-mcp/src/embeddedci_mcp/server.py +++ b/packages/embeddedci-mcp/src/embeddedci_mcp/server.py @@ -623,7 +623,8 @@ def op() -> m.WiringResult: async def set_wiring( profile: Annotated[Dict[str, Any], Field(description=( "The profile as a JSON object (schema version 2): la_mv, efuse, uart_rx/uart_tx/uart_baud, " - "i2c_sda/i2c_scl/i2c_addr, swd_swclk/swd_swdio/swd_nreset/swd_target, jtag_tck/jtag_tms/jtag_tdi/jtag_tdo/jtag_trst, spi_*, and signals " + "i2c_sda/i2c_scl/i2c_addr, swd_swclk/swd_swdio/swd_nreset/swd_target, jtag_tck/jtag_tms/jtag_tdi/jtag_tdo/jtag_trst, debug_port (\"swd\" or \"jtag\": what flash uses when " + "the call passes no pins; null: automatic), spi_*, and signals " "[{name, la, direction, active_low, description}], pulls (the pull resistors power_on switches " "on, the others off: {\"1\": \"up\", \"2\": \"up\", \"8\": \"down\"}, or a list of channels " "that pull their own way, e.g. [1, 2]; today LA1-LA6 pull up, LA7/LA8 down; left out: " @@ -1135,7 +1136,8 @@ async def flash( SWD or JTAG is chosen for you, there is no transport argument: JTAG when the four JTAG pins (`tck`, `tms`, `tdi`, `tdo`, from the arguments or the profile's jtag_*) are known and either no SWD pins are, or the target is JTAG only (esp32, esp32s2, gd32vf103, fe310, k210 and riscv* - OpenOCD configs, e.g. target/esp32.cfg); otherwise SWD. JTAG needs the pod's `jtag` feature + OpenOCD configs, e.g. target/esp32.cfg); otherwise SWD. JTAG pins passed in the call mean JTAG, + SWD pins passed mean SWD, else the profile's debug_port (swd or jtag) decides. JTAG needs the pod's `jtag` feature (newer firmware): without it the call fails before anything is armed. Give either `file` (a path on this server's host) or an uploaded build (`build`, or diff --git a/packages/embeddedci-mcp/src/embeddedci_mcp/summaries.py b/packages/embeddedci-mcp/src/embeddedci_mcp/summaries.py index a59d253..d9196ed 100644 --- a/packages/embeddedci-mcp/src/embeddedci_mcp/summaries.py +++ b/packages/embeddedci-mcp/src/embeddedci_mcp/summaries.py @@ -134,6 +134,7 @@ def wiring_summary(wiring: Wiring, *, saved: bool = False) -> m.WiringResult: profile_rev=wiring.profile_rev, la_voltage=wiring.la_voltage, efuse=wiring.efuse, uart_baud=wiring.uart_baud, i2c_address=wiring.i2c_address, swd_nreset=wiring.swd_nreset, swd_target=wiring.swd_target, + debug_port=getattr(wiring, "debug_port", None), pins=[m.WiringPin(la=la, wired_to=pins.get(la), pull=_bias(la), dut_dir=dirs.get(la)) for la in range(1, 15)], signals=[m.WiringSignal(name=s.name, la=s.la, direction=s.direction, diff --git a/packages/embeddedci-mcp/tests/test_wiring_tools.py b/packages/embeddedci-mcp/tests/test_wiring_tools.py index 14184f9..e223e9b 100644 --- a/packages/embeddedci-mcp/tests/test_wiring_tools.py +++ b/packages/embeddedci-mcp/tests/test_wiring_tools.py @@ -200,6 +200,8 @@ def test_wiring_draws_the_profile_on_the_pods_header(connected, monkeypatch): # SDK the MCP supports, which may predate them. needs_jtag_sdk = pytest.mark.skipif(not hasattr(Wiring, "jtag_tck"), reason="the installed embeddedci SDK has no jtag_* wiring fields") +needs_debug_port_sdk = pytest.mark.skipif(not hasattr(Wiring, "debug_port"), + reason="the installed embeddedci SDK has no debug_port wiring field") @needs_jtag_sdk @@ -251,3 +253,37 @@ def old_flash(**kwargs): monkeypatch.setattr(SESSION.require(), "flash", old_flash) with pytest.raises(ToolError, match="JTAG needs a newer embeddedci SDK"): call("flash", file="a.elf", tck=3, tms=4, tdi=5, tdo=6) + + +@needs_debug_port_sdk +def test_set_wiring_takes_and_shows_debug_port(connected): + assert call("set_wiring", profile={"version": 2, "la_mv": 3300})["debug_port"] is None + result = call("set_wiring", profile={"version": 2, "la_mv": 3300, "debug_port": "jtag"}) + assert result["debug_port"] == "jtag" and result["profile"]["debug_port"] == "jtag" + assert SESSION.require().wiring.debug_port == "jtag" + assert call("wiring")["debug_port"] == "jtag" + + +@needs_debug_port_sdk +def test_set_wiring_validates_debug_port(connected): + with pytest.raises(ToolError, match="debug_port must be 'swd' or 'jtag'"): + call("set_wiring", profile={"version": 2, "debug_port": "usb"}) + + +@needs_debug_port_sdk +def test_flash_adds_no_argument_for_debug_port(connected, monkeypatch): + from embeddedci.benchpod.flash import FlashResult + + seen = {} + + def fake_flash(**kwargs): + seen.update(kwargs) + return FlashResult(ok=True, returncode=0, stdout="", stderr="") + + _set({**BENCH, "debug_port": "jtag"}) + monkeypatch.setattr(SESSION.require(), "flash", fake_flash) + assert call("flash", file="a.elf")["ok"] is True + assert "debug_port" not in seen and "port" not in seen and "transport" not in seen + assert "flash" in mcp._tool_manager._tools + desc = mcp._tool_manager._tools["flash"].description + assert "debug_port" in desc diff --git a/packages/embeddedci-mcp/tests/tools_surface.json b/packages/embeddedci-mcp/tests/tools_surface.json index b38602c..a59995b 100644 --- a/packages/embeddedci-mcp/tests/tools_surface.json +++ b/packages/embeddedci-mcp/tests/tools_surface.json @@ -5389,7 +5389,7 @@ "properties": { "profile": { "additionalProperties": true, - "description": "The profile as a JSON object (schema version 2): la_mv, efuse, uart_rx/uart_tx/uart_baud, i2c_sda/i2c_scl/i2c_addr, swd_swclk/swd_swdio/swd_nreset/swd_target, jtag_tck/jtag_tms/jtag_tdi/jtag_tdo/jtag_trst, spi_*, and signals [{name, la, direction, active_low, description}], pulls (the pull resistors power_on switches on, the others off: {\"1\": \"up\", \"2\": \"up\", \"8\": \"down\"}, or a list of channels that pull their own way, e.g. [1, 2]; today LA1-LA6 pull up, LA7/LA8 down; left out: power_on leaves the pulls as they are). What it leaves out is undefined (not wired); a pin set to null is not wired. {\"version\": 1} objects fill in the old default pins.", + "description": "The profile as a JSON object (schema version 2): la_mv, efuse, uart_rx/uart_tx/uart_baud, i2c_sda/i2c_scl/i2c_addr, swd_swclk/swd_swdio/swd_nreset/swd_target, jtag_tck/jtag_tms/jtag_tdi/jtag_tdo/jtag_trst, debug_port (\"swd\" or \"jtag\": what flash uses when the call passes no pins; null: automatic), spi_*, and signals [{name, la, direction, active_low, description}], pulls (the pull resistors power_on switches on, the others off: {\"1\": \"up\", \"2\": \"up\", \"8\": \"down\"}, or a list of channels that pull their own way, e.g. [1, 2]; today LA1-LA6 pull up, LA7/LA8 down; left out: power_on leaves the pulls as they are). What it leaves out is undefined (not wired); a pin set to null is not wired. {\"version\": 1} objects fill in the old default pins.", "title": "Profile", "type": "object" },