diff options
| author | Louis <louis@dnge.de> | 2026-11-11 10:32:51 +0100 |
|---|---|---|
| committer | Louis <louis@dnge.de> | 2026-11-11 10:32:51 +0100 |
| commit | a59e8e99243a895197a30d6cd6dd8d744dd606de (patch) | |
| tree | 8910cc7476ff1317932257dfc8497a0f9ffc29f7 /knowledge | |
| parent | 6f2bbe1f94e4321c329f92ec3420f6d32fd7b26d (diff) | |
| download | hw-toolchain-main.tar.gz | |
The sequential Python/C router (pcbroute.py, pipeline.py, astar.c) is
gone. The Rust crate takes its place at tools/router/, next to the KiCad
I/O scripts it uses (dump_board.py, write_board.py, plot_routes.py).
knowledge/tooling/router.md is now the Rust router's reference; the
small Python venv for plots and 3D checks moves to tools/.venv
(tools/requirements.txt).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'knowledge')
| -rw-r--r-- | knowledge/index.md | 4 | ||||
| -rw-r--r-- | knowledge/tooling/compose-board.md | 2 | ||||
| -rw-r--r-- | knowledge/tooling/height-check.md | 2 | ||||
| -rw-r--r-- | knowledge/tooling/housing-inputs.md | 2 | ||||
| -rw-r--r-- | knowledge/tooling/model-check.md | 2 | ||||
| -rw-r--r-- | knowledge/tooling/placement-check.md | 2 | ||||
| -rw-r--r-- | knowledge/tooling/placer.md | 4 | ||||
| -rw-r--r-- | knowledge/tooling/pnr.md | 19 | ||||
| -rw-r--r-- | knowledge/tooling/router.md | 194 | ||||
| -rw-r--r-- | knowledge/tooling/rust-router.md | 151 | ||||
| -rw-r--r-- | knowledge/tooling/thermal-vias.md | 2 |
11 files changed, 144 insertions, 240 deletions
diff --git a/knowledge/index.md b/knowledge/index.md index 58273ab..f9ed069 100644 --- a/knowledge/index.md +++ b/knowledge/index.md @@ -8,12 +8,10 @@ Entry point to the notes on the toolchain. Conventions: [AGENTS.md](../AGENTS.md ## Tooling -[Rust router](tooling/rust-router.md) +[Router](tooling/router.md) [Placer (automatic placement)](tooling/placer.md) -[Router (Python, sequential)](tooling/router.md) - [Placement check](tooling/placement-check.md) [Schematic generator (schgen)](tooling/schgen.md) diff --git a/knowledge/tooling/compose-board.md b/knowledge/tooling/compose-board.md index 41c8b25..c850b9c 100644 --- a/knowledge/tooling/compose-board.md +++ b/knowledge/tooling/compose-board.md @@ -38,7 +38,7 @@ Close the board in KiCad first. The `.kicad_pro` is restored after SaveBoard. ## What it handles and what not - Each tile's pours get priority `module priority + index`. Butting tiles' GND pours touch, which DRC reports as `zones_intersect` at equal priority. -- Copies keep the module's routing exactly. Base-board nets between tiles stay unrouted; route them afterwards, e.g. with the [Rust router](rust-router.md) and the whole board as `[[fixed]]` copper. +- Copies keep the module's routing exactly. Base-board nets between tiles stay unrouted; route them afterwards, e.g. with the [Router](router.md) and the whole board as `[[fixed]]` copper. - Rotations other than 0/180° are not tested. - KiCad API notes: - `FOOTPRINT.Duplicate(addToParentGroup)` takes an argument; the other items' `Duplicate()` takes none. diff --git a/knowledge/tooling/height-check.md b/knowledge/tooling/height-check.md index f64fcff..745ef28 100644 --- a/knowledge/tooling/height-check.md +++ b/knowledge/tooling/height-check.md @@ -3,7 +3,7 @@ `tools/height_check.py` checks every top-side part of a DIN-rail board against the height limits of the printed housing ([DIN-rail board format](../topics/din-rail-format.md)). ``` sh -tools/router/.venv/bin/python tools/height_check.py myboard/myboard.kicad_pcb +tools/.venv/bin/python tools/height_check.py myboard/myboard.kicad_pcb ``` - **Housing footprint:** the board must carry `HW:DIN_Housing_<k>TE` (from `tools/din_template.py`). Its position is the housing centre, and `k` selects the dimensions. The limits come from `dims()` in `tools/din_template.py`, so the check, the footprint notes and the templates share one source. diff --git a/knowledge/tooling/housing-inputs.md b/knowledge/tooling/housing-inputs.md index a0e38ae..51e0916 100644 --- a/knowledge/tooling/housing-inputs.md +++ b/knowledge/tooling/housing-inputs.md @@ -3,7 +3,7 @@ `tools/housing_inputs.py` prints the numbers a board's DIN-rail housing needs: where each PCB tab is, what kind of opening each edge connector gets, and where the spacer frame must clear through-hole pins. ``` sh -tools/router/.venv/bin/python tools/housing_inputs.py myboard/myboard.kicad_pcb +tools/.venv/bin/python tools/housing_inputs.py myboard/myboard.kicad_pcb ``` - **Housing coordinates:** origin at the `HW:DIN_Housing_<k>TE` footprint, X = board x − centre, Y = centre − board y (Y up), so the numbers go straight into CAD box features. diff --git a/knowledge/tooling/model-check.md b/knowledge/tooling/model-check.md index a4ae99c..77dfefc 100644 --- a/knowledge/tooling/model-check.md +++ b/knowledge/tooling/model-check.md @@ -3,7 +3,7 @@ `tools/model_check.py` checks a footprint's 3D model placement against its pads, without opening KiCad. ``` sh -tools/router/.venv/bin/python tools/model_check.py libraries/footprints/HW.pretty/<name>.kicad_mod [-D HW_LIB=...] +tools/.venv/bin/python tools/model_check.py libraries/footprints/HW.pretty/<name>.kicad_mod [-D HW_LIB=...] ``` - **Transform:** it reads the footprint's `(model …)` offset, scale and rotation, loads the STEP file's vertices and applies KiCad's transform: scale, then rotate X, Y, Z (KiCad negates the angles), then offset. 3D Y points up, footprint Y points down. diff --git a/knowledge/tooling/placement-check.md b/knowledge/tooling/placement-check.md index bfe3897..7e4b6af 100644 --- a/knowledge/tooling/placement-check.md +++ b/knowledge/tooling/placement-check.md @@ -55,4 +55,4 @@ Score: 100, minus a penalty per finding, down to a minimum of 0: - **Rotation suggestions cover only 2-pad parts.** For ICs and connectors, try the turns by hand. - `tools/placement_slots.py board.kicad_pcb slots.json [--out moved.kicad_pcb]` hill-climbs which part sits in which of a set of fixed slots (and which way it faces) against this checker's penalties. See the script's docstring for the format. It does not replace setting decoupling caps by hand: it can swap two caps out of pin order, because the ratsnest does not care. -Related: [Placer](placer.md), [Rust router](rust-router.md). +Related: [Placer](placer.md), [Router](router.md). diff --git a/knowledge/tooling/placer.md b/knowledge/tooling/placer.md index 3e3493d..1f6dc59 100644 --- a/knowledge/tooling/placer.md +++ b/knowledge/tooling/placer.md @@ -1,6 +1,6 @@ # Placer -`tools/placer/` places a board's footprints automatically by simulated annealing (Rust). The cost terms were learned from hand placement, and the [Rust router](rust-router.md) is the final judge: route the best candidates and pick by the routed result. [Place and route your own board](pnr.md) runs the whole chain. +`tools/placer/` places a board's footprints automatically by simulated annealing (Rust). The cost terms were learned from hand placement, and the [Router](router.md) is the final judge: route the best candidates and pick by the routed result. [Place and route your own board](pnr.md) runs the whole chain. ## Run @@ -121,4 +121,4 @@ What went wrong first, so it does not again: - Not modelled, so check by hand: Kelvin taps, and references that `silk_refs.py` cannot fit (those go to F.Fab). - Footprints with bottom-side pads (KiCad `_ThermalVias` variants) are refused. Use the plain footprint and [Thermal vias](thermal-vias.md) after routing. -Related: [Placement check](placement-check.md), [Router (Python)](router.md), [Placement for routing](../topics/placement-for-routing.md). +Related: [Placement check](placement-check.md), [Placement for routing](../topics/placement-for-routing.md). diff --git a/knowledge/tooling/pnr.md b/knowledge/tooling/pnr.md index 4d2489b..2326cbc 100644 --- a/knowledge/tooling/pnr.md +++ b/knowledge/tooling/pnr.md @@ -1,6 +1,6 @@ # Place and route your own board (pnr) -`tools/pnr.sh` takes a KiCad board with its schematic and gives you routed candidates that KiCad's DRC has checked. It places the parts with the [Placer](placer.md) (simulated annealing, 32 runs), routes every candidate with the [Rust router](rust-router.md) (negotiated congestion), then runs apply → route → write → DRC on the best six and installs the one you choose. +`tools/pnr.sh` takes a KiCad board with its schematic and gives you routed candidates that KiCad's DRC has checked. It places the parts with the [Placer](placer.md) (simulated annealing, 32 runs), routes every candidate with the [Router](router.md) (negotiated congestion), then runs apply → route → write → DRC on the best six and installs the one you choose. You can also run only the router on a board you placed yourself. See [Route a placed board](#route-a-placed-board). @@ -10,8 +10,9 @@ You can also run only the router on a board you placed yourself. See [Route a pl - On macOS both come from `KiCad.app` and are found automatically. - On Linux, the distribution package puts `pcbnew` into the system Python. - Override with `KICAD_PYTHON=...` and `KICAD_CLI=...` ([tools/kicad_env.sh](../../tools/kicad_env.sh)). -- **Rust** (stable, via rustup). `pnr.sh` builds the placer, and the router crate links into it. Build the router binary yourself once: `cargo build --release --manifest-path tools/router-rs/Cargo.toml`. -- `python3` (3.8+) for the small JSON summaries. +- **Rust** (stable, via rustup). `pnr.sh` builds the placer, and the router crate links into it. Build the router binary yourself once: `cargo build --release --manifest-path tools/router/Cargo.toml`. +- `python3` (3.8+) for the small JSON summaries. For the plots and the 3D/height checks, make a venv once: + `python3 -m venv tools/.venv && tools/.venv/bin/pip install -r tools/requirements.txt` (numpy, matplotlib). - Two-layer boards. The router uses F.Cu and B.Cu only. ## The board directory @@ -20,7 +21,7 @@ You can also run only the router on a board you placed yourself. See [Route a pl myboard/ myboard.kicad_pro / .kicad_sch / .kicad_pcb name = directory name placement.toml what the placer must know (can be almost empty) - route.toml how to route (see rust-router.md) + route.toml how to route (see router.md) ``` The board needs: @@ -64,7 +65,7 @@ do = "cleanup" do = "pour" # check the GND pour for islands and join them with vias ``` -That is enough for a first run. The [Placer](placer.md) and [Rust router](rust-router.md) pages list what else there is: groups and regions, decap pin assignments, links, pulls, replicated channels, bundles, escapes, cost zones and per-net settings. +That is enough for a first run. The [Placer](placer.md) and [Router](router.md) pages list what else there is: groups and regions, decap pin assignments, links, pulls, replicated channels, bundles, escapes, cost zones and per-net settings. ## Run @@ -97,13 +98,13 @@ Close the board in KiCad first. Both commands refuse while KiCad's lock file is ``` sh . tools/kicad_env.sh -R=tools/router-rs/target/release/router +R=tools/router/target/release/router $KPY tools/router/dump_board.py myboard.kicad_pcb /tmp/board.json $R route /tmp/board.json route.toml /tmp/routes.json $KPY tools/router/write_board.py myboard.kicad_pcb /tmp/routes.json /tmp/routed.kicad_pcb --replace --prune-vias GND cp myboard.kicad_pro /tmp/routed.kicad_pro $KC pcb drc --refill-zones /tmp/routed.kicad_pcb -$KPY tools/router/plot_routes.py /tmp/board.json /tmp/routes.json /tmp/routes.png # needs the router venv +tools/.venv/bin/python tools/router/plot_routes.py /tmp/board.json /tmp/routes.json /tmp/routes.png ``` The last line of `route` reads `RESULT split=0 failed=0 islands=0 vias=… length=…`. Zeros mean complete. KiCad's DRC is the judge. @@ -113,8 +114,8 @@ The last line of `route` reads `RESULT split=0 failed=0 islands=0 vias=… lengt Most failures come from the placement, not the router: - **A pin that never connects** on every candidate: something walls it in. Look at the plot, then give it a fan-out band, keep a corridor free, or raise the net's `priority` in `route.toml`. -- **Interface lines of a block** with one pad each: add `[[escape]]` lines (see the [Rust router](rust-router.md) page), or complete boards will wall them in. +- **Interface lines of a block** with one pad each: add `[[escape]]` lines (see the [Router](router.md) page), or complete boards will wall them in. - **Fine-pitch parts off the 0.05 mm grid**: put fixed parts' origins on multiples of 0.05 mm. - **Placer cost does not predict routability.** Route more candidates (`RUNS=64`) rather than tuning the cost weights. -See [Placement for routing](../topics/placement-for-routing.md) for the rules behind this. The [Rust router](rust-router.md) page has the lessons that came out of real boards. +See [Placement for routing](../topics/placement-for-routing.md) for the rules behind this. The [Router](router.md) page has the lessons that came out of real boards. diff --git a/knowledge/tooling/router.md b/knowledge/tooling/router.md index 136f1e8..c6c615f 100644 --- a/knowledge/tooling/router.md +++ b/knowledge/tooling/router.md @@ -1,95 +1,151 @@ -# Router (Python) +# Router -`tools/router/` is the scripted grid maze router that came before the [Rust router](rust-router.md). It routes one net at a time, in an order the board's own `route.py` script sets. New boards should use the Rust router, with a `route.toml` instead of a `route.py`. This router stays for existing scripts. Its board I/O (`dump_board.py`, `write_board.py`, `plot_routes.py`) is shared by both. +`tools/router/` routes 2-layer KiCad boards on a 0.05 mm grid with **negotiated congestion** (PathFinder): all nets at once, so there is no net order to get wrong. The Rust crate sits next to its KiCad I/O scripts: `dump_board.py` exports the board, `write_board.py` writes the routes back, and `plot_routes.py` draws them (each net in its own colour, B.Cu dashed, failed pins crossed). It is also a library: the [Placer](placer.md) links it to route its candidates in-process. KiCad's DRC stays the judge. -## Setup +For the end-to-end flow on your own board, start with [Place and route your own board](pnr.md). + +## Use ``` sh -python3 -m venv tools/router/.venv -tools/router/.venv/bin/pip install -r tools/router/requirements.txt # numpy, shapely, scipy, matplotlib +cargo build --release --manifest-path tools/router/Cargo.toml +. tools/kicad_env.sh # KPY, KC +R=tools/router/target/release/router +$KPY tools/router/dump_board.py board.kicad_pcb board.json +$R route board.json route.toml routes.json [--fixed NAME=FILE]... [--stop LABEL] +$R place board.json placement.json moved.json # move parts to a placer result, without KiCad +$KPY tools/router/write_board.py board.kicad_pcb routes.json out.kicad_pcb --replace [--zones zones.json] --prune-vias GND +$KC pcb drc --refill-zones out.kicad_pcb ``` -The C core (`astar.c`) compiles itself with `cc` on first import. +The last line of `route` is: -## How it works +``` +RESULT split=<nets in pieces> failed=<connections> islands=<plane-net pad groups the pours leave apart> + vias= length= bcu= neck=<mm narrower than the net's width, outside pads> score=<[rank]> time= +``` -- **Grid:** 0.05 mm cells on F.Cu and B.Cu. - - Exact distance fields hold, for every cell, the distance to the nearest copper and to the nearest copper of another net. - - A track of width w fits where foreign copper is at least w/2 + 0.2 mm away. - - Near pads it may neck down (e.g. 0.5 mm power to 0.2 mm), so fine-pitch pins can be entered. -- **Search** (`astar.c`): 8-direction A* with costs for vias (default 40 cells), turns and B.Cu (`bmult`, default 3), plus optional cost maps per layer. No via lands in a pad. -- **Strategies** (`pipeline.py`): - - `route_plane_net("GND")`: each SMD ground pad gets its own via to the B.Cu plane; - - `connect(net)`: joins all copper of a net, nearest piece first; - - `route_pair(net, a, b)`: pad to pad; - - `connect_decaps(net)`: each decoupling cap to its own nearest IC pin, before the rest of the net; - - `tie(net, pad, xy)`: pad to a new via; - - `route_waypoints(...)`: a trunk through fixed points; - - `stitch("GND", pitch)`: stitching vias wherever they fit (finished boards only); - - `load_existing(dump)`: fixed copper, e.g. a planned bus, loaded before anything else; - - `route_net(...)` + `repair()`: rip-up and reroute. -- **Board I/O** (KiCad's Python): - - `dump_board.py` writes pads, net classes and widths, keep-outs, zones and existing copper to JSON. The output is sorted, so the same placement always gives the same dump. KiCad orders footprints by UUID, which a regenerated board draws anew, and the router's result depends on pad order. - - `write_board.py` writes tracks, vias and pours (`--replace`, `--diff`, `--zones`, `--prune-vias GND`). `--diff` changes only the items that differ, so untouched copper keeps its exact drill and width. Every mode restores the `.kicad_pro` that `SaveBoard` resets ([KiCad notes](kicad.md)). -- **Debugging:** `plot_routes.py board.json routes.json out.png [x0 y0 x1 y1]` draws each net in its own colour with its name, B.Cu dashed and failed pins crossed. KiCad's SVG export cannot show which net blocks which; this can. +`ROUTER_TIMES=1` prints each step's time. `ROUTER_TRACE=1` prints each search and the conflicts left after the last negotiation round. -## Workflow +`write_board.py` options: -``` sh -. tools/kicad_env.sh # KPY, KC -VPY=tools/router/.venv/bin/python -$KPY tools/router/dump_board.py board.kicad_pcb /tmp/board.json -$VPY my_board/route.py /tmp/board.json /tmp/routes.json # the board's own script -$VPY tools/router/plot_routes.py /tmp/board.json /tmp/routes.json /tmp/z.png 110 90 130 110 -$KPY tools/router/write_board.py board.kicad_pcb /tmp/routes.json board.kicad_pcb --replace --prune-vias GND -$KC pcb drc --refill-zones --schematic-parity board.kicad_pcb -$KPY tools/silk_refs.py board.kicad_pcb # references off pads and each other -``` +- `--replace` deletes the board's tracks and vias first. +- `--diff` changes only what differs from the board. Use it when `routes.json` also holds the board's own copper, for example a `[[fixed]]` dump, so that untouched copper keeps its exact drill and width. +- `--prune-vias NET` removes that net's vias that connect nothing. +- `--zones` adds pours from a JSON file. -`silk_refs.py` keeps each reference 0.5 mm inside the real board outline, clear of pads, vias and other silk. +All three restore the `.kicad_pro` that pcbnew's `SaveBoard` would reset. -Close the board in KiCad and check for its `~*.lck` file before writing. Run with `PYTHONHASHSEED=0` so a rebuild gives the same board. +## route.toml -To take parts off a routed board, use `tools/prune_copper.py board.kicad_pcb --remove J3 [--clip X0,Y0,X1,Y1] [--netlist board.net]`. It does four things: +The doc comment of `src/config.rs` is the full reference. In short: -1. deletes the footprints; -2. optionally cuts the outline down, clipping zones and deleting copper outside; -3. renames nets to the schematic's names; -4. deletes whatever DRC would call dangling, repeating until nothing is left (KiCad's own `TestTrackEndpointDangling`). +### Board-wide -## Order that works (sequential routing) +- `prefix`: a sheet path such as `"/My Sheet/"` that net names in the file may leave out. +- `plane_nets`: nets that reach their pads through pours and drop vias, usually `["GND"]`. With `area = "pours"`, give a sheet-local plane its full name (e.g. `/Iso/GND_ISO`). Zones are matched literally, and a missing pour silently shrinks the area. +- `area`: limits where copper may go. + - `"pours"` keeps all copper inside the plane nets' pours, e.g. a design block that will be pasted onto a larger board. + - `[[x, y], ...]` gives a polygon instead. + - `area_margin` (mm) keeps copper that far inside. +- `[[bundle]] nets = [...]`: pairs and buses that should run side by side. A step that does not hug a partner's copper costs `attract` more. Set `attract` on the negotiate step (0.5 works well); the cleanup step defaults to 0.5. It is soft attraction, not coupled routing. +- `[[fixed]]`: copper loaded before the steps. + - `name` + `--fixed NAME=FILE`, or `path` relative to the toml. + - A JSON of tracks and vias, or a board dump with `unmoved = true` (keep the copper of nets whose parts did not move). + - `done = true` marks its nets complete. `protect = true` protects it from rip-up. + - `exclude = [...]`, `optional = true`. +- `[costs.NAME]`: cost maps. + - `zones = [{ layer = "F"|"B", fp = "U1", rect = [x0, y0, x1, y1], cost = 200 }]`. Rectangles are relative to the footprint origin and do not turn with it. Without `fp` they are absolute. Zones add up. + - `rudy = { scale, max }`: RUDY congestion of the pads' MSTs. + - `none` is empty. A step's `cost` stays set for the following steps. +- `[nets.NAME]`: per-net settings. + - `w` (width) and `wneck` (neck width). + - `neck_zone`: how far from a pad a track may neck; default 1 mm. + - `root` (`"U1:19"`, where the net's tree grows from) and `decaps` (each decoupling cap to its pin first). + - `priority` (higher routes first), `cost`, `bmult` (B.Cu multiplier) and `via_cost`. + - `[[nets.NAME.join]]` pad pairs routed first: `{ a = "U1:4", b = "U1:8", w, wneck, cost, allow_via }`. +- `[[escape]]`: reserves a way out for nets that leave a block, such as a module's interface lines with one pad each. + - Keys: `nets = [...]`, `from = [x, y]`, `to = [x, y]`, `size` (default 0.25), `w`, `keep`. + - It adds one virtual pad per net, evenly spaced from `from` to `to` and snapped to the grid, and routes them in the same negotiation. + - Their copper is left out of `routes.json` unless `keep = true`. + - A board only counts as complete if every interface line can still reach the edge. +- `[rank]`: how routed boards compare (used by `placer --route` and `pnr.sh`). + - First: split nets, failed connections, pour islands. + - Then a score: Σ length × net weight (`nets = [{ match = "*/ETH_*", weight = 2 }]`, `*` globs) + `via_mm` (2) × vias + `neck_mm` (2) × necked mm + `bcu_mm` (0) × B.Cu mm. -1. **Keep hand routing** of nets whose parts did not move (`load_existing`). Those nets are protected from rip-up. -2. **Ground:** fixed vias under a QFP without an exposed pad, its GND pins to those vias, then every other SMD ground pad to its own via. If signal lines will close in THT ground pads, tie them to their own via now. -3. **The fine-pitch IC's own pins first:** crystal, regulator caps, bias resistor. Join analog supply pins inside the pad ring, then the PHY pairs, then control and bus lines. -4. **Supplies after the signals:** decaps pad to pad, then the rest of each rail. Wide power trunks go around signals easily; signals cannot get past an early trunk. -5. **Other signals, shortest first.** -6. `repair()`, then pours, then DRC. +### Steps (`[[step]] do = ...`, in order) -**Buses between two ICs:** do not leave them to a one-net-at-a-time router; a six-line bus took 37 vias that way. Instead: +| Step | What it does | +| ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| `negotiate` | routes every net still in pieces at once. `planes = true` negotiates the plane nets' pad drops too, so they give way to signals. Also: `cost`, `rounds`, `pres`, `pres_mult`, `hist`, `margin` (search window around each connection, default 1.5 mm), `attract` | +| `repair` | rips up what lies around failures and routes again (`rounds`) | +| `cleanup` | reroutes each net once with the rest fixed: shorter, fewer vias, tighter octilinear shortcuts, necks widened (`neck`, default 4) | +| `pour` | models the plane net's pour fill, finds islands that leave pads apart and joins them with vias (`fix`, default true; `d` via size) | +| `via` | fixed vias, e.g. ground vias inside a QFP pad ring | +| `plane` | each SMD pad of a plane net drops to its own via before the negotiation (`only`, `skip`) | +| `connect`, `pair`, `decaps`, `rest`, `tie`, `stitch` | ordered steps (outside the negotiation): nets in a given order, pad to pad, caps to pins, everything left, pad to a new via, stitching vias (finished boards only) | -1. Work out the crossings from the pin orders on both ends. -2. Give each layer an order that needs no crossings. -3. Write the geometry as fixed copper and route the rest around it. +`label = "x"` on a step and `--stop x` end the run after that step. -That took 8 vias. The Rust router's negotiation makes this mostly unnecessary. +Two typical recipes: -## Cost zones +- `negotiate` (with `planes = true`) → `repair` → `cleanup` → `pour`. +- `via` + `plane` for ground first, then `negotiate` → `repair` → `cleanup`. -- **F.Cu under a module** with pads underneath, and **inside a QFP pad ring** (reserved for its ground vias): cost 200 per cell. That is effectively forbidden, but not a hard keep-out, so GND drops can still use it. -- **Block tile:** make the board outline the tile plus the copper-to-edge clearance on the sides that face other blocks. The router and DRC then keep all copper inside the tile. -- **Quiet zone on B.Cu** under a crystal, PHY or Ethernet IC: +10 per cell for signals, which keeps the ground plane whole where it matters. -- **A failure within 0.4 s** means a pin is enclosed, not that the search budget ran out. Test the net alone on the empty board, and after each phase, to find which phase encloses it. +## How it works -## Rip-up and reroute +- **Grid**: 0.05 mm, origin snapped to a multiple of 0.05 mm (the edge clearance uses the true outline), so pads of parts on the placer's grid sit on cell centres. +- **Fields** (`board.rs`): + - Per cell and layer: the distance to and net of the nearest copper, and the distance to the nearest copper of another net. + - Board edge, NPTH and keep-outs are static no-net copper. + - Footprint copper that is not a pad (a net tie's bridge) is static too, sampled every 0.025 mm. + - A track of width w fits where foreign copper is at least w/2 + clearance away. It may neck to `wneck` only within `neck_zone` of the pads it joins. + - Separate fields keep vias out of SMD pads and drill edges 0.25 mm apart (KiCad's `min_hole_to_hole`). +- **Search** (`astar.rs`): 8-direction A* with turn cost (no turns over 90°), via cost, B.Cu multiplier (3) and cost maps. It uses a radix heap, which works because the octile heuristic is consistent. +- **Negotiation** (`router.rs`): + - Every net in pieces is a job. Tracks routed in negotiation are soft. + - A step onto other nets' soft copper costs (base + history) × (1 + present factor). + - After each round only the conflicting connections are rerouted. A net that stays in conflict for three rounds is rerouted whole with a wider window. + - Present factor 4, × 1.5 per round, up to 30 rounds. + - Whatever still conflicts is routed with soft copper made hard, trying every order of up to four such nets. Then all copper is hardened and dangling remnants are pruned. +- **Cleanup** (`cleanup.rs`): + - Each net is rerouted with the rest fixed. Steps that hug other copper are cheaper, so lines bundle. + - A reroute is kept if it is at most 2 % worse (length + 2 mm per via + 2 × necked mm) and splits nothing. + - Vias that are not needed go. + - Same-layer chains are pulled tight to octilinear shortcuts, and necked tracks widen wherever clearance allows. +- **Pours** (`pour.rs`): + - A plane net's pours fill where foreign copper is at least clearance + half the minimum fill width away. + - The net's copper joins an island where tracks and vias touch it, and where pads are within thermal-gap + spoke distance. + - Pad groups left apart get a via or a short track to the main island. Whatever stays apart is reported as `islands`. +- **Placer link**: + - `placer run ... --route board.json route.toml --route-top K` routes the best K placements in parallel and ranks them. + - `placer route PROBLEM CONFIG board.json route.toml OUT PLACEMENT...` does the same for existing placement files. + - Memory: about 160 MB per board routed at once. -`route_net(name, cost=..., **kw)` records each connection's cost map and settings. `repair(rounds, protect=kept_nets)` works in four steps: +## Lessons from real boards -1. finds nets still in several pieces; -2. removes the nets with copper within 1.2 mm of the unconnected pieces; -3. routes the failed net first; -4. routes the removed nets again with their recorded settings. +- **Net order was the limit, and negotiation removes it.** With the same grid, costs and A*, a one-net-at-a-time router completed 5 of 32 placer candidates on a dense ESP32 + Ethernet board. Negotiation completed 20, and later 26 with bundle attraction. +- **PathFinder's multiplicative form matters.** With additive costs (step + present + history), the present factor swamps history, and two nets keep trading the same crossing. (base + history) × (1 + present) converged in about 12 rounds. +- **Hard pre-routes can box in pins** that negotiation cannot free later. Prefer `planes = true` over `plane` steps when ground drops wall in signal pins. +- **Interface escapes in a block.** Without `[[escape]]`, "complete" boards walled in an MCU's interface pins with supply and crystal tracks. + - Don't put cost zones on the escape channels. They price the layer for the escape lines too, which then drop to B.Cu with a via at each fine-pitch pin. + - Space escape pads at least 0.5 mm apart for 0.2 mm lines. Lines need 0.415 mm, and at 0.45 mm the slack is less than one grid step. + - Choose `from`/`to` so that the pitch is a multiple of 0.05 mm. + - Keep the escape corridor free of parts. Covering the whole pin side with a footprint rule area works. +- **Fine-pitch parts must sit on the 0.05 mm grid.** A QFN 0.01 mm off the cells failed random pins on every placement. Put fixed parts' origins on multiples of 0.05 mm. The placer keeps its own parts on the grid. +- **0.4 mm QFN vs 0.5 mm LQFP**: two 0.2 mm tracks cannot leave neighbouring 0.4 mm-pitch pins side by side. If nearly every pin is used, all lines must neck (`wneck = 0.15`, `neck_zone = 2`). When a fine-pitch MCU has many consecutive pins in use, compare packages before tuning the router: the LQFP routed every time, the QFN never. +- **Routing a base board between fixed blocks**: + - Load the whole board dump as `[[fixed]] unmoved = true, protect = true` and route only what is still apart. + - Raise `margin` on the negotiate step (e.g. 20). Otherwise the 1.5 mm window never reaches the free channel between blocks, and lines cut through them. + - Write the result with `write_board.py --diff`. +- **Rerouting a few nets on a finished board**: strip those nets' copper from a copy, dump it, and load the dump as `[[fixed]] unmoved = true` **without** `done = true`. With `done = true` the stripped nets count as done too, and negotiate gets no jobs. +- **Starved thermal spokes**: the router does not model thermal-relief spokes. Through-hole plane pads next to signal pins can end with one spoke (KiCad DRC `starved_thermal`). On small pours, solid zone connections for those pads fix it. +- **Bundle attraction only works in the negotiation.** In the cleanup the space beside a partner is already taken. With `attract = 0.5` on negotiate, pair coupling went from about 2–9 % to about 30 %, with fewer vias. Too strong (2) pulls nets off their short paths. +- **Placement measures do not predict routability.** Spearman |ρ| ≤ 0.3 between unconnected pins and placer cost, crossings, RUDY or a coarse global route. Rank by routing instead (`--route`). +- A weighted A* heuristic (1.5–2) made negotiation slower and worse. Search windows and partial reroutes were what helped. -It cannot fix placement. If a net fails alone on the empty board, or fails again after repair, change the placement ([Placement for routing](../topics/placement-for-routing.md)). +## Limits -Related: [Placement check](placement-check.md), [Net classes](../topics/netclasses.md). +- Two layers only. Footprints with bottom-side pads are refused: use the plain exposed-pad footprint, then [Thermal vias](thermal-vias.md) after routing. +- Differential pairs are routed as single nets; bundles only attract. +- No thermal-spoke check in the pour model. +- Negotiated boards take 4–15 s each on a 50-part board, single-threaded per board. diff --git a/knowledge/tooling/rust-router.md b/knowledge/tooling/rust-router.md deleted file mode 100644 index 430fbd2..0000000 --- a/knowledge/tooling/rust-router.md +++ /dev/null @@ -1,151 +0,0 @@ -# Rust router - -`tools/router-rs/` routes 2-layer KiCad boards on a 0.05 mm grid with **negotiated congestion** (PathFinder): all nets at once, so there is no net order to get wrong. It reads a board dump from `tools/router/dump_board.py` and writes `routes.json` for `tools/router/write_board.py`. It is also a library: the [Placer](placer.md) links it to route its candidates in-process. KiCad's DRC stays the judge. - -For the end-to-end flow on your own board, start with [Place and route your own board](pnr.md). - -## Use - -``` sh -cargo build --release --manifest-path tools/router-rs/Cargo.toml -. tools/kicad_env.sh # KPY, KC -R=tools/router-rs/target/release/router -$KPY tools/router/dump_board.py board.kicad_pcb board.json -$R route board.json route.toml routes.json [--fixed NAME=FILE]... [--stop LABEL] -$R place board.json placement.json moved.json # move parts to a placer result, without KiCad -$KPY tools/router/write_board.py board.kicad_pcb routes.json out.kicad_pcb --replace [--zones zones.json] --prune-vias GND -$KC pcb drc --refill-zones out.kicad_pcb -``` - -The last line of `route` is: - -``` -RESULT split=<nets in pieces> failed=<connections> islands=<plane-net pad groups the pours leave apart> - vias= length= bcu= neck=<mm narrower than the net's width, outside pads> score=<[rank]> time= -``` - -`ROUTER_TIMES=1` prints each step's time. `ROUTER_TRACE=1` prints each search and the conflicts left after the last negotiation round. - -`write_board.py` options: - -- `--replace` deletes the board's tracks and vias first. -- `--diff` changes only what differs from the board. Use it when `routes.json` also holds the board's own copper, for example a `[[fixed]]` dump, so that untouched copper keeps its exact drill and width. -- `--prune-vias NET` removes that net's vias that connect nothing. -- `--zones` adds pours from a JSON file. - -All three restore the `.kicad_pro` that pcbnew's `SaveBoard` would reset. - -## route.toml - -The doc comment of `src/config.rs` is the full reference. In short: - -### Board-wide - -- `prefix`: a sheet path such as `"/My Sheet/"` that net names in the file may leave out. -- `plane_nets`: nets that reach their pads through pours and drop vias, usually `["GND"]`. With `area = "pours"`, give a sheet-local plane its full name (e.g. `/Iso/GND_ISO`). Zones are matched literally, and a missing pour silently shrinks the area. -- `area`: limits where copper may go. - - `"pours"` keeps all copper inside the plane nets' pours, e.g. a design block that will be pasted onto a larger board. - - `[[x, y], ...]` gives a polygon instead. - - `area_margin` (mm) keeps copper that far inside. -- `[[bundle]] nets = [...]`: pairs and buses that should run side by side. A step that does not hug a partner's copper costs `attract` more. Set `attract` on the negotiate step (0.5 works well); the cleanup step defaults to 0.5. It is soft attraction, not coupled routing. -- `[[fixed]]`: copper loaded before the steps. - - `name` + `--fixed NAME=FILE`, or `path` relative to the toml. - - A JSON of tracks and vias, or a board dump with `unmoved = true` (keep the copper of nets whose parts did not move). - - `done = true` marks its nets complete. `protect = true` protects it from rip-up. - - `exclude = [...]`, `optional = true`. -- `[costs.NAME]`: cost maps. - - `zones = [{ layer = "F"|"B", fp = "U1", rect = [x0, y0, x1, y1], cost = 200 }]`. Rectangles are relative to the footprint origin and do not turn with it. Without `fp` they are absolute. Zones add up. - - `rudy = { scale, max }`: RUDY congestion of the pads' MSTs. - - `none` is empty. A step's `cost` stays set for the following steps. -- `[nets.NAME]`: per-net settings. - - `w` (width) and `wneck` (neck width). - - `neck_zone`: how far from a pad a track may neck; default 1 mm. - - `root` (`"U1:19"`, where the net's tree grows from) and `decaps` (each decoupling cap to its pin first). - - `priority` (higher routes first), `cost`, `bmult` (B.Cu multiplier) and `via_cost`. - - `[[nets.NAME.join]]` pad pairs routed first: `{ a = "U1:4", b = "U1:8", w, wneck, cost, allow_via }`. -- `[[escape]]`: reserves a way out for nets that leave a block, such as a module's interface lines with one pad each. - - Keys: `nets = [...]`, `from = [x, y]`, `to = [x, y]`, `size` (default 0.25), `w`, `keep`. - - It adds one virtual pad per net, evenly spaced from `from` to `to` and snapped to the grid, and routes them in the same negotiation. - - Their copper is left out of `routes.json` unless `keep = true`. - - A board only counts as complete if every interface line can still reach the edge. -- `[rank]`: how routed boards compare (used by `placer --route` and `pnr.sh`). - - First: split nets, failed connections, pour islands. - - Then a score: Σ length × net weight (`nets = [{ match = "*/ETH_*", weight = 2 }]`, `*` globs) + `via_mm` (2) × vias + `neck_mm` (2) × necked mm + `bcu_mm` (0) × B.Cu mm. - -### Steps (`[[step]] do = ...`, in order) - -| Step | What it does | -| ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ | -| `negotiate` | routes every net still in pieces at once. `planes = true` negotiates the plane nets' pad drops too, so they give way to signals. Also: `cost`, `rounds`, `pres`, `pres_mult`, `hist`, `margin` (search window around each connection, default 1.5 mm), `attract` | -| `repair` | rips up what lies around failures and routes again (`rounds`) | -| `cleanup` | reroutes each net once with the rest fixed: shorter, fewer vias, tighter octilinear shortcuts, necks widened (`neck`, default 4) | -| `pour` | models the plane net's pour fill, finds islands that leave pads apart and joins them with vias (`fix`, default true; `d` via size) | -| `via` | fixed vias, e.g. ground vias inside a QFP pad ring | -| `plane` | each SMD pad of a plane net drops to its own via before the negotiation (`only`, `skip`) | -| `connect`, `pair`, `decaps`, `rest`, `tie`, `stitch` | sequential steps: nets in a given order, pad to pad, caps to pins, everything left, pad to a new via, stitching vias (finished boards only) | - -`label = "x"` on a step and `--stop x` end the run after that step. - -Two typical recipes: - -- `negotiate` (with `planes = true`) → `repair` → `cleanup` → `pour`. -- `via` + `plane` for ground first, then `negotiate` → `repair` → `cleanup`. - -## How it works - -- **Grid**: 0.05 mm, origin snapped to a multiple of 0.05 mm (the edge clearance uses the true outline), so pads of parts on the placer's grid sit on cell centres. -- **Fields** (`board.rs`): - - Per cell and layer: the distance to and net of the nearest copper, and the distance to the nearest copper of another net. - - Board edge, NPTH and keep-outs are static no-net copper. - - Footprint copper that is not a pad (a net tie's bridge) is static too, sampled every 0.025 mm. - - A track of width w fits where foreign copper is at least w/2 + clearance away. It may neck to `wneck` only within `neck_zone` of the pads it joins. - - Separate fields keep vias out of SMD pads and drill edges 0.25 mm apart (KiCad's `min_hole_to_hole`). -- **Search** (`astar.rs`): 8-direction A* with turn cost (no turns over 90°), via cost, B.Cu multiplier (3) and cost maps. It uses a radix heap, which works because the octile heuristic is consistent. -- **Negotiation** (`router.rs`): - - Every net in pieces is a job. Tracks routed in negotiation are soft. - - A step onto other nets' soft copper costs (base + history) × (1 + present factor). - - After each round only the conflicting connections are rerouted. A net that stays in conflict for three rounds is rerouted whole with a wider window. - - Present factor 4, × 1.5 per round, up to 30 rounds. - - Whatever still conflicts is routed with soft copper made hard, trying every order of up to four such nets. Then all copper is hardened and dangling remnants are pruned. -- **Cleanup** (`cleanup.rs`): - - Each net is rerouted with the rest fixed. Steps that hug other copper are cheaper, so lines bundle. - - A reroute is kept if it is at most 2 % worse (length + 2 mm per via + 2 × necked mm) and splits nothing. - - Vias that are not needed go. - - Same-layer chains are pulled tight to octilinear shortcuts, and necked tracks widen wherever clearance allows. -- **Pours** (`pour.rs`): - - A plane net's pours fill where foreign copper is at least clearance + half the minimum fill width away. - - The net's copper joins an island where tracks and vias touch it, and where pads are within thermal-gap + spoke distance. - - Pad groups left apart get a via or a short track to the main island. Whatever stays apart is reported as `islands`. -- **Placer link**: - - `placer run ... --route board.json route.toml --route-top K` routes the best K placements in parallel and ranks them. - - `placer route PROBLEM CONFIG board.json route.toml OUT PLACEMENT...` does the same for existing placement files. - - Memory: about 160 MB per board routed at once. - -## Lessons from real boards - -- **Net order was the limit, and negotiation removes it.** With the same grid, costs and A*, a one-net-at-a-time router completed 5 of 32 placer candidates on a dense ESP32 + Ethernet board. Negotiation completed 20, and later 26 with bundle attraction. -- **PathFinder's multiplicative form matters.** With additive costs (step + present + history), the present factor swamps history, and two nets keep trading the same crossing. (base + history) × (1 + present) converged in about 12 rounds. -- **Hard pre-routes can box in pins** that negotiation cannot free later. Prefer `planes = true` over `plane` steps when ground drops wall in signal pins. -- **Interface escapes in a block.** Without `[[escape]]`, "complete" boards walled in an MCU's interface pins with supply and crystal tracks. - - Don't put cost zones on the escape channels. They price the layer for the escape lines too, which then drop to B.Cu with a via at each fine-pitch pin. - - Space escape pads at least 0.5 mm apart for 0.2 mm lines. Lines need 0.415 mm, and at 0.45 mm the slack is less than one grid step. - - Choose `from`/`to` so that the pitch is a multiple of 0.05 mm. - - Keep the escape corridor free of parts. Covering the whole pin side with a footprint rule area works. -- **Fine-pitch parts must sit on the 0.05 mm grid.** A QFN 0.01 mm off the cells failed random pins on every placement. Put fixed parts' origins on multiples of 0.05 mm. The placer keeps its own parts on the grid. -- **0.4 mm QFN vs 0.5 mm LQFP**: two 0.2 mm tracks cannot leave neighbouring 0.4 mm-pitch pins side by side. If nearly every pin is used, all lines must neck (`wneck = 0.15`, `neck_zone = 2`). When a fine-pitch MCU has many consecutive pins in use, compare packages before tuning the router: the LQFP routed every time, the QFN never. -- **Routing a base board between fixed blocks**: - - Load the whole board dump as `[[fixed]] unmoved = true, protect = true` and route only what is still apart. - - Raise `margin` on the negotiate step (e.g. 20). Otherwise the 1.5 mm window never reaches the free channel between blocks, and lines cut through them. - - Write the result with `write_board.py --diff`. -- **Rerouting a few nets on a finished board**: strip those nets' copper from a copy, dump it, and load the dump as `[[fixed]] unmoved = true` **without** `done = true`. With `done = true` the stripped nets count as done too, and negotiate gets no jobs. -- **Starved thermal spokes**: the router does not model thermal-relief spokes. Through-hole plane pads next to signal pins can end with one spoke (KiCad DRC `starved_thermal`). On small pours, solid zone connections for those pads fix it. -- **Bundle attraction only works in the negotiation.** In the cleanup the space beside a partner is already taken. With `attract = 0.5` on negotiate, pair coupling went from about 2–9 % to about 30 %, with fewer vias. Too strong (2) pulls nets off their short paths. -- **Placement measures do not predict routability.** Spearman |ρ| ≤ 0.3 between unconnected pins and placer cost, crossings, RUDY or a coarse global route. Rank by routing instead (`--route`). -- A weighted A* heuristic (1.5–2) made negotiation slower and worse. Search windows and partial reroutes were what helped. - -## Limits - -- Two layers only. Footprints with bottom-side pads are refused: use the plain exposed-pad footprint, then [Thermal vias](thermal-vias.md) after routing. -- Differential pairs are routed as single nets; bundles only attract. -- No thermal-spoke check in the pour model. -- Negotiated boards take 4–15 s each on a 50-part board, single-threaded per board. diff --git a/knowledge/tooling/thermal-vias.md b/knowledge/tooling/thermal-vias.md index ecd9390..47efba6 100644 --- a/knowledge/tooling/thermal-vias.md +++ b/knowledge/tooling/thermal-vias.md @@ -1,6 +1,6 @@ # Thermal vias (tools/thermal_vias.py) -The [Placer](placer.md) and the [Rust router](rust-router.md) refuse footprints with bottom-side pads ("U1 has a bottom-side pad; flipped footprints are not supported"). KiCad's `_ThermalVias` footprint variants carry a B.Cu copy of the exposed pad, so they are refused too. Instead: +The [Placer](placer.md) and the [Router](router.md) refuse footprints with bottom-side pads ("U1 has a bottom-side pad; flipped footprints are not supported"). KiCad's `_ThermalVias` footprint variants carry a B.Cu copy of the exposed pad, so they are refused too. Instead: 1. Use the plain exposed-pad footprint in the schematic. 2. Keep B.Cu free under the pad with a cost zone in `route.toml`, e.g. `{ layer = "B", fp = "U3", rect = [-2.5, -2.5, 2.5, 2.5], cost = 100 }`. Zone rects do not turn with the part, so use a square that both orientations share. |
