Implement special register access mode to write to AVR protected registers#784
Implement special register access mode to write to AVR protected registers#784G33KatWork wants to merge 1 commit into
Conversation
If ATDF contains this information, |
|
I just checked again. There is a key link missing. The CCP register is defined and the two magic values that are required to be written into it to either unlock an IOREG or perform a flash memory write. However, if we look at the register description for an IOREG-protected register like CLKCTRL.MCLKCTRLB, I don't see mentioned that it is actually IOREG protected. So I think I was wrong. I have this stuff laying around for a while now and must've forgotten that this link is missing. |
|
Generic part looks good to me. But I still don't see sense to merge this without As we have a possibility to assign |
9298272 to
8d32b00
Compare
|
I just revisited the patch and implemented the Once this patch has been merged, I'll put up a PR for avr-device that makes use of this feature. |
Add an `avr` target to svd2rust, whose main purpose is support for the
configuration change protection (CCP) mechanism of modern (xmega based)
AVR cores: certain registers only accept writes within four instructions
after a magic value has been written to an unlock register (`CPU.CCP`).
The support consists of two parts:
First, a new generic file (`generic_avr_ccp.rs`, included for the `avr`
target) provides the `UnlockRegister` and `Protected` marker traits and,
for protected registers, `write_protected`/`modify_protected` methods.
These mirror `write`/`modify` but perform the unlock sequence in inline
assembly, so the protected write is guaranteed to hit the four
instruction window regardless of optimization level.
Second, since SVD files carry no information about CCP (neither which
register unlocks protected writes, nor which registers are protected),
the protected register list is declared in the `--settings` YAML file,
mirroring the RISC-V approach:
avr_config:
ccp:
unlock_register: "CPU.CCP"
protected_registers:
- { register: "NVMCTRL.CTRLA", magic: 0x9D }
- { register: "WDT.CTRLA", magic: 0xD8 }
For every listed register, an `impl Protected` is emitted at the end of
the generated device module.
Design notes:
- The unlock register's address is derived from the SVD (peripheral
base address plus register offset) rather than duplicated in the
settings file; only its `PERIPHERAL.REGISTER` path is given.
- The magic is a raw byte rather than a named signature (SPM/IOREG) to
keep svd2rust agnostic of particular AVR families.
- Registers referenced in the settings file are resolved against the
SVD and generation fails on unknown paths, so typos surface as build
errors instead of silently missing impls.
- The impls use module-relative paths (`cpu::ccp::CCP_SPEC`), so they
are correct regardless of where the device module is mounted in the
PAC crate (e.g. `avr-device` places it under `crate::<mcu>`).
- A new `Settings::from_yaml` constructor lets PACs that drive svd2rust
as a library (like `avr-device`) parse settings files without
depending on `serde_yaml` themselves; the CLI uses it too.
The newer AVR Attiny chips contain a mechanism Microchip calls CCP: Configuration Change Protection.
There are a few special registers that configure system-level peripherals like the clock- or the flash-controller which should not be modified by accident. To prevent that, a special unlock byte has to be written to the CCP unlock register and then the protected register has to be written within the next four executed instructions. See chapter 8.5.7 on page 56 on the linked datasheet for more details.
I tried doing this using the regular register access mechanisms, but the rust compiler always reordered and optimized code so that this didn't work out. Even if it would have, it wouldn't be exactly reliable.
This patch adds a new Target type for AVRs and if it's selected the new file
generic_avr_ccp.rsis emitted when generating code. This file contains a bunch of traits to define an unlock register, protected registers and blanket implementations that implement protected writes in case a register implements the aforementioned marker traits.This has to be used in conjunction with atdf2svd to get an SVD. After generating the pac, you still need to manually define the list of protected registers like this:
I have not found a way yet to figure out the information that a register is protected by using the ATDF/SVD.
There will be a follow-up PR on avr-device which uses svd2rust to generate PACs for all kinds of AVR devices. I added support for the attiny817 which depends on this new CCP register access code.
As soon as I submitted that PR, I'll cross-reference it.
I am open to suggestions on how to make the code nicer. Especially the parts written in assembler. It was very finicky to make this work and I am still not 100% satisfied with how it looks. I tried getting rid of the
ldiinstruction, but couldn't make it work.