4 from pprint
import pprint
6 def _byteify(data
, ignore_dicts
= False):
7 # if this is a unicode string, return its string representation
9 if isinstance(data
, unicode):
10 return data
.encode('utf-8')
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
26 def get_pinspecs(chipname
=None, subset
=None):
27 chip
= load_pinouts(chipname
)
28 pinmap
= chip
['pins.map']
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
:
37 pname
= '%s_%s' % (name
, pin
[:-1])
39 newpin
= pinmap
[pname
][2:]
40 newpin
= '_'.join(newpin
.split("_")[1:])
41 pin
= newpin
+ pin
[-1]
43 specs
['%s%s' % (k
, num
)] = pins
47 def load_pinouts(chipname
=None):
48 """load_pinouts - loads the JSON-formatted dictionary of a chip spec
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"
54 # default pinouts for now: ls180
58 # load JSON-formatted pad info from pinmux
59 pth
= os
.path
.abspath(__file__
)
60 pth
= os
.path
.split(pth
)[0]
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
:
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)
73 if __name__
== '__main__':
75 chipname
= sys
.argv
[1]
78 chip
= load_pinouts(chipname
)
79 for k
, v
in chip
.items():
80 print ("\n****", k
, "****")