aboutsummaryrefslogtreecommitdiff
path: root/knowledge/tooling/pnr.md
blob: 2326cbcd38a53a3a53620c59bb47c179ac9998e1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# 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 [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).

## 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](../../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`:

``` 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`:

``` 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](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

``` sh
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
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

``` sh
. 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](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 [Router](router.md) page has the lessons that came out of real boards.