submodule update
[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 def load_pinouts(chipname=None):
26 """load_pinouts - loads the JSON-formatted dictionary of a chip spec
27
28 note: this works only when pinmux is a correctly-initialised git submodule
29 and when the spec has been actually generated. see Makefile "make mkpinmux"
30 """
31
32 # default pinouts for now: ls180
33 if chipname is None:
34 chipname = 'ls180'
35
36 # load JSON-formatted pad info from pinmux
37 pth = os.path.abspath(__file__)
38 pth = os.path.split(pth)[0]
39
40 # path is relative to this filename, in the pinmux submodule
41 fname = "%s/../../../pinmux/%s/litex_pinpads.json" % (pth, chipname)
42 with open(fname) as f:
43 txt = f.read()
44
45 # decode the json, strip unicode formatting (has to be recursive)
46 chip = json.loads(txt, object_hook=_byteify)
47 chip = _byteify(chip, ignore_dicts=True)
48
49 return chip
50
51 if __name__ == '__main__':
52 if sys.argv == 2:
53 chipname = sys.argv[1]
54 else:
55 chipname = None
56 chip = load_pinouts(chipname)
57 for k, v in chip.items():
58 print ("\n****", k, "****")
59 pprint(v)