8fdbcbe01ba13c9e6a9cd7e8690eb9c2d595ed08
[nmigen-boards.git] / nmigen_boards / dev / sd.py
1 from nmigen.build import *
2
3
4 __all__ = ["SDCardResources"]
5
6
7 def SDCardResources(*args, clk, cmd, dat0, dat1=None, dat2=None, dat3=None,
8 cd=None, wp=None, attrs=None):
9 resources = []
10
11 io_common = []
12 if attrs is not None:
13 io_common.append(attrs)
14 if cd is not None:
15 io_common.append(Subsignal("cd", Pins(cd, dir="i", assert_width=1)))
16 if wp is not None:
17 io_common.append(Subsignal("wp", PinsN(wp, dir="i", assert_width=1)))
18
19 io_native = list(io_common)
20 io_native.append(Subsignal("clk", Pins(clk, dir="o", assert_width=1)))
21 io_native.append(Subsignal("cmd", Pins(cmd, dir="o", assert_width=1)))
22
23 io_1bit = list(io_native)
24 io_1bit.append(Subsignal("dat", Pins(dat0, dir="io", assert_width=1)))
25 if dat3 is not None: # works as electronic card detect
26 io_1bit.append(Subsignal("ecd", Pins(dat3, dir="i", assert_width=1)))
27 resources.append(Resource.family(*args, default_name="sd_card", ios=io_1bit,
28 name_suffix="1bit"))
29
30 if dat1 is not None and dat2 is not None and dat3 is not None:
31 io_4bit = list(io_native)
32 io_4bit.append(Subsignal("dat", Pins(" ".join((dat0, dat1, dat2, dat3)), dir="io",
33 assert_width=4)))
34 resources.append(Resource.family(*args, default_name="sd_card", ios=io_4bit,
35 name_suffix="4bit"))
36
37 if dat3 is not None:
38 io_spi = list(io_common)
39 io_spi.append(Subsignal("cs", PinsN(dat3, dir="io"))) # doubles as electronic card detect
40 io_spi.append(Subsignal("clk", Pins(clk, dir="o", assert_width=1)))
41 io_spi.append(Subsignal("mosi", Pins(cmd, dir="o", assert_width=1)))
42 io_spi.append(Subsignal("miso", Pins(dat0, dir="i", assert_width=1)))
43 resources.append(Resource.family(*args, default_name="sd_card", ios=io_spi,
44 name_suffix="spi"))
45
46 return resources