4 from pprint
import pprint
5 from collections
import OrderedDict
8 def _byteify(data
, ignore_dicts
= False):
9 # if this is a unicode string, return its string representation
11 if isinstance(data
, unicode):
12 return data
.encode('utf-8')
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
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
:
39 pname
= '%s_%s' % (name
, pin
[:-1])
41 newpin
= pinmap
[pname
][2:]
42 newpin
= '_'.join(newpin
.split("_")[1:])
43 pin
= newpin
+ pin
[-1]
45 specs
['%s%s' % (k
, num
)] = pins
49 def load_pinouts(chipname
=None):
50 """load_pinouts - loads the JSON-formatted dictionary of a chip spec
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"
56 # default pinouts for now: ls180
60 # load JSON-formatted pad info from pinmux
61 pth
= os
.path
.abspath(__file__
)
62 pth
= os
.path
.split(pth
)[0]
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
:
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)
76 if __name__
== '__main__':
78 chipname
= sys
.argv
[1]
81 chip
= load_pinouts(chipname
)
82 for k
, v
in chip
.items():
83 print ("\n****", k
, "****")