Skip to content

Commit 8736384

Browse files
committed
[wip]
1 parent ba21aac commit 8736384

File tree

6 files changed

+229
-28
lines changed

6 files changed

+229
-28
lines changed

modm_devices/device_file.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ def get_devices(self):
5454
valid_devices = [node.text for node in device_node.iterfind(self._VALID_DEVICE)]
5555
devices = identifiers
5656
if len(invalid_devices):
57-
devices = [did for did in devices if did.string not in invalid_devices]
57+
devices = (did for did in devices if did.string not in invalid_devices)
5858
if len(valid_devices):
59-
devices = [did for did in devices if did.string in valid_devices]
59+
devices = (did for did in devices if did.string in valid_devices)
6060
return [build_device(did, self) for did in devices]
6161

6262
@staticmethod
@@ -68,9 +68,10 @@ def is_valid(node, identifier: DeviceIdentifier):
6868
Returns:
6969
True if the selectors match, False otherwise.
7070
"""
71-
device_keys = filter(lambda k: k.startswith(DeviceFile._PREFIX_ATTRIBUTE_DEVICE), node.attrib.keys())
72-
properties = {k.replace(DeviceFile._PREFIX_ATTRIBUTE_DEVICE, ''):node.attrib[k].split("|") for k in device_keys}
73-
return not any(identifier[key] not in value for key, value in properties.items())
71+
device_keys = (k for k in node.attrib.keys() if k.startswith(DeviceFile._PREFIX_ATTRIBUTE_DEVICE))
72+
properties = ((k.replace(DeviceFile._PREFIX_ATTRIBUTE_DEVICE, ''), node.attrib[k].split("|"))
73+
for k in device_keys)
74+
return all(identifier[key] in value for key, value in properties)
7475

7576
def get_properties(self, identifier: DeviceIdentifier):
7677
class Converter:
@@ -86,9 +87,9 @@ def is_valid(self, node):
8687
return DeviceFile.is_valid(node, self.identifier)
8788

8889
def strip_attrib(self, node):
89-
stripped_keys = filter(lambda k: not k.startswith(DeviceFile._PREFIX_ATTRIBUTE_DEVICE), node.attrib.keys())
90+
stripped_keys = (k for k in node.attrib.keys() if not k.startswith(DeviceFile._PREFIX_ATTRIBUTE_DEVICE))
9091
if node.getparent().getparent() is None and node.tag == 'device':
91-
stripped_keys = filter(lambda k: k not in self.identifier.keys(), stripped_keys)
92+
stripped_keys = (k for k in stripped_keys if k not in self.identifier.keys())
9293
return {k:node.attrib[k] for k in stripped_keys}
9394

9495
def to_dict(self, t):
@@ -97,14 +98,13 @@ def to_dict(self, t):
9798
return {}
9899
attrib = self.strip_attrib(t)
99100
d = {t.tag: {} if len(attrib) else None}
100-
children = []
101-
for c in t:
102-
if self.is_valid(c):
103-
children.append(c)
101+
children = filter(self.is_valid, t)
104102
if children:
105103
dd = defaultdict(list)
106104
for dc in map(self.to_dict, children):
105+
# print(dc)
107106
for k, v in dc.items():
107+
if k == "signal" and v.get("name") == "seg40": print(v)
108108
dd[k].append(v)
109109
dk = {}
110110
for k, v in dd.items():
@@ -120,8 +120,11 @@ def to_dict(self, t):
120120
elif len(attrib):
121121
if any(k in d[t.tag] for k in attrib.keys()):
122122
raise ParserException("Node children are overwriting attribute '{}'!".format(k))
123+
# print(attrib.items())
123124
d[t.tag].update(attrib.items())
124125
return read_only({k:read_only(v) for k,v in d.items()})
125126

126127
properties = Converter(identifier).to_dict(self.rootnode.find("device"))
128+
# print(properties)
129+
# exit(1)
127130
return properties["device"]

modm_devices/device_identifier.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,14 @@ def __init__(self, objs=None):
9999

100100
if isinstance(objs, DeviceIdentifier):
101101
self._ids = [objs.copy()]
102-
if isinstance(objs, (list, set, tuple)):
102+
elif isinstance(objs, (list, set, tuple)):
103103
for obj in objs:
104-
if isinstance(objs, DeviceIdentifier):
105-
self._ids.append(objs)
106-
if isinstance(objs, MultiDeviceIdentifier):
104+
if isinstance(obj, DeviceIdentifier):
105+
self._ids.append(obj)
106+
elif isinstance(objs, MultiDeviceIdentifier):
107107
self._ids = [dev for dev in objs.ids]
108+
elif objs is not None:
109+
print("No known conversion of '{}' to MultiDeviceIdentifier!".format(objs))
108110

109111
@property
110112
def ids(self):
@@ -126,13 +128,14 @@ def from_list(device_ids: list):
126128
mid._ids = [dev for dev in device_ids]
127129
return mid
128130

129-
def append(self, did):
130-
assert isinstance(did, DeviceIdentifier)
131+
def append(self, *dids):
132+
for did in dids:
133+
assert isinstance(did, DeviceIdentifier)
131134

132-
self._ids.append(did)
133-
self.__dirty = True
134-
self.__string = None
135-
self.__naming_schema = None
135+
self._ids.append(did)
136+
self.__dirty = True
137+
self.__string = None
138+
self.__naming_schema = None
136139

137140
def extend(self, dids):
138141
assert isinstance(dids, (MultiDeviceIdentifier, list))
@@ -344,4 +347,4 @@ def __str__(self):
344347
return self.string
345348

346349
def __repr__(self):
347-
return self.string
350+
return self.string

test/device_file_test.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ def setUp(self):
1414
if DEVICE_FILES is None:
1515
DEVICE_FILES = {}
1616
device_files = os.path.join(os.path.dirname(__file__), "../../../devices/**/*.xml")
17+
device_files = os.path.join(os.path.dirname(__file__), "../../../devices/stm32/stm32l1-51_52_62-c_d_e.xml")
1718
device_file_names = glob.glob(device_files)
1819

1920
# Parse the files and build the :target enumeration
2021
parser = modm_devices.parser.DeviceParser()
2122
for device_file_name in device_file_names:
2223
for device in parser.parse(device_file_name).get_devices():
2324
DEVICE_FILES[device.partname] = device
24-
self.devices = DEVICE_FILES
25+
self.devices = {"stm32l152vdt6": DEVICE_FILES["stm32l152vdt6"]}
2526

2627

2728
def tearDown(self):
@@ -69,6 +70,7 @@ def assertIn(key, obj):
6970
print("{}: duplicated signals for P{}{}: {}".format(
7071
name, gpio["port"].upper(), gpio["pin"], duplicates))
7172
failures += 1
73+
# print(gpio)
7274

7375
self.assertEqual(failures, 0, "Found inconsistencies in the device files!")
7476

test/device_identifier_test.py

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,191 @@ def test_copy(self):
9191
self.assertEqual(ident.naming_schema, "{platform}")
9292

9393

94+
def id_from_string(string):
95+
i = DeviceIdentifier("{platform}{family}{name}{pin}{size}{package}{temperature}{variant}")
96+
i.set("platform", string[:5])
97+
i.set("family", string[5:7])
98+
i.set("name", string[7:9])
99+
i.set("pin", string[9])
100+
i.set("size", string[10])
101+
i.set("package", string[11])
102+
i.set("temperature", string[12])
103+
if len(string) >= 14:
104+
i.set("variant", string[13])
105+
else:
106+
i.set("variant", "")
107+
return i
108+
94109

95110
class MultiDeviceIdentifierTest(unittest.TestCase):
96111

97112
def setUp(self):
98113
self.ident = MultiDeviceIdentifier()
114+
self.devices = MultiDeviceIdentifier(list(map(id_from_string, [
115+
"stm32l151cct6",
116+
"stm32l151cct7",
117+
"stm32l151ccu6",
118+
"stm32l151ccu7",
119+
"stm32l151qch6",
120+
"stm32l151qdh6",
121+
"stm32l151qeh6",
122+
"stm32l151rct6",
123+
"stm32l151rct6a",
124+
"stm32l151rcy6",
125+
"stm32l151rdt6",
126+
"stm32l151rdt7",
127+
"stm32l151rdy6",
128+
"stm32l151rdy7",
129+
"stm32l151ret6",
130+
"stm32l151ucy6",
131+
"stm32l151ucy7",
132+
"stm32l151vch6",
133+
"stm32l151vct6",
134+
"stm32l151vct6a",
135+
"stm32l151vdt6",
136+
"stm32l151vdt6x",
137+
"stm32l151vdy6x",
138+
"stm32l151vdy7x",
139+
"stm32l151vet6",
140+
"stm32l151vet7",
141+
"stm32l151vey6",
142+
"stm32l151vey7",
143+
"stm32l151zct6",
144+
"stm32l151zdt6",
145+
"stm32l151zet6",
146+
"stm32l152cct6",
147+
"stm32l152ccu6",
148+
"stm32l152qch6",
149+
"stm32l152qdh6",
150+
"stm32l152qeh6",
151+
"stm32l152rct6",
152+
"stm32l152rct6a",
153+
"stm32l152rdt6",
154+
"stm32l152rdy6",
155+
"stm32l152ret6",
156+
"stm32l152ucy6",
157+
"stm32l152vch6",
158+
"stm32l152vct6",
159+
"stm32l152vct6a",
160+
"stm32l152vdt6",
161+
"stm32l152vdt6x",
162+
"stm32l152vet6",
163+
"stm32l152vey6",
164+
"stm32l152zct6",
165+
"stm32l152zdt6",
166+
"stm32l152zet6",
167+
"stm32l162qdh6",
168+
"stm32l162rct6",
169+
"stm32l162rct6a",
170+
"stm32l162rdt6",
171+
"stm32l162rdy6",
172+
"stm32l162ret6",
173+
"stm32l162vch6",
174+
"stm32l162vct6",
175+
"stm32l162vct6a",
176+
"stm32l162vdt6",
177+
"stm32l162vdy6x",
178+
"stm32l162vet6",
179+
"stm32l162vey6",
180+
"stm32l162zdt6",
181+
"stm32l162zet6"
182+
])))
183+
self.child_devices = MultiDeviceIdentifier(list(map(id_from_string, [
184+
"stm32l152qch6",
185+
"stm32l152qdh6",
186+
"stm32l152qeh6",
187+
"stm32l152vch6",
188+
"stm32l152rct6a",
189+
"stm32l152rdt6",
190+
"stm32l152ret6",
191+
"stm32l152vct6a",
192+
"stm32l152vct6",
193+
"stm32l152vdt6x",
194+
"stm32l152vdt6",
195+
"stm32l152vet6",
196+
"stm32l152zct6",
197+
"stm32l152zdt6",
198+
"stm32l152zet6",
199+
"stm32l152rdy6",
200+
"stm32l152vey6",
201+
"stm32l162qdh6",
202+
"stm32l162vch6",
203+
"stm32l162rct6a",
204+
"stm32l162rdt6",
205+
"stm32l162ret6",
206+
"stm32l162vct6a",
207+
"stm32l162vct6",
208+
"stm32l162vdt6",
209+
"stm32l162vet6",
210+
"stm32l162zdt6",
211+
"stm32l162zet6",
212+
"stm32l162rdy6",
213+
"stm32l162vdy6x",
214+
"stm32l162vey6",
215+
])))
216+
self.parent_devices = MultiDeviceIdentifier(list(map(id_from_string, [
217+
"stm32l151qch6",
218+
"stm32l151qdh6",
219+
"stm32l151qeh6",
220+
"stm32l151vch6",
221+
"stm32l151rct6a",
222+
"stm32l151rct6",
223+
"stm32l151rdt6",
224+
"stm32l151rdt7",
225+
"stm32l151ret6",
226+
"stm32l151vct6a",
227+
"stm32l151vct6",
228+
"stm32l151vdt6x",
229+
"stm32l151vdt6",
230+
"stm32l151vet6",
231+
"stm32l151vet7",
232+
"stm32l151zct6",
233+
"stm32l151zdt6",
234+
"stm32l151zet6",
235+
"stm32l151rcy6",
236+
"stm32l151rdy6",
237+
"stm32l151rdy7",
238+
"stm32l151ucy6",
239+
"stm32l151ucy7",
240+
"stm32l151vdy6x",
241+
"stm32l151vdy7x",
242+
"stm32l151vey6",
243+
"stm32l151vey7",
244+
"stm32l152qch6",
245+
"stm32l152qdh6",
246+
"stm32l152qeh6",
247+
"stm32l152vch6",
248+
"stm32l152rct6a",
249+
"stm32l152rct6",
250+
"stm32l152rdt6",
251+
"stm32l152ret6",
252+
"stm32l152vct6a",
253+
"stm32l152vct6",
254+
"stm32l152vdt6x",
255+
"stm32l152vdt6",
256+
"stm32l152vet6",
257+
"stm32l152zct6",
258+
"stm32l152zdt6",
259+
"stm32l152zet6",
260+
"stm32l152rdy6",
261+
"stm32l152ucy6",
262+
"stm32l152vey6",
263+
"stm32l162qdh6",
264+
"stm32l162vch6",
265+
"stm32l162rct6a",
266+
"stm32l162rct6",
267+
"stm32l162rdt6",
268+
"stm32l162ret6",
269+
"stm32l162vct6a",
270+
"stm32l162vct6",
271+
"stm32l162vdt6",
272+
"stm32l162vet6",
273+
"stm32l162zdt6",
274+
"stm32l162zet6",
275+
"stm32l162rdy6",
276+
"stm32l162vdy6x",
277+
"stm32l162vey6",
278+
])))
99279

100280
def test_should_construct_empty(self):
101281
self.assertEqual(self.ident.string, "")
@@ -111,3 +291,13 @@ def test_should_merge_naming_schemas(self):
111291

112292
self.ident.append(DeviceIdentifier("{one}{two}"))
113293
self.assertEqual(self.ident.naming_schema, "{one}{one}{two}")
294+
295+
def test_minimal_subtract_set(self):
296+
print(self.devices)
297+
print(self.parent_devices)
298+
print(self.child_devices)
299+
300+
min_set = self.child_devices.minimal_subtract_set(self.devices, self.parent_devices)
301+
for m in min_set:
302+
print([(k, m.getAttribute(k)) for k in self.devices.keys() if m.getAttribute(k)])
303+
# self.assertEqual(min_set, "[f1]")

tools/generator/dfg/device_tree.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,20 +119,23 @@ def _sortTree(self):
119119
for ch in self.children:
120120
ch._sortTree()
121121

122-
def toString(self, indent=0):
122+
def toString(self, indent=0, long_ids=False):
123123
ind = ' ' * indent
124124
if indent >= 2:
125125
ind = ind[:-2] + '. '
126126
if self.parent is None or self.parent.ids == self.ids:
127127
ident = ""
128128
else:
129-
ident = self.ids.string
129+
if long_ids:
130+
ident = " ".join(did.string for did in self.ids)
131+
else:
132+
ident = self.ids.string
130133
string = "{}{} {}\n".format(
131134
ind,
132135
self._toCompactString(),
133136
ident)
134137
for ch in self.children:
135-
string += ch.toString(indent + 2)
138+
string += ch.toString(indent + 2, long_ids)
136139
return string
137140

138141
def _toCompactString(self):

tools/generator/dfg/stm32/stm_device_tree.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,10 +316,10 @@ def rv(param, default=[]):
316316
else:
317317
allSignals = gpioFile.compactQuery('//GPIO_Pin[@Name="{}"]/PinSignal/SpecificParameter[@Name="GPIO_AF"]/..'.format(rname))
318318
signalMap = { a.get("Name"): a[0][0].text.lower().replace("gpio_af", "")[:2].replace("_", "") for a in allSignals }
319-
altFunctions = [ (s.lower(), (signalMap[s] if s in signalMap else "-1")) for s in localSignals ]
319+
altFunctions = [ (s.lower(), signalMap.get(s, "-1")) for s in localSignals ]
320320

321321
afs = []
322-
for af in altFunctions:
322+
for af in set(altFunctions):
323323
for raf in split_multi_af(af[0]):
324324
naf = {}
325325
naf["driver"], naf["instance"], naf["name"] = raf

0 commit comments

Comments
 (0)