svd2py - Convert CMSIS SVD file to Python data structure
Introduction
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.
Project structure
📦Svd2Py
┣━📂doc ─ Documentation
┣━📂svd2py ─ Python sources
┣━📂tests ─ pytest tests
┗━📜pyproject.toml
How it works
The parser translate SVD elements directly to Python data structures like dictionaries and list.
:white_check_mark: It does following thing:
- Translate SVD elements directly to Python data structures (dict and list),
- Resolves the derivedFrom attribute on
peripheral,cluster,registerandfieldelements, including cross-scope references (e.g.derivedFrom="otherPeripheral.otherRegister"). This is enabled by default and can be disabled, see Reference. - Resolves the derivedFrom attribute on
enumeratedValues. Unlike the elements above, it is referenced by its own<name>, searched throughout the whole device, optionally qualified with a trailingfield,register.fieldorperipheral.register.fieldprefix only as needed to disambiguate (e.g.derivedFrom="clk.dis_en_enum",derivedFrom="ctrl.clk.dis_en_enum"orderivedFrom="timer0.ctrl.clk.dis_en_enum"). An ambiguous reference (matching more than oneenumeratedValues) raises an error asking for more qualification.
:no_entry_sign: What is missing:
- 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"
}
}
}
Note: the shape above (with the attributes.derivedFrom marker left in place) is what you get with
resolve_derived_from=False. By default, TestRegister above would instead be merged with the element it is
derived from (TestDerivedRegister), and the attributes.derivedFrom entry would be removed. See
Reference below.
Install
pip install svd2py
How to use
import svd2py
svd_file = "sample.svd"
# Create SvdParser object passing path to SVD file
parser = svd2py.SvdParser()
# Invoke conver() function
result = parser.convert(svd_file)
Extras
The package also includes two command line tools:
- svd2yaml - Convert SVD file to YAML format,
- svd2json - Convert SVD file to JSON format.
svd2yaml
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.
svd2json
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.
Reference
class svd2py.SvdParser()
SVD file parser class. This is the main class for parsing SVD files.
convert(svd: Path | str, resolve_derived_from: bool = True)
svd - path to SVD file to parse.
resolve_derived_from - if True (default), every derivedFrom attribute on a
peripheral, cluster, register, field or enumeratedValues element is resolved: the element is merged with
the element it is derived from (same-scope name, or a cross-scope path such as "otherPeripheral.otherRegister";
enumeratedValues uses its own name-based, whole-device lookup, see above), and the attributes.derivedFrom
marker is removed from the result. Set to False to get the raw, unresolved output (the attributes.derivedFrom
entry is kept as-is, matching the pre-resolution behavior).
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.
Metadata
Release files for svd2py 2.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| svd2py-2.0.0.tar.gz | 48.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| svd2py-2.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 62.1 kB
Release files / svd2py-2.0.0.tar.gz
| Download URL | svd2py-2.0.0.tar.gz |
|---|---|
| Size | 48.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1d6a9df0534e0bc873c26c2d4ed084713da73019ac8945af2080399444fbb84d
|
|
BLAKE2b-256 checksum How to use checksums |
314ad48d6ac1b35070c458c1e5fe5992ade7a963e1337f8e4bdaf45b6e813efa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / svd2py-2.0.0-py3-none-any.whl
| Download URL | svd2py-2.0.0-py3-none-any.whl |
|---|---|
| Size | 13.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3333e1fb53cd2c09a2bcd6b759a7049d71f5bce6445eaf1263f4d9899360a3ce
|
|
BLAKE2b-256 checksum How to use checksums |
713f9c241b9bce32787e90370943796b4b7c8c41683f65ee23b61f414803fa2e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|