add linebreak on long line
[pinmux.git] / src / interface_decl.py
1
2 class Pin(object):
3 """ pin interface declaration.
4 * name is the name of the pin
5 * ready, enabled and io all create a (* .... *) prefix
6 * action changes it to an "in" if true
7 """
8
9 def __init__(self, name,
10 ready=True,
11 enabled=True,
12 io=False,
13 action=False):
14 self.name = name
15 self.ready = ready
16 self.enabled = enabled
17 self.io = io
18 self.action = action
19
20 def __str__(self):
21 res = ' '
22 status = []
23 if self.ready:
24 status.append('always_ready')
25 if self.enabled:
26 status.append('always_enabled')
27 if self.io:
28 status.append('result="io"')
29 if status:
30 res += '(*'
31 res += ','.join(status)
32 res += '*)'
33 res += " method "
34 if self.io:
35 res += "\n "
36 if self.action:
37 res += " Action "
38 res += self.name
39 res += ' (Bit#(1) in)'
40 else:
41 res += " Bit#(1) "
42 res += self.name
43 res += ";"
44 return res
45
46
47 class Interface(object):
48 """ create an interface from a list of pinspecs.
49 each pinspec is a dictionary, see Pin class arguments
50 """
51
52 def __init__(self, pinspecs):
53 self.pins = []
54 for p in pinspecs:
55 if p.get('outen') is True: # special case, generate 3 pins
56 _p = {}
57 _p.update(p)
58 del _p['outen']
59 for psuffix in ['out', 'outen', 'in']:
60 _p['name'] = "%s_%s" % (p['name'], psuffix)
61 _p['action'] = psuffix != 'in'
62 self.pins.append(Pin(**_p))
63 else:
64 self.pins.append(Pin(**p))
65
66 def __str__(self):
67 return '\n'+'\n'.join(map(str, self.pins))
68
69 def format(self, i):
70 return str(self).format(i)
71
72 # basic test
73 if __name__ == '__main__':
74
75 def _pinmunge(p, sep, repl, dedupe=True):
76 """ munges the text so it's easier to compare.
77 splits by separator, strips out blanks, re-joins.
78 """
79 p = p.strip()
80 p = p.split(sep)
81 if dedupe:
82 p = filter(lambda x: x, p) # filter out blanks
83 return repl.join(p)
84
85 def pinmunge(p):
86 """ munges the text so it's easier to compare.
87 """
88 # first join lines by semicolons, strip out returns
89 p = p.split(";")
90 p = map(lambda x: x.replace('\n', ''), p)
91 p = '\n'.join(p)
92 # now split first by brackets, then spaces (deduping on spaces)
93 p = _pinmunge(p, "(", " ( ", False)
94 p = _pinmunge(p, ")", " ) ", False)
95 p = _pinmunge(p, " ", " ")
96 return p
97
98 # ========= Interface declarations ================ #
99
100 mux_interface = '''
101 method Action cell{0}_mux(Bit#({1}) in);'''
102
103 io_interface = Interface([{'name': 'io_outputval_{0}', 'enabled': False},
104 {'name': 'io_output_en_{0}', 'enabled': False},
105 {'name': 'io_input_en_{0}', 'enabled': False},
106 {'name': 'io_pullup_en_{0}', 'enabled': False},
107 {'name': 'io_pulldown_en_{0}', 'enabled': False},
108 {'name': 'io_drivestrength_{0}', 'enabled': False},
109 {'name': 'io_pushpull_en_{0}', 'enabled': False},
110 {'name': 'io_opendrain_en_{0}', 'enabled': False},
111 {'name': 'io_inputval_{0}', 'action': True, 'io': True},
112 ])
113
114 # == Peripheral Interface definitions == #
115 # these are the interface of the peripherals to the pin mux
116 # Outputs from the peripherals will be inputs to the pinmux
117 # module. Hence the change in direction for most pins
118
119 uartinterface_decl = Interface([{'name': 'tx_{0}', 'action': True},
120 {'name': 'rx_{0}'},
121 ])
122
123 spiinterface_decl = Interface([{'name': 'sclk_{0}', 'action': True},
124 {'name': 'mosi_{0}', 'action': True},
125 {'name': 'ss_{0}', 'action': True},
126 {'name': 'miso_{0}'},
127 ])
128
129 twiinterface_decl = Interface([{'name': 'sda{0}', 'outen': True},
130 {'name': 'scl{0}', 'outen': True},
131 ])
132
133 sdinterface_decl = Interface([{'name': 'sd{0}_clk', 'action': True},
134 {'name': 'sd{0}_cmd', 'action': True},
135 {'name': 'sd{0}_d0', 'outen': True},
136 {'name': 'sd{0}_d1', 'outen': True},
137 {'name': 'sd{0}_d2', 'outen': True},
138 {'name': 'sd{0}_d3', 'outen': True}
139 ])
140
141 jtaginterface_decl = Interface([{'name': 'jtag{0}_tdi'},
142 {'name': 'jtag{0}_tms'},
143 {'name': 'jtag{0}_tclk'},
144 {'name': 'jtag{0}_trst'},
145 {'name': 'jtag{0}_tdo', 'action': True}])
146
147 pwminterface_decl = Interface([{'name': "pwm{0}", 'action': True}])
148
149 # ======================================= #