start adding myhdl IO class
[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 iface = ifacekls(name, spec, ganged, count == 1)
36 self.ifaceadd(name, count, iface)
37
38 def getifacetype(self, fname):
39 # finds the interface type, e.g sd_d0 returns "inout"
40 for iface in self.values():
41 typ = iface.getifacetype(fname)
42 if typ:
43 return typ
44 return None
45
46 def ifaceadd(self, name, count, iface, at=None):
47 if at is None:
48 at = len(self.ifacecount) # ifacecount is a list
49 self.ifacecount.insert(at, (name, count)) # appends the list
50 # with (name,count) *at* times
51 self[name] = iface
52
53 """
54 will check specific files of kind peripheral.txt like spi.txt,
55 uart.txt in test directory
56 """
57
58 def read_spec(self, pth, name):
59 spec = []
60 ganged = {}
61 fname = '%s.txt' % name
62 if pth:
63 ift = os.path.join(pth, fname)
64 with open(ift, 'r') as sfile:
65 for ln in sfile.readlines():
66 ln = ln.strip()
67 ln = ln.split("\t")
68 name = ln[0]
69 d = {'name': name, # here we start to make the dictionary
70 'type': ln[1]}
71 if ln[1] == 'out':
72 d['action'] = True # adding element to the dict
73 elif ln[1] == 'inout':
74 d['outen'] = True
75 if len(ln) == 3:
76 bus = ln[2]
77 if bus not in ganged:
78 ganged[bus] = []
79 ganged[bus].append(name)
80 spec.append(d)
81 return spec, ganged