3 from spec
.pinfunctions
import pinspec
4 from copy
import deepcopy
7 def namesuffix(name
, suffix
, namelist
):
11 names
.append("%s%s_%s" % (name
, suffix
, n
))
13 names
.append("%s_%s" % (name
, suffix
))
18 """ a meta-helper which creates pins from the pinspec
19 and adds them to the pinouts.
21 __call__ is used to effectively create a lambda function, which
22 in combination with setattr (below) gives the function a name
23 in the Pinouts class, according to the pinspec.
26 def __init__(self
, pinouts
, fname
, pinfn
, bankspec
):
27 self
.pinouts
= pinouts
28 self
.bankspec
= bankspec
32 def __call__(self
, suffix
, offs
, bank
, mux
,
33 start
=None, limit
=None, spec
=None, origsuffix
=None):
34 pingroup
= self
.pinfn(suffix
, bank
)
35 if isinstance(pingroup
, tuple):
36 prefix
, pingroup
= pingroup
39 if start
and limit
: # limit turns into an offset from start
41 pingroup
= pingroup
[start
:limit
] # see comment in spec.pinfunctions
42 pins
= Pins(prefix
, pingroup
, self
.bankspec
,
43 suffix
, offs
, bank
, mux
,
44 spec
, origsuffix
=suffix
)
45 self
.pinouts
.pinmerge(pins
)
50 class Pinouts(object):
51 def __init__(self
, bankspec
):
52 self
.bankspec
= bankspec
55 for fname
, pinfn
in pinspec
:
56 if isinstance(pinfn
, tuple):
60 setattr(self
, name
, PinGen(self
, fname
, pinfn
, self
.bankspec
))
62 def __contains__(self
, k
):
68 def add_spec(self
, k
, v
):
71 def update(self
, pinidx
, v
):
72 if pinidx
not in self
.pins
:
76 assert k
not in self
.pins
[pinidx
], \
77 "pin %d position %d already taken\n%s\n%s" % \
78 (pinidx
, k
, str(v
), self
.pins
[pinidx
])
79 self
.pins
[pinidx
].update(v
)
82 return self
.pins
.keys()
85 return self
.pins
.items()
93 def __delitem__(self
, k
):
96 def __getitem__(self
, k
):
99 def pinmerge(self
, fn
):
100 # hack, store the function specs in the pins dict
102 suffix
= fn
.origsuffix
105 if not hasattr(self
, 'fnspec'):
109 assert 'EINT' not in self
110 if fname
not in self
.fnspec
:
111 self
.add_spec(fname
, {})
112 if suffix
or fname
== 'EINT' or fname
== 'PWM':
113 specname
= fname
+ suffix
116 # print "fname bank specname suffix ", fname, bank, specname, repr(
118 if specname
in self
.fnspec
[fname
]:
119 # ok so some declarations may bring in different
120 # names at different stages (EINT, PWM, flexbus1/2)
121 # so we have to merge the names in. main thing is
123 tomerge
= self
.fnspec
[fname
][specname
]
124 for p
in fn
.pingroup
:
125 if p
not in tomerge
.pingroup
:
126 tomerge
.pingroup
.append(p
)
127 tomerge
.pins
.update(fn
.pins
)
128 tomerge
.fntype
.update(fn
.fntype
)
130 self
.fnspec
[fname
][specname
] = deepcopy(fn
)
133 for (pinidx
, v
) in fn
.pins
.items():
134 self
.update(pinidx
, v
)
139 def __init__(self
, fname
, pingroup
, bankspec
, suffix
, offs
, bank
, mux
,
140 spec
=None, limit
=None, origsuffix
=None):
142 # function type can be in, out or inout, represented by - + *
143 # strip function type out of each pin name
145 for i
in range(len(pingroup
)):
150 if fntype
not in '+-*':
153 fntype
= {'-': 'in', '+': 'out', '*': 'inout'}[fntype
]
154 self
.fntype
[pname
] = fntype
158 self
.pingroup
= pingroup
159 self
.bankspec
= bankspec
161 self
.origsuffix
= origsuffix
or suffix
165 # create consistent name suffixes
166 pingroup
= namesuffix(fname
, suffix
, pingroup
)
172 for name
in pingroup
[:limit
]:
174 name_
= "%s_%s" % (name
, suffix
)
177 if spec
and name
in spec
:
179 pin
= {mux
: (name_
, bank
)}
180 offs_bank
, offs_
= offs
183 idx_
+= bankspec
[bank
]
186 for name
in pingroup
:
188 name_
= "%s_%s" % (name
, suffix
)
195 idx_
, mux_
, bank_
= spec
[name
]
197 pin
= {mux_
: (name_
, bank_
)}
199 res
[idx_
].update(pin
)