dev.spi: Update SPIResources to accept a name and direction.
[nmigen-boards.git] / nmigen_boards / dev / spi.py
1 from nmigen.build import *
2
3
4 __all__ = ["SPIResource"]
5
6
7 def SPIResource(*args, cs, clk, mosi, miso, int=None, reset=None, attrs=None, role="host"):
8 assert role in ("host", "device")
9
10 io = []
11 if role == "host":
12 io.append(Subsignal("cs", PinsN(cs, dir="o")))
13 io.append(Subsignal("clk", Pins(clk, dir="o", assert_width=1)))
14 io.append(Subsignal("mosi", Pins(mosi, dir="o", assert_width=1)))
15 io.append(Subsignal("miso", Pins(miso, dir="i", assert_width=1)))
16 else: # device
17 io.append(Subsignal("cs", PinsN(cs, dir="i", assert_width=1)))
18 io.append(Subsignal("clk", Pins(clk, dir="i", assert_width=1)))
19 io.append(Subsignal("mosi", Pins(mosi, dir="i", assert_width=1)))
20 io.append(Subsignal("miso", Pins(miso, dir="oe", assert_width=1)))
21 if int is not None:
22 if role == "host":
23 io.append(Subsignal("int", Pins(int, dir="i")))
24 else:
25 io.append(Subsignal("int", Pins(int, dir="oe", assert_width=1)))
26 if reset is not None:
27 if role == "host":
28 io.append(Subsignal("reset", Pins(reset, dir="o")))
29 else:
30 io.append(Subsignal("reset", Pins(reset, dir="i", assert_width=1)))
31 if attrs is not None:
32 io.append(attrs)
33 return Resource.family(*args, default_name="spi", ios=io)