81e4e9d2fcaa8da932a5bd04206744effcbd5052
[pinmux.git] / src / ifacebase.py
1 import os.path
2
3 try:
4 from UserDict import UserDict
5 except ImportError:
6 from collections import UserDict
7
8
9 class InterfacesBase(UserDict):
10 """ contains a list of interface definitions
11 """
12
13 def __init__(self, ifacekls, pth=None, ifaceklsdict=None):
14 self.pth = pth
15 self.fastbus = []
16 self.ifacecount = []
17 if ifaceklsdict is None:
18 ifaceklsdict = {}
19 UserDict.__init__(self, {})
20 if not pth:
21 return
22 ift = 'interfaces.txt'
23 if pth:
24 ift = os.path.join(pth, ift)
25 with open(ift, 'r') as ifile:
26 for ln in ifile.readlines():
27 ln = ln.strip()
28 ln = ln.split("\t")
29 name = ln[0] # will have uart
30 count = int(ln[1]) # will have count of uart
31 self.fastbus += ln[2:]
32 # spec looks like this:
33 """
34 [{'name': 'sda', 'outen': True},
35 {'name': 'scl', 'outen': True},
36 ]
37 """
38 ikls = ifacekls
39 for k, v in ifaceklsdict.items():
40 if name.startswith(k):
41 ikls = v
42 break
43 spec, ganged = self.read_spec(pth, name)
44 # XXX HORRIBLE hack!!!
45 if name == 'pwm' and count == 1 and len(spec) != 1:
46 #print "read", name, count, spec, ganged
47 #print "multi pwm", spec[:1], len(spec)
48 spec[0]['name'] = 'out'
49 iface = ikls(name, spec[:1], ganged, False)
50 self.ifaceadd(name, len(spec), iface)
51 else:
52 iface = ikls(name, spec, ganged, count == 1)
53 self.ifaceadd(name, count, iface)
54
55 def getifacetype(self, fname):
56 # finds the interface type, e.g sd_d0 returns "inout"
57 for iface in self.values():
58 typ = iface.getifacetype(fname)
59 # if fname.startswith('pwm'):
60 # print fname, iface.ifacename, typ
61 if typ:
62 return typ
63 return None
64
65 def ifaceadd(self, name, count, iface, at=None):
66 if at is None:
67 at = len(self.ifacecount) # ifacecount is a list
68 self.ifacecount.insert(at, (name, count)) # appends the list
69 # with (name,count) *at* times
70 self[name] = iface
71
72 """
73 will check specific files of kind peripheral.txt like spi.txt,
74 uart.txt in test directory
75 """
76
77 def read_spec(self, pth, name):
78 spec = []
79 ganged = {}
80 fname = '%s.txt' % name
81 if pth:
82 ift = os.path.join(pth, fname)
83 with open(ift, 'r') as sfile:
84 for ln in sfile.readlines():
85 ln = ln.strip()
86 ln = ln.split("\t")
87 name = ln[0]
88 d = {'name': name, # here we start to make the dictionary
89 'type': ln[1]}
90 if ln[1] == 'out':
91 d['action'] = True # adding element to the dict
92 elif ln[1] == 'inout':
93 d['outen'] = True
94 if len(ln) == 3:
95 bus = ln[2]
96 if bus not in ganged:
97 ganged[bus] = []
98 ganged[bus].append(name)
99 spec.append(d)
100 return spec, ganged