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-motherboard

In 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.

circuit/fast-routing.ts

/**
 * One-pass routing for the 4-layer board.
 *
 * The whole board is one routing phase. Its copper comes from routing/ALL.routed.json, written by the
 * native router in tools/router/ (sh tools/router/route.sh). This file only (a) dumps the routing problem and
 * (b) replays the saved copper; core then runs its normal connectivity and DRC checks on it.
 *
 *   ROUTE_DUMP=1 tsci build ...   writes routing/inputs/ALL.input.json and routes nothing
 *   tsci build ...                replays routing/ALL.routed.json
 *
 * Saved routes name their end pads by position (x, y, layer), not by id, so adding a part does not
 * invalidate the file; a route whose pad moved is dropped and shows up as an unconnected pin.
 */
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"

type Listener = (event: any) => void
const emitter = (run: (emit: (name: string, e: any) => void) => Promise<void>) => {
  const listeners = new Map<string, Listener[]>()
  const emit = (name: string, e: any) => { for (const cb of listeners.get(name) ?? []) cb(e) }
  return {
    on(name: string, cb: Listener) { listeners.set(name, [...(listeners.get(name) ?? []), cb]) },
    start() { run(emit).catch((error) => emit("error", { error })) },
    stop() {},
  }
}
const routingDir = `${process.cwd()}/routing/`
const log = (...a: unknown[]) => console.log("[fast-routing]", ...a)
const key = (x: number, y: number) => `${Math.round(x * 1e3)},${Math.round(y * 1e3)}`

type XY = { x: number; y: number }
/** from / to: the pads a route starts and ends on (null where it ends on other copper of its net or in a plane via).
 *  ref: any pad of the net, used to find the net when a route touches no pad itself. */
export type SavedRoute = { net: string; ref: XY; from: XY | null; to: XY | null; route: any[] }

export const fastRouting = (phase = "ALL") => async (input: any) => emitter(async (emit) => {
  if (process.env.ROUTE_DUMP) {
    mkdirSync(`${routingDir}inputs`, { recursive: true })
    writeFileSync(`${routingDir}inputs/${phase}.input.json`, JSON.stringify(input))
    log(`${phase}: dumped ${input.connections.length} connections, ${input.obstacles.length} obstacles`)
    emit("complete", { traces: [] })
    return
  }
  const file = `${routingDir}${phase}.routed.json`
  if (!existsSync(file)) throw new Error(`No saved routing: ${file} (run: sh tools/router/route.sh)`)
  const saved: SavedRoute[] = JSON.parse(readFileSync(file, "utf8"))
  // pad position -> { port id, connection name }
  const pads = new Map<string, { id: string; conn: string }>()
  for (const c of input.connections) for (const p of c.pointsToConnect) if (p.pcb_port_id) pads.set(key(p.x, p.y), { id: p.pcb_port_id, conn: c.name })
  const traces: any[] = []
  let dropped = 0
  saved.forEach((s, i) => {
    const a = s.from ? pads.get(key(s.from.x, s.from.y)) : undefined
    const b = s.to ? pads.get(key(s.to.x, s.to.y)) : undefined
    const ref = pads.get(key(s.ref.x, s.ref.y))
    if ((s.from && !a) || (s.to && !b) || !(a ?? b ?? ref)) { dropped++; return }
    const conn = (a ?? b ?? ref)!.conn
    const route = structuredClone(s.route)
    if (a) route[0].start_pcb_port_id = a.id
    if (b) route[route.length - 1].end_pcb_port_id = b.id
    traces.push({
      type: "pcb_trace", pcb_trace_id: `fr_${i}`, source_trace_id: conn, connection_name: conn,
      connectsTo: [conn, ...(a ? [a.id] : []), ...(b ? [b.id] : [])], route,
    })
  })
  log(`${phase}: replaying ${traces.length}/${saved.length} saved routes${dropped ? `, ${dropped} dropped (pad moved)` : ""}`)
  emit("complete", { traces })
})