new axi addr map class
[pinmux.git] / src / ifacebase.py
1 import json
2 import os.path
3
4 try:
5 from UserDict import UserDict
6 except ImportError:
7 from collections import UserDict
8
9
10 def _decode_list(data):
11 rv = []
12 for item in data:
13 if isinstance(item, unicode):
14 item = item.encode('utf-8')
15 elif isinstance(item, list):
16 item = _decode_list(item)
17 elif isinstance(item, dict):
18 item = _decode_dict(item)
19 rv.append(item)
20 return rv
21
22
23 def _decode_dict(data):
24 rv = {}
25 for key, value in data.iteritems():
26 if isinstance(key, unicode):
27 key = key.encode('utf-8')
28 if isinstance(value, unicode):
29 value = value.encode('utf-8')
30 elif isinstance(value, list):
31 value = _decode_list(value)
32 elif isinstance(value, dict):
33 value = _decode_dict(value)
34 rv[key] = value
35 return rv
36
37
38 class InterfacesBase(UserDict):
39 """ contains a list of interface definitions
40 """
41
42 def __init__(self, ifacekls, pth=None, ifaceklsdict=None):
43 self.pth = pth
44 self.ifacecount = []
45 self.fastbus = []
46 if ifaceklsdict is None:
47 ifaceklsdict = {}
48 UserDict.__init__(self, {})
49 if not pth:
50 return
51 ift = 'interfaces.txt'
52 cfg = 'configs.txt'
53 if pth:
54 ift = os.path.join(pth, ift)
55 cfg = os.path.join(pth, cfg)
56
57 # read in configs in JSON format, but strip out unicode
58 with open(cfg, 'r') as ifile:
59 self.configs = json.loads(ifile.read(), object_hook=_decode_dict)
60
61 # process the configs, look for "bus" type... XXX TODO; make this
62 # a bit more sophisticated
63 self.fastbus = []
64 for (ifacename, v) in self.configs.items():
65 if v.get('bus', "") == "fastbus":
66 self.fastbus.append(ifacename)
67
68 # reads the interfaces, name and quantity of each
69 with open(ift, 'r') as ifile:
70 for ln in ifile.readlines():
71 ln = ln.strip()
72 ln = ln.split("\t")
73 name = ln[0] # will have uart
74 count = int(ln[1]) # will have count of uart
75
76 # spec looks like this:
77 """
78 [{'name': 'sda', 'outen': True},
79 {'name': 'scl', 'outen': True},
80 ]
81 """
82 ikls = ifacekls
83 for k, v in ifaceklsdict.items():
84 if name.startswith(k):
85 ikls = v
86 break
87 spec, ganged = self.read_spec(pth, name)
88 # XXX HORRIBLE hack!!!
89 if name == 'pwm' and count == 1 and len(spec) != 1:
90 #print "read", name, count, spec, ganged
91 #print "multi pwm", spec[:1], len(spec)
92 spec[0]['name'] = 'out'
93 iface = ikls(name, spec[:1], ganged, False)
94 self.ifaceadd(name, len(spec), iface)
95 else:
96 iface = ikls(name, spec, ganged, count == 1)
97 self.ifaceadd(name, count, iface)
98 cfgs = self.getconfigs(name, count)
99 iface.configs = cfgs
100 print name, count, cfgs
101
102 def getconfigs(self, fname, count):
103 cfgs = []
104 for i in range(count):
105 if count == 1:
106 name = fname
107 else:
108 name = "%s%d" % (fname, i)
109 cfgs.append(self.configs.get(name, {}))
110 return cfgs
111
112 def getifacetype(self, fname):
113 # finds the interface type, e.g sd_d0 returns "inout"
114 for iface in self.values():
115 typ = iface.getifacetype(fname)
116 # if fname.startswith('pwm'):
117 # print fname, iface.ifacename, typ
118 if typ:
119 return typ
120 return None
121
122 def ifaceadd(self, name, count, iface, at=None):
123 if at is None:
124 at = len(self.ifacecount) # ifacecount is a list
125 self.ifacecount.insert(at, (name, count)) # appends the list
126 # with (name,count) *at* times
127 self[name] = iface
128
129 """
130 will check specific files of kind peripheral.txt like spi.txt,
131 uart.txt in test directory
132 """
133
134 def read_spec(self, pth, name):
135 spec = []
136 ganged = {}
137 fname = '%s.txt' % name
138 if pth:
139 ift = os.path.join(pth, fname)
140 with open(ift, 'r') as sfile:
141 for ln in sfile.readlines():
142 ln = ln.strip()
143 ln = ln.split("\t")
144 name = ln[0]
145 d = {'name': name, # here we start to make the dictionary
146 'type': ln[1]}
147 if ln[1] == 'out':
148 d['action'] = True # adding element to the dict
149 elif ln[1] == 'inout':
150 d['outen'] = True
151 if len(ln) == 3:
152 bus = ln[2]
153 if bus not in ganged:
154 ganged[bus] = []
155 ganged[bus].append(name)
156 spec.append(d)
157 return spec, ganged