{% import 'macros.jinja2' as utils %}
{% macro numtype(len, sign) -%}
{%- if sign -%}
{%- if len <= 8 %}
int8_t
{%- elif len <= 16 %}
int16_t
{%- elif len <= 32 %}
int32_t
{%- else %}
int64_t
{%- endif %}
{%- else %}
{%- if len <= 8 %}
uint8_t
{%- elif len <= 16 %}
uint16_t
{%- elif len <= 32 %}
uint32_t
{%- else %}
uint64_t
{%- endif %}
{%- endif %}
{%- endmacro %}
<?xml version="1.0" encoding="UTF-8"?>
<!--
    Autogenerated for {{ info.title }} v{{ info.version }}.
    Generated from {{ fileName }} using Cyanobyte Codegen v{{ version }}
-->
<device schemaVersion="1.1" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xs:noNamespaceSchemaLocation="CMSIS-SVD_Schema_1_1.xsd">
    <vendor>{{ info.copyright.name }}</vendor>
    <name>{{ info.title }}</name>
    <version>{{ info.version }}</version>
    <description>{{ info.description }}</description>
    <licenseText>
    {{ utils.license(info.copyright.name, info.copyright.date, info.license.name) }}
    </licenseText>
    <!-- Placeholder values -->
    <addressUnitBits>8</addressUnitBits>
    <width>8</width>
    <!-- Define our peripheral -->
    <peripherals>
        <peripheral>
            <name>{{ info.title }}</name>
            <description>{{ info.description }}</description>
            {% if i2c.address is iterable and i2c.address is not string %}
            <baseAddress>{{ i2c.address[0] }}</baseAddress>
            {% else %}
            <baseAddress>{{ i2c.address }}</baseAddress>
            {% endif %}
            <addressBlock>
                <offset>0</offset>
                <size>{{ i2c.addressMask }}</size>
                <usage>registers</usage>
            </addressBlock>
            <registers>
            {% for key,register in registers|dictsort %}
                <register>
                    <name>{{key}}</name>
                    <description>{{register.description}}</description>
                    <addressOffset>{{register.address}}</addressOffset>
                    <size>{{register.length}}</size>
                    {% if (not 'readWrite' in register) or (register.readWrite == 'R/W') %}
                    <access>read-write</access>
                    {% elif register.readWrite == 'W' %}
                    <access>write</access>
                    {% else %}
                    <access>read</access>
                    {% endif %}
                    <resetValue>0x00</resetValue>
                    <resetMask>0xFF</resetMask>
                    <dataType>{{ numtype(register.length) }}</dataType>
                    {% if fields %}
                    {%- set template = namespace(fields=false) -%}
                    {% for fkey,field in fields|dictsort -%}
                    {% if field.register == '#/registers/' + key and field.readWrite != 'n' %}
                    {% if template.fields == false %}
                    <fields>
                    {% set template.fields = true %}
                    {% endif %}
                        <field>
                            <name>{{ fkey }}</name>
                            <description>
                            {{ field.description }}
                            </description>
                            <lsb>{{ field.bitEnd }}</lsb>
                            <msb>{{ field.bitStart }}</msb>
                            {% if field.readWrite == 'W' %}
                            <access>write-only</access>
                            {% elif field.readWrite == 'R' %}
                            <access>read-only</access>
                            {% elif field.readWrite == 'R/W' %}
                            <access>read-write</access>
                            {% endif %}
                            {% if field.type == 'enum' %}
                            <enumeratedValues>
                            {% for ekey,enum in field.enum|dictsort %}
                                <enumeratedValue>
                                    <name>{{ ekey }}</name>
                                    <description>{{ enum.title }}</description>
                                    <value>{{ enum.value }}</value>
                                </enumeratedValue>
                            {% endfor %}
                            </enumeratedValues>
                            {% endif %}
                            <writeConstraint>
                                {% if field.type == 'enum' %}
                                <useEnumeratedValues>true</useEnumeratedValues>
                                {% endif %}
                                <range>
                                    <minimum>{{field.bitEnd}}</minimum>
                                    <maximum>{{field.bitStart}}</maximum>
                                </range>
                            </writeConstraint>
                        </field>
                    {% endif %}
                    {% endfor %}
                    {{- "                    </fields>" if template.fields == 1 }}
                    {% endif %}
                </register>
            {% endfor %}
            </registers>
        </peripheral>
    </peripherals>
</device>