3 from copy
import deepcopy
4 from collections
import OrderedDict
9 def create_sv(fname
, pins
):
10 """unsophisticated drawer of an SVG
14 width
= len(pins
['pads.north']) * scale
15 height
= len(pins
['pads.east']) * scale
16 woffs
= scale
*18#-width/2
17 hoffs
= scale
*18#-height/2
19 dwg
= svgwrite
.Drawing(fname
, profile
='full',
20 size
=(width
+scale
*40, height
+scale
*40))
21 dwg
.add(dwg
.rect((woffs
-scale
*2, hoffs
-scale
*2),
22 (woffs
+width
-scale
*12, hoffs
+height
-scale
*12),
23 stroke
=svgwrite
.rgb(255, 255, 16, '%'),
24 stroke_width
=scale
/10.0))
25 for i
, pin
in enumerate(pins
['pads.west']):
26 ht
= hoffs
+ height
- (i
* scale
) + scale
*0.5
27 dwg
.add(dwg
.line((woffs
-scale
*2, ht
-scale
*0.5),
28 (woffs
-scale
*4.5, ht
-scale
*0.5),
29 stroke
=svgwrite
.rgb(255, 255, 16, '%'),
30 stroke_width
=scale
/10.0))
31 dwg
.add(dwg
.text(pin
.upper(), insert
=(woffs
-scale
*12, ht
),
33 dwg
.add(dwg
.text("W%d" % (i
+1), insert
=(woffs
-scale
*1.5, ht
),
36 for i
, pin
in enumerate(pins
['pads.east']):
37 ht
= hoffs
+ height
- (i
* scale
) + scale
*0.5
38 wd
= width
+ woffs
+ scale
*2
39 dwg
.add(dwg
.line((wd
+scale
*2, ht
-scale
*0.5),
40 (wd
+scale
*4.5, ht
-scale
*0.5),
41 stroke
=svgwrite
.rgb(255, 255, 16, '%'),
42 stroke_width
=scale
/10.0))
43 dwg
.add(dwg
.text(pin
.upper(), insert
=(wd
+scale
*5, ht
-scale
*0.25),
45 dwg
.add(dwg
.text("E%d" % (i
+1), insert
=(wd
, ht
-scale
*0.25),
48 for i
, pin
in enumerate(pins
['pads.north']):
49 wd
= woffs
+ i
* scale
+ scale
*1.5
50 dwg
.add(dwg
.line((wd
, hoffs
-scale
*2),
51 (wd
, hoffs
-scale
*4.5),
52 stroke
=svgwrite
.rgb(255, 255, 16, '%'),
53 stroke_width
=scale
/10.0))
54 pos
=(wd
, hoffs
-scale
*5.0)
55 txt
= dwg
.text(pin
.upper(), insert
=pos
, fill
='white')
58 pos
=(wd
+scale
*0.25, hoffs
-scale
*0.25)
59 txt
= dwg
.text("N%d" % (i
+1), insert
=pos
, fill
='white')
63 for i
, pin
in enumerate(pins
['pads.south']):
64 wd
= woffs
+ i
* scale
+ scale
*1.5
65 ht
= hoffs
+ height
+ scale
*2
66 dwg
.add(dwg
.line((wd
, ht
+scale
*2),
68 stroke
=svgwrite
.rgb(255, 255, 16, '%'),
69 stroke_width
=scale
/10.0))
70 pos
=(wd
-scale
*0.25, ht
+scale
*5.0)
71 txt
= dwg
.text(pin
.upper(), insert
=pos
, fill
='white')
74 pos
=(wd
-scale
*0.25, ht
+scale
*0.25)
75 txt
= dwg
.text("S%d" % (i
+1), insert
=pos
, fill
='white')
82 def display(of
, pins
, banksel
=None, muxwidth
=4):
84 | Pin | Mux0 | Mux1 | Mux2 | Mux3 |
85 | --- | ----------- | ----------- | ----------- | ----------- |
87 pinidx
= sorted(pins
.keys())
92 for mux
in range(muxwidth
):
95 name
, bank
= pdata
[mux
]
100 res
= '| %3d |' % pin
101 for mux
in range(muxwidth
):
105 name
, bank
= pdata
[mux
]
106 res
+= " %s %-9s |" % (bank
, name
)
107 of
.write("%s\n" % res
)
113 if not f
.startswith('FB_'):
117 return f2
[0], int(f2
[1])
120 while f
and not f
[0].isdigit():
123 return a
, int(f
) if f
else None
135 def find_fn(fname
, names
):
137 if fname
.startswith(n
):
140 def map_name(pinmap
, fn
, fblower
, pin
, rename
):
142 if pin
[:-1].isdigit():
143 print "map name digit", pin
, fn
, fblower
144 if fn
in ['PWM', 'EINT', 'VDD', 'VSS']:
145 return fn
.lower() + pin
.lower()
147 return 'gpio' + pin
[1:].lower()
151 pk
= '%s%s_%s' % (fblower
, pin
[0], pin
[:-1])
152 elif pin
[:-1].isdigit() and fn
!= 'EINT':
153 pk
= '%s%s_out' % (fblower
, pin
[:-1])
155 pk
= '%s_%s' % (fblower
, pin
[:-1])
156 print "map name", pk
, fblower
, pinmap
.has_key(pk
)
157 if not pinmap
.has_key(pk
):
159 remapped
= pinmap
[pk
]
160 uscore
= remapped
.find('_')
163 fn
, pin
= remapped
[:uscore
], remapped
[uscore
+1:] + pin
[-1]
166 def python_pindict(of
, pinmap
, pins
, function_names
, dname
, remap
):
169 of
.write("\n%s = OrderedDict()\n" % dname
)
171 for k
, pingroup
in pins
.byspec
.items():
172 (a
, n
) = k
.split(":")
176 of
.write("%s['%s'] = [ " % (dname
, fblower
))
179 for i
, p
in enumerate(pingroup
):
180 name
= map_name(pinmap
, k
[0], fblower
, p
, remap
)
181 res
[fblower
].append(name
)
182 of
.write("'%s', " % name
)
184 if count
== 4 and i
!= len(pingroup
)-1:
188 print " dict %s" % dname
, a
, n
, pingroup
192 def python_dict_fns(of
, pinmap
, pins
, function_names
):
193 of
.write("# auto-generated by Libre-SOC pinmux program: do not edit\n")
194 of
.write("# python src/pinmux_generator.py -v -s {spec} -o {output}\n")
195 of
.write("# use OrderedDict to fix stable order for JTAG Boundary Scan\n")
196 of
.write("from collections import OrderedDict\n")
198 fn_names
= function_names
.keys()
201 fnidx
= list(fns
.keys())
202 fnidx
.sort(key
=fnsplit
)
204 print "python fnames", function_names
205 print "python speckeys", pins
.byspec
.keys()
206 print "python dict fns", dir(pins
.gpio
)
207 print pins
.gpio
.pinfn('', '')
208 print pins
.pwm
.pinfn('', '')
209 print pins
.sdmmc
.pinfn('', '')
210 print "by spec", pins
.byspec
213 pd
= python_pindict(of
, {}, pins
, function_names
, 'pindict', False)
214 ld
= python_pindict(of
, pinmap
, pins
, function_names
, 'litexdict', True)
218 # process results and create name map
219 litexmap
= OrderedDict()
223 for pname
, lname
in zip(pl
, ll
):
224 pname
= "%s_%s" % (k
, pname
[:-1]) # strip direction +/-/*
225 lname
= lname
[:-1] # strip direction +/-/*
226 if k
in ['eint', 'pwm', 'gpio', 'vdd', 'vss']: # sigh
227 lname
= "%s_%s" % (k
, lname
)
228 litexmap
[pname
] = lname
229 print "litexmap", litexmap
230 of
.write("litexmap = {\n")
231 for k
, v
in litexmap
.items():
232 of
.write("\t'%s': '%s',\n" % (k
, v
))
237 def display_fns(of
, bankspec
, pins
, function_names
):
238 fn_names
= function_names
.keys()
240 for (pin
, pdata
) in pins
.items():
241 for mux
in range(0, 4): # skip GPIO for now
244 name
, bank
= pdata
[mux
]
245 assert name
is not None, str(bank
)
248 fns
[name
].append((pin
- bankspec
[bank
], mux
, bank
))
250 fnidx
= list(fns
.keys())
251 fnidx
.sort(key
=fnsplit
)
254 fnbase
= find_fn(fname
, fn_names
)
255 #fblower = fnbase.lower()
256 assert fnbase
in function_names
, "fn %s not in descriptions %s" % \
257 (fname
, str(function_names
.keys()))
258 #print "name", fname, fnbase
259 if fnbase
!= current_fn
:
260 if current_fn
is not None:
262 of
.write("## %s\n\n%s\n\n" % (fnbase
, function_names
[fnbase
]))
264 of
.write("* %-9s :" % fname
)
265 for (pin
, mux
, bank
) in fns
[fname
]:
266 of
.write(" %s%d/%d" % (bank
, pin
, mux
))
272 def check_functions(of
, title
, bankspec
, fns
, pins
, required
, eint
, pwm
,
275 pins
= deepcopy(pins
)
276 if descriptions
is None:
283 of
.write("# Pinmap for %s\n\n" % title
)
285 print "fn_idx", fnidx
287 print "fnspec", pins
.fnspec
.keys()
288 print "required", required
289 for name
in required
:
290 of
.write("## %s\n\n" % name
)
291 if descriptions
and name
in descriptions
:
292 of
.write("%s\n\n" % descriptions
[name
])
294 name
= name
.split(':')
296 findbank
= name
[0][0]
297 findmux
= int(name
[0][1:])
303 name
= name
.split('/')
314 if not fname
.startswith(name
):
316 for k
in pins
.fnspec
.keys():
317 if fname
.startswith(k
):
318 fk
= list(pins
.fnspec
[k
].keys())
321 #print fname, fn, dir(fn)
323 count
= len(fn
.pingroup
)
324 for pin
, mux
, bank
in fns
[fname
]:
325 if findbank
is not None:
330 pin_
= pin
+ bankspec
[bank
]
332 pinfound
[pin_
] = (fname
, pin_
, bank
, pin
, mux
)
334 pinidx
= sorted(pinfound
.keys())
338 print ("pinidx", pinidx
)
340 fname
, pin_
, bank
, pin
, mux
= pinfound
[pin_
]
344 if len(found
) > count
:
348 of
.write("* %s %d %s%d/%d\n" % (fname
, pin_
, bank
, pin
, mux
))
351 if removedcount
!= count
:
353 print "no match between required and available pins"
355 print ("not all found", name
, removedcount
, count
, title
, found
,
357 print ("pins found", pinfound
)
364 for name
in descriptions
.keys():
365 if not name
.startswith('GPIO'):
373 of
.write("## GPIO\n\n")
379 if descriptions
and fname
in descriptions
:
380 desc
= ': %s' % descriptions
[fname
]
383 pin_
= pin
+ bankspec
[bank
]
388 of
.write("* %-8s %d %s%-2d %s\n" % (fname
, pin_
, bank
, pin
, desc
))
392 display_group(of
, bankspec
, "EINT", eint
, fns
, pins
, descriptions
)
394 display_group(of
, bankspec
, "PWM", pwm
, fns
, pins
, descriptions
)
396 of
.write("## Unused Pinouts (spare as GPIO) for '%s'\n\n" % title
)
397 if descriptions
and 'GPIO' in descriptions
:
398 of
.write("%s\n\n" % descriptions
['GPIO'])
405 def display_group(of
, bankspec
, title
, todisplay
, fns
, pins
, descriptions
):
406 of
.write("## %s\n\n" % title
)
409 for fname
in todisplay
:
411 if descriptions
and fname
in descriptions
:
412 desc
= ': %s' % descriptions
[fname
]
413 fname
= fname
.split(':')
415 findbank
= fname
[0][0]
416 findmux
= int(fname
[0][1:])
422 for (pin
, mux
, bank
) in fns
[fname
]:
423 if findbank
is not None:
430 pin_
= pin
+ bankspec
[bank
]
435 of
.write("* %s %d %s%d/%d %s\n" %
436 (fname
, pin_
, bank
, pin
, mux
, desc
))
440 def display_fixed(of
, fixed
, offs
):
442 fkeys
= sorted(fixed
.keys())
445 for pin
, k
in enumerate(fkeys
):
446 of
.write("## %s\n\n" % k
)
449 for name
in fixed
[k
]:
453 if prevname
[:2] == name
[:2] and linecount
!= 0:
454 of
.write(" %s" % name
)
459 of
.write("* %d: %d %s" % (pin_
, pin
, name
))
461 res
.append((pin_
, name
))