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"
12 c4m_drawing
= cwd
+ "/c4mlogo.png"
14 def bond_int_to_ext(pin
, bank
):
15 """ note that internal numbering is 0-31 whereas the DISPLAY internal
16 numbering is 1-32. this uses the INTERNAL numbering.
20 outer ring numbers: the python list order of the outer pads
21 middle numbers: the package number wires (plus side they are on)
22 inner numbers: the IO pad *python internal (0-31)* numbers plus side
25 N102 N101 N100 99 N 68 N67 N66 N65
26 W29 W30 W31 N0 N N31 E29 E30 E31
32 W2 W1 W0 S0 S S31 E0 E1 E2
33 S1 S2 S3 4 S 35 S36 S37 S38
37 # returns side, order-on-the-side, pin number
39 return 'N', pin
+3, 99-pin
41 return 'S', pin
+3, 4+pin
43 if pin
>= 29: # 29, 30, 31
44 return 'N', pin
-29, 100+(31-pin
)
45 if pin
<= 2: # 0, 1, 2
46 return 'S', 2-pin
, 3-pin
47 return 'W', 28-pin
, 103+(28-pin
)
50 return 'N', 35+(31-pin
), 67-(31-pin
)
52 return 'S', pin
+35, 36+pin
53 return 'E', 28-pin
, 39+(pin
-3)
56 def create_sv(fname
, pins
):
57 """unsophisticated drawer of an SVG
63 print ("WARNING, no SVG image, not producing image %s" % fname
)
66 # create internal to external map
67 bondmap
= {'N': {}, 'S': {}, 'E': {}, 'W': {} }
68 padside
= {'pads.north': 'N', 'pads.east': 'E', 'pads.south': 'S',
71 for pinpad
, bank
in padside
.items():
72 sidepad
[bank
] = pinpad
73 for ipin
in range(len(pins
[pinpad
])):
74 eside
, enum
, epinnum
= bond_int_to_ext(ipin
, bank
)
75 bondmap
[eside
][enum
] = (epinnum
, ipin
, bank
)
76 with
open("/tmp/bondmap.txt", "w") as f
:
77 for k
,v
in bondmap
.items():
79 for enum
, (epinnum
, ipin
, bank
) in v
.items():
80 f
.write(" %d %d -> %s %d\n" % (enum
, epinnum
, bank
, ipin
))
83 outerscale
= scale
* 2.0
85 width
= len(pins
['pads.north']) * scale
86 height
= len(pins
['pads.east']) * scale
87 woffs
= scale
*40#-width/2
88 hoffs
= scale
*40#-height/2
90 nepads
= bondmap
['N'].keys()
92 wepads
= bondmap
['W'].keys()
94 eepads
= bondmap
['E'].keys()
96 sepads
= bondmap
['S'].keys()
99 owoffs
= woffs
+ (width
/2) - len(nepads
)/2 * outerscale
100 ohoffs
= hoffs
+ (height
/2) - len(wepads
)/2 * outerscale
102 dwg
= svgwrite
.Drawing(fname
, profile
='full',
103 size
=(width
+scale
*85, height
+scale
*80))
106 dwg
.add(dwg
.rect((owoffs
-scale
*2.5, ohoffs
-scale
*4.5),
107 (len(nepads
)*outerscale
+scale
*9,
108 len(wepads
)*outerscale
+scale
*13),
110 stroke
=svgwrite
.rgb(0, 128, 0, '%'),
111 stroke_width
=scale
/5.0))
114 dwg
.add(dwg
.rect((woffs
-scale
*2, hoffs
-scale
*2),
115 (width
+scale
*6, height
+scale
*6),
116 stroke
=svgwrite
.rgb(16, 255, 16, '%'),
117 stroke_width
=scale
/10.0))
119 # record the inner line (iopad) position so that the outer one can
121 innerpos
= {'N': {}, 'S': {}, 'E': {}, 'W': {} }
123 # create the inner diagram
124 for i
, pin
in enumerate(pins
['pads.west']):
125 ht
= hoffs
+ height
- (i
* scale
) + scale
*0.5
126 endline
= (woffs
-scale
*4.5, ht
-scale
*0.5)
127 innerpos
['W'][i
] = endline
128 dwg
.add(dwg
.line((woffs
-scale
*2, ht
-scale
*0.5),
130 stroke
=svgwrite
.rgb(16, 255, 16, '%'),
131 stroke_width
=scale
/10.0))
132 dwg
.add(dwg
.text(pin
.upper(), insert
=(woffs
-scale
*14, ht
),
134 dwg
.add(dwg
.text("W%d" % (i
+1), insert
=(woffs
-scale
*1.5, ht
),
137 for i
, pin
in enumerate(pins
['pads.east']):
138 ht
= hoffs
+ height
- (i
* scale
) + scale
*0.5
139 wd
= width
+ woffs
+ scale
*2
140 endline
= (wd
+scale
*4.5, ht
-scale
*0.5)
141 innerpos
['E'][i
] = endline
142 dwg
.add(dwg
.line((wd
+scale
*2, ht
-scale
*0.5),
144 stroke
=svgwrite
.rgb(16, 255, 16, '%'),
145 stroke_width
=scale
/10.0))
146 dwg
.add(dwg
.text(pin
.upper(), insert
=(wd
+scale
*5, ht
-scale
*0.25),
148 dwg
.add(dwg
.text("E%d" % (i
+1), insert
=(wd
, ht
-scale
*0.25),
151 for i
, pin
in enumerate(pins
['pads.north']):
152 wd
= woffs
+ i
* scale
+ scale
*1.5
153 endline
= (wd
, hoffs
-scale
*4.5)
154 innerpos
['N'][i
] = endline
155 dwg
.add(dwg
.line((wd
, hoffs
-scale
*2),
157 stroke
=svgwrite
.rgb(16, 255, 16, '%'),
158 stroke_width
=scale
/10.0))
159 pos
=(wd
, hoffs
-scale
*5.0)
160 txt
= dwg
.text(pin
.upper(), insert
=pos
, fill
='black')
163 pos
=(wd
+scale
*0.25, hoffs
-scale
*0.25)
164 txt
= dwg
.text("N%d" % (i
+1), insert
=pos
, fill
='white')
168 for i
, pin
in enumerate(pins
['pads.south']):
169 wd
= woffs
+ i
* scale
+ scale
*1.5
170 ht
= hoffs
+ height
+ scale
*2
171 endline
= (wd
, ht
+scale
*4.5)
172 innerpos
['S'][i
] = endline
173 dwg
.add(dwg
.line((wd
, ht
+scale
*2),
175 stroke
=svgwrite
.rgb(16, 255, 16, '%'),
176 stroke_width
=scale
/10.0))
177 pos
=(wd
-scale
*0.25, ht
+scale
*5.0)
178 txt
= dwg
.text(pin
.upper(), insert
=pos
, fill
='black')
181 pos
=(wd
-scale
*0.25, ht
+scale
*0.25)
182 txt
= dwg
.text("S%d" % (i
+1), insert
=pos
, fill
='white')
188 (epinnum
, ipin
, bank
) = pad
= bondmap
['N'][i
]
189 wd
= owoffs
+ i
* outerscale
+ outerscale
*1.5
190 endline
= (wd
, ohoffs
-outerscale
*2)
191 startline
= innerpos
[bank
][ipin
]
192 dwg
.add(dwg
.line(startline
,
194 stroke
=svgwrite
.rgb(255, 16, 16, '%'),
195 stroke_width
=scale
/10.0))
196 pin
= pins
[sidepad
[bank
]][ipin
]
197 pos
=(wd
, ohoffs
-outerscale
*4.0)
198 txt
= dwg
.text("%s (%s%d)" % (pin
.upper(), bank
, ipin
+1),
199 insert
=pos
, fill
='black')
202 pos
=(wd
, ohoffs
-outerscale
*2.5)
203 txt
= dwg
.text("%d N" % epinnum
, insert
=pos
, fill
='blue')
209 (epinnum
, ipin
, bank
) = pad
= bondmap
['W'][i
]
210 ht
= ohoffs
+ (i
* outerscale
) + outerscale
*1.5
211 endline
= (owoffs
+outerscale
*0.5, ht
)
212 startline
= innerpos
[bank
][ipin
]
213 dwg
.add(dwg
.line(startline
,
215 stroke
=svgwrite
.rgb(255, 16, 16, '%'),
216 stroke_width
=scale
/10.0))
217 pin
= pins
[sidepad
[bank
]][ipin
]
218 pos
= (owoffs
-outerscale
*6.0, ht
)
219 dwg
.add(dwg
.text("%s (%s%d)" % (pin
.upper(), bank
, ipin
+1),
222 pos
= (owoffs
-outerscale
*1.0, ht
)
223 dwg
.add(dwg
.text("%d W" % epinnum
, insert
=pos
,
228 (epinnum
, ipin
, bank
) = pad
= bondmap
['S'][i
]
229 wd
= owoffs
+ i
* outerscale
+ outerscale
*1.5
230 ht
= ohoffs
+ len(wepads
)*outerscale
+ outerscale
*4
232 startline
= innerpos
[bank
][ipin
]
233 dwg
.add(dwg
.line(startline
,
235 stroke
=svgwrite
.rgb(255, 16, 16, '%'),
236 stroke_width
=scale
/10.0))
237 pin
= pins
[sidepad
[bank
]][ipin
]
238 pos
=(wd
-outerscale
*0.25, ht
+outerscale
*1.5)
239 txt
= dwg
.text("%s (%s%d)" % (pin
.upper(), bank
, ipin
+1),
240 insert
=pos
, fill
='black')
243 pos
=(wd
-outerscale
*0.25, ht
+outerscale
*0.5)
244 txt
= dwg
.text("%d S" % epinnum
, insert
=pos
, fill
='blue')
250 (epinnum
, ipin
, bank
) = pad
= bondmap
['E'][i
]
251 ht
= ohoffs
+ (i
* outerscale
) + outerscale
*1.5
252 wd
= owoffs
+len(nepads
)*outerscale
+ outerscale
*1
253 endline
= (wd
+outerscale
*0.5, ht
)
254 startline
= innerpos
[bank
][ipin
]
255 dwg
.add(dwg
.line(startline
,
257 stroke
=svgwrite
.rgb(255, 16, 16, '%'),
258 stroke_width
=scale
/10.0))
259 pin
= pins
[sidepad
[bank
]][ipin
]
260 pos
= (wd
+outerscale
*2.5, ht
)
261 dwg
.add(dwg
.text("%s (%s%d)" % (pin
.upper(), bank
, ipin
+1),
264 pos
= (wd
+outerscale
*1.0, ht
)
265 dwg
.add(dwg
.text("%d E" % epinnum
, insert
=pos
,
268 # add QFP pack image in top-right
269 image_data
= open(pack_drawing
, "rb").read()
270 encoded
= base64
.b64encode(image_data
).decode()
271 data
= 'data:image/png;base64,{}'.format(encoded
)
273 leads
= svgwrite
.image
.Image(data
, pos
,
276 dwg
.add(dwg
.text("GREATEK QFP128L",
280 dwg
.add(dwg
.text("D/W J1-03128-001",
286 # add QFP lead image in centre
287 image_data
= open(lead_drawing
, "rb").read()
288 encoded
= base64
.b64encode(image_data
).decode()
289 data
= 'data:image/png;base64,{}'.format(encoded
)
290 pos
=(woffs
+scale
*3.5, hoffs
+scale
*3.5)
291 leads
= svgwrite
.image
.Image(data
, pos
,
293 leads
.rotate(-90, (pos
[0]+200, pos
[1]+200))
296 dwg
.add(dwg
.text("GREATEK ELECTRONICS INC.",
297 insert
=(woffs
+width
/2-scale
*5, woffs
+height
/2-scale
*6),
299 dwg
.add(dwg
.text("INNER LEAD DRAWING",
300 insert
=(woffs
+width
/2-scale
*5, woffs
+height
/2-scale
*5),
302 dwg
.add(dwg
.text("QFP 128L OPEN STAMPING",
303 insert
=(woffs
+width
/2-scale
*5, woffs
+height
/2-scale
*4),
305 dwg
.add(dwg
.text("BODY 14x20x2.75mm",
306 insert
=(woffs
+width
/2-scale
*5, woffs
+height
/2-scale
*3),
308 dwg
.add(dwg
.text("L/F PAD SIZE 236x236mil^2",
309 insert
=(woffs
+width
/2-scale
*5, woffs
+height
/2-scale
*2),
313 image_data
= open(c4m_drawing
, "rb").read()
314 encoded
= base64
.b64encode(image_data
).decode()
315 data
= 'data:image/png;base64,{}'.format(encoded
)
316 pos
=(woffs
+scale
*0.0, hoffs
+height
-scale
*1.5)
317 leads
= svgwrite
.image
.Image(data
, pos
,
322 dwg
.add(dwg
.text("Libre-SOC ls180 QFP-128",
323 insert
=(woffs
+width
/2-scale
*5, woffs
+height
/2),
325 dwg
.add(dwg
.text("In collaboration with LIP6.fr",
326 insert
=(woffs
+width
/2-scale
*5, woffs
+height
/2+scale
),
328 dwg
.add(dwg
.text("Cell Libraries by Chips4Makers",
329 insert
=(woffs
+width
/2-scale
*5, woffs
+height
/2+scale
*2),
331 dwg
.add(dwg
.text("IMEC TSMC 180nm",
332 insert
=(woffs
+width
/2-scale
*5, woffs
+height
/2+scale
*3),
334 dwg
.add(dwg
.text("RED Semiconductor",
335 insert
=(woffs
+width
/2-scale
*5, woffs
+height
/2+scale
*4),
338 # add package marking circles
339 pos
= (owoffs
-outerscale
*0, ohoffs
+len(wepads
)*outerscale
+outerscale
*2.5)
340 dwg
.add(dwg
.circle(pos
, scale
*2,
342 stroke
=svgwrite
.rgb(16, 16, 16, '%'),
343 stroke_width
=scale
/5.0))
344 dwg
.add(dwg
.circle(pos
, scale
*1,
346 stroke
=svgwrite
.rgb(255, 16, 16, '%'),
347 stroke_width
=scale
/5.0))
348 pos
= (owoffs
+len(nepads
)*outerscale
+outerscale
*2, ohoffs
-outerscale
*0.5)
349 dwg
.add(dwg
.circle(pos
, scale
*2,
351 stroke
=svgwrite
.rgb(16, 16, 16, '%'),
352 stroke_width
=scale
/5.0))
358 def display(of
, pins
, banksel
=None, muxwidth
=4):
360 | Pin | Mux0 | Mux1 | Mux2 | Mux3 |
361 | --- | ----------- | ----------- | ----------- | ----------- |
363 pinidx
= sorted(pins
.keys())
365 pdata
= pins
.get(pin
)
368 for mux
in range(muxwidth
):
371 name
, bank
= pdata
[mux
]
376 res
= '| %3d |' % pin
377 for mux
in range(muxwidth
):
381 name
, bank
= pdata
[mux
]
382 res
+= " %s %-9s |" % (bank
, name
)
383 of
.write("%s\n" % res
)
389 if not f
.startswith('FB_'):
393 return f2
[0], int(f2
[1])
396 while f
and not f
[0].isdigit():
399 return a
, int(f
) if f
else None
411 def find_fn(fname
, names
):
413 if fname
.startswith(n
):
416 def map_name(pinmap
, fn
, fblower
, pin
, rename
):
418 if pin
[:-1].isdigit():
419 print "map name digit", pin
, fn
, fblower
420 if fn
in ['PWM', 'EINT', 'VDD', 'VSS']:
421 return fn
.lower() + pin
.lower()
423 return 'gpio' + pin
[1:].lower()
427 pk
= '%s%s_%s' % (fblower
, pin
[0], pin
[:-1])
428 elif pin
[:-1].isdigit() and fn
!= 'EINT':
429 pk
= '%s%s_out' % (fblower
, pin
[:-1])
431 pk
= '%s_%s' % (fblower
, pin
[:-1])
432 print "map name", pk
, fblower
, pinmap
.has_key(pk
)
433 if not pinmap
.has_key(pk
):
435 remapped
= pinmap
[pk
]
436 uscore
= remapped
.find('_')
439 fn
, pin
= remapped
[:uscore
], remapped
[uscore
+1:] + pin
[-1]
442 def python_pindict(of
, pinmap
, pins
, function_names
, dname
, remap
):
445 of
.write("\n%s = OrderedDict()\n" % dname
)
447 for k
, pingroup
in pins
.byspec
.items():
448 (a
, n
) = k
.split(":")
452 of
.write("%s['%s'] = [ " % (dname
, fblower
))
455 for i
, p
in enumerate(pingroup
):
456 name
= map_name(pinmap
, k
[0], fblower
, p
, remap
)
457 res
[fblower
].append(name
)
458 of
.write("'%s', " % name
)
460 if count
== 4 and i
!= len(pingroup
)-1:
464 print " dict %s" % dname
, a
, n
, pingroup
468 def python_dict_fns(of
, pinmap
, pins
, function_names
):
469 of
.write("# auto-generated by Libre-SOC pinmux program: do not edit\n")
470 of
.write("# python src/pinmux_generator.py -v -s {spec} -o {output}\n")
471 of
.write("# use OrderedDict to fix stable order for JTAG Boundary Scan\n")
472 of
.write("from collections import OrderedDict\n")
474 fn_names
= function_names
.keys()
477 fnidx
= list(fns
.keys())
478 fnidx
.sort(key
=fnsplit
)
480 print "python fnames", function_names
481 print "python speckeys", pins
.byspec
.keys()
482 print "python dict fns", dir(pins
.gpio
)
483 print pins
.gpio
.pinfn('', '')
484 print pins
.pwm
.pinfn('', '')
485 print pins
.sdmmc
.pinfn('', '')
486 print "by spec", pins
.byspec
489 pd
= python_pindict(of
, {}, pins
, function_names
, 'pindict', False)
490 ld
= python_pindict(of
, pinmap
, pins
, function_names
, 'litexdict', True)
494 # process results and create name map
495 litexmap
= OrderedDict()
499 for pname
, lname
in zip(pl
, ll
):
500 pname
= "%s_%s" % (k
, pname
[:-1]) # strip direction +/-/*
501 lname
= lname
[:-1] # strip direction +/-/*
502 if k
in ['eint', 'pwm', 'gpio', 'vdd', 'vss']: # sigh
503 lname
= "%s_%s" % (k
, lname
)
504 litexmap
[pname
] = lname
505 print "litexmap", litexmap
506 of
.write("litexmap = {\n")
507 for k
, v
in litexmap
.items():
508 of
.write("\t'%s': '%s',\n" % (k
, v
))
513 def display_fns(of
, bankspec
, pins
, function_names
):
514 fn_names
= function_names
.keys()
516 for (pin
, pdata
) in pins
.items():
517 for mux
in range(0, 4): # skip GPIO for now
520 name
, bank
= pdata
[mux
]
521 assert name
is not None, str(bank
)
524 fns
[name
].append((pin
- bankspec
[bank
], mux
, bank
))
526 fnidx
= list(fns
.keys())
527 fnidx
.sort(key
=fnsplit
)
530 fnbase
= find_fn(fname
, fn_names
)
531 #fblower = fnbase.lower()
532 assert fnbase
in function_names
, "fn %s not in descriptions %s" % \
533 (fname
, str(function_names
.keys()))
534 #print "name", fname, fnbase
535 if fnbase
!= current_fn
:
536 if current_fn
is not None:
538 of
.write("## %s\n\n%s\n\n" % (fnbase
, function_names
[fnbase
]))
540 of
.write("* %-9s :" % fname
)
541 for (pin
, mux
, bank
) in fns
[fname
]:
542 of
.write(" %s%d/%d" % (bank
, pin
, mux
))
548 def check_functions(of
, title
, bankspec
, fns
, pins
, required
, eint
, pwm
,
551 pins
= deepcopy(pins
)
552 if descriptions
is None:
559 of
.write("# Pinmap for %s\n\n" % title
)
561 print "fn_idx", fnidx
563 print "fnspec", pins
.fnspec
.keys()
564 print "required", required
565 for name
in required
:
566 of
.write("## %s\n\n" % name
)
567 if descriptions
and name
in descriptions
:
568 of
.write("%s\n\n" % descriptions
[name
])
570 name
= name
.split(':')
572 findbank
= name
[0][0]
573 findmux
= int(name
[0][1:])
579 name
= name
.split('/')
590 if not fname
.startswith(name
):
592 for k
in pins
.fnspec
.keys():
593 if fname
.startswith(k
):
594 fk
= list(pins
.fnspec
[k
].keys())
597 #print fname, fn, dir(fn)
599 count
= len(fn
.pingroup
)
600 for pin
, mux
, bank
in fns
[fname
]:
601 if findbank
is not None:
606 pin_
= pin
+ bankspec
[bank
]
608 pinfound
[pin_
] = (fname
, pin_
, bank
, pin
, mux
)
610 pinidx
= sorted(pinfound
.keys())
614 print ("pinidx", pinidx
)
616 fname
, pin_
, bank
, pin
, mux
= pinfound
[pin_
]
620 if len(found
) > count
:
624 of
.write("* %s %d %s%d/%d\n" % (fname
, pin_
, bank
, pin
, mux
))
627 if removedcount
!= count
:
629 print "no match between required and available pins"
631 print ("not all found", name
, removedcount
, count
, title
, found
,
633 print ("pins found", pinfound
)
640 for name
in descriptions
.keys():
641 if not name
.startswith('GPIO'):
649 of
.write("## GPIO\n\n")
655 if descriptions
and fname
in descriptions
:
656 desc
= ': %s' % descriptions
[fname
]
659 pin_
= pin
+ bankspec
[bank
]
664 of
.write("* %-8s %d %s%-2d %s\n" % (fname
, pin_
, bank
, pin
, desc
))
668 display_group(of
, bankspec
, "EINT", eint
, fns
, pins
, descriptions
)
670 display_group(of
, bankspec
, "PWM", pwm
, fns
, pins
, descriptions
)
672 of
.write("## Unused Pinouts (spare as GPIO) for '%s'\n\n" % title
)
673 if descriptions
and 'GPIO' in descriptions
:
674 of
.write("%s\n\n" % descriptions
['GPIO'])
681 def display_group(of
, bankspec
, title
, todisplay
, fns
, pins
, descriptions
):
682 of
.write("## %s\n\n" % title
)
685 for fname
in todisplay
:
687 if descriptions
and fname
in descriptions
:
688 desc
= ': %s' % descriptions
[fname
]
689 fname
= fname
.split(':')
691 findbank
= fname
[0][0]
692 findmux
= int(fname
[0][1:])
698 for (pin
, mux
, bank
) in fns
[fname
]:
699 if findbank
is not None:
706 pin_
= pin
+ bankspec
[bank
]
711 of
.write("* %s %d %s%d/%d %s\n" %
712 (fname
, pin_
, bank
, pin
, mux
, desc
))
716 def display_fixed(of
, fixed
, offs
):
718 fkeys
= sorted(fixed
.keys())
721 for pin
, k
in enumerate(fkeys
):
722 of
.write("## %s\n\n" % k
)
725 for name
in fixed
[k
]:
729 if prevname
[:2] == name
[:2] and linecount
!= 0:
730 of
.write(" %s" % name
)
735 of
.write("* %d: %d %s" % (pin_
, pin
, name
))
737 res
.append((pin_
, name
))