Debian Codegraph @

tools/atdf-to-avrdude.xslt

raw symbols SCIP
1 <?xml version="1.0" encoding="UTF-8"?>
2 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
3 xmlns:xs="http://www.w3.org/2001/XMLSchema"
4 xmlns:f="http://functions"
5 exclude-result-prefixes="xs f"
6 version="2.0">
7
8 <!--
9 avrdude - A Downloader/Uploader for AVR device programmers
10 Copyright (C) 2018 Morten Engelhardt Olsen <[email protected]>
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 -->
25
26 <xsl:output method="text"/>
27
28 <xsl:template match="/">
29
30 <xsl:variable name="architecture"
31 select="/avr-tools-device-file/devices/device/@architecture"/>
32
33 <xsl:choose>
34 <xsl:when test="$architecture = 'AVR8X'">
35 <xsl:call-template name="create-avr8x-config"/>
36 </xsl:when>
37 <xsl:otherwise>
38 <xsl:message terminate="yes">No support for architecture '<xsl:value-of select="$architecture"/>'</xsl:message>
39 </xsl:otherwise>
40 </xsl:choose>
41 </xsl:template>
42
43 <xsl:template name="create-avr8x-config">
44
45 <xsl:variable name="device-name"
46 select="/avr-tools-device-file/devices/device/@name"/>
47 <xsl:variable name="device-id">
48 <xsl:call-template name="get-device-id">
49 <xsl:with-param name="device-name" select="$device-name"/>
50 </xsl:call-template>
51 </xsl:variable>
52 <xsl:variable name="parent-name">
53 <xsl:call-template name="get-parent-name">
54 <xsl:with-param name="device-name" select="$device-name"/>
55 </xsl:call-template>
56 </xsl:variable>
57 <xsl:variable name="device-signature">
58 <xsl:call-template name="get-device-signature">
59 <xsl:with-param name="device-name" select="$device-name"/>
60 </xsl:call-template>
61 </xsl:variable>
62
63 <xsl:call-template name="write-conf-header">
64 <xsl:with-param name="device-name" select="$device-name"/>
65 </xsl:call-template>
66
67 <xsl:text>part parent "</xsl:text><xsl:value-of select="normalize-space($parent-name)"/><xsl:text>";&#xa;</xsl:text>
68 <xsl:text> id = "</xsl:text><xsl:value-of select="normalize-space($device-id)"/><xsl:text>";&#xa;</xsl:text>
69 <xsl:text> desc = "</xsl:text><xsl:value-of select="normalize-space($device-name)"/><xsl:text>";&#xa;</xsl:text>
70 <xsl:text> signature = </xsl:text><xsl:value-of select="normalize-space($device-signature)"/><xsl:text>;&#xa;</xsl:text>
71
72 <xsl:call-template name="write-memories">
73 <xsl:with-param name="device-name" select="$device-name"/>
74 </xsl:call-template>
75
76 <xsl:text>;&#xa;</xsl:text>
77 </xsl:template>
78
79 <xsl:template name="write-memories">
80 <xsl:param name="device-name"/>
81
82 <xsl:call-template name="write-memory">
83 <xsl:with-param name="memory-name" select="'PROGMEM'"/>
84 </xsl:call-template>
85 <xsl:call-template name="write-memory">
86 <xsl:with-param name="memory-name" select="'EEPROM'"/>
87 </xsl:call-template>
88 </xsl:template>
89
90 <xsl:template name="write-memory">
91 <xsl:param name="memory-name"/>
92
93 <xsl:variable name="memory-segment" select="//memory-segment[@name = $memory-name]"/>
94 <xsl:choose>
95 <xsl:when test="not($memory-segment)">
96 <xsl:message terminate="yes">No memory-segment named '<xsl:value-of select="$memory-name"/>'</xsl:message>
97 </xsl:when>
98 <xsl:when test="count($memory-segment) > 1">
99 <xsl:message terminate="yes">More than one memory-segments named '<xsl:value-of select="$memory-name"/>'</xsl:message>
100 </xsl:when>
101 <xsl:otherwise>
102 <xsl:call-template name="write-memory-segment">
103 <xsl:with-param name="memory-segment" select="$memory-segment"/>
104 </xsl:call-template>
105 </xsl:otherwise>
106 </xsl:choose>
107 </xsl:template>
108
109 <xsl:template name="write-memory-segment">
110 <xsl:param name="memory-segment"/>
111
112 <xsl:variable name="size">
113 <xsl:value-of select="concat('0x', f:decimalToHex(f:hexToDec($memory-segment/@size)))"/>
114 </xsl:variable>
115
116 <xsl:variable name="offset">
117 <xsl:choose>
118 <xsl:when test="$memory-segment/@type = 'flash' and $memory-segment/ancestor::device/@architecture = 'AVR8X'">
119 <xsl:variable name="progmem-base"
120 select="f:hexToDec($memory-segment/ancestor::device/property-groups/property-group[@name = 'UPDI_INTERFACE']/property[@name = 'PROGMEM_OFFSET']/@value)"/>
121 <xsl:value-of select="concat('0x', f:decimalToHex(f:hexToDec($memory-segment/@start) + $progmem-base))"/>
122 </xsl:when>
123 <xsl:otherwise>
124 <xsl:value-of select="concat('0x', f:decimalToHex(f:hexToDec($memory-segment/@start)))"/>
125 </xsl:otherwise>
126 </xsl:choose>
127 </xsl:variable>
128
129 <xsl:text>&#xa;</xsl:text>
130 <xsl:text> memory "</xsl:text><xsl:value-of select="$memory-segment/@type"/><xsl:text>"&#xa;</xsl:text>
131 <xsl:text> size = </xsl:text><xsl:value-of select="$size"/><xsl:text>;&#xa;</xsl:text>
132 <xsl:text> offset = </xsl:text><xsl:value-of select="$offset"/><xsl:text>;&#xa;</xsl:text>
133 <xsl:text> page_size = </xsl:text><xsl:value-of select="$memory-segment/@pagesize"/><xsl:text>;&#xa;</xsl:text>
134 <xsl:text> readsize = 0x100;&#xa;</xsl:text>
135 <xsl:text> ;&#xa;</xsl:text>
136 </xsl:template>
137
138 <xsl:template name="write-conf-header">
139 <xsl:param name="device-name"/>
140
141 <xsl:text>#------------------------------------------------------------&#xa;</xsl:text>
142 <xsl:text># </xsl:text><xsl:value-of select="$device-name"/><xsl:text>&#xa;</xsl:text>
143 <xsl:text>#------------------------------------------------------------&#xa;</xsl:text>
144 <xsl:text>&#xa;</xsl:text>
145 </xsl:template>
146
147 <xsl:template name="get-device-id">
148 <xsl:param name="device-name"/>
149
150 <xsl:choose>
151 <xsl:when test="contains($device-name, 'ATtiny')">
152 <xsl:value-of select="replace($device-name, 'ATtiny', 't')"/>
153 </xsl:when>
154 <xsl:when test="contains($device-name, 'ATmega')">
155 <xsl:value-of select="replace($device-name, 'ATmega', 'm')"/>
156 </xsl:when>
157 <xsl:when test="starts-with($device-name, 'AVR')">
158 <xsl:value-of select="lower-case($device-name)"/>
159 </xsl:when>
160 <xsl:otherwise>
161 <xsl:message terminate="yes">Unable to deduce device id from '<xsl:value-of select="$device-name"/>'</xsl:message>
162 </xsl:otherwise>
163 </xsl:choose>
164 </xsl:template>
165
166 <xsl:template name="get-parent-name">
167 <xsl:param name="device-name"/>
168
169 <xsl:choose>
170 <xsl:when test="contains($device-name, 'ATtiny')">.avr8x_tiny</xsl:when>
171 <xsl:when test="contains($device-name, 'ATmega')">.avr8x_mega</xsl:when>
172 <xsl:when test="matches($device-name, '^AVR[0-9]{1,3}D')">.avrdx</xsl:when>
173 <xsl:when test="matches($device-name, '^AVR[0-9]{1,3}E')">.avrex</xsl:when>
174 <xsl:otherwise>
175 <xsl:message terminate="yes">Unable to deduce parent name from '<xsl:value-of select="$device-name"/>'</xsl:message>
176 </xsl:otherwise>
177 </xsl:choose>
178 </xsl:template>
179
180 <xsl:template name="get-device-signature">
181 <xsl:param name="device-name"/>
182
183 <xsl:variable name="signatures"
184 select="/avr-tools-device-file/devices/device/property-groups/property-group[@name = 'SIGNATURES']"/>
185
186 <xsl:for-each select="$signatures/property[contains(@name, 'SIGNATURE')]">
187 <xsl:value-of select="@value"/><xsl:text> </xsl:text>
188 </xsl:for-each>
189 </xsl:template>
190
191 <!-- Helper functions (from https://stackoverflow.com/questions/22905134/convert-a-hexadecimal-number-to-an-integer-in-xslt) -->
192 <xsl:function name="f:hexToDec">
193 <xsl:param name="hex"/>
194 <xsl:variable name="dec"
195 select="string-length(substring-before('0123456789ABCDEF', substring($hex,1,1)))"/>
196 <xsl:choose>
197 <xsl:when test="matches($hex, '([0-9]*|[A-F]*)')">
198 <xsl:value-of
199 select="if ($hex = '') then 0
200 else $dec * f:power(16, string-length($hex) - 1) + f:hexToDec(substring($hex,2))"/>
201 </xsl:when>
202 <xsl:otherwise>
203 <xsl:message>Provided value is not hexadecimal...</xsl:message>
204 <xsl:value-of select="$hex"/>
205 </xsl:otherwise>
206 </xsl:choose>
207 </xsl:function>
208
209 <xsl:function name="f:power">
210 <xsl:param name="base"/>
211 <xsl:param name="exp"/>
212 <xsl:sequence
213 select="if ($exp lt 0) then f:power(1.0 div $base, -$exp)
214 else if ($exp eq 0)
215 then 1e0
216 else $base * f:power($base, $exp - 1)"
217 />
218 </xsl:function>
219
220 <xsl:function name="f:decimalToHex">
221 <xsl:param name="dec"/>
222 <xsl:if test="$dec > 0">
223 <xsl:value-of
224 select="f:decimalToHex(floor($dec div 16)),substring('0123456789ABCDEF', (($dec mod 16) + 1), 1)"
225 separator=""
226 />
227 </xsl:if>
228 </xsl:function>
229 </xsl:stylesheet>