add horrible hack for turning single-interface PWM into multi-single pin
[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):
14 self.pth = pth
15 self.ifacecount = []
16 UserDict.__init__(self, {})
17 if not pth:
18 return
19 ift = 'interfaces.txt'
20 if pth:
21 ift = os.path.join(pth, ift)
22 with open(ift, 'r') as ifile:
23 for ln in ifile.readlines():
24 ln = ln.strip()
25 ln = ln.split("\t")
26 name = ln[0] # will have uart
27 count = int(ln[1]) # will have count of uart
28 # spec looks like this:
29 """
30 [{'name': 'sda', 'outen': True},
31 {'name': 'scl', 'outen': True},
32 ]
33 """
34 spec, ganged = self.read_spec(pth, name)
35 # XXX HORRIBLE hack!!!
36 if name == 'pwm' and count == 1 and len(spec) != 1:
37 #print "read", name, count, spec, ganged
38 #print "multi pwm", spec[:1], len(spec)
39 spec[0]['name'] = 'out'
40 iface = ifacekls(name, spec[:1], ganged, False)
41 self.ifaceadd(name, len(spec), iface)
42 else:
43 iface = ifacekls(name, spec, ganged, count == 1)
44 self.ifaceadd(name, count, iface)
45
46 def getifacetype(self, fname):
47 # finds the interface type, e.g sd_d0 returns "inout"
48 for iface in self.values():
49 typ = iface.getifacetype(fname)
50 #if fname.startswith('pwm'):
51 # print fname, iface.ifacename, typ
52 if typ:
53 return typ
54 return None
55
56 def ifaceadd(self, name, count, iface, at=None):
57 if at is None:
58 at = len(self.ifacecount) # ifacecount is a list
59 self.ifacecount.insert(at, (name, count)) # appends the list
60 # with (name,count) *at* times
61 self[name] = iface
62
63 """
64 will check specific files of kind peripheral.txt like spi.txt,
65 uart.txt in test directory
66 """
67
68 def read_spec(self, pth, name):
69 spec = []
70 ganged = {}
71 fname = '%s.txt' % name
72 if pth:
73 ift = os.path.join(pth, fname)
74 with open(ift, 'r') as sfile:
75 for ln in sfile.readlines():
76 ln = ln.strip()
77 ln = ln.split("\t")
78 name = ln[0]
79 d = {'name': name, # here we start to make the dictionary
80 'type': ln[1]}
81 if ln[1] == 'out':
82 d['action'] = True # adding element to the dict
83 elif ln[1] == 'inout':
84 d['outen'] = True
85 if len(ln) == 3:
86 bus = ln[2]
87 if bus not in ganged:
88 ganged[bus] = []
89 ganged[bus].append(name)
90 spec.append(d)
91 return spec, ganged