96843ce11764d34f795d20fe3557f5486ce1beca
[pinmux.git] / src / spec / gen.py
1 import os
2 import os.path
3 from spec.interfaces import Pinouts
4
5
6 def specgen(of, pth, pinouts, bankspec, muxwidths, pinbanks, fixedpins,
7 fastbus):
8 """ generates a specification of pinouts (tsv files)
9 for reading in by pinmux.
10
11 files generated:
12 * interfaces.txt - contains name and number of interfaces
13 * {interfacename}.txt - contains name of pin, type, and bus
14
15 type may be in, out or inout.
16 if type is "inout" then a THIRD optional parameter of type
17 "bus" indicates whether the bus is ganged together. in
18 future this may be "bus1", "bus2" and so on if an interface
19 contains more than one ganged group.
20
21 basically if the function needs to control whether a group
22 of pins shall be switched from input to output (as opposed
23 to the *pinmux* via user control deciding that), bus is
24 the way to indicate it.
25 """
26 pth = pth or ''
27 #print bankspec.keys()
28 #print fixedpins.keys()
29 #print pinouts.ganged.items()
30 if not os.path.exists(pth):
31 os.makedirs(pth)
32 with open(os.path.join(pth, 'interfaces.txt'), 'w') as f:
33 for k in pinouts.fnspec.keys():
34 s = pinouts.fnspec[k]
35 line = [k.lower(), str(len(s))]
36 for b in fastbus:
37 if b.startswith(k.lower()):
38 line.append(b)
39 line = '\t'.join(line)
40 f.write("%s\n" % line)
41 s0 = s[list(s.keys())[0]] # hack, take first
42 gangedgroup = pinouts.ganged[k]
43 with open(os.path.join(pth, '%s.txt' % k.lower()), 'w') as g:
44 if len(s0.pingroup) == 1: # only one function, grouped higher
45 for ks in s.keys(): # grouped by interface
46 assert False, "TODO, single-function"
47 fntype = 'inout' # XXX TODO
48 k = s[ks].suffix
49 k_ = k.lower()
50 g.write("%s\t%s\n" % (k_, fntype))
51 else:
52 for pinname in s0.pingroup:
53 fntype = s0.fntype.get(pinname, 'inout')
54 pn = pinname.lower()
55 g.write("%s\t%s" % (pn, fntype))
56 if fntype == 'inout' and pinname in gangedgroup:
57 g.write("\tbus")
58 g.write("\n")
59
60 pks = sorted(pinouts.keys())
61
62 # truly dreadful way to work out the max mux size...
63 muxsz = 0
64 for k in pks:
65 for m in pinouts[k].keys():
66 muxsz = max(muxsz, m + 1)
67
68 # write out the mux...
69 with open(os.path.join(pth, 'pinmap.txt'), 'w') as g:
70 for k in pks:
71 res = [str(k)]
72 # append pin mux
73 for midx in range(muxsz):
74 if midx in pinouts[k]:
75 fname = pinouts[k][midx][0]
76 else:
77 fname = ''
78 res.append(fname.lower())
79 g.write('\t'.join(res) + '\n')
80
81 # ... and the dedicated pins
82 with open(os.path.join(pth, 'fixedpins.txt'), 'w') as g:
83 for p in fixedpins:
84 p = map(str, p)
85 p = map(str.lower, p)
86 g.write('\t'.join(p) + '\n')
87
88 # lists bankspec, shows where the pin-numbers *start*
89 of.write("# Pin Bank starting points and lengths\n\n")
90 with open(os.path.join(pth, 'pinspec.txt'), 'w') as g:
91 keys = sorted(bankspec.keys())
92 for bank in keys:
93 pinstart = bankspec[bank]
94 wid = muxwidths[bank]
95 of.write("* %s %d %d %d\n" % (bank, pinstart, pinbanks[bank], wid))
96 g.write("%s\t%d\t%d\t%d\n" % (bank, pinstart, pinbanks[bank], wid))