From d5a733ea988098cfb486dce640c8057b9b039906 Mon Sep 17 00:00:00 2001 From: octycs Date: Sat, 11 May 2019 16:06:51 +0200 Subject: [PATCH] Allow overwriting enumeratedValues and add exemplary patch --- README.md | 11 +++++++++++ patch/attiny85.yaml | 2 ++ patch/common/ac.yaml | 2 ++ patch/common/ac/acsr.yaml | 11 +++++++++++ patch/svdpatch.py | 22 +++++++++++++++++----- 5 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 patch/attiny85.yaml create mode 100644 patch/common/ac.yaml create mode 100644 patch/common/ac/acsr.yaml diff --git a/README.md b/README.md index 6d3b8ce..4258a8d 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,17 @@ Via the feature you can select which chip you want the register specifications f ### Adding a new Chip To add a new chip, download the *atdf* from and place it in `vendor/`. Be sure to name it like the Rust module that should be generated. Next, you need to integrate it into the base crate and build system. Follow what was done in [this commit](https://github.com/Rahix/avr-device/commit/8b1679a89704f5a303a1578b261aa2aee53e1251). Please adhere to the alphabetical sorting that is present so far. +If patches need to be applied, create a `.yaml` in `patch/`. The patching format is compatible to the [YAML patching format of stm32](https://github.com/stm32-rs/stm32-rs/#device-and-peripheral-yaml-format), except that you do not need to provide the path to the svd file (`_svd: ...`) since it is generated in the build process. Additionally, the following patching was added: +```yaml +PERIPHERIAL: + REGISTER: + FIELD: + # Replaces the enumeratedValues definition for this field. + # If it does not exist yet, it is created. + _replace_enum: + NAME: [VALUE, DESCRIPTION] +``` + ## License *avr-device* is licensed under either of diff --git a/patch/attiny85.yaml b/patch/attiny85.yaml new file mode 100644 index 0000000..9632e98 --- /dev/null +++ b/patch/attiny85.yaml @@ -0,0 +1,2 @@ +_include: + - "common/ac.yaml" diff --git a/patch/common/ac.yaml b/patch/common/ac.yaml new file mode 100644 index 0000000..c39cf7c --- /dev/null +++ b/patch/common/ac.yaml @@ -0,0 +1,2 @@ +_include: + - "ac/acsr.yaml" diff --git a/patch/common/ac/acsr.yaml b/patch/common/ac/acsr.yaml new file mode 100644 index 0000000..0531e03 --- /dev/null +++ b/patch/common/ac/acsr.yaml @@ -0,0 +1,11 @@ +AC: + ACSR: + _modify: + ACIS: + description: "Analog Comparator Interrupt Mode Select" + ACIS: + _replace_enum: + ON_TOGGLE: [0, "Interrupt on Toggle"] + # Leaving [1, 'Reserved'] out + ON_FALLING_EDGE: [2, "Interrupt on Falling Edge"] + ON_RISING_EDGE: [3, "Interrupt on Rising Edge"] diff --git a/patch/svdpatch.py b/patch/svdpatch.py index 4dba5a8..4e47ada 100644 --- a/patch/svdpatch.py +++ b/patch/svdpatch.py @@ -6,6 +6,9 @@ Licensed under the MIT and Apache 2.0 licenses. See LICENSE files for details. This script was adapted from stm32-rs (https://github.com/stm32-rs/stm32-rs/blob/master/scripts/svdpatch.py). To be integrated into the build system, support for an input / output svd argument is added. + +Additional changes: + - Support for _replace_enum to overwrite existing """ import copy @@ -595,6 +598,11 @@ def process_register_split(rtag, fspec): def process_field_enum(pname, rtag, fspec, field, usage="read-write"): """Add an enumeratedValues given by field to all fspec in rtag.""" + replace_if_exists = False + if "_replace_enum" in field: + field = field["_replace_enum"] + replace_if_exists = True + derived = None for ftag in iter_fields(rtag, fspec): name = ftag.find('name').text @@ -603,12 +611,16 @@ def process_field_enum(pname, rtag, fspec, field, usage="read-write"): enum_name = enum.find('name').text enum_usage = enum.find('usage').text for ev in ftag.iter('enumeratedValues'): - ev_usage = ev.find('usage').text + ev_usage_tag = ev.find('usage') + ev_usage = ev_usage_tag.text if ev_usage_tag is not None else 'read-write' if ev_usage == enum_usage or ev_usage == "read-write": - print(pname, fspec, field) - raise SvdPatchError( - "{}: field {} already has enumeratedValues for {}" - .format(pname, name, ev_usage)) + if replace_if_exists: + ftag.remove(ev) + else: + print(pname, fspec, field) + raise SvdPatchError( + "{}: field {} already has enumeratedValues for {}. Use '_replace_enum' to overwrite." + .format(pname, name, ev_usage)) ftag.append(enum) derived = make_derived_enumerated_values(enum_name) else: -- 2.48.1