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
|
# Router (Python)
`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.
## Setup
``` sh
python3 -m venv tools/router/.venv
tools/router/.venv/bin/pip install -r tools/router/requirements.txt # numpy, shapely, scipy, matplotlib
```
The C core (`astar.c`) compiles itself with `cc` on first import.
## How it works
- **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.
## Workflow
``` 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
```
`silk_refs.py` keeps each reference 0.5 mm inside the real board outline, clear of pads, vias and other silk.
Close the board in KiCad and check for its `~*.lck` file before writing. Run with `PYTHONHASHSEED=0` so a rebuild gives the same board.
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:
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`).
## Order that works (sequential routing)
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.
**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:
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.
That took 8 vias. The Rust router's negotiation makes this mostly unnecessary.
## Cost zones
- **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.
## Rip-up and reroute
`route_net(name, cost=..., **kw)` records each connection's cost map and settings. `repair(rounds, protect=kept_nets)` works in four steps:
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.
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)).
Related: [Placement check](placement-check.md), [Net classes](../topics/netclasses.md).
|