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/power-copper.ts
/**
* Generate circuit/power-copper.json: top-layer power pours and power-via arrays for the
* four motor ports (shapes drawn once for M1, shifted to M2..M4) and the power-input area.
* Via sites are searched against the CURRENT routed board (dist/index/circuit.json) on all
* layers, so they never touch other-net copper. bun tools/power-copper.ts
*/
import { readFileSync, writeFileSync } from "node:fs"
import { getFullConnectivityMapFromCircuitJson } from "circuit-json-to-connectivity-map"
import { PORT_Y } from "../circuit/port-positions"
type P = { x: number; y: number }
// Baseline = a routed board WITHOUT the copper being generated (else same-net vias would be stacked).
let cj: any[] = [], cm: any, netKey = new Map<string, string>()
const keyOf = (id?: string) => (id ? cm.getNetConnectedToId(id) : undefined)
// ---- obstacles: {key, layers, d(x,y) = distance from point to copper edge} ----
type Obs = { key?: string; top: boolean; bottom: boolean; inner: boolean; pad: boolean; via?: boolean; d: (x: number, y: number) => number }
const rectD = (cx: number, cy: number, w: number, h: number, rot = 0) => (x: number, y: number) => {
const a = (-rot * Math.PI) / 180, dx0 = x - cx, dy0 = y - cy
const dx = Math.abs(dx0 * Math.cos(a) - dy0 * Math.sin(a)) - w / 2, dy = Math.abs(dx0 * Math.sin(a) + dy0 * Math.cos(a)) - h / 2
return Math.hypot(Math.max(dx, 0), Math.max(dy, 0)) + Math.min(Math.max(dx, dy), 0)
}
const segD = (a: P, b: P, w: number) => (x: number, y: number) => {
const L = (b.x - a.x) ** 2 + (b.y - a.y) ** 2
const t = L ? Math.max(0, Math.min(1, ((x - a.x) * (b.x - a.x) + (y - a.y) * (b.y - a.y)) / L)) : 0
return Math.hypot(x - (a.x + t * (b.x - a.x)), y - (a.y + t * (b.y - a.y))) - w / 2
}
let obs: Obs[] = []
function load(file: string) {
cj = JSON.parse(readFileSync(file, "utf8")); cm = getFullConnectivityMapFromCircuitJson(cj)
netKey = new Map(); obs = []
for (const n of cj.filter((e: any) => e.type === "source_net")) { const k = cm.getNetConnectedToId(n.source_net_id); if (k) netKey.set(n.name, k) }
for (const e of cj) {
if (e.type === "pcb_smtpad") {
let { x, y, width: w, height: h } = e
if (e.shape === "circle") { w = h = e.radius * 2 }
if (e.points) { const xs = e.points.map((p: P) => p.x), ys = e.points.map((p: P) => p.y); x = (Math.max(...xs) + Math.min(...xs)) / 2; y = (Math.max(...ys) + Math.min(...ys)) / 2; w = Math.max(...xs) - Math.min(...xs); h = Math.max(...ys) - Math.min(...ys) }
obs.push({ key: keyOf(e.pcb_port_id), top: e.layer === "top", bottom: e.layer === "bottom", inner: false, pad: true, d: rectD(x, y, w, h, e.ccw_rotation ?? 0) })
} else if (e.type === "pcb_via") {
obs.push({ key: keyOf(e.pcb_trace_id) ?? keyOf(e.pcb_via_id), top: true, bottom: true, inner: true, pad: false, via: true, d: (x, y) => Math.hypot(x - e.x, y - e.y) - e.outer_diameter / 2 })
} else if (e.type === "pcb_plated_hole" || e.type === "pcb_hole") {
const r = (e.outer_diameter ?? e.outer_width ?? e.hole_diameter ?? e.hole_width ?? 1) / 2 + 0.2
obs.push({ key: keyOf(e.pcb_port_id), top: true, bottom: true, inner: true, pad: true, d: (x, y) => Math.hypot(x - e.x, y - e.y) - r })
} else if (e.type === "pcb_trace") {
const k = keyOf(e.pcb_trace_id) ?? keyOf(e.source_trace_id)
const r = e.route
for (let i = 0; i + 1 < r.length; i++) {
const a = r[i], b = r[i + 1]
if (a.route_type === "via") obs.push({ key: k, top: true, bottom: true, inner: true, pad: false, via: true, d: (x, y) => Math.hypot(x - a.x, y - a.y) - 0.175 })
if (a.route_type !== "wire" || b.route_type !== "wire" || a.layer !== b.layer) continue
obs.push({ key: k, top: a.layer === "top", bottom: a.layer === "bottom", inner: a.layer.startsWith("inner"), pad: false, d: segD(a, b, b.width ?? a.width ?? 0.1) })
}
const last = r[r.length - 1]; if (last?.route_type === "via") obs.push({ key: k, top: true, bottom: true, inner: true, pad: false, via: true, d: (x, y) => Math.hypot(x - last.x, y - last.y) - 0.175 })
}
}
}
// Motor ports: the routed board before any power copper. Power-input area: the board after the
// 5 V buck was re-placed and routed (BUCK5 phase), built with POWER_COPPER_PORTS_ONLY=1.
load(process.env.CJ ?? "routing/inputs/board-before-power.circuit.json")
const VIA_D = 0.6, VIA_HOLE = 0.3, CLR = 0.15
/** Free for a through via of `net` at (x,y)? Other-net copper >= CLR from the via ring (+ hole rule),
* never on any pad (no via-in-pad), also not on same-net pads. */
function free(net: string, x: number, y: number, placed: P[], VIA_D = 0.6) {
const k = netKey.get(net)
for (const o of obs) {
const d = o.d(x, y)
if (o.pad && d < VIA_D / 2 + 0.1) return false
if (o.via && d < VIA_D / 2 + 0.15) return false // any via, same net too: hole/ring spacing
if (o.key === k && k) continue
if (d < VIA_D / 2 + (VIA_D < 0.6 ? 0.12 : CLR + (o.pad ? 0 : 0.05))) return false
}
return placed.every((p) => Math.hypot(p.x - x, p.y - y) >= Math.max(0.9, VIA_D / 2 + 0.6))
}
const inPoly = (poly: P[], x: number, y: number) => {
let c = false
for (let i = 0, j = poly.length - 1; i < poly.length; j = i++) {
const a = poly[i], b = poly[j]
if (a.y > y !== b.y > y && x < ((b.x - a.x) * (y - a.y)) / (b.y - a.y) + a.x) c = !c
}
return c
}
const rect = (x0: number, y0: number, x1: number, y1: number): P[] => [{ x: x0, y: y0 }, { x: x1, y: y0 }, { x: x1, y: y1 }, { x: x0, y: y1 }]
/** Up to `n` free via sites for `net` inside `area`, nearest to `near` first (within maxR). */
function sites(net: string, area: P[], near: P, n: number, maxR = 2.5, all: P[] = [], d = 0.6) {
const cand: P[] = []
const xs = area.map((p) => p.x), ys = area.map((p) => p.y)
for (let x = Math.min(...xs); x <= Math.max(...xs); x += 0.05)
for (let y = Math.min(...ys); y <= Math.max(...ys); y += 0.05)
if (inPoly(area, x, y) && Math.hypot(x - near.x, y - near.y) <= maxR) cand.push({ x: +x.toFixed(3), y: +y.toFixed(3) })
cand.sort((a, b) => Math.hypot(a.x - near.x, a.y - near.y) - Math.hypot(b.x - near.x, b.y - near.y))
const got: P[] = []
for (const c of cand) { if (got.length >= n) break; if (free(net, c.x, c.y, [...all, ...got], d)) got.push(c) }
return got
}
type Pour = { name: string; net: string; outline: P[]; zone?: string }
type Via = { net: string; x: number; y: number; zone?: string; d?: number }
const pours: Pour[] = [], vias: Via[] = [], report: string[] = []
const sh = (poly: P[], dy: number) => poly.map((p) => ({ x: p.x, y: +(p.y + dy).toFixed(3) }))
const want = (tag: string, net: string, area: P[], near: P, n: number, maxR?: number, d = 0.6) => {
const got = sites(net, area, near, n, maxR, vias, d)
vias.push(...got.map((p) => ({ net, ...p, zone: tag.startsWith("IN") ? "input" : "port", ...(d !== 0.6 ? { d } : {}) })))
report.push(`${tag.padEnd(22)} ${net.padEnd(10)} ${got.length}/${n}`)
}
// ---- motor ports: shapes drawn for the TOP port (y 38.2, port 4 since the 2026-11-30 renumbering) ----
const M1 = {
GND: rect(-0.6, 32.1, 37.9, 44.3),
VIN_SYS: [{ x: -0.6, y: 39.45 }, { x: 5.3, y: 39.45 }, { x: 5.3, y: 42.9 }, { x: 7.3, y: 42.9 }, { x: 7.3, y: 44.2 }, { x: -0.6, y: 44.2 }],
SW1: [{ x: 1.9, y: 34.95 }, { x: 7.45, y: 34.95 }, { x: 7.45, y: 33.85 }, { x: 11.4, y: 33.85 }, { x: 11.4, y: 36.6 }, { x: 5.4, y: 39.38 }, { x: 1.9, y: 39.38 }],
SW2: rect(7.45, 39.5, 16.15, 42.75),
VBB_PRE: [{ x: 16.0, y: 37.25 }, { x: 24.8, y: 37.25 }, { x: 24.8, y: 41.3 }, { x: 17.6, y: 41.3 }, { x: 17.6, y: 39.4 }, { x: 16.0, y: 39.4 }],
VBB: rect(26.45, 36.7, 34.15, 39.65),
VBUS: rect(34.3, 33.9, 37.85, 37.0),
}
for (const i of [1, 2, 3, 4]) {
const dy = PORT_Y[i] - 38.2, m = `P${i}`
pours.push({ name: `POUR_${m}_GND_TOP`, net: "GND", outline: sh(M1.GND, dy) })
// M3: signal vias cut the upper VIN strip off from the FET pads (it was a floating island), so stop below it
pours.push({ name: `POUR_${m}_VIN_TOP`, net: "VIN_SYS", outline: sh(i === 2 ? rect(-0.6, 39.45, 5.3, 42.45) : M1.VIN_SYS, dy) })
for (const n of ["SW1", "SW2", "VBB_PRE", "VBB", "VBUS"] as const) pours.push({ name: `POUR_${m}_${n}`, net: `${m}_${n}`, outline: sh(M1[n], dy) })
// VIN_SYS into the inner4 plane (x >= 0.6 there): high-side FET drain + input caps
want(`${m} VIN at QM_H`, "VIN_SYS", sh(M1.VIN_SYS.map((p) => ({ x: Math.max(p.x, 0.6), y: p.y })), dy), { x: 3.6, y: 42.7 + dy }, 6, 4.5)
want(`${m} VIN at CIB`, "VIN_SYS", sh(rect(0.6, 32.1, 1.9, 35.0), dy), { x: 1.2, y: 34.1 + dy }, 2, 2)
// GND returns into inner1/bottom planes
want(`${m} GND at QM_L`, "GND", sh(rect(-0.6, 32.1, 7.4, 34.9), dy), { x: 3.3, y: 33.6 + dy }, 6, 3)
want(`${m} GND at C_IN`, "GND", sh(rect(-0.6, 35.0, 1.9, 44.3), dy), { x: 1.0, y: 37.0 + dy }, 3, 6)
want(`${m} GND at BB PGND`, "GND", sh(rect(12.0, 32.1, 16.0, 37.3), dy), { x: 14.9, y: 36.0 + dy }, 4, 3)
want(`${m} GND at C_OUT`, "GND", sh(rect(19.5, 41.4, 25.8, 44.3), dy), { x: 21.5, y: 43.0 + dy }, 4, 3)
want(`${m} GND at C_OA`, "GND", sh(rect(21.0, 36.0, 23.5, 37.25), dy), { x: 22.1, y: 36.7 + dy }, 2, 2)
}
// ---- power input: PD connector -> eFuse (UIN) -> VIN_SYS; 5 V buck (U5V, L5V, D5V) -> V5V0 ----
// Searched against the board with the re-placed buck routed (see load() note above).
load(process.env.CJ_IN ?? "routing/inputs/board-buck-baseline.circuit.json")
// Listed low -> high priority (later pours win where outlines overlap).
const poly = (...xy: number[]): P[] => xy.reduce((a: P[], v, i) => (i % 2 ? a : [...a, { x: v, y: xy[i + 1] }]), [])
pours.push({ name: "POUR_IN_GND_TOP", net: "GND", outline: rect(-38.0, 12.2, -1.0, 48.6) })
pours.push({ name: "POUR_IN_VIN_UOUT", net: "VIN_SYS", outline: rect(-23.65, 42.8, -10.1, 45.95) })
pours.push({ name: "POUR_IN_VBUS_IN", net: "VBUS_IN", outline: rect(-37.85, 39.9, -27.55, 48.4) })
// 5 V buck (U5V turned 270 at (-24, 25.5)): VIN pin (top) -> C5VIN1/2 above it -> R5VEN1's VIN pin
const BUCK_VIN = poly(-24.25, 27.6, -22.5, 27.6, -22.5, 34.7, -25.6, 34.7, -25.6, 33.75, -24.25, 33.75)
// SW pin (bottom right) + C5VBT SW pin + D5V cathode (below) + L5V pin1 (right): one copper area
const BUCK_SW = poly(-23.0, 15.0, -19.7, 15.0, -19.7, 20.45, -14.6, 20.45, -14.6, 23.25, -20.1, 23.25, -20.1, 25.4, -21.6, 25.4, -21.6, 23.6, -22.6, 23.6, -22.6, 21.5, -23.0, 21.5)
// L5V pin2 -> C5VO1-3 pin1 -> down the left of UAON to its VIN pin and CAONI
const BUCK_5V = poly(-18.5, 25.5, -14.3, 25.5, -14.3, 17.8, -10.9, 17.8, -10.9, 19.3, -11.4, 19.3, -11.4, 31.0, -18.5, 31.0)
pours.push({ name: "POUR_IN_VIN_BUCK", net: "VIN_SYS", outline: BUCK_VIN })
pours.push({ name: "POUR_IN_BUCK5_SW", net: "BUCK5_SW", outline: BUCK_SW })
pours.push({ name: "POUR_IN_V5V0", net: "V5V0", outline: BUCK_5V })
// FB divider top (R5VFB1 pin1) -> its own V5V0 via (5 V plane is under it)
const FB_5V = rect(-25.35, 15.45, -24.45, 17.1)
pours.push({ name: "POUR_IN_V5V0_FB", net: "V5V0", outline: FB_5V })
want("IN VIN at UIN OUT", "VIN_SYS", rect(-23.65, 42.8, -10.1, 45.95), { x: -21.2, y: 44.0 }, 6, 3)
want("IN VIN at CVSYS", "VIN_SYS", rect(-23.65, 42.8, -10.1, 45.95), { x: -14.5, y: 45.3 }, 4, 5)
want("IN VIN at C5VIN", "VIN_SYS", BUCK_VIN, { x: -23.4, y: 34.0 }, 4, 3)
// V5V0 plane (inner4) is under x >= -14 (y < 33.5) and under x < -14 only below y 27
want("IN V5V0 at L5V", "V5V0", rect(-13.9, 25.5, -11.4, 31.0), { x: -13.6, y: 27.9 }, 4, 4)
want("IN V5V0 below L5V", "V5V0", rect(-18.5, 25.5, -14.3, 26.6), { x: -16.5, y: 26.0 }, 3, 3)
want("IN V5V0 at UAON", "V5V0", rect(-14.3, 17.8, -11.4, 25.5), { x: -13.5, y: 21.0 }, 3, 4)
// R5VFB1's V5V0 pin (top) -> one signal-size via (microamps) placed by hand: 0.30 mm from the nearest
// other copper (PORT_HW_EN on inner3); the 0.375 mm margin the search uses is stricter than DRC's 0.1 mm.
vias.push({ net: "V5V0", x: -24.95, y: 16.75, zone: "input", d: 0.35 }); report.push("IN V5V0 at R5VFB1 V5V0 1/1 (hand-placed)")
want("IN GND at UIN EP", "GND", rect(-27.2, 42.9, -24.0, 45.0), { x: -25.6, y: 43.6 }, 4, 2)
want("IN GND below UIN EP", "GND", rect(-27.2, 38.0, -24.0, 39.9), { x: -25.6, y: 39.3 }, 3, 2)
want("IN GND at U5V EP L", "GND", rect(-27.0, 23.8, -25.9, 27.2), { x: -26.3, y: 25.5 }, 3, 2)
want("IN GND at U5V EP R", "GND", rect(-22.1, 25.6, -20.9, 27.4), { x: -21.7, y: 26.6 }, 2, 2)
want("IN GND at D5V anode", "GND", rect(-16.6, 12.6, -12.3, 15.0), { x: -14.5, y: 14.6 }, 3, 3)
want("IN GND at C5VIN", "GND", rect(-20.2, 29.4, -18.6, 33.8), { x: -19.4, y: 31.1 }, 3, 3)
want("IN GND at C5VO", "GND", rect(-9.6, 24.5, -7.8, 31.5), { x: -9.2, y: 27.9 }, 4, 4)
want("IN GND at RT/EN", "GND", rect(-27.2, 31.8, -25.6, 33.4), { x: -26.4, y: 32.4 }, 1, 2)
want("IN GND at FB/COMP", "GND", rect(-26.0, 16.6, -22.9, 19.1), { x: -24.35, y: 18.6 }, 2, 3)
// three small-signal GND pins whose top GND fill is cut off by signal wires: one signal-size via each
want("IN GND isl R5VEN2", "GND", rect(-25.35, 29.5, -24.25, 32.62), { x: -24.8, y: 32.1 }, 1, 2, 0.35)
want("IN GND isl R5VFB2", "GND", rect(-25.25, 16.25, -24.2, 20.04), { x: -24.7, y: 18.6 }, 1, 3, 0.35)
want("IN GND at C5VCC", "GND", rect(-25.49, 12.4, -23.3, 14.42), { x: -24.2, y: 12.8 }, 1, 2, 0.35)
want("IN GND at CAONI", "GND", rect(-10.0, 16.6, -8.0, 19.4), { x: -9.4, y: 18.0 }, 1, 3)
want("IN GND at CVSYS", "GND", rect(-19.0, 47.7, -10.0, 48.5), { x: -14.5, y: 48.1 }, 4, 5)
// ---- fix-ups searched against the board after the USB-pair rework (RIPUP wires cut the V5V0 fill strip to CAONI) ----
load(process.env.CJ_FIX ?? "routing/inputs/board-usb-final.circuit.json")
want("IN V5V0 at CAONI (fix)", "V5V0", rect(-12.6, 17.5, -10.85, 19.4), { x: -11.6, y: 18.4 }, 1, 2, 0.35)
for (const p of pours) p.zone = p.name.startsWith("POUR_IN_") ? "input" : "port"
writeFileSync("circuit/power-copper.json", JSON.stringify({ pours, vias }, null, 1))
console.log(report.join("\n")); console.log(`pours ${pours.length}, vias ${vias.length}`)