3 from copy
import deepcopy
4 from collections
import OrderedDict
9 cwd
= os
.path
.split(os
.path
.abspath(__file__
))[0]
10 lead_drawing
= cwd
+ "/greatek_qfp_128L.png"
11 pack_drawing
= cwd
+ "/greatek_qfp_128_fp.png"
13 def bond_int_to_ext(pin
, bank
):
14 """ note that internal numbering is 0-31 whereas the DISPLAY internal
15 numbering is 1-32. this uses the INTERNAL numbering.
19 outer ring numbers: the python list order of the outer pads
20 middle numbers: the package number wires (plus side they are on)
21 inner numbers: the IO pad *python internal (0-31)* numbers plus side
24 N102 N101 N100 99 N 68 N67 N66 N65
25 W29 W30 W31 N0 N N31 E29 E30 E31
31 W2 W1 W0 S0 S S31 E0 E1 E2
32 S1 S2 S3 4 S 35 S36 S37 S38
36 # returns side, order-on-the-side, pin number
38 return 'N', pin
+3, 99-pin
40 return 'S', pin
+3, 4+pin
42 if pin
>= 29: # 29, 30, 31
43 return 'N', pin
-29, 100+(31-pin
)
45 return 'S', 2-pin
, 3-pin
46 return 'W', 28-pin
, 103+(28-pin
)
49 return 'N', 35+(31-pin
), 67-(31-pin
)
51 return 'S', pin
+35, 36+pin
52 return 'E', 28-pin
, 39+(28-pin
)
55 def create_sv(fname
, pins
):
56 """unsophisticated drawer of an SVG
62 print ("WARNING, no SVG image, not producing image %s" % fname
)
65 # create internal to external map
66 bondmap
= {'N': {}, 'S': {}, 'E': {}, 'W': {} }
67 padside
= {'pads.north': 'N', 'pads.east': 'E', 'pads.south': 'S',
70 for pinpad
, bank
in padside
.items():
71 sidepad
[bank
] = pinpad
72 for ipin
in range(len(pins
[pinpad
])):
73 eside
, enum
, epinnum
= bond_int_to_ext(ipin
, bank
)
74 bondmap
[eside
][enum
] = (epinnum
, ipin
, bank
)
75 with
open("/tmp/bondmap.txt", "w") as f
:
76 for k
,v
in bondmap
.items():
78 for enum
, (epinnum
, ipin
, bank
) in v
.items():
79 f
.write(" %d %d -> %s %d\n" % (enum
, epinnum
, bank
, ipin
))
82 outerscale
= scale
* 2.0
84 width
= len(pins
['pads.north']) * scale
85 height
= len(pins
['pads.east']) * scale
86 woffs
= scale
*40#-width/2
87 hoffs
= scale
*40#-height/2
89 dwg
= svgwrite
.Drawing(fname
, profile
='full',
90 size
=(width
+scale
*80, height
+scale
*80))
91 dwg
.add(dwg
.rect((woffs
-scale
*2, hoffs
-scale
*2),
92 (woffs
+width
-scale
*34, hoffs
+height
-scale
*34),
93 stroke
=svgwrite
.rgb(16, 255, 16, '%'),
94 stroke_width
=scale
/10.0))
96 # record the inner line (iopad) position so that the outer one can
98 innerpos
= {'N': {}, 'S': {}, 'E': {}, 'W': {} }
100 # create the inner diagram
101 for i
, pin
in enumerate(pins
['pads.west']):
102 ht
= hoffs
+ height
- (i
* scale
) + scale
*0.5
103 endline
= (woffs
-scale
*4.5, ht
-scale
*0.5)
104 innerpos
['W'][i
] = endline
105 dwg
.add(dwg
.line((woffs
-scale
*2, ht
-scale
*0.5),
107 stroke
=svgwrite
.rgb(16, 255, 16, '%'),
108 stroke_width
=scale
/10.0))
109 dwg
.add(dwg
.text(pin
.upper(), insert
=(woffs
-scale
*14, ht
),
111 dwg
.add(dwg
.text("W%d" % (i
+1), insert
=(woffs
-scale
*1.5, ht
),
114 for i
, pin
in enumerate(pins
['pads.east']):
115 ht
= hoffs
+ height
- (i
* scale
) + scale
*0.5
116 wd
= width
+ woffs
+ scale
*2
117 endline
= (wd
+scale
*4.5, ht
-scale
*0.5)
118 innerpos
['E'][i
] = endline
119 dwg
.add(dwg
.line((wd
+scale
*2, ht
-scale
*0.5),
121 stroke
=svgwrite
.rgb(16, 255, 16, '%'),
122 stroke_width
=scale
/10.0))
123 dwg
.add(dwg
.text(pin
.upper(), insert
=(wd
+scale
*5, ht
-scale
*0.25),
125 dwg
.add(dwg
.text("E%d" % (i
+1), insert
=(wd
, ht
-scale
*0.25),
128 for i
, pin
in enumerate(pins
['pads.north']):
129 wd
= woffs
+ i
* scale
+ scale
*1.5
130 endline
= (wd
, hoffs
-scale
*4.5)
131 innerpos
['N'][i
] = endline
132 dwg
.add(dwg
.line((wd
, hoffs
-scale
*2),
134 stroke
=svgwrite
.rgb(16, 255, 16, '%'),
135 stroke_width
=scale
/10.0))
136 pos
=(wd
, hoffs
-scale
*5.0)
137 txt
= dwg
.text(pin
.upper(), insert
=pos
, fill
='black')
140 pos
=(wd
+scale
*0.25, hoffs
-scale
*0.25)
141 txt
= dwg
.text("N%d" % (i
+1), insert
=pos
, fill
='white')
145 for i
, pin
in enumerate(pins
['pads.south']):
146 wd
= woffs
+ i
* scale
+ scale
*1.5
147 ht
= hoffs
+ height
+ scale
*2
148 endline
= (wd
, ht
+scale
*4.5)
149 innerpos
['S'][i
] = endline
150 dwg
.add(dwg
.line((wd
, ht
+scale
*2),
152 stroke
=svgwrite
.rgb(16, 255, 16, '%'),
153 stroke_width
=scale
/10.0))
154 pos
=(wd
-scale
*0.25, ht
+scale
*5.0)
155 txt
= dwg
.text(pin
.upper(), insert
=pos
, fill
='black')
158 pos
=(wd
-scale
*0.25, ht
+scale
*0.25)
159 txt
= dwg
.text("S%d" % (i
+1), insert
=pos
, fill
='white')
163 nepads
= bondmap
['N'].keys()
165 wepads
= bondmap
['W'].keys()
167 eepads
= bondmap
['E'].keys()
169 sepads
= bondmap
['S'].keys()
172 owoffs
= woffs
+ (width
/2) - len(nepads
)/2 * outerscale
173 ohoffs
= hoffs
+ (height
/2) - len(wepads
)/2 * outerscale
177 (epinnum
, ipin
, bank
) = pad
= bondmap
['N'][i
]
178 wd
= owoffs
+ i
* outerscale
+ outerscale
*1.5
179 endline
= (wd
, ohoffs
-outerscale
*2)
180 startline
= innerpos
[bank
][ipin
]
181 dwg
.add(dwg
.line(startline
,
183 stroke
=svgwrite
.rgb(255, 16, 16, '%'),
184 stroke_width
=scale
/10.0))
185 pin
= pins
[sidepad
[bank
]][ipin
]
186 pos
=(wd
, ohoffs
-outerscale
*4.0)
187 txt
= dwg
.text("%s (%s%d)" % (pin
.upper(), bank
, ipin
+1),
188 insert
=pos
, fill
='black')
191 pos
=(wd
, ohoffs
-outerscale
*2.5)
192 txt
= dwg
.text("%d N" % epinnum
, insert
=pos
, fill
='blue')
198 (epinnum
, ipin
, bank
) = pad
= bondmap
['W'][i
]
199 ht
= ohoffs
+ (i
* outerscale
) + outerscale
*1.5
200 endline
= (owoffs
+outerscale
*0.5, ht
)
201 startline
= innerpos
[bank
][ipin
]
202 dwg
.add(dwg
.line(startline
,
204 stroke
=svgwrite
.rgb(255, 16, 16, '%'),
205 stroke_width
=scale
/10.0))
206 pin
= pins
[sidepad
[bank
]][ipin
]
207 pos
= (owoffs
-outerscale
*6.0, ht
)
208 dwg
.add(dwg
.text("%s (%s%d)" % (pin
.upper(), bank
, ipin
+1),
211 pos
= (owoffs
-outerscale
*1.0, ht
)
212 dwg
.add(dwg
.text("%d W" % epinnum
, insert
=pos
,
217 (epinnum
, ipin
, bank
) = pad
= bondmap
['S'][i
]
218 wd
= owoffs
+ i
* outerscale
+ outerscale
*1.5
219 ht
= ohoffs
+ len(wepads
)*outerscale
+ outerscale
*4
221 startline
= innerpos
[bank
][ipin
]
222 dwg
.add(dwg
.line(startline
,
224 stroke
=svgwrite
.rgb(255, 16, 16, '%'),
225 stroke_width
=scale
/10.0))
226 pin
= pins
[sidepad
[bank
]][ipin
]
227 pos
=(wd
-outerscale
*0.25, ht
+outerscale
*1.5)
228 txt
= dwg
.text("%s (%s%d)" % (pin
.upper(), bank
, ipin
+1),
229 insert
=pos
, fill
='black')
232 pos
=(wd
-outerscale
*0.25, ht
+outerscale
*0.5)
233 txt
= dwg
.text("%d S" % epinnum
, insert
=pos
, fill
='blue')
239 (epinnum
, ipin
, bank
) = pad
= bondmap
['E'][i
]
240 ht
= ohoffs
+ (i
* outerscale
) + outerscale
*1.5
241 wd
= owoffs
+len(nepads
)*outerscale
242 endline
= (wd
+outerscale
*0.5, ht
)
243 startline
= innerpos
[bank
][ipin
]
244 dwg
.add(dwg
.line(startline
,
246 stroke
=svgwrite
.rgb(255, 16, 16, '%'),
247 stroke_width
=scale
/10.0))
248 pin
= pins
[sidepad
[bank
]][ipin
]
249 pos
= (wd
+outerscale
*2.5, ht
)
250 dwg
.add(dwg
.text("%s (%s%d)" % (pin
.upper(), bank
, ipin
+1),
253 pos
= (wd
+outerscale
*1.0, ht
)
254 dwg
.add(dwg
.text("%d E" % epinnum
, insert
=pos
,
257 # add QFP pack image in top-right
258 image_data
= open(pack_drawing
, "rb").read()
259 encoded
= base64
.b64encode(image_data
).decode()
260 data
= 'data:image/png;base64,{}'.format(encoded
)
262 leads
= svgwrite
.image
.Image(data
, pos
,
266 # add QFP lead image in centre
267 image_data
= open(lead_drawing
, "rb").read()
268 encoded
= base64
.b64encode(image_data
).decode()
269 data
= 'data:image/png;base64,{}'.format(encoded
)
270 pos
=(woffs
+scale
*3.5, hoffs
+scale
*3.5)
271 leads
= svgwrite
.image
.Image(data
, pos
,
273 leads
.rotate(-90, (pos
[0]+200, pos
[1]+200))
276 dwg
.add(dwg
.text("GREATEK ELECTRONICS INC.",
277 insert
=(woffs
+width
/2-scale
*5, woffs
+height
/2-scale
*6),
279 dwg
.add(dwg
.text("INNER LEAD DRAWING",
280 insert
=(woffs
+width
/2-scale
*5, woffs
+height
/2-scale
*5),
282 dwg
.add(dwg
.text("QFP 128L OPEN STAMPING",
283 insert
=(woffs
+width
/2-scale
*5, woffs
+height
/2-scale
*4),
285 dwg
.add(dwg
.text("BODY 14x20x2.75mm",
286 insert
=(woffs
+width
/2-scale
*5, woffs
+height
/2-scale
*3),
288 dwg
.add(dwg
.text("L/F PAD SIZE 236x236mil^2",
289 insert
=(woffs
+width
/2-scale
*5, woffs
+height
/2-scale
*2),
293 dwg
.add(dwg
.text("Libre-SOC ls180 QFP-128",
294 insert
=(woffs
+width
/2-scale
*5, woffs
+height
/2),
296 dwg
.add(dwg
.text("In collaboration with LIP6.fr",
297 insert
=(woffs
+width
/2-scale
*5, woffs
+height
/2+scale
),
299 dwg
.add(dwg
.text("Cell Libraries by Chips4Makers",
300 insert
=(woffs
+width
/2-scale
*5, woffs
+height
/2+scale
*2),
302 dwg
.add(dwg
.text("IMEC TSMC 180nm",
303 insert
=(woffs
+width
/2-scale
*5, woffs
+height
/2+scale
*3),
309 def display(of
, pins
, banksel
=None, muxwidth
=4):
311 | Pin | Mux0 | Mux1 | Mux2 | Mux3 |
312 | --- | ----------- | ----------- | ----------- | ----------- |
314 pinidx
= sorted(pins
.keys())
316 pdata
= pins
.get(pin
)
319 for mux
in range(muxwidth
):
322 name
, bank
= pdata
[mux
]
327 res
= '| %3d |' % pin
328 for mux
in range(muxwidth
):
332 name
, bank
= pdata
[mux
]
333 res
+= " %s %-9s |" % (bank
, name
)
334 of
.write("%s\n" % res
)
340 if not f
.startswith('FB_'):
344 return f2
[0], int(f2
[1])
347 while f
and not f
[0].isdigit():
350 return a
, int(f
) if f
else None
362 def find_fn(fname
, names
):
364 if fname
.startswith(n
):
367 def map_name(pinmap
, fn
, fblower
, pin
, rename
):
369 if pin
[:-1].isdigit():
370 print "map name digit", pin
, fn
, fblower
371 if fn
in ['PWM', 'EINT', 'VDD', 'VSS']:
372 return fn
.lower() + pin
.lower()
374 return 'gpio' + pin
[1:].lower()
378 pk
= '%s%s_%s' % (fblower
, pin
[0], pin
[:-1])
379 elif pin
[:-1].isdigit() and fn
!= 'EINT':
380 pk
= '%s%s_out' % (fblower
, pin
[:-1])
382 pk
= '%s_%s' % (fblower
, pin
[:-1])
383 print "map name", pk
, fblower
, pinmap
.has_key(pk
)
384 if not pinmap
.has_key(pk
):
386 remapped
= pinmap
[pk
]
387 uscore
= remapped
.find('_')
390 fn
, pin
= remapped
[:uscore
], remapped
[uscore
+1:] + pin
[-1]
393 def python_pindict(of
, pinmap
, pins
, function_names
, dname
, remap
):
396 of
.write("\n%s = OrderedDict()\n" % dname
)
398 for k
, pingroup
in pins
.byspec
.items():
399 (a
, n
) = k
.split(":")
403 of
.write("%s['%s'] = [ " % (dname
, fblower
))
406 for i
, p
in enumerate(pingroup
):
407 name
= map_name(pinmap
, k
[0], fblower
, p
, remap
)
408 res
[fblower
].append(name
)
409 of
.write("'%s', " % name
)
411 if count
== 4 and i
!= len(pingroup
)-1:
415 print " dict %s" % dname
, a
, n
, pingroup
419 def python_dict_fns(of
, pinmap
, pins
, function_names
):
420 of
.write("# auto-generated by Libre-SOC pinmux program: do not edit\n")
421 of
.write("# python src/pinmux_generator.py -v -s {spec} -o {output}\n")
422 of
.write("# use OrderedDict to fix stable order for JTAG Boundary Scan\n")
423 of
.write("from collections import OrderedDict\n")
425 fn_names
= function_names
.keys()
428 fnidx
= list(fns
.keys())
429 fnidx
.sort(key
=fnsplit
)
431 print "python fnames", function_names
432 print "python speckeys", pins
.byspec
.keys()
433 print "python dict fns", dir(pins
.gpio
)
434 print pins
.gpio
.pinfn('', '')
435 print pins
.pwm
.pinfn('', '')
436 print pins
.sdmmc
.pinfn('', '')
437 print "by spec", pins
.byspec
440 pd
= python_pindict(of
, {}, pins
, function_names
, 'pindict', False)
441 ld
= python_pindict(of
, pinmap
, pins
, function_names
, 'litexdict', True)
445 # process results and create name map
446 litexmap
= OrderedDict()
450 for pname
, lname
in zip(pl
, ll
):
451 pname
= "%s_%s" % (k
, pname
[:-1]) # strip direction +/-/*
452 lname
= lname
[:-1] # strip direction +/-/*
453 if k
in ['eint', 'pwm', 'gpio', 'vdd', 'vss']: # sigh
454 lname
= "%s_%s" % (k
, lname
)
455 litexmap
[pname
] = lname
456 print "litexmap", litexmap
457 of
.write("litexmap = {\n")
458 for k
, v
in litexmap
.items():
459 of
.write("\t'%s': '%s',\n" % (k
, v
))
464 def display_fns(of
, bankspec
, pins
, function_names
):
465 fn_names
= function_names
.keys()
467 for (pin
, pdata
) in pins
.items():
468 for mux
in range(0, 4): # skip GPIO for now
471 name
, bank
= pdata
[mux
]
472 assert name
is not None, str(bank
)
475 fns
[name
].append((pin
- bankspec
[bank
], mux
, bank
))
477 fnidx
= list(fns
.keys())
478 fnidx
.sort(key
=fnsplit
)
481 fnbase
= find_fn(fname
, fn_names
)
482 #fblower = fnbase.lower()
483 assert fnbase
in function_names
, "fn %s not in descriptions %s" % \
484 (fname
, str(function_names
.keys()))
485 #print "name", fname, fnbase
486 if fnbase
!= current_fn
:
487 if current_fn
is not None:
489 of
.write("## %s\n\n%s\n\n" % (fnbase
, function_names
[fnbase
]))
491 of
.write("* %-9s :" % fname
)
492 for (pin
, mux
, bank
) in fns
[fname
]:
493 of
.write(" %s%d/%d" % (bank
, pin
, mux
))
499 def check_functions(of
, title
, bankspec
, fns
, pins
, required
, eint
, pwm
,
502 pins
= deepcopy(pins
)
503 if descriptions
is None:
510 of
.write("# Pinmap for %s\n\n" % title
)
512 print "fn_idx", fnidx
514 print "fnspec", pins
.fnspec
.keys()
515 print "required", required
516 for name
in required
:
517 of
.write("## %s\n\n" % name
)
518 if descriptions
and name
in descriptions
:
519 of
.write("%s\n\n" % descriptions
[name
])
521 name
= name
.split(':')
523 findbank
= name
[0][0]
524 findmux
= int(name
[0][1:])
530 name
= name
.split('/')
541 if not fname
.startswith(name
):
543 for k
in pins
.fnspec
.keys():
544 if fname
.startswith(k
):
545 fk
= list(pins
.fnspec
[k
].keys())
548 #print fname, fn, dir(fn)
550 count
= len(fn
.pingroup
)
551 for pin
, mux
, bank
in fns
[fname
]:
552 if findbank
is not None:
557 pin_
= pin
+ bankspec
[bank
]
559 pinfound
[pin_
] = (fname
, pin_
, bank
, pin
, mux
)
561 pinidx
= sorted(pinfound
.keys())
565 print ("pinidx", pinidx
)
567 fname
, pin_
, bank
, pin
, mux
= pinfound
[pin_
]
571 if len(found
) > count
:
575 of
.write("* %s %d %s%d/%d\n" % (fname
, pin_
, bank
, pin
, mux
))
578 if removedcount
!= count
:
580 print "no match between required and available pins"
582 print ("not all found", name
, removedcount
, count
, title
, found
,
584 print ("pins found", pinfound
)
591 for name
in descriptions
.keys():
592 if not name
.startswith('GPIO'):
600 of
.write("## GPIO\n\n")
606 if descriptions
and fname
in descriptions
:
607 desc
= ': %s' % descriptions
[fname
]
610 pin_
= pin
+ bankspec
[bank
]
615 of
.write("* %-8s %d %s%-2d %s\n" % (fname
, pin_
, bank
, pin
, desc
))
619 display_group(of
, bankspec
, "EINT", eint
, fns
, pins
, descriptions
)
621 display_group(of
, bankspec
, "PWM", pwm
, fns
, pins
, descriptions
)
623 of
.write("## Unused Pinouts (spare as GPIO) for '%s'\n\n" % title
)
624 if descriptions
and 'GPIO' in descriptions
:
625 of
.write("%s\n\n" % descriptions
['GPIO'])
632 def display_group(of
, bankspec
, title
, todisplay
, fns
, pins
, descriptions
):
633 of
.write("## %s\n\n" % title
)
636 for fname
in todisplay
:
638 if descriptions
and fname
in descriptions
:
639 desc
= ': %s' % descriptions
[fname
]
640 fname
= fname
.split(':')
642 findbank
= fname
[0][0]
643 findmux
= int(fname
[0][1:])
649 for (pin
, mux
, bank
) in fns
[fname
]:
650 if findbank
is not None:
657 pin_
= pin
+ bankspec
[bank
]
662 of
.write("* %s %d %s%d/%d %s\n" %
663 (fname
, pin_
, bank
, pin
, mux
, desc
))
667 def display_fixed(of
, fixed
, offs
):
669 fkeys
= sorted(fixed
.keys())
672 for pin
, k
in enumerate(fkeys
):
673 of
.write("## %s\n\n" % k
)
676 for name
in fixed
[k
]:
680 if prevname
[:2] == name
[:2] and linecount
!= 0:
681 of
.write(" %s" % name
)
686 of
.write("* %d: %d %s" % (pin_
, pin
, name
))
688 res
.append((pin_
, name
))