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
)
44 if pin
<= 2: # 0, 1, 2
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+(pin
-3)
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 nepads
= bondmap
['N'].keys()
91 wepads
= bondmap
['W'].keys()
93 eepads
= bondmap
['E'].keys()
95 sepads
= bondmap
['S'].keys()
98 owoffs
= woffs
+ (width
/2) - len(nepads
)/2 * outerscale
99 ohoffs
= hoffs
+ (height
/2) - len(wepads
)/2 * outerscale
101 dwg
= svgwrite
.Drawing(fname
, profile
='full',
102 size
=(width
+scale
*85, height
+scale
*80))
105 dwg
.add(dwg
.rect((owoffs
-scale
*2.5, ohoffs
-scale
*4.5),
106 (len(nepads
)*outerscale
+scale
*9,
107 len(wepads
)*outerscale
+scale
*13),
109 stroke
=svgwrite
.rgb(0, 128, 0, '%'),
110 stroke_width
=scale
/5.0))
113 dwg
.add(dwg
.rect((woffs
-scale
*2, hoffs
-scale
*2),
114 (width
+scale
*6, height
+scale
*6),
115 stroke
=svgwrite
.rgb(16, 255, 16, '%'),
116 stroke_width
=scale
/10.0))
118 # record the inner line (iopad) position so that the outer one can
120 innerpos
= {'N': {}, 'S': {}, 'E': {}, 'W': {} }
122 # create the inner diagram
123 for i
, pin
in enumerate(pins
['pads.west']):
124 ht
= hoffs
+ height
- (i
* scale
) + scale
*0.5
125 endline
= (woffs
-scale
*4.5, ht
-scale
*0.5)
126 innerpos
['W'][i
] = endline
127 dwg
.add(dwg
.line((woffs
-scale
*2, ht
-scale
*0.5),
129 stroke
=svgwrite
.rgb(16, 255, 16, '%'),
130 stroke_width
=scale
/10.0))
131 dwg
.add(dwg
.text(pin
.upper(), insert
=(woffs
-scale
*14, ht
),
133 dwg
.add(dwg
.text("W%d" % (i
+1), insert
=(woffs
-scale
*1.5, ht
),
136 for i
, pin
in enumerate(pins
['pads.east']):
137 ht
= hoffs
+ height
- (i
* scale
) + scale
*0.5
138 wd
= width
+ woffs
+ scale
*2
139 endline
= (wd
+scale
*4.5, ht
-scale
*0.5)
140 innerpos
['E'][i
] = endline
141 dwg
.add(dwg
.line((wd
+scale
*2, ht
-scale
*0.5),
143 stroke
=svgwrite
.rgb(16, 255, 16, '%'),
144 stroke_width
=scale
/10.0))
145 dwg
.add(dwg
.text(pin
.upper(), insert
=(wd
+scale
*5, ht
-scale
*0.25),
147 dwg
.add(dwg
.text("E%d" % (i
+1), insert
=(wd
, ht
-scale
*0.25),
150 for i
, pin
in enumerate(pins
['pads.north']):
151 wd
= woffs
+ i
* scale
+ scale
*1.5
152 endline
= (wd
, hoffs
-scale
*4.5)
153 innerpos
['N'][i
] = endline
154 dwg
.add(dwg
.line((wd
, hoffs
-scale
*2),
156 stroke
=svgwrite
.rgb(16, 255, 16, '%'),
157 stroke_width
=scale
/10.0))
158 pos
=(wd
, hoffs
-scale
*5.0)
159 txt
= dwg
.text(pin
.upper(), insert
=pos
, fill
='black')
162 pos
=(wd
+scale
*0.25, hoffs
-scale
*0.25)
163 txt
= dwg
.text("N%d" % (i
+1), insert
=pos
, fill
='white')
167 for i
, pin
in enumerate(pins
['pads.south']):
168 wd
= woffs
+ i
* scale
+ scale
*1.5
169 ht
= hoffs
+ height
+ scale
*2
170 endline
= (wd
, ht
+scale
*4.5)
171 innerpos
['S'][i
] = endline
172 dwg
.add(dwg
.line((wd
, ht
+scale
*2),
174 stroke
=svgwrite
.rgb(16, 255, 16, '%'),
175 stroke_width
=scale
/10.0))
176 pos
=(wd
-scale
*0.25, ht
+scale
*5.0)
177 txt
= dwg
.text(pin
.upper(), insert
=pos
, fill
='black')
180 pos
=(wd
-scale
*0.25, ht
+scale
*0.25)
181 txt
= dwg
.text("S%d" % (i
+1), insert
=pos
, fill
='white')
187 (epinnum
, ipin
, bank
) = pad
= bondmap
['N'][i
]
188 wd
= owoffs
+ i
* outerscale
+ outerscale
*1.5
189 endline
= (wd
, ohoffs
-outerscale
*2)
190 startline
= innerpos
[bank
][ipin
]
191 dwg
.add(dwg
.line(startline
,
193 stroke
=svgwrite
.rgb(255, 16, 16, '%'),
194 stroke_width
=scale
/10.0))
195 pin
= pins
[sidepad
[bank
]][ipin
]
196 pos
=(wd
, ohoffs
-outerscale
*4.0)
197 txt
= dwg
.text("%s (%s%d)" % (pin
.upper(), bank
, ipin
+1),
198 insert
=pos
, fill
='black')
201 pos
=(wd
, ohoffs
-outerscale
*2.5)
202 txt
= dwg
.text("%d N" % epinnum
, insert
=pos
, fill
='blue')
208 (epinnum
, ipin
, bank
) = pad
= bondmap
['W'][i
]
209 ht
= ohoffs
+ (i
* outerscale
) + outerscale
*1.5
210 endline
= (owoffs
+outerscale
*0.5, ht
)
211 startline
= innerpos
[bank
][ipin
]
212 dwg
.add(dwg
.line(startline
,
214 stroke
=svgwrite
.rgb(255, 16, 16, '%'),
215 stroke_width
=scale
/10.0))
216 pin
= pins
[sidepad
[bank
]][ipin
]
217 pos
= (owoffs
-outerscale
*6.0, ht
)
218 dwg
.add(dwg
.text("%s (%s%d)" % (pin
.upper(), bank
, ipin
+1),
221 pos
= (owoffs
-outerscale
*1.0, ht
)
222 dwg
.add(dwg
.text("%d W" % epinnum
, insert
=pos
,
227 (epinnum
, ipin
, bank
) = pad
= bondmap
['S'][i
]
228 wd
= owoffs
+ i
* outerscale
+ outerscale
*1.5
229 ht
= ohoffs
+ len(wepads
)*outerscale
+ outerscale
*4
231 startline
= innerpos
[bank
][ipin
]
232 dwg
.add(dwg
.line(startline
,
234 stroke
=svgwrite
.rgb(255, 16, 16, '%'),
235 stroke_width
=scale
/10.0))
236 pin
= pins
[sidepad
[bank
]][ipin
]
237 pos
=(wd
-outerscale
*0.25, ht
+outerscale
*1.5)
238 txt
= dwg
.text("%s (%s%d)" % (pin
.upper(), bank
, ipin
+1),
239 insert
=pos
, fill
='black')
242 pos
=(wd
-outerscale
*0.25, ht
+outerscale
*0.5)
243 txt
= dwg
.text("%d S" % epinnum
, insert
=pos
, fill
='blue')
249 (epinnum
, ipin
, bank
) = pad
= bondmap
['E'][i
]
250 ht
= ohoffs
+ (i
* outerscale
) + outerscale
*1.5
251 wd
= owoffs
+len(nepads
)*outerscale
+ outerscale
*1
252 endline
= (wd
+outerscale
*0.5, ht
)
253 startline
= innerpos
[bank
][ipin
]
254 dwg
.add(dwg
.line(startline
,
256 stroke
=svgwrite
.rgb(255, 16, 16, '%'),
257 stroke_width
=scale
/10.0))
258 pin
= pins
[sidepad
[bank
]][ipin
]
259 pos
= (wd
+outerscale
*2.5, ht
)
260 dwg
.add(dwg
.text("%s (%s%d)" % (pin
.upper(), bank
, ipin
+1),
263 pos
= (wd
+outerscale
*1.0, ht
)
264 dwg
.add(dwg
.text("%d E" % epinnum
, insert
=pos
,
267 # add QFP pack image in top-right
268 image_data
= open(pack_drawing
, "rb").read()
269 encoded
= base64
.b64encode(image_data
).decode()
270 data
= 'data:image/png;base64,{}'.format(encoded
)
272 leads
= svgwrite
.image
.Image(data
, pos
,
275 dwg
.add(dwg
.text("GREATEK QFP128L",
279 dwg
.add(dwg
.text("D/W J1-03128-001",
285 # add QFP lead image in centre
286 image_data
= open(lead_drawing
, "rb").read()
287 encoded
= base64
.b64encode(image_data
).decode()
288 data
= 'data:image/png;base64,{}'.format(encoded
)
289 pos
=(woffs
+scale
*3.5, hoffs
+scale
*3.5)
290 leads
= svgwrite
.image
.Image(data
, pos
,
292 leads
.rotate(-90, (pos
[0]+200, pos
[1]+200))
295 dwg
.add(dwg
.text("GREATEK ELECTRONICS INC.",
296 insert
=(woffs
+width
/2-scale
*5, woffs
+height
/2-scale
*6),
298 dwg
.add(dwg
.text("INNER LEAD DRAWING",
299 insert
=(woffs
+width
/2-scale
*5, woffs
+height
/2-scale
*5),
301 dwg
.add(dwg
.text("QFP 128L OPEN STAMPING",
302 insert
=(woffs
+width
/2-scale
*5, woffs
+height
/2-scale
*4),
304 dwg
.add(dwg
.text("BODY 14x20x2.75mm",
305 insert
=(woffs
+width
/2-scale
*5, woffs
+height
/2-scale
*3),
307 dwg
.add(dwg
.text("L/F PAD SIZE 236x236mil^2",
308 insert
=(woffs
+width
/2-scale
*5, woffs
+height
/2-scale
*2),
312 dwg
.add(dwg
.text("Libre-SOC ls180 QFP-128",
313 insert
=(woffs
+width
/2-scale
*5, woffs
+height
/2),
315 dwg
.add(dwg
.text("In collaboration with LIP6.fr",
316 insert
=(woffs
+width
/2-scale
*5, woffs
+height
/2+scale
),
318 dwg
.add(dwg
.text("Cell Libraries by Chips4Makers",
319 insert
=(woffs
+width
/2-scale
*5, woffs
+height
/2+scale
*2),
321 dwg
.add(dwg
.text("IMEC TSMC 180nm",
322 insert
=(woffs
+width
/2-scale
*5, woffs
+height
/2+scale
*3),
325 # add package marking circles
326 pos
= (owoffs
-outerscale
*0, ohoffs
+len(wepads
)*outerscale
+outerscale
*2.5)
327 dwg
.add(dwg
.circle(pos
, scale
*2,
329 stroke
=svgwrite
.rgb(16, 16, 16, '%'),
330 stroke_width
=scale
/5.0))
331 dwg
.add(dwg
.circle(pos
, scale
*1,
333 stroke
=svgwrite
.rgb(255, 16, 16, '%'),
334 stroke_width
=scale
/5.0))
335 pos
= (owoffs
+len(nepads
)*outerscale
+outerscale
*2, ohoffs
-outerscale
*0.5)
336 dwg
.add(dwg
.circle(pos
, scale
*2,
338 stroke
=svgwrite
.rgb(16, 16, 16, '%'),
339 stroke_width
=scale
/5.0))
345 def display(of
, pins
, banksel
=None, muxwidth
=4):
347 | Pin | Mux0 | Mux1 | Mux2 | Mux3 |
348 | --- | ----------- | ----------- | ----------- | ----------- |
350 pinidx
= sorted(pins
.keys())
352 pdata
= pins
.get(pin
)
355 for mux
in range(muxwidth
):
358 name
, bank
= pdata
[mux
]
363 res
= '| %3d |' % pin
364 for mux
in range(muxwidth
):
368 name
, bank
= pdata
[mux
]
369 res
+= " %s %-9s |" % (bank
, name
)
370 of
.write("%s\n" % res
)
376 if not f
.startswith('FB_'):
380 return f2
[0], int(f2
[1])
383 while f
and not f
[0].isdigit():
386 return a
, int(f
) if f
else None
398 def find_fn(fname
, names
):
400 if fname
.startswith(n
):
403 def map_name(pinmap
, fn
, fblower
, pin
, rename
):
405 if pin
[:-1].isdigit():
406 print "map name digit", pin
, fn
, fblower
407 if fn
in ['PWM', 'EINT', 'VDD', 'VSS']:
408 return fn
.lower() + pin
.lower()
410 return 'gpio' + pin
[1:].lower()
414 pk
= '%s%s_%s' % (fblower
, pin
[0], pin
[:-1])
415 elif pin
[:-1].isdigit() and fn
!= 'EINT':
416 pk
= '%s%s_out' % (fblower
, pin
[:-1])
418 pk
= '%s_%s' % (fblower
, pin
[:-1])
419 print "map name", pk
, fblower
, pinmap
.has_key(pk
)
420 if not pinmap
.has_key(pk
):
422 remapped
= pinmap
[pk
]
423 uscore
= remapped
.find('_')
426 fn
, pin
= remapped
[:uscore
], remapped
[uscore
+1:] + pin
[-1]
429 def python_pindict(of
, pinmap
, pins
, function_names
, dname
, remap
):
432 of
.write("\n%s = OrderedDict()\n" % dname
)
434 for k
, pingroup
in pins
.byspec
.items():
435 (a
, n
) = k
.split(":")
439 of
.write("%s['%s'] = [ " % (dname
, fblower
))
442 for i
, p
in enumerate(pingroup
):
443 name
= map_name(pinmap
, k
[0], fblower
, p
, remap
)
444 res
[fblower
].append(name
)
445 of
.write("'%s', " % name
)
447 if count
== 4 and i
!= len(pingroup
)-1:
451 print " dict %s" % dname
, a
, n
, pingroup
455 def python_dict_fns(of
, pinmap
, pins
, function_names
):
456 of
.write("# auto-generated by Libre-SOC pinmux program: do not edit\n")
457 of
.write("# python src/pinmux_generator.py -v -s {spec} -o {output}\n")
458 of
.write("# use OrderedDict to fix stable order for JTAG Boundary Scan\n")
459 of
.write("from collections import OrderedDict\n")
461 fn_names
= function_names
.keys()
464 fnidx
= list(fns
.keys())
465 fnidx
.sort(key
=fnsplit
)
467 print "python fnames", function_names
468 print "python speckeys", pins
.byspec
.keys()
469 print "python dict fns", dir(pins
.gpio
)
470 print pins
.gpio
.pinfn('', '')
471 print pins
.pwm
.pinfn('', '')
472 print pins
.sdmmc
.pinfn('', '')
473 print "by spec", pins
.byspec
476 pd
= python_pindict(of
, {}, pins
, function_names
, 'pindict', False)
477 ld
= python_pindict(of
, pinmap
, pins
, function_names
, 'litexdict', True)
481 # process results and create name map
482 litexmap
= OrderedDict()
486 for pname
, lname
in zip(pl
, ll
):
487 pname
= "%s_%s" % (k
, pname
[:-1]) # strip direction +/-/*
488 lname
= lname
[:-1] # strip direction +/-/*
489 if k
in ['eint', 'pwm', 'gpio', 'vdd', 'vss']: # sigh
490 lname
= "%s_%s" % (k
, lname
)
491 litexmap
[pname
] = lname
492 print "litexmap", litexmap
493 of
.write("litexmap = {\n")
494 for k
, v
in litexmap
.items():
495 of
.write("\t'%s': '%s',\n" % (k
, v
))
500 def display_fns(of
, bankspec
, pins
, function_names
):
501 fn_names
= function_names
.keys()
503 for (pin
, pdata
) in pins
.items():
504 for mux
in range(0, 4): # skip GPIO for now
507 name
, bank
= pdata
[mux
]
508 assert name
is not None, str(bank
)
511 fns
[name
].append((pin
- bankspec
[bank
], mux
, bank
))
513 fnidx
= list(fns
.keys())
514 fnidx
.sort(key
=fnsplit
)
517 fnbase
= find_fn(fname
, fn_names
)
518 #fblower = fnbase.lower()
519 assert fnbase
in function_names
, "fn %s not in descriptions %s" % \
520 (fname
, str(function_names
.keys()))
521 #print "name", fname, fnbase
522 if fnbase
!= current_fn
:
523 if current_fn
is not None:
525 of
.write("## %s\n\n%s\n\n" % (fnbase
, function_names
[fnbase
]))
527 of
.write("* %-9s :" % fname
)
528 for (pin
, mux
, bank
) in fns
[fname
]:
529 of
.write(" %s%d/%d" % (bank
, pin
, mux
))
535 def check_functions(of
, title
, bankspec
, fns
, pins
, required
, eint
, pwm
,
538 pins
= deepcopy(pins
)
539 if descriptions
is None:
546 of
.write("# Pinmap for %s\n\n" % title
)
548 print "fn_idx", fnidx
550 print "fnspec", pins
.fnspec
.keys()
551 print "required", required
552 for name
in required
:
553 of
.write("## %s\n\n" % name
)
554 if descriptions
and name
in descriptions
:
555 of
.write("%s\n\n" % descriptions
[name
])
557 name
= name
.split(':')
559 findbank
= name
[0][0]
560 findmux
= int(name
[0][1:])
566 name
= name
.split('/')
577 if not fname
.startswith(name
):
579 for k
in pins
.fnspec
.keys():
580 if fname
.startswith(k
):
581 fk
= list(pins
.fnspec
[k
].keys())
584 #print fname, fn, dir(fn)
586 count
= len(fn
.pingroup
)
587 for pin
, mux
, bank
in fns
[fname
]:
588 if findbank
is not None:
593 pin_
= pin
+ bankspec
[bank
]
595 pinfound
[pin_
] = (fname
, pin_
, bank
, pin
, mux
)
597 pinidx
= sorted(pinfound
.keys())
601 print ("pinidx", pinidx
)
603 fname
, pin_
, bank
, pin
, mux
= pinfound
[pin_
]
607 if len(found
) > count
:
611 of
.write("* %s %d %s%d/%d\n" % (fname
, pin_
, bank
, pin
, mux
))
614 if removedcount
!= count
:
616 print "no match between required and available pins"
618 print ("not all found", name
, removedcount
, count
, title
, found
,
620 print ("pins found", pinfound
)
627 for name
in descriptions
.keys():
628 if not name
.startswith('GPIO'):
636 of
.write("## GPIO\n\n")
642 if descriptions
and fname
in descriptions
:
643 desc
= ': %s' % descriptions
[fname
]
646 pin_
= pin
+ bankspec
[bank
]
651 of
.write("* %-8s %d %s%-2d %s\n" % (fname
, pin_
, bank
, pin
, desc
))
655 display_group(of
, bankspec
, "EINT", eint
, fns
, pins
, descriptions
)
657 display_group(of
, bankspec
, "PWM", pwm
, fns
, pins
, descriptions
)
659 of
.write("## Unused Pinouts (spare as GPIO) for '%s'\n\n" % title
)
660 if descriptions
and 'GPIO' in descriptions
:
661 of
.write("%s\n\n" % descriptions
['GPIO'])
668 def display_group(of
, bankspec
, title
, todisplay
, fns
, pins
, descriptions
):
669 of
.write("## %s\n\n" % title
)
672 for fname
in todisplay
:
674 if descriptions
and fname
in descriptions
:
675 desc
= ': %s' % descriptions
[fname
]
676 fname
= fname
.split(':')
678 findbank
= fname
[0][0]
679 findmux
= int(fname
[0][1:])
685 for (pin
, mux
, bank
) in fns
[fname
]:
686 if findbank
is not None:
693 pin_
= pin
+ bankspec
[bank
]
698 of
.write("* %s %d %s%d/%d %s\n" %
699 (fname
, pin_
, bank
, pin
, mux
, desc
))
703 def display_fixed(of
, fixed
, offs
):
705 fkeys
= sorted(fixed
.keys())
708 for pin
, k
in enumerate(fkeys
):
709 of
.write("## %s\n\n" % k
)
712 for name
in fixed
[k
]:
716 if prevname
[:2] == name
[:2] and linecount
!= 0:
717 of
.write(" %s" % name
)
722 of
.write("* %d: %d %s" % (pin_
, pin
, name
))
724 res
.append((pin_
, name
))