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
2 changes: 2 additions & 0 deletions packages/embeddedci-mcp/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
- `uart_open` and `capture_uart` take `half_duplex` (a one-wire UART on `rx`) and `entry`
(`reset_ms`, `tx_low`, `hold_ms`, `idle_ms`, `data_hex`: a boot-mode entry the pod times before
the session). Needs gateware v57.
- `uart_open` and `capture_uart` take `de` and `de_active_low`: the pod drives an RS-485
transceiver's DE on that LA channel while it sends. Needs gateware v58.

- Pod features come from the SDK's copy of the firmware's features table for firmware after
3.10.10 (embeddedci 3.0.2); older firmware is read as before.
Expand Down
4 changes: 2 additions & 2 deletions packages/embeddedci-mcp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,9 @@ the board's with `pullups="external"`.

| Tool | Purpose | Parameters |
| --- | --- | --- |
| `capture_uart` | Record the DUT's UART for a window, or until a regex matches. | **`duration`**, `rx`, `tx`, `baud`, `until_regex`, `parity`, `stop_bits`, `half_duplex`, `entry` |
| `capture_uart` | Record the DUT's UART for a window, or until a regex matches. | **`duration`**, `rx`, `tx`, `baud`, `until_regex`, `parity`, `stop_bits`, `half_duplex`, `entry`, `de`, `de_active_low` |
| `power_cycle_and_capture` | Power-cycle the target and capture UART across the boot. | `rx`, `tx`, `efuse`, `delay=1`, `duration=4`, `baud`, `until_regex`, `off_settle=0.3`, `parity`, `stop_bits` |
| `uart_open` | Start buffering the DUT's UART in the background. The frame is the profile's (`uart_parity`, `uart_stop_bits`; 8N1 unless set) unless `parity` (`none`/`even`/`odd`) or `stop_bits` (1/2) say otherwise; anything but 8N1 needs firmware 3.10.8+. `half_duplex` runs it on one wire (`rx`; `tx` defaults to it) and `entry` (`reset_ms`, `tx_low`, `hold_ms`, `idle_ms`, `data_hex`) runs a pod-timed boot-mode entry first: reset with TX held low, release, the first bytes (gateware v57, for boot-ROM pins such as RL78 TOOL0). | `rx`, `tx`, `baud`, `parity`, `stop_bits`, `half_duplex`, `entry` |
| `uart_open` | Start buffering the DUT's UART in the background. The frame is the profile's (`uart_parity`, `uart_stop_bits`; 8N1 unless set) unless `parity` (`none`/`even`/`odd`) or `stop_bits` (1/2) say otherwise; anything but 8N1 needs firmware 3.10.8+. `half_duplex` runs it on one wire (`rx`; `tx` defaults to it) and `entry` (`reset_ms`, `tx_low`, `hold_ms`, `idle_ms`, `data_hex`) runs a pod-timed boot-mode entry first: reset with TX held low, release, the first bytes (gateware v57, for boot-ROM pins such as RL78 TOOL0). | `rx`, `tx`, `baud`, `parity`, `stop_bits`, `half_duplex`, `entry`, `de`, `de_active_low` |
| `uart_write` | Send text to the DUT through the open UART session. | **`text`**, `line_ending=lf` |
| `uart_read` | Return output received since the last read, optionally waiting for a match. | `until_regex`, `timeout=2` |
| `uart_close` | Stop the background UART session. | none |
Expand Down
28 changes: 26 additions & 2 deletions packages/embeddedci-mcp/src/embeddedci_mcp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,22 @@ def _uart_frame(pod: Any, method: str, parity: Optional[str], stop_bits: Optiona
return {k: v for k, v in (("parity", parity), ("stop_bits", stop_bits)) if v is not None}


UartDe = Annotated[Optional[LaRef], Field(description=(
"LA channel (or wiring name) wired to an RS-485 transceiver's DE + /RE. The pod drives it high "
"while it sends and low otherwise, so the transceiver listens whenever the pod is not "
"sending and a fast device's reply is heard. Needs gateware v58. Omit for a plain UART."))]
UartDeActiveLow = Annotated[bool, Field(description="Drive DE low while sending (an active-low enable).")]


def _uart_de(pod: Any, method: str, de: Any, de_active_low: bool) -> Dict[str, Any]:
"""The RS-485 DE keyword arguments for an SDK UART call: none when unused."""
if de is None:
return {}
if "de" not in inspect.signature(getattr(type(pod), method)).parameters:
raise ValueError("UART de needs a newer embeddedci SDK; upgrade embeddedci")
return {"de": de, "de_active_low": True} if de_active_low else {"de": de}


def _uart_line(pod: Any, method: str, half_duplex: bool, entry: Optional[UartEntryArg]) -> Dict[str, Any]:
"""The one-wire and boot-mode entry keyword arguments for an SDK UART call: none when unused,
so an older SDK still works; refused when the SDK predates them."""
Expand All @@ -1191,6 +1207,8 @@ async def capture_uart(
stop_bits: UartStopBits = None,
half_duplex: UartHalfDuplex = False,
entry: UartEntryOpt = None,
de: UartDe = None,
de_active_low: UartDeActiveLow = False,
ctx: Context = None, # type: ignore[assignment]
) -> m.UartCaptureResult:
"""Record the DUT's UART output for a fixed window (or until until_regex matches).
Expand All @@ -1199,14 +1217,16 @@ async def capture_uart(
the profile says otherwise).
half_duplex runs it on one wire, and entry runs a pod-timed boot-mode entry first (reset
with TX low, release, first bytes).
de: the pod switches an RS-485 transceiver's DE on that LA channel while it sends.
"""
pattern = _compile(until_regex)

def op() -> m.UartCaptureResult:
pod = SESSION.require()
return _uart_result(pod.capture_uart(rx=rx, tx=tx, baud=baud, duration=duration, until=pattern,
**_uart_frame(pod, "capture_uart", parity, stop_bits),
**_uart_line(pod, "capture_uart", half_duplex, entry)))
**_uart_line(pod, "capture_uart", half_duplex, entry),
**_uart_de(pod, "capture_uart", de, de_active_low)))

return await _call_reporting(ctx, "capturing UART", op)

Expand Down Expand Up @@ -1250,6 +1270,8 @@ async def uart_open(
stop_bits: UartStopBits = None,
half_duplex: UartHalfDuplex = False,
entry: UartEntryOpt = None,
de: UartDe = None,
de_active_low: UartDeActiveLow = False,
) -> m.UartSessionResult:
"""Start buffering the DUT's UART in the background (replacing any open session).

Expand All @@ -1260,7 +1282,8 @@ async def uart_open(
(Modbus RTU) an external RS-485 transceiver in between. A channel used as GPIO must be released
first (gpio_release). half_duplex runs the session on one wire (rx; tx defaults to it), and
entry runs a pod-timed boot-mode entry before it starts (reset with TX held low, release, the
first bytes), for boot-ROM programming such as RL78 TOOL0.
first bytes), for boot-ROM programming such as RL78 TOOL0. de puts an RS-485 transceiver's
DE on an LA channel the pod switches itself while it sends.
"""
def op() -> m.UartSessionResult:
pod = SESSION.require()
Expand All @@ -1269,6 +1292,7 @@ def op() -> m.UartSessionResult:
rate = int(pod.wiring.uart_baud if baud is None else baud)
frame = _uart_frame(pod, "open_uart", parity, stop_bits)
line = _uart_line(pod, "open_uart", half_duplex, entry)
line.update(_uart_de(pod, "open_uart", de, de_active_low))
SESSION.close_uart()
SESSION.uart = pod.open_uart(rx=rx_i, tx=tx_i, baud=rate, **frame, **line)
SESSION.uart_port = (rx_i, tx_i, rate)
Expand Down
5 changes: 4 additions & 1 deletion packages/embeddedci-mcp/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def __init__(self) -> None:
self.power_efuse = 1
self.uart_frames: List[Tuple[str, int]] = [] # (parity, stop_bits) per uart_proxy_start
self.uart_lines: List[Tuple[int, int, bool, Any]] = [] # (rx, tx, half_duplex, entry)
self.uart_des: List[Tuple[Any, bool]] = [] # (de, de_active_low)
#: The bin-averaged trace a power_profile reply carries (µA / mV, µs since the start).
self.power_samples: Dict[str, Any] = {
"t_us": [0, 250_000, 500_000, 750_000],
Expand Down Expand Up @@ -133,8 +134,10 @@ def dap_start(self, swclk: int, swdio: int):
return FakeRawLink()

def uart_proxy_start(self, rx: int, tx: int, baud: int, parity: str = "none", stop_bits: int = 1,
half_duplex: bool = False, entry: Any = None):
half_duplex: bool = False, entry: Any = None, de: Any = None,
de_active_low: bool = False):
self.uart_frames.append((parity, stop_bits))
self.uart_des.append((de, de_active_low))
self.uart_lines.append((rx, tx, half_duplex, entry))
for la, fn in list(self.function.items()): # a new proxy replaces the previous one
if fn in ("uart_rx", "uart_tx", "uart_hd"):
Expand Down
20 changes: 20 additions & 0 deletions packages/embeddedci-mcp/tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,26 @@ def test_uart_half_duplex_and_entry(connected):
call("uart_close")


def test_uart_de(connected):
if "de" not in inspect.signature(BenchPod.open_uart).parameters:
# The minimum SDK (mcp-min-sdk) predates the argument: the tool says to upgrade it.
with pytest.raises(ToolError, match="newer embeddedci SDK"):
call("uart_open", rx=5, tx=4, de=9)
return
# the pod's RS-485 DE needs the uart_de feature (gateware v58)
with pytest.raises(ToolError, match="gateware v58"):
call("uart_open", rx=5, tx=4, de=9)
connected.caps.append("uart_de")
SESSION.require().refresh_capabilities()
call("uart_open", rx=5, tx=4, de=9)
assert connected.uart_des[-1] == (9, False)
call("capture_uart", rx=5, tx=4, duration=0.2, de=9, de_active_low=True)
assert connected.uart_des[-1] == (9, True)
call("uart_open", rx=5, tx=4)
assert connected.uart_des[-1] == (None, False)
call("uart_close")


# -- I2C sensor + pulls ----------------------------------------------------------------

def test_sensor_library_tools_need_no_pod():
Expand Down
58 changes: 58 additions & 0 deletions packages/embeddedci-mcp/tests/tools_surface.json
Original file line number Diff line number Diff line change
Expand Up @@ -1125,6 +1125,35 @@
"description": "UART baud rate; omit for the wiring profile's uart_baud.",
"title": "Baud"
},
"de": {
"anyOf": [
{
"anyOf": [
{
"maximum": 14,
"minimum": 1,
"type": "integer"
},
{
"type": "string"
}
],
"description": "LA channel 1-14, or a name from the wiring profile \u2014 a signal ('READY') or a role ('uart_rx', 'i2c_sda', 'swd_swclk'). Call `wiring` to see the names. The gpio_* tools also take expander pins ('EXT1') once one is attached (expansion)."
},
{
"type": "null"
}
],
"default": null,
"description": "LA channel (or wiring name) wired to an RS-485 transceiver's DE + /RE. The pod drives it high while it sends and low otherwise, so the transceiver listens whenever the pod is not sending and a fast device's reply is heard. Needs gateware v58. Omit for a plain UART.",
"title": "De"
},
"de_active_low": {
"default": false,
"description": "Drive DE low while sending (an active-low enable).",
"title": "De Active Low",
"type": "boolean"
},
"duration": {
"description": "Capture window in seconds.",
"exclusiveMinimum": 0,
Expand Down Expand Up @@ -5588,6 +5617,35 @@
"description": "UART baud rate; omit for the wiring profile's uart_baud.",
"title": "Baud"
},
"de": {
"anyOf": [
{
"anyOf": [
{
"maximum": 14,
"minimum": 1,
"type": "integer"
},
{
"type": "string"
}
],
"description": "LA channel 1-14, or a name from the wiring profile \u2014 a signal ('READY') or a role ('uart_rx', 'i2c_sda', 'swd_swclk'). Call `wiring` to see the names. The gpio_* tools also take expander pins ('EXT1') once one is attached (expansion)."
},
{
"type": "null"
}
],
"default": null,
"description": "LA channel (or wiring name) wired to an RS-485 transceiver's DE + /RE. The pod drives it high while it sends and low otherwise, so the transceiver listens whenever the pod is not sending and a fast device's reply is heard. Needs gateware v58. Omit for a plain UART.",
"title": "De"
},
"de_active_low": {
"default": false,
"description": "Drive DE low while sending (an active-low enable).",
"title": "De Active Low",
"type": "boolean"
},
"entry": {
"anyOf": [
{
Expand Down
5 changes: 5 additions & 0 deletions packages/embeddedci/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
(rx and tx on one LA pin; the pod only pulls the line low and does not read back its own bytes)
and `entry=UartEntry(...)`, a reset with TX held low, its release and the first bytes, all timed
on the pod. Needs gateware v57 (capability `uart_hd`); the entry also needs the reset pin.
- **RS-485 direction driven by the pod:** `open_uart` and `capture_uart` take `de=` (the LA channel
on a transceiver's DE + /RE) and `de_active_low=`. The pod switches DE itself, from before the
first start bit to the end of the last stop bit, so a device that answers within a millisecond
is heard (DE switched from the host with GPIO calls drops about 25 ms after the request over
LAN). Needs gateware v58 (feature `uart_de`, features table revision 2).

- **Features from the firmware's features table:** the SDK ships benchpod-firmware's
`features/features.json` (`embeddedci.benchpod.feature_map`) and reads the pod's status with
Expand Down
17 changes: 17 additions & 0 deletions packages/embeddedci/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,23 @@ character, so `uart.read().encode("latin-1")` gives the bytes back unchanged, an
sent as is. For the adapter wiring and a Modbus RTU test through a transceiver, see
[5 V logic and RS-485](https://www.embeddedci.com/docs/benchpod-targets/5v-rs485).

**RS-485 direction (DE).** A half-duplex RS-485 transceiver sends while its DE (tied to /RE) is
high and listens while it is low. `de=` names the LA channel wired to DE, and the pod switches it
itself: high from before the first start bit of what it sends to the end of the last stop bit, low
otherwise. Nothing on the host times it, so a device that answers within a millisecond is still
heard (switching DE with `gpio` calls from the host takes tens of milliseconds over the network,
and the reply of a fast device is lost meanwhile). `de_active_low=True` inverts it:

```python
with bp.open_uart(rx=PIN5, tx=PIN4, de=PIN9, baud=19200, parity="even",
encoding="latin-1") as uart:
uart.write(request) # DE goes high and low with the bytes
reply = uart.read(timeout=0.2)
```

`de` needs gateware v58 (`bp.supports("uart_de")`); older pods raise `UnsupportedFeatureError`
before anything is sent. The USB console has no DE.

## LA bias resistors

Eight LA channels carry a fixed, switchable bias resistor. They are referenced to 3V3, so the pod
Expand Down
4 changes: 4 additions & 0 deletions packages/embeddedci/src/embeddedci/benchpod/capabilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ class Capabilities:
#: boot-mode entry (``entry``, with ``nrst_pin``) (gateware v57+): :meth:`BenchPod.open_uart`
#: / :meth:`BenchPod.capture_uart` ``half_duplex``, ``entry``.
uart_hd: bool = False
#: ``uart_de``: the UART proxy drives an RS-485 transceiver's DE (gateware v58+):
#: :meth:`BenchPod.open_uart` / :meth:`BenchPod.capture_uart` ``de``, ``de_active_low``.
uart_de: bool = False
#: The running gateware version (``status`` ``gateware``); 0 when not reported.
gateware: int = 0
#: The features-table revision the pod's firmware is built from (``status`` with
Expand Down Expand Up @@ -382,6 +385,7 @@ def from_status(cls, status: Mapping[str, Any]) -> "Capabilities":
("spi_stream", "spi_stream"),
("uart_frame", "uart_frame"),
("uart_hd", "uart_hd"),
("uart_de", "uart_de"),
("calibrate", "calibrate"),
("cal_dmm", "cal_dmm"),
("current_out", "current_out"),
Expand Down
Loading