Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions lib/components/primitive-components/Port/Port.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import { applyToPoint, compose, translate } from "transformation-matrix"
import { z } from "zod"
import { PrimitiveComponent } from "../../base-components/PrimitiveComponent"
import type { Trace } from "../Trace/Trace"
import type { LayerRef, SchematicPort } from "circuit-json"
import type { LayerRef, SchematicPort, SourcePort } from "circuit-json"
import { areAllPcbPrimitivesOverlapping } from "./areAllPcbPrimitivesOverlapping"
import { getCenterOfPcbPrimitives } from "./getCenterOfPcbPrimitives"
import { type PinAttributeMap, portProps } from "@tscircuit/props"
import type { INormalComponent } from "lib/components/base-components/NormalComponent/INormalComponent"
import { applyPinAttributesToSourcePort } from "./apply-pin-attributes-to-source-port"
import { applyDefaultDecouplingRequirementToSourcePort } from "./apply-default-decoupling-requirement-to-source-port"
import { Port_doInitialCreateTracesFromProps } from "./Port_doInitialCreateTracesFromProps"
import { Port_tryRenderGroupPcbPort } from "./Port_tryRenderGroupPcbPort"
import { getSourcePortNetLabelText } from "lib/utils/schematic/getSourcePortNetLabelText"
Expand Down Expand Up @@ -393,19 +394,24 @@ export class Port extends PrimitiveComponent<typeof portProps> {

// Get pin attributes from parent component and apply them to this port
const pinAttributes = this._getMatchingPinAttributes()
const portAttributesFromParent: Record<string, unknown> = {}
const sourcePortAttributes: Partial<SourcePort> = {}

for (const attributes of pinAttributes) {
applyPinAttributesToSourcePort(portAttributesFromParent, attributes)
applyPinAttributesToSourcePort(sourcePortAttributes, attributes)
}
applyDefaultDecouplingRequirementToSourcePort({
sourcePortAttributes,
sourcePortLabels: port_hints,
parentNormalComponentName: parentNormalComponent?.config.componentName,
})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can it be moved to capacitor?


const source_port = db.source_port.insert({
name: props.name!,
pin_number: props.pinNumber,
port_hints,
source_component_id: source_component_id!,
subcircuit_id: this.getSubcircuit()?.subcircuit_id!,
...portAttributesFromParent,
...sourcePortAttributes,
})

this.source_port_id = source_port.source_port_id
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { SourcePort } from "circuit-json"
import { POWER_NET_REGEX } from "lib/utils/gnd-power-net-regex"

export const applyDefaultDecouplingRequirementToSourcePort = ({
sourcePortAttributes,
sourcePortLabels,
parentNormalComponentName,
}: {
sourcePortAttributes: Partial<SourcePort>
sourcePortLabels: string[]
parentNormalComponentName: string | undefined
}): void => {
if (parentNormalComponentName !== "Chip") return
if (sourcePortAttributes.should_have_decoupling_capacitor !== undefined) {
return
}

if (sourcePortAttributes.provides_power === true) {
sourcePortAttributes.should_have_decoupling_capacitor = false
return
}

if (sourcePortAttributes.requires_power !== undefined) {
sourcePortAttributes.should_have_decoupling_capacitor =
sourcePortAttributes.requires_power
return
}

if (
sourcePortLabels.some((sourcePortLabel) =>
POWER_NET_REGEX.test(sourcePortLabel),
)
) {
sourcePortAttributes.should_have_decoupling_capacitor = true
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you move this stuff to capacitor

Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { PinAttributeMap } from "@tscircuit/props"
import type { SourcePort } from "circuit-json"

export const applyPinAttributesToSourcePort = (
sourcePortProps: Record<string, unknown>,
sourcePortProps: Partial<SourcePort>,
attributes: PinAttributeMap,
): void => {
if (attributes.mustBeConnected !== undefined) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { expect, test } from "bun:test"
import { getTestFixture } from "tests/fixtures/get-test-fixture"

test("emits chip decoupling metadata for external checks", async () => {
const { circuit } = getTestFixture()

circuit.add(
<board width="30mm" height="20mm" routingDisabled>
<chip
name="U_MISSING"
pinLabels={{ pin1: "VCC", pin2: "GND" }}
pinAttributes={{
VCC: {
requiresPower: true,
recommendedDecouplingCapacitorCapacitance: "100nF",
},
GND: { requiresGround: true },
}}
/>
<trace from=".U_MISSING > .VCC" to="net.VCC_MISSING" />
<trace from=".U_MISSING > .GND" to="net.GND" />

<chip
name="U_WITH_CAP"
pinLabels={{ pin1: "VDD", pin2: "GND" }}
pinAttributes={{ GND: { requiresGround: true } }}
/>
<capacitor name="C1" capacitance="100nF" />
<trace from=".U_WITH_CAP > .VDD" to=".C1 > .pin1" />
<trace from=".C1 > .pin2" to="net.GND" />
<trace from=".U_WITH_CAP > .GND" to="net.GND" />

<chip
name="U_OPT_OUT"
pinLabels={{ pin1: "VBAT", pin2: "GND" }}
pinAttributes={{
VBAT: {
requiresPower: true,
shouldHaveDecouplingCapacitor: false,
},
GND: { requiresGround: true },
}}
/>
<trace from=".U_OPT_OUT > .VBAT" to="net.VBAT" />
<trace from=".U_OPT_OUT > .GND" to="net.GND" />

<chip
name="U_POWER_SOURCE"
pinLabels={{ pin1: "VCC", pin2: "GND" }}
pinAttributes={{
VCC: { providesPower: true },
GND: { providesGround: true },
}}
/>
<trace from=".U_POWER_SOURCE > .VCC" to="net.VCC_SOURCE" />
<trace from=".U_POWER_SOURCE > .GND" to="net.GND" />
</board>,
)

await circuit.renderUntilSettled()

const sourceComponentsByName = new Map(
circuit.db.source_component
.list()
.map((sourceComponent) => [sourceComponent.name, sourceComponent]),
)
const getSourcePortByHint = (sourceComponentName: string, portHint: string) =>
circuit.db.source_port
.list()
.find(
(sourcePort) =>
sourcePort.source_component_id ===
sourceComponentsByName.get(sourceComponentName)
?.source_component_id &&
sourcePort.port_hints?.includes(portHint),
)

expect(getSourcePortByHint("U_MISSING", "VCC")).toMatchObject({
requires_power: true,
should_have_decoupling_capacitor: true,
recommended_decoupling_capacitor_capacitance: "100nF",
})
expect(getSourcePortByHint("U_MISSING", "GND")).toMatchObject({
requires_ground: true,
})
expect(getSourcePortByHint("U_WITH_CAP", "VDD")).toMatchObject({
should_have_decoupling_capacitor: true,
})
expect(getSourcePortByHint("U_OPT_OUT", "VBAT")).toMatchObject({
requires_power: true,
should_have_decoupling_capacitor: false,
})
expect(getSourcePortByHint("U_POWER_SOURCE", "VCC")).toMatchObject({
provides_power: true,
should_have_decoupling_capacitor: false,
})
})
Loading