Parser that allows to convert CMSIS SVD file format to Python data structure. Parser does not check SVD file syntax. It assume that parsing file is a proper SVD file.
📦Svd2Py
┣━📂doc ─ Documentation
┣━📂svd2py ─ Python sources
┣━📂tests ─ pytest tests
┗━📜pyproject.toml
The parser translate SVD elements directly to Python data structures like dictionaries and list.
✅ It does following thing:
- Translate SVD elements directly to Python data structures (dict and list),
🚫 What is missing:
- Resolves derivedFrom element attribute,
- Parses and resolves dimElementGroup,
Let's assume you have following element in you SVD file:
<device schemaVersion="1.3" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xs:noNamespaceSchemaLocation="CMSIS-SVD.xsd">
<name>TestDevic</name>
...
<peripherals>
<peripheral>
<name>TestPeripheral</name>
...
<registers>
<register derivedFrom="TestDerivedRegister">
<name>TestRegister</name>
...
<fields>
<field>
<name>TestField0</name>
...
</field>
<field>
<name>TestField1</name>
...
</field>
</fields>
</register>
...
</registers>
</peripheral>
...
</peripherals>
</device>This will be converted to Python like this:
{
"device": {
"name": "TestDevice",
...
"peripherals": {
"peripheral": [
{
"name": "TestPeripheral",
...
"registers": {
"register": [
{
"attributes": {
"derivedFrom": "TestDerivedRegister"
},
"name": "TestRegister",
...
"fields": {
"field": [
{
"name": "TestField0",
...
},
{
"name": "TestField1",
...
}
]
}
},
]
...
}
},
]
...
},
"attributes": {
"schemaVersion": "1.3"
}
}
}pip install svd2pyimport svd2py
svd_file = "sample.svd"
# Create SvdParser object passing path to SVD file
parser = svd2py.SvdParser()
# Invoke conver() function
result = parser.convert(svd_file)The package also includes two command line tools:
- svd2yaml - Convert SVD file to YAML format,
- svd2json - Convert SVD file to JSON format.
Usage: svd2yaml [OPTIONS] INPUT
svd2yaml - CMSIS SVD to YAML converter.
CMSIS SVD file parser that allows to convert SVD format to YAML data
structure
INPUT - path to SVD file.
Options:
--version Show the version and exit.
--help Show this message and exit.Usage: svd2json [OPTIONS] INPUT
svd2json - CMSIS SVD to JSON converter.
CMSIS SVD file parser that allows to convert SVD format to JSON data
structure
INPUT - path to SVD file.
Options:
--version Show the version and exit.
--help Show this message and exit.
class svd2py.SvdParser()
SVD file parser class. This is the main class for parsing SVD files.
convert(svd: Path | str)
svd - path to SVD file to parse.
Convert SVD file and return content in Python data structure.
It does not check SVD file syntax. If it is a proper XML file it will always return something.