humpback-dds/nmigen/resources.py

91 lines
2.3 KiB
Python

from nmigen.build import *
__all__ = ["I2CResource", "GPIOResources", "DiffResources"]
def I2CResource(*args, sda, scl, conn=None, attrs=None, role="host"):
assert role in ("host", "device")
io = []
# sda line: I/O port for the data line
io.append(Subsignal("sda", Pins(sda, dir="io", conn=conn, assert_width=1)))
# sck line: I2C clock signal outputs from master to slave
if role == "host":
io.append(Subsignal("scl", Pins(scl, dir="o", conn=conn, assert_width=1)))
else: #device
io.append(Subsignal("scl", Pins(scl, dir="i", conn=conn, assert_width=1)))
if attrs is not None:
io.append(attrs)
return Resource.family(*args, default_name="i2c", ios=io)
# Auto create a resource list given a set of iCE40 pins and STM32 pin names (pins_dict)
def GPIOResources(*args, pins_dict, dir = "o", invert=False, conn=None, attrs=None):
# Check data integrity: pins_dict must be a dict AND port must be from a to k
assert isinstance(pins_dict, dict)
# Debug: dir == "o"
assert dir == "o"
# List of resources to be returned
resources = []
for STM32_pin, iCE40_pin in pins_dict.items():
# Set all gpio pins to be output only for the time being
# TODO: Allow dir argument.
ios = [Pins(iCE40_pin, dir=dir, invert=invert, conn=conn)]
if attrs is not None:
ios.append(attrs)
# Extract GPIO port and port number from STM32_pin
# Strip "P" from P<port><port_num>
if STM32_pin.startswith('P'):
STM32_pin = STM32_pin[1:]
# Acquire port from <port><port_num>
port = STM32_pin[0].lower()
port_num = int(STM32_pin[1:])
# Insert gpio<port>.<portNum> into resources list
resources.append(Resource.family(*args, port_num, default_name=("gpio"+port), ios=ios))
return resources
# Auto create a resource list for differential I/O
def DiffResources(*args, eem_pins, invert=False, conn=None, attrs=None, dir):
# TODO: Everything
# assert dimensionality
assert isinstance(eem_pins, list)
assert isinstance(eem_pins[0], list)
assert isinstance(eem_pins[0][0], list)
assert isinstance(eem_pins[0][0][0], str)
# assert direction to be either input or output
# reject tristate or bidirectional pin
assert dir in ("i", "o")
if __name__ == "__main__":
from pin_mapper import *
eem = diffMapping()
DiffResources(eem_pins = eem, dir = "o",
attrs=Attrs(IO_STANDARD="SB_LVCMOS")
)