Skip to content

Commit 0faef6f

Browse files
mciantyreburrbull
authored andcommitted
Allow renames of derived peripherals
This commit enables two features (F): F1. We're permitted to rename a peripheral that has a derivedFrom attribute. F2. When we rename a peripheral that has derivations, svd patch will automatically update all derivations to use the new name. These behaviors support renames of peripheral hierarchies. Suppose a chip has two USB peripherals: USB_OTG1 and USB_OTG2. USB_OTG2 derives from USB_OTG1. Before this commit, renaming these peripherals to USB1 and USB2 looks like this: _modify: # Step 1: Rename the base peripheral USB_OTG1 => USB1. _peripherals: USB_OTG1: name: USB1 _delete: # Step 2: Delete USB_OTG2, because it can't be renamed. - USB_OTG2 _add: # Step 3: Add USB2, derived from USB1, with values from the SVD. USB2: derivedFrom: USB1 baseAddress: 0x4042C000 addressBlock: # Re-define address block interrupts: # Re-define interrupts Step 2 is required because modifications don't apply to derivedFrom peripherals (line 180 in README.md). Step 3 means we duplicate SVD information which was otherwise correct. This could be cumbersome when trying to rename many instances. (I wasn't able to figure out any other way to achieve this rename. Let me know if this is already supported using other svdtool directives.) F1 makes it easier to achieve the rename. There's no need for steps 2 and 3. When combined with F2, we write _modify: _peripherals: USB_OTG1: name: USB1 USB_OTG2: name: USB2 F2 is a convenience. Without it, it's possible to use _derive directives to fix the names. (You can't use _rebase though; _rebase needs both peripherals to exist.) But _derive seems to focus on collapsing independently-defined peripherals, which is not what we're doing when changing peripheral names.
1 parent bd760e5 commit 0faef6f

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

svdtools/patch.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,22 @@ def modify_peripheral(self, pspec, pmod):
435435
"""Modify pspec inside device according to pmod."""
436436
for ptag in self.iter_peripherals(pspec):
437437
for (key, value) in pmod.items():
438+
if key == "name":
439+
# If this peripheral has derivations, update the derived
440+
# peripherals to reference the new name.
441+
parent = self.device.find("peripherals")
442+
old_name = ptag.find("name").text
443+
for derived in parent.findall(
444+
"./peripheral[@derivedFrom='{}']".format(old_name)
445+
):
446+
derived.set("derivedFrom", value)
447+
ptag.find("name").text = value
448+
continue
449+
450+
# We do not change derivedFrom peripherals beyond this point.
451+
if "derivedFrom" in ptag.attrib:
452+
continue
453+
438454
if key == "addressBlock":
439455
ab = ptag.find(key)
440456
for (ab_key, ab_value) in value.items():

0 commit comments

Comments
 (0)