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