Skip to content

Commit fa928e8

Browse files
aykevldeadprogram
authored andcommitted
tools/gen-device-svd: be a bit more forgiving for stm32 svd files
Some newer files have a few mistakes. Allow them to be processed.
1 parent c49d806 commit fa928e8

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

tools/gen-device-svd.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import re
99
import argparse
1010

11+
validName = re.compile('^[a-zA-Z0-9_]+$')
12+
13+
1114
class Device:
1215
# dummy
1316
pass
@@ -23,6 +26,13 @@ def formatText(text):
2326
text = text.strip()
2427
return text
2528

29+
# Replace characters that are not allowed in a symbol name with a '_'. This is
30+
# useful to be able to process SVD files with errors.
31+
def cleanName(text):
32+
if not validName.match(text):
33+
return ''.join(list(map(lambda c: c if validName.match(c) else '_', text)))
34+
return text
35+
2636
def readSVD(path, sourceURL):
2737
# Read ARM SVD files.
2838
device = Device()
@@ -54,7 +64,9 @@ def readSVD(path, sourceURL):
5464
groupNameTags = periphEl.findall('groupName')
5565
groupName = None
5666
if groupNameTags:
57-
groupName = getText(groupNameTags[0])
67+
# Some group names (for example the STM32H7A3x) have an invalid
68+
# group name. Replace invalid characters with '_'.
69+
groupName = cleanName(getText(groupNameTags[0]))
5870

5971
interruptEls = periphEl.findall('interrupt')
6072
for interrupt in interruptEls:
@@ -180,7 +192,9 @@ def readSVD(path, sourceURL):
180192
def addInterrupt(interrupts, intrName, intrIndex, description):
181193
if intrName in interrupts:
182194
if interrupts[intrName]['index'] != intrIndex:
183-
raise ValueError('interrupt with the same name has different indexes: %s (%d vs %d)'
195+
# Note: some SVD files like the one for STM32H7x7 contain mistakes.
196+
# Instead of throwing an error, simply log it.
197+
print ('interrupt with the same name has different indexes: %s (%d vs %d)'
184198
% (intrName, interrupts[intrName]['index'], intrIndex))
185199
if description not in interrupts[intrName]['description'].split(' // '):
186200
interrupts[intrName]['description'] += ' // ' + description
@@ -195,8 +209,10 @@ def parseBitfields(groupName, regName, fieldsEls, bitfieldPrefix=''):
195209
fields = []
196210
if fieldsEls:
197211
for fieldEl in fieldsEls[0].findall('field'):
198-
fieldName = getText(fieldEl.find('name'))
199-
descrEls = fieldEl.findall('description')
212+
# Some bitfields (like the STM32H7x7) contain invalid bitfield
213+
# names like 'CNT[31]'. Replace invalid characters with '_' when
214+
# needed.
215+
fieldName = cleanName(getText(fieldEl.find('name')))
200216
lsbTags = fieldEl.findall('lsb')
201217
if len(lsbTags) == 1:
202218
lsb = int(getText(lsbTags[0]))

0 commit comments

Comments
 (0)