dev.spi: Update SPIResources to accept a name and direction.
authorWilliam D. Jones <thor0505@comcast.net>
Wed, 10 Jul 2019 07:58:43 +0000 (03:58 -0400)
committerwhitequark <whitequark@whitequark.org>
Wed, 10 Jul 2019 07:58:42 +0000 (07:58 +0000)
nmigen_boards/dev/spi.py

index e8baae04c411a9cfe89f186a1439ae26b5c8d753..2d045bc6266edddc04aed5bd225c2e7d990d0e5f 100644 (file)
@@ -4,16 +4,30 @@ from nmigen.build import *
 __all__ = ["SPIResource"]
 
 
-def SPIResource(number, *, cs, clk, mosi, miso, int=None, reset=None, attrs=None):
+def SPIResource(*args, cs, clk, mosi, miso, int=None, reset=None, attrs=None, role="host"):
+    assert role in ("host", "device")
+
     io = []
-    io.append(Subsignal("cs", PinsN(cs, dir="o")))
-    io.append(Subsignal("clk", Pins(clk, dir="o")))
-    io.append(Subsignal("mosi", Pins(mosi, dir="o")))
-    io.append(Subsignal("miso", Pins(miso, dir="i")))
+    if role == "host":
+        io.append(Subsignal("cs", PinsN(cs, dir="o")))
+        io.append(Subsignal("clk", Pins(clk, dir="o", assert_width=1)))
+        io.append(Subsignal("mosi", Pins(mosi, dir="o", assert_width=1)))
+        io.append(Subsignal("miso", Pins(miso, dir="i", assert_width=1)))
+    else:  # device
+        io.append(Subsignal("cs", PinsN(cs, dir="i", assert_width=1)))
+        io.append(Subsignal("clk", Pins(clk, dir="i", assert_width=1)))
+        io.append(Subsignal("mosi", Pins(mosi, dir="i", assert_width=1)))
+        io.append(Subsignal("miso", Pins(miso, dir="oe", assert_width=1)))
     if int is not None:
-        io.append(Subsignal("int", Pins(int, dir="i")))
+        if role == "host":
+            io.append(Subsignal("int", Pins(int, dir="i")))
+        else:
+            io.append(Subsignal("int", Pins(int, dir="oe", assert_width=1)))
     if reset is not None:
-        io.append(Subsignal("reset", Pins(reset, dir="o")))
+        if role == "host":
+            io.append(Subsignal("reset", Pins(reset, dir="o")))
+        else:
+            io.append(Subsignal("reset", Pins(reset, dir="i", assert_width=1)))
     if attrs is not None:
         io.append(attrs)
-    return Resource("spi", number, *io)
+    return Resource.family(*args, default_name="spi", ios=io)