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