808985ce5f4fa884939bbb217e73672508f385dd
3 from copy
import deepcopy
4 from collections
import OrderedDict
6 def display(of
, pins
, banksel
=None, muxwidth
=4):
8 | Pin | Mux0 | Mux1 | Mux2 | Mux3 |
9 | --- | ----------- | ----------- | ----------- | ----------- |
11 pinidx
= sorted(pins
.keys())
16 for mux
in range(muxwidth
):
19 name
, bank
= pdata
[mux
]
25 for mux
in range(muxwidth
):
29 name
, bank
= pdata
[mux
]
30 res
+= " %s %-9s |" % (bank
, name
)
31 of
.write("%s\n" % res
)
37 if not f
.startswith('FB_'):
41 return f2
[0], int(f2
[1])
44 while f
and not f
[0].isdigit():
47 return a
, int(f
) if f
else None
59 def find_fn(fname
, names
):
61 if fname
.startswith(n
):
64 def map_name(pinmap
, fn
, fblower
, pin
, rename
):
66 if pin
[:-1].isdigit():
67 print "map name digit", pin
, fn
, fblower
68 if fn
in ['PWM', 'EINT', 'VDD', 'VSS']:
69 return fn
.lower() + pin
.lower()
71 return 'gpio' + pin
[1:].lower()
75 pk
= '%s%s_%s' % (fblower
, pin
[0], pin
[:-1])
76 elif pin
[:-1].isdigit() and fn
!= 'EINT':
77 pk
= '%s%s_out' % (fblower
, pin
[:-1])
79 pk
= '%s_%s' % (fblower
, pin
[:-1])
80 print "map name", pk
, fblower
, pinmap
.has_key(pk
)
81 if not pinmap
.has_key(pk
):
84 uscore
= remapped
.find('_')
87 fn
, pin
= remapped
[:uscore
], remapped
[uscore
+1:] + pin
[-1]
90 def python_pindict(of
, pinmap
, pins
, function_names
, dname
, remap
):
93 of
.write("\n%s = OrderedDict()\n" % dname
)
95 for k
, pingroup
in pins
.byspec
.items():
100 of
.write("%s['%s'] = [ " % (dname
, fblower
))
103 for i
, p
in enumerate(pingroup
):
104 name
= map_name(pinmap
, k
[0], fblower
, p
, remap
)
105 res
[fblower
].append(name
)
106 of
.write("'%s', " % name
)
108 if count
== 4 and i
!= len(pingroup
)-1:
112 print " dict %s" % dname
, a
, n
, pingroup
116 def python_dict_fns(of
, pinmap
, pins
, function_names
):
117 of
.write("# auto-generated by Libre-SOC pinmux program: do not edit\n")
118 of
.write("# python src/pinmux_generator.py -v -s {spec} -o {output}\n")
119 of
.write("# use OrderedDict to fix stable order for JTAG Boundary Scan\n")
120 of
.write("from collections import OrderedDict\n")
122 fn_names
= function_names
.keys()
125 fnidx
= list(fns
.keys())
126 fnidx
.sort(key
=fnsplit
)
128 print "python fnames", function_names
129 print "python speckeys", pins
.byspec
.keys()
130 print "python dict fns", dir(pins
.gpio
)
131 print pins
.gpio
.pinfn('', '')
132 print pins
.pwm
.pinfn('', '')
133 print pins
.sdmmc
.pinfn('', '')
134 print "by spec", pins
.byspec
137 pd
= python_pindict(of
, {}, pins
, function_names
, 'pindict', False)
138 ld
= python_pindict(of
, pinmap
, pins
, function_names
, 'litexdict', True)
142 # process results and create name map
143 litexmap
= OrderedDict()
147 for pname
, lname
in zip(pl
, ll
):
148 pname
= "%s_%s" % (k
, pname
[:-1]) # strip direction +/-/*
149 lname
= lname
[:-1] # strip direction +/-/*
150 if k
in ['eint', 'pwm', 'gpio', 'vdd', 'vss']: # sigh
151 lname
= "%s_%s" % (k
, lname
)
152 litexmap
[pname
] = lname
153 print "litexmap", litexmap
154 of
.write("litexmap = {\n")
155 for k
, v
in litexmap
.items():
156 of
.write("\t'%s': '%s',\n" % (k
, v
))
161 def display_fns(of
, bankspec
, pins
, function_names
):
162 fn_names
= function_names
.keys()
164 for (pin
, pdata
) in pins
.items():
165 for mux
in range(0, 4): # skip GPIO for now
168 name
, bank
= pdata
[mux
]
169 assert name
is not None, str(bank
)
172 fns
[name
].append((pin
- bankspec
[bank
], mux
, bank
))
174 fnidx
= list(fns
.keys())
175 fnidx
.sort(key
=fnsplit
)
178 fnbase
= find_fn(fname
, fn_names
)
179 #fblower = fnbase.lower()
180 assert fnbase
in function_names
, "fn %s not in descriptions %s" % \
181 (fname
, str(function_names
.keys()))
182 #print "name", fname, fnbase
183 if fnbase
!= current_fn
:
184 if current_fn
is not None:
186 of
.write("## %s\n\n%s\n\n" % (fnbase
, function_names
[fnbase
]))
188 of
.write("* %-9s :" % fname
)
189 for (pin
, mux
, bank
) in fns
[fname
]:
190 of
.write(" %s%d/%d" % (bank
, pin
, mux
))
196 def check_functions(of
, title
, bankspec
, fns
, pins
, required
, eint
, pwm
,
199 pins
= deepcopy(pins
)
200 if descriptions
is None:
207 of
.write("# Pinmap for %s\n\n" % title
)
209 print "fn_idx", fnidx
211 print "fnspec", pins
.fnspec
.keys()
212 print "required", required
213 for name
in required
:
214 of
.write("## %s\n\n" % name
)
215 if descriptions
and name
in descriptions
:
216 of
.write("%s\n\n" % descriptions
[name
])
218 name
= name
.split(':')
220 findbank
= name
[0][0]
221 findmux
= int(name
[0][1:])
227 name
= name
.split('/')
238 if not fname
.startswith(name
):
240 for k
in pins
.fnspec
.keys():
241 if fname
.startswith(k
):
242 fk
= list(pins
.fnspec
[k
].keys())
245 #print fname, fn, dir(fn)
247 count
= len(fn
.pingroup
)
248 for pin
, mux
, bank
in fns
[fname
]:
249 if findbank
is not None:
254 pin_
= pin
+ bankspec
[bank
]
256 pinfound
[pin_
] = (fname
, pin_
, bank
, pin
, mux
)
258 pinidx
= sorted(pinfound
.keys())
262 print ("pinidx", pinidx
)
264 fname
, pin_
, bank
, pin
, mux
= pinfound
[pin_
]
268 if len(found
) > count
:
272 of
.write("* %s %d %s%d/%d\n" % (fname
, pin_
, bank
, pin
, mux
))
275 if removedcount
!= count
:
277 print "no match between required and available pins"
279 print ("not all found", name
, removedcount
, count
, title
, found
,
281 print ("pins found", pinfound
)
288 for name
in descriptions
.keys():
289 if not name
.startswith('GPIO'):
297 of
.write("## GPIO\n\n")
303 if descriptions
and fname
in descriptions
:
304 desc
= ': %s' % descriptions
[fname
]
307 pin_
= pin
+ bankspec
[bank
]
312 of
.write("* %-8s %d %s%-2d %s\n" % (fname
, pin_
, bank
, pin
, desc
))
316 display_group(of
, bankspec
, "EINT", eint
, fns
, pins
, descriptions
)
318 display_group(of
, bankspec
, "PWM", pwm
, fns
, pins
, descriptions
)
320 of
.write("## Unused Pinouts (spare as GPIO) for '%s'\n\n" % title
)
321 if descriptions
and 'GPIO' in descriptions
:
322 of
.write("%s\n\n" % descriptions
['GPIO'])
329 def display_group(of
, bankspec
, title
, todisplay
, fns
, pins
, descriptions
):
330 of
.write("## %s\n\n" % title
)
333 for fname
in todisplay
:
335 if descriptions
and fname
in descriptions
:
336 desc
= ': %s' % descriptions
[fname
]
337 fname
= fname
.split(':')
339 findbank
= fname
[0][0]
340 findmux
= int(fname
[0][1:])
346 for (pin
, mux
, bank
) in fns
[fname
]:
347 if findbank
is not None:
354 pin_
= pin
+ bankspec
[bank
]
359 of
.write("* %s %d %s%d/%d %s\n" %
360 (fname
, pin_
, bank
, pin
, mux
, desc
))
364 def display_fixed(of
, fixed
, offs
):
366 fkeys
= sorted(fixed
.keys())
369 for pin
, k
in enumerate(fkeys
):
370 of
.write("## %s\n\n" % k
)
373 for name
in fixed
[k
]:
377 if prevname
[:2] == name
[:2] and linecount
!= 0:
378 of
.write(" %s" % name
)
383 of
.write("* %d: %d %s" % (pin_
, pin
, name
))
385 res
.append((pin_
, name
))