MustafaMulla29/usb-pd-motherboard
4-layer USB-C PD hub motherboard with 20 V USB-C input, Linux T113-S3 computer, CH338F USB hub, four individually controlled 5–20 V PD outputs, USB data/camera ports, regulated rails, and hardware watchdog/E-stop safety interlock.
- Version
- 1.0.24
- License
- unset
- Stars
- 0
Use with the tscircuit CLI
tsci is the tscircuit CLI. Install it with npm install -g tscircuit.
tsci clone MustafaMulla29/usb-pd-motherboard # download the package files into ./usb-pd-motherboard
tsci add MustafaMulla29/usb-pd-motherboard # add it to your project as @tsci/MustafaMulla29.usb-pd-motherboardIn a cloned or new (tsci init) project: tsci dev starts a local preview, tsci build outputs Circuit JSON, and tsci push publishes to tscircuit.com.
tools/router/post.py
"""Router output -> routing/ALL.routed.json (the saved copper circuit/fast-routing.ts replays).
python3 -I tools/router/post.py whole board (after tools/router/fastroute)
python3 -I tools/router/post.py --part NET,NET partial run: replace only these nets in ALL.routed.json
(reads routing/inputs/PART.fr.txt / PART.fr.out)
[--add NET,NET] nets that kept their copper: the new routes are added to it
"""
import json, sys
_args = dict(zip(sys.argv[1::2], sys.argv[2::2]))
PART = set(k for k in _args["--part"].split(",") if k) if "--part" in _args else None
ADD = set(k for k in _args.get("--add", "").split(",") if k)
STEM = "routing/inputs/PART" if PART is not None else "routing/inputs/ALL"
L = []; nets = {}; pins = []; plane_layer = {}; first_pin = {}
for line in open(STEM + ".fr.txt"):
t = line.split()
if t[0] == "L": L = t[1:]
elif t[0] == "N": nets[int(t[1])] = t[2]; plane_layer[int(t[1])] = int(t[5])
elif t[0] == "PIN": first_pin.setdefault(int(t[1]), (float(t[2]), float(t[3]))); pins.append((float(t[2]), float(t[3])))
out = []; unrouted = []; cur = None
def flush():
global cur
if not cur: return
net, a, b, plane, pts = cur
route = []; last_wire = None
for k, p in enumerate(pts):
if p[0] == "w":
_, x, y, l, w = p
if last_wire is None:
# first point takes the width of the first segment
nxt = next((q for q in pts[k + 1:] if q[0] == "w" and q[3] == l), None)
w0 = nxt[4] if nxt and (k + 1 < len(pts) and pts[k + 1][0] == "w") else w
route.append({"route_type": "wire", "x": x, "y": y, "width": w0, "layer": L[l]})
else:
if last_wire["layer"] == L[l] and abs(last_wire["width"] - w) > 1e-9:
route.append({**last_wire, "width": w}) # width change: repeat the joint at the new width
route.append({"route_type": "wire", "x": x, "y": y, "width": w, "layer": L[l]})
last_wire = route[-1]
else:
_, x, y, l = p
frm = last_wire["layer"]
to = L[l] if l >= 0 else L[plane_layer[net]]
route.append({"route_type": "via", "x": x, "y": y, "from_layer": frm, "to_layer": to, "via_diameter": 0.45, "hole_diameter": 0.3})
last_wire = None if l >= 0 else last_wire
# "ref": any pad of the net, so a branch that touches no pad itself can still be matched to its net
out.append({"net": nets[net], "ref": {"x": first_pin[net][0], "y": first_pin[net][1]}, "from": {"x": pins[a][0], "y": pins[a][1]} if a >= 0 else None,
"to": {"x": pins[b][0], "y": pins[b][1]} if b >= 0 else None, "route": route})
cur = None
for line in open(STEM + ".fr.out"):
t = line.split()
if t[0] == "T": flush(); cur = (int(t[1]), int(t[2]), int(t[3]), int(t[4]), [])
elif t[0] == "w": cur[4].append(("w", float(t[1]), float(t[2]), int(t[3]), float(t[4])))
elif t[0] == "v": cur[4].append(("v", float(t[1]), float(t[2]), int(t[3])))
elif t[0] == "U": unrouted.append({"net": t[3], "x": float(t[4]), "y": float(t[5])})
flush()
if PART is not None:
kept = [r for r in json.load(open("routing/ALL.routed.json")) if r["net"] not in PART]
out = kept + [r for r in out if r["net"] in PART or r["net"] in ADD]
unrouted = [u for u in unrouted if u["net"] in PART or u["net"] in ADD]
# A route may end with a via that lands on another trace of its net on the other layer (or start that way).
# tscircuit's dangling-end check needs a real wire segment at a trace end that is not on a pad, so such an end
# gets a 0.1 mm tail laid along the trace it joins. The tail is inside that trace's copper: nothing changes
# electrically or for clearances.
import math
def _tail(route_idx, x, y, layer):
me = out[route_idx]
for j, q in enumerate(out):
if j == route_idx or q["net"] != me["net"]: continue
pts = q["route"]
for u, v in zip(pts, pts[1:]):
if u["route_type"] != "wire" or v["route_type"] != "wire" or u["layer"] != layer or v["layer"] != layer: continue
vx, vy = v["x"] - u["x"], v["y"] - u["y"]; L = math.hypot(vx, vy)
if L < 1e-9: continue
t = ((x - u["x"]) * vx + (y - u["y"]) * vy) / (L * L)
if t < -1e-9 or t > 1 + 1e-9 or abs((x - u["x"]) * vy - (y - u["y"]) * vx) / L > 1e-6: continue
far = v if (1 - t) >= t else u # head for the farther end of that segment
d = math.hypot(far["x"] - x, far["y"] - y)
if d < 0.02: continue
k = min(0.1, d) / d
return {"route_type": "wire", "x": x + (far["x"] - x) * k, "y": y + (far["y"] - y) * k, "width": min(u["width"], v["width"]), "layer": layer}
return None
tails = 0
for i, r in enumerate(out):
pts = r["route"]
if len(pts) >= 2 and r["to"] is None and pts[-1]["route_type"] == "wire" and pts[-2]["route_type"] == "via":
t = _tail(i, pts[-1]["x"], pts[-1]["y"], pts[-1]["layer"])
if t: pts[-1]["width"] = t["width"]; pts.append(t); tails += 1
if len(pts) >= 2 and r["from"] is None and pts[0]["route_type"] == "wire" and pts[1]["route_type"] == "via":
t = _tail(i, pts[0]["x"], pts[0]["y"], pts[0]["layer"])
if t: pts[0]["width"] = t["width"]; pts.insert(0, t); tails += 1
if tails: print(f"{tails} via-ended junction(s) given a wire tail")
json.dump(out, open("routing/ALL.routed.json", "w"), separators=(",", ":"))
if PART is None: json.dump(unrouted, open("routing/ALL.unrouted.json", "w"), indent=1)
print(f"{len(out)} routes, {sum(1 for r in out for p in r['route'] if p['route_type'] == 'via')} vias, {len(unrouted)} unconnected pins" + (f" in {sorted(PART | ADD)}" if PART is not None else ""))
if PART is not None and unrouted: sys.exit("partial run left pins unconnected; ALL.routed.json was still written - restore it or run route.sh")