significant reorg of the litex pinspecs to use pinmux JSON files
[soc.git] / src / soc / config / pinouts.py
1 import os
2 import sys
3 import json
4 from pprint import pprint
5
6 def _byteify(data, ignore_dicts = False):
7 # if this is a unicode string, return its string representation
8 try:
9 if isinstance(data, unicode):
10 return data.encode('utf-8')
11 except NameError:
12 return data
13 # if this is a list of values, return list of byteified values
14 if isinstance(data, list):
15 return [ _byteify(item, ignore_dicts=True) for item in data ]
16 # if this is a dictionary, return dictionary of byteified keys and values
17 # but only if we haven't already byteified it
18 if isinstance(data, dict) and not ignore_dicts:
19 return dict((_byteify(key, ignore_dicts=True),
20 _byteify(value, ignore_dicts=True))
21 for key, value in data.iteritems())
22 # if it's anything else, return it in its original form
23 return data
24
25
26 def get_pinspecs(chipname=None, subset=None):
27 chip = load_pinouts(chipname)
28 pinmap = chip['pins.map']
29 specs = {}
30 for k, bus in chip['pins.specs'].items():
31 k, num = k.lower().split(":")
32 name = '%s%s' % (k, num)
33 if subset is None or name in subset:
34 pins = []
35 for pin in bus:
36 pin = pin.lower()
37 pname = '%s_%s' % (name, pin[:-1])
38 if pname in pinmap:
39 newpin = pinmap[pname][2:]
40 newpin = '_'.join(newpin.split("_")[1:])
41 pin = newpin + pin[-1]
42 pins.append(pin)
43 specs['%s%s' % (k, num)] = pins
44 return specs
45
46
47 def load_pinouts(chipname=None):
48 """load_pinouts - loads the JSON-formatted dictionary of a chip spec
49
50 note: this works only when pinmux is a correctly-initialised git submodule
51 and when the spec has been actually generated. see Makefile "make mkpinmux"
52 """
53
54 # default pinouts for now: ls180
55 if chipname is None:
56 chipname = 'ls180'
57
58 # load JSON-formatted pad info from pinmux
59 pth = os.path.abspath(__file__)
60 pth = os.path.split(pth)[0]
61
62 # path is relative to this filename, in the pinmux submodule
63 fname = "%s/../../../pinmux/%s/litex_pinpads.json" % (pth, chipname)
64 with open(fname) as f:
65 txt = f.read()
66
67 # decode the json, strip unicode formatting (has to be recursive)
68 chip = json.loads(txt, object_hook=_byteify)
69 chip = _byteify(chip, ignore_dicts=True)
70
71 return chip
72
73 if __name__ == '__main__':
74 if sys.argv == 2:
75 chipname = sys.argv[1]
76 else:
77 chipname = None
78 chip = load_pinouts(chipname)
79 for k, v in chip.items():
80 print ("\n****", k, "****")
81 pprint(v)