aboutsummaryrefslogtreecommitdiff

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 (simulated annealing, 32 runs), routes every candidate with the Router (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.

Requirements

  • KiCad 10, with kicad-cli and a Python that can import pcbnew.
    • 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).
  • 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

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 router.md)

The board needs:

  • a closed Edge.Cuts outline;
  • all footprints on the board, in any position (Update PCB from Schematic and leave them in a heap);
  • the net classes set in the project, because the router takes widths and clearances from them;
  • zones for the plane net (usually GND on B.Cu, and F.Cu too if you like). The router models their fill and checks that every pad reaches them.

Lock the parts that must not move: connectors at the edge, a module with an antenna, mounting holes. Or list them in placement.toml under fixed = [...].

Minimal configs

placement.toml:

fixed = ["J1", "U1"]            # besides locked footprints and mounting holes

[class]                          # net-class weights for the ratsnest length
Power = 0.5                      # wide supply tracks are routed last and go around signals
Default = 1.0

route.toml:

plane_nets = ["GND"]

[[step]]
do = "negotiate"
planes = true                    # the GND drops are negotiated with everything else
cost = "none"

[[step]]
do = "repair"

[[step]]
do = "cleanup"

[[step]]
do = "pour"                      # check the GND pour for islands and join them with vias

That is enough for a first run. The Placer and Router 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

sh tools/pnr.sh run myboard          # RUNS=32 JOBS=6 by default; TOP (6) is the 3rd argument

Output goes to myboard/out/pnr/:

  • run/routed.tsv: every candidate, ranked: legal placement first, then split nets, failed connections, pour islands, then the [rank] score (length, vias, necks);
  • eval/rank_NN/: the best TOP candidates after apply → route → write → KiCad DRC (routed.kicad_pcb, drc.json, route.log);
  • the summary line: SUMMARY complete=<boards DRC calls fully connected>/<total> unconnected=... vias=... length=....

Open a few eval/rank_NN/routed.kicad_pcb files in KiCad and pick one. Then:

sh tools/pnr.sh install myboard rank_00

This checkpoints the board to myboard/out/before-pnr-<date>/, then:

  1. places references with silk_refs.py;
  2. fills the pours and saves;
  3. restores the .kicad_pro, which pcbnew's SaveBoard would otherwise reset;
  4. runs DRC with schematic parity and ERC.

Close the board in KiCad first. Both commands refuse while KiCad's lock file is there.

Route a placed board

. tools/kicad_env.sh
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
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.

When it does not route

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 Router 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 for the rules behind this. The Router page has the lessons that came out of real boards.