Files
diodeinc__pcb/examples/power_checks.zen
Akhil Velagapudi eee722e306 Remove special handling of single-net interfaces (#229)
* Fix interface template cloning

Also, remove special-cased single net interface naming.

Signed-off-by: akhilles <akhilvelagapudi@gmail.com>

* Update snapshots

Signed-off-by: akhilles <akhilvelagapudi@gmail.com>

* Delete using(), interface promotion

Signed-off-by: akhilles <akhilvelagapudi@gmail.com>

* Add back proper interface type checking

Signed-off-by: akhilles <akhilvelagapudi@gmail.com>

* Update power check example

Signed-off-by: akhilles <akhilvelagapudi@gmail.com>

* Extract default value before type compilation

Signed-off-by: akhilles <akhilvelagapudi@gmail.com>

---------

Signed-off-by: akhilles <akhilvelagapudi@gmail.com>
2025-10-14 20:39:38 -04:00

49 lines
1.7 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
VoltageRange = builtin.physical_range("V")
Severity = enum("error", "warning", "advice")
def ElectricalCheck(check_fn: typing.Callable, **kwargs):
check_fn(**kwargs)
Power = builtin.net_type(
"Power",
voltage=VoltageRange,
)
def check_voltage_within(
within: str | VoltageRange, name: str = "voltage_check", severity: Severity = Severity("error")
) -> typing.Callable:
def ensure_voltage_within(_module, net_name: str, voltage: VoltageRange, within: str | VoltageRange):
within = VoltageRange(within)
# drop nominal in within:
within = VoltageRange(min=within.min, max=within.max)
# ensure voltage is within within
min_valid = voltage.min >= within.min
max_valid = voltage.max <= within.max
err_msg = "Voltage range " + str(voltage) + " of " + net_name + " is not within " + str(within)
check(min_valid and max_valid, err_msg)
def check_gen(power: Power, severity: Severity = severity, name: str = name):
check_name = power.NET.name + "_" + name
if not getattr(power, "voltage", None):
return
builtin.add_electrical_check(
name=check_name,
check_fn=ensure_voltage_within,
inputs={"voltage": power.voltage, "within": within, "net_name": power.NET.name},
severity=severity.value,
)
return check_gen
# Option 1: Use stdlib ElectricalCheck
vbat = Power("VBAT", voltage=VoltageRange("11–26 V (12 V nom.)"))
ElectricalCheck(
severity=Severity("warning"),
check_fn=check_voltage_within(VoltageRange("20–30 V")),
power=vbat,
)
# Option 2: Use helper in io()
vbat = io("VBAT", Power, check_voltage_within("20–30 V", severity=Severity("warning")))