Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _erc_setup(cls):
"""
Initialize the electrical rules checker.
"""
# Initialize the pin contention matrix.
cls._erc_matrix = [[cls.OK for c in range(11)] for r in range(11)]
cls._erc_matrix[Pin.OUTPUT][Pin.OUTPUT] = cls.ERROR
cls._erc_matrix[Pin.TRISTATE][Pin.OUTPUT] = cls.WARNING
cls._erc_matrix[Pin.UNSPEC][Pin.INPUT] = cls.WARNING
cls._erc_matrix[Pin.UNSPEC][Pin.OUTPUT] = cls.WARNING
cls._erc_matrix[Pin.UNSPEC][Pin.BIDIR] = cls.WARNING
cls._erc_matrix[Pin.UNSPEC][Pin.TRISTATE] = cls.WARNING
cls._erc_matrix[Pin.UNSPEC][Pin.PASSIVE] = cls.WARNING
cls._erc_matrix[Pin.UNSPEC][Pin.UNSPEC] = cls.WARNING
cls._erc_matrix[Pin.PWRIN][Pin.TRISTATE] = cls.WARNING
cls._erc_matrix[Pin.PWRIN][Pin.UNSPEC] = cls.WARNING
cls._erc_matrix[Pin.PWROUT][Pin.OUTPUT] = cls.ERROR
cls._erc_matrix[Pin.PWROUT][Pin.BIDIR] = cls.WARNING
cls._erc_matrix[Pin.PWROUT][Pin.TRISTATE] = cls.ERROR
cls._erc_matrix[Pin.PWROUT][Pin.UNSPEC] = cls.WARNING
cls._erc_matrix[Pin.PWROUT][Pin.PWROUT] = cls.ERROR
cls._erc_matrix[Pin.OPENCOLL][Pin.OUTPUT] = cls.ERROR
cls._erc_matrix[Pin.OPENCOLL][Pin.TRISTATE] = cls.ERROR
cls._erc_matrix[Pin.OPENCOLL][Pin.UNSPEC] = cls.WARNING
cls._erc_matrix[Pin.OPENCOLL][Pin.PWROUT] = cls.ERROR
cls._erc_matrix[Pin.OPENEMIT][Pin.OUTPUT] = cls.ERROR
cls._erc_matrix[Pin.OPENEMIT][Pin.BIDIR] = cls.WARNING
cls._erc_matrix[Pin.OPENEMIT][Pin.TRISTATE] = cls.WARNING
cls._erc_matrix[Pin.OPENEMIT][Pin.UNSPEC] = cls.WARNING
cls._erc_matrix[Pin.OPENEMIT][Pin.PWROUT] = cls.ERROR
cls._erc_matrix[Pin.NOCONNECT][Pin.INPUT] = cls.ERROR
cls._erc_matrix = [[cls.OK for c in range(11)] for r in range(11)]
cls._erc_matrix[Pin.OUTPUT][Pin.OUTPUT] = cls.ERROR
cls._erc_matrix[Pin.TRISTATE][Pin.OUTPUT] = cls.WARNING
cls._erc_matrix[Pin.UNSPEC][Pin.INPUT] = cls.WARNING
cls._erc_matrix[Pin.UNSPEC][Pin.OUTPUT] = cls.WARNING
cls._erc_matrix[Pin.UNSPEC][Pin.BIDIR] = cls.WARNING
cls._erc_matrix[Pin.UNSPEC][Pin.TRISTATE] = cls.WARNING
cls._erc_matrix[Pin.UNSPEC][Pin.PASSIVE] = cls.WARNING
cls._erc_matrix[Pin.UNSPEC][Pin.UNSPEC] = cls.WARNING
cls._erc_matrix[Pin.PWRIN][Pin.TRISTATE] = cls.WARNING
cls._erc_matrix[Pin.PWRIN][Pin.UNSPEC] = cls.WARNING
cls._erc_matrix[Pin.PWROUT][Pin.OUTPUT] = cls.ERROR
cls._erc_matrix[Pin.PWROUT][Pin.BIDIR] = cls.WARNING
cls._erc_matrix[Pin.PWROUT][Pin.TRISTATE] = cls.ERROR
cls._erc_matrix[Pin.PWROUT][Pin.UNSPEC] = cls.WARNING
cls._erc_matrix[Pin.PWROUT][Pin.PWROUT] = cls.ERROR
cls._erc_matrix[Pin.OPENCOLL][Pin.OUTPUT] = cls.ERROR
cls._erc_matrix[Pin.OPENCOLL][Pin.TRISTATE] = cls.ERROR
cls._erc_matrix[Pin.OPENCOLL][Pin.UNSPEC] = cls.WARNING
cls._erc_matrix[Pin.OPENCOLL][Pin.PWROUT] = cls.ERROR
cls._erc_matrix[Pin.OPENEMIT][Pin.OUTPUT] = cls.ERROR
cls._erc_matrix[Pin.OPENEMIT][Pin.BIDIR] = cls.WARNING
cls._erc_matrix[Pin.OPENEMIT][Pin.TRISTATE] = cls.WARNING
cls._erc_matrix[Pin.OPENEMIT][Pin.UNSPEC] = cls.WARNING
cls._erc_matrix[Pin.OPENEMIT][Pin.PWROUT] = cls.ERROR
cls._erc_matrix[Pin.NOCONNECT][Pin.INPUT] = cls.ERROR
cls._erc_matrix[Pin.NOCONNECT][Pin.OUTPUT] = cls.ERROR
cls._erc_matrix[Pin.NOCONNECT][Pin.BIDIR] = cls.ERROR
cls._erc_matrix[Pin.NOCONNECT][Pin.TRISTATE] = cls.ERROR
cls._erc_matrix[Pin.NOCONNECT][Pin.PASSIVE] = cls.ERROR
cls._erc_matrix[Pin.NOCONNECT][Pin.UNSPEC] = cls.ERROR
cls._erc_matrix[Pin.NOCONNECT][Pin.PWRIN] = cls.ERROR
cls._erc_matrix[Pin.UNSPEC][Pin.UNSPEC] = cls.WARNING
cls._erc_matrix[Pin.PWRIN][Pin.TRISTATE] = cls.WARNING
cls._erc_matrix[Pin.PWRIN][Pin.UNSPEC] = cls.WARNING
cls._erc_matrix[Pin.PWROUT][Pin.OUTPUT] = cls.ERROR
cls._erc_matrix[Pin.PWROUT][Pin.BIDIR] = cls.WARNING
cls._erc_matrix[Pin.PWROUT][Pin.TRISTATE] = cls.ERROR
cls._erc_matrix[Pin.PWROUT][Pin.UNSPEC] = cls.WARNING
cls._erc_matrix[Pin.PWROUT][Pin.PWROUT] = cls.ERROR
cls._erc_matrix[Pin.OPENCOLL][Pin.OUTPUT] = cls.ERROR
cls._erc_matrix[Pin.OPENCOLL][Pin.TRISTATE] = cls.ERROR
cls._erc_matrix[Pin.OPENCOLL][Pin.UNSPEC] = cls.WARNING
cls._erc_matrix[Pin.OPENCOLL][Pin.PWROUT] = cls.ERROR
cls._erc_matrix[Pin.OPENEMIT][Pin.OUTPUT] = cls.ERROR
cls._erc_matrix[Pin.OPENEMIT][Pin.BIDIR] = cls.WARNING
cls._erc_matrix[Pin.OPENEMIT][Pin.TRISTATE] = cls.WARNING
cls._erc_matrix[Pin.OPENEMIT][Pin.UNSPEC] = cls.WARNING
cls._erc_matrix[Pin.OPENEMIT][Pin.PWROUT] = cls.ERROR
cls._erc_matrix[Pin.NOCONNECT][Pin.INPUT] = cls.ERROR
cls._erc_matrix[Pin.NOCONNECT][Pin.OUTPUT] = cls.ERROR
cls._erc_matrix[Pin.NOCONNECT][Pin.BIDIR] = cls.ERROR
cls._erc_matrix[Pin.NOCONNECT][Pin.TRISTATE] = cls.ERROR
cls._erc_matrix[Pin.NOCONNECT][Pin.PASSIVE] = cls.ERROR
cls._erc_matrix[Pin.NOCONNECT][Pin.UNSPEC] = cls.ERROR
cls._erc_matrix[Pin.NOCONNECT][Pin.PWRIN] = cls.ERROR
cls._erc_matrix[Pin.NOCONNECT][Pin.PWROUT] = cls.ERROR
cls._erc_matrix[Pin.NOCONNECT][Pin.OPENCOLL] = cls.ERROR
cls._erc_matrix[Pin.NOCONNECT][Pin.OPENEMIT] = cls.ERROR
cls._erc_matrix[Pin.NOCONNECT][Pin.NOCONNECT] = cls.ERROR
# Fill-in the other half of the symmetrical matrix.
for c in range(1, 11):
for r in range(c):
def show(lib_name, part_name):
"""Print the I/O pins for a given part in a library."""
try:
return Part(lib_name, re.escape(part_name))
except Exception:
return None # Suppress the traceback information.
Circuit = SubCircuit
ERC = SubCircuit._ERC
generate_netlist = SubCircuit._generate_netlist
generate_xml = SubCircuit._generate_xml
POWER = Pin.POWER_DRIVE
# This is a NOCONNECT net for attaching to pins which are intentionally left open.
NC = _NCNet('NOCONNECT')
def net_drive_chk():
"""
Check the drive level on the net to see if it is within bounds.
"""
# Find the maximum signal driver on this net.
net_drive = self.drive # Start with user-set drive level.
pins = self._get_pins()
for p in pins:
net_drive = max(net_drive, Pin.pin_info[p.func]['drive'])
if net_drive <= Pin.NO_DRIVE:
erc_logger.warning('No drivers for net {n}'.format(
n=self.name))
for p in pins:
if Pin.pin_info[p.func]['min_rcv'] > net_drive:
erc_logger.warning(
'Insufficient drive current on net {n} for pin {p}'.format(
n=self.name, p=p._erc_desc()))
def __init__(self, name=None, *pins_nets_buses, **attribs):
super(_NCNet, self).__init__(name, *pins_nets_buses, **attribs)
self._drive = Pin.NOCONNECT_DRIVE
def kicad_pin_to_pin(kicad_pin):
p = Pin()
# Replicate the KiCad pin fields as attributes in the Pin object.
# Note that this update will not give the pins valid references
# to the current part, but we'll fix that soon.
p.__dict__.update(kicad_pin)
pin_type_translation = {'I': Pin.INPUT,
'O': Pin.OUTPUT,
'B': Pin.BIDIR,
'T': Pin.TRISTATE,
'P': Pin.PASSIVE,
'U': Pin.UNSPEC,
'W': Pin.PWRIN,
'w': Pin.PWROUT,
'C': Pin.OPENCOLL,
'E': Pin.OPENEMIT,
'N': Pin.NOCONNECT}
p.func = pin_type_translation[p.electrical_type]
return p
def kicad_pin_to_pin(kicad_pin):
p = Pin()
# Replicate the KiCad pin fields as attributes in the Pin object.
# Note that this update will not give the pins valid references
# to the current part, but we'll fix that soon.
p.__dict__.update(kicad_pin)
pin_type_translation = {'I': Pin.INPUT,
'O': Pin.OUTPUT,
'B': Pin.BIDIR,
'T': Pin.TRISTATE,
'P': Pin.PASSIVE,
'U': Pin.UNSPEC,
'W': Pin.PWRIN,
'w': Pin.PWROUT,
'C': Pin.OPENCOLL,
'E': Pin.OPENEMIT,
'N': Pin.NOCONNECT}
def _erc_desc(self):
"""Return a string describing this pin for ERC."""
desc = "{func} pin {num}/{name} of {part}".format(
part=self.part._erc_desc(),
num=self.num,
name=self.name,
func=Pin.pin_info[self.func]['function'])
return desc
lists/tuples of them.
Returns:
The updated pin with the new connections.
Notes:
You can connect nets or pins to a pin like so::
p = Pin() # Create a pin.
n = Net() # Create a net.
p += net # Connect the net to the pin.
"""
# Go through all the pins and/or nets and connect them to this pin.
for pn in _expand_buses(_flatten(pins_nets_buses)):
if isinstance(pn, Pin):
# Connecting pin-to-pin.
if self._is_connected():
# If self is already connected to a net, then add the
# other pin to the same net.
self.nets[0] += pn
elif pn._is_connected():
# If self is unconnected but the other pin is, then
# connect self to the other pin's net.
pn.nets[0] += self
else:
# Neither pin is connected to a net, so create a net
# and attach both to it.
Net().connect(self, pn)
elif isinstance(pn, Net):
# Connecting pin-to-net, so just connect the pin to the net.
pn += self