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
25 def load_pinouts(chipname
=None):
26 """load_pinouts - loads the JSON-formatted dictionary of a chip spec
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"
32 # default pinouts for now: ls180
36 # load JSON-formatted pad info from pinmux
37 pth
= os
.path
.abspath(__file__
)
38 pth
= os
.path
.split(pth
)[0]
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
:
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)
51 if __name__
== '__main__':
53 chipname
= sys
.argv
[1]
56 chip
= load_pinouts(chipname
)
57 for k
, v
in chip
.items():
58 print ("\n****", k
, "****")