Skip to content

Instantly share code, notes, and snippets.

@pr1metine
Forked from Sauci/ti_to_svd.py
Created November 4, 2023 14:48
Show Gist options
  • Save pr1metine/a1c02ba69006f7a3e36bf1efd7f87616 to your computer and use it in GitHub Desktop.
Save pr1metine/a1c02ba69006f7a3e36bf1efd7f87616 to your computer and use it in GitHub Desktop.
utility script to convert target description files from TI format into svd format
"""
@file ti_to_svd.py
@author Guillaume Sottas
@date 25/09/2019
This script can be used to convert the target description files from Texas Instruments into more
widely supported .svd file.
Before using this script, make sure Code Composer Studio is installed (available for MacOS, Linux and Windows).
For example, if a .svd file should be generated for target tms570lc4357zwt, invoke this script like
this:
python ti_to_svd.py /Applications/ti/ccs910/ccs/ccs_base/common/targetdb/devices/tms570lc43xx.xml tms570lc43xx.svd
"""
import sys
import lxml.etree as et
def main(xml_file, output_file):
dom = et.parse(xml_file)
xslt = et.XSLT(et.parse('ti_to_svd.xslt'))
with open(output_file, 'w') as fp:
fp.write(et.tostring(xslt(dom)).decode('utf-8'))
if __name__ == '__main__':
args = sys.argv
if len(sys.argv) != 3:
print('usage: ti_to_svd.py path_to_ti_target_xml_file path_to_output_svd_file')
else:
main(*sys.argv[1:])
<?xml version="1.0" ?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" encoding="utf-8"/>
<xsl:template match="device">
<device schemaVersion="1.1" xmlns:xs="http://www.w3.org/2001/XMLSchema" xs:noNamespaceSchemaLocation="CMSIS-SVD.xsd">
<vendor>Texas Instruments</vendor>
<vendorID>TI</vendorID>
<name>
<xsl:value-of select="@partnum"/>
</name>
<series>
Hercules
</series>
<version>
<xsl:value-of select="@HW_revision"/>
</version>
<description>
<xsl:value-of select="@description"/>
</description>
<xsl:apply-templates/>
</device>
</xsl:template>
<xsl:template match="cpu">
<cpu>
<xsl:comment>TODO: uncomment here once CR4/CR5 will be supported by CMSIS</xsl:comment>
<name>
<xsl:text>CM0</xsl:text>
</name>
<revision>r1p0</revision>
<endian>big</endian>
<mpuPresent>true</mpuPresent>
<fpuPresent>true</fpuPresent>
<nvicPrioBits>3</nvicPrioBits>
<vendorSystickConfig>true</vendorSystickConfig>
</cpu>
<peripherals>
<xsl:for-each select="instance">
<peripheral>
<name>
<xsl:value-of select="@id"/>
</name>
<description>
<xsl:value-of select="document(@href)/module/@description"/>
</description>
<baseAddress>
<xsl:value-of select="@baseaddr"/>
</baseAddress>
<addressBlock>
<offset>0x00</offset>
<size>
<xsl:value-of select="@size"/>
</size>
<usage>registers</usage>
<protection>
<xsl:value-of select="@permissions"/>
</protection>
</addressBlock>
<registers>
<xsl:for-each select="document(@href)/module/register">
<xsl:if test="boolean(@offset)">
<register>
<name>
<xsl:value-of select="@id"/>
</name>
<xsl:if test="boolean(@description)">
<description>
<xsl:value-of select="@description"/>
</description>
</xsl:if>
<addressOffset>
<xsl:value-of select="@offset"/>
</addressOffset>
<size>
<xsl:value-of select="@width"/>
</size>
<resetValue>
<xsl:value-of select="@width"/>
</resetValue>
<xsl:if test="bitfield">
<fields>
<xsl:for-each select="bitfield">
<field>
<name>
<xsl:value-of select="@id"/>
</name>
<bitRange>
<xsl:text>[</xsl:text><xsl:value-of select="@begin"/><xsl:text>:</xsl:text><xsl:value-of select="@end"/><xsl:text>]</xsl:text>
</bitRange>
<xsl:if test="bitenum">
<enumeratedValues>
<xsl:for-each select="bitenum">
<enumeratedValue>
<name>
<xsl:value-of select="@id"/>
</name>
<value>
<xsl:value-of select="@value"/>
</value>
</enumeratedValue>
</xsl:for-each>
</enumeratedValues>
</xsl:if>
<xsl:if test="not(@rwaccess = 'N')">
<access>
<xsl:if test="@rwaccess = 'R'">
<xsl:text>read-only</xsl:text>
</xsl:if>
<xsl:if test="@rwaccess = 'W'">
<xsl:text>write-only</xsl:text>
</xsl:if>
<xsl:if test="@rwaccess = 'RW'">
<xsl:text>read-write</xsl:text>
</xsl:if>
</access>
</xsl:if>
<xsl:if test="boolean(@description)">
<description>
<xsl:value-of select="@description"/>
</description>
</xsl:if>
</field>
</xsl:for-each>
</fields>
</xsl:if>
</register>
</xsl:if>
</xsl:for-each>
</registers>
</peripheral>
</xsl:for-each>
</peripherals>
</xsl:template>
</xsl:transform>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment