|
| 1 | +use crate::{svd::Peripheral, util, Config}; |
| 2 | +use anyhow::{anyhow, Context, Result}; |
| 3 | +use log::debug; |
| 4 | +use proc_macro2::{Span, TokenStream}; |
| 5 | +use quote::quote; |
| 6 | +use svd_parser::svd::{Access, Register}; |
| 7 | + |
| 8 | +/// Whole AVR-specific generation. |
| 9 | +/// |
| 10 | +/// SVD files for AVR devices carry no information about the configuration |
| 11 | +/// change protection (CCP) mechanism of modern (xmega based) AVR cores, so the |
| 12 | +/// list of protected registers and their unlock magic comes from the settings |
| 13 | +/// file ([`crate::config::avr::AvrConfig`]). Here we translate that list into |
| 14 | +/// `UnlockRegister` and `Protected` trait implementations for the generated |
| 15 | +/// register types, so that protected registers can be written with |
| 16 | +/// `write_protected`/`modify_protected` from `generic_avr_ccp.rs`. |
| 17 | +pub fn render(peripherals: &[Peripheral], config: &Config) -> Result<TokenStream> { |
| 18 | + let mut mod_items = TokenStream::new(); |
| 19 | + |
| 20 | + let Some(ccp) = config |
| 21 | + .settings |
| 22 | + .avr_config |
| 23 | + .as_ref() |
| 24 | + .and_then(|avr| avr.ccp.as_ref()) |
| 25 | + else { |
| 26 | + return Ok(mod_items); |
| 27 | + }; |
| 28 | + |
| 29 | + debug!("Rendering AVR configuration change protection impls"); |
| 30 | + |
| 31 | + // The unlock register itself is not listed as protected in the SVD either; |
| 32 | + // its address is all the unlock sequence needs, and we can derive that |
| 33 | + // from the SVD instead of asking for it in the settings file. |
| 34 | + let (unlock_periph, unlock_reg, unlock_addr) = |
| 35 | + resolve_register(peripherals, &ccp.unlock_register) |
| 36 | + .context("can't resolve CCP unlock register")?; |
| 37 | + |
| 38 | + // The unlock sequence writes the magic with `out`, which can only reach |
| 39 | + // I/O addresses below 0x40. On every CCP-bearing core those are identical |
| 40 | + // to the data-space addresses the SVD describes; reject anything else so |
| 41 | + // a bad settings entry fails here instead of in the assembler. |
| 42 | + if unlock_addr >= 0x40 { |
| 43 | + return Err(anyhow!( |
| 44 | + "CCP unlock register {} is at address {:#x}, but `out` can only \ |
| 45 | + reach I/O addresses below 0x40", |
| 46 | + ccp.unlock_register, |
| 47 | + unlock_addr |
| 48 | + )); |
| 49 | + } |
| 50 | + |
| 51 | + // Anchor the address on the peripheral's `PeripheralSpec::ADDRESS` const |
| 52 | + // instead of duplicating it as a literal; only the register's byte offset |
| 53 | + // is emitted directly. |
| 54 | + let unlock_spec = register_spec_path(unlock_periph, unlock_reg, config); |
| 55 | + let unlock_periph_spec = util::ident( |
| 56 | + &unlock_periph.name, |
| 57 | + config, |
| 58 | + "peripheral_spec", |
| 59 | + Span::call_site(), |
| 60 | + ); |
| 61 | + let unlock_offset = util::hex(unlock_reg.address_offset as u64); |
| 62 | + |
| 63 | + mod_items.extend(quote! { |
| 64 | + impl crate::UnlockRegister for #unlock_spec { |
| 65 | + const ADDR: u8 = |
| 66 | + <#unlock_periph_spec as crate::PeripheralSpec>::ADDRESS as u8 + #unlock_offset; |
| 67 | + } |
| 68 | + }); |
| 69 | + |
| 70 | + for protected in &ccp.protected_registers { |
| 71 | + let (periph, reg, _) = resolve_register(peripherals, &protected.register) |
| 72 | + .with_context(|| format!("can't resolve protected register {}", protected.register))?; |
| 73 | + |
| 74 | + // The unlock sequence in `generic_avr_ccp.rs` only supports 8-bit |
| 75 | + // writable registers (`RegisterSpec<Ux = u8> + Writable`); catch |
| 76 | + // mismatches here instead of failing later in the PAC build. Size and |
| 77 | + // access are usually inherited defaults on AVR SVDs, so only reject |
| 78 | + // explicit contradictions. |
| 79 | + if let Some(size) = reg.properties.size { |
| 80 | + if size != 8 { |
| 81 | + return Err(anyhow!( |
| 82 | + "protected register {} is {} bits wide; only 8-bit registers are supported", |
| 83 | + protected.register, |
| 84 | + size |
| 85 | + )); |
| 86 | + } |
| 87 | + } |
| 88 | + if reg.properties.access == Some(Access::ReadOnly) { |
| 89 | + return Err(anyhow!( |
| 90 | + "protected register {} is read-only", |
| 91 | + protected.register |
| 92 | + )); |
| 93 | + } |
| 94 | + |
| 95 | + let reg_spec = register_spec_path(periph, reg, config); |
| 96 | + // Emit the magic as a hex literal so the generated code matches the |
| 97 | + // datasheet notation (0x9D/0xD8) instead of decimal. |
| 98 | + let magic = util::hex(protected.magic as u64); |
| 99 | + |
| 100 | + mod_items.extend(quote! { |
| 101 | + impl crate::Protected for #reg_spec { |
| 102 | + const MAGIC: u8 = #magic; |
| 103 | + type CcpReg = #unlock_spec; |
| 104 | + } |
| 105 | + }); |
| 106 | + } |
| 107 | + |
| 108 | + Ok(mod_items) |
| 109 | +} |
| 110 | + |
| 111 | +/// Look up a `PERIPHERAL.REGISTER` path from the settings file in the SVD and |
| 112 | +/// return the peripheral, the register and the register's data-space address. |
| 113 | +/// |
| 114 | +/// Registers of derived peripherals are found on the base peripheral, but the |
| 115 | +/// address is computed from the derived peripheral's own base address. |
| 116 | +fn resolve_register<'a>( |
| 117 | + peripherals: &'a [Peripheral], |
| 118 | + path: &str, |
| 119 | +) -> Result<(&'a Peripheral, &'a Register, u64)> { |
| 120 | + let (periph_name, reg_name) = path.split_once('.').ok_or_else(|| { |
| 121 | + anyhow!("register path `{path}` must have the form `PERIPHERAL.REGISTER`") |
| 122 | + })?; |
| 123 | + |
| 124 | + let periph = peripherals |
| 125 | + .iter() |
| 126 | + .find(|p| p.name == periph_name) |
| 127 | + .ok_or_else(|| anyhow!("no peripheral named `{periph_name}` in the SVD"))?; |
| 128 | + |
| 129 | + // Follow `derivedFrom` (once, as mandated by the SVD spec) in case the |
| 130 | + // peripheral inherits its registers from another one. |
| 131 | + let base = match periph.derived_from.as_deref() { |
| 132 | + Some(base_name) => peripherals |
| 133 | + .iter() |
| 134 | + .find(|p| p.name == base_name) |
| 135 | + .ok_or_else(|| { |
| 136 | + anyhow!("peripheral `{periph_name}` is derived from unknown `{base_name}`") |
| 137 | + })?, |
| 138 | + None => periph, |
| 139 | + }; |
| 140 | + |
| 141 | + let reg = base |
| 142 | + .registers() |
| 143 | + .find(|r| r.name == reg_name) |
| 144 | + .ok_or_else(|| anyhow!("no register named `{reg_name}` in peripheral `{periph_name}`"))?; |
| 145 | + |
| 146 | + let address = periph.base_address + reg.address_offset as u64; |
| 147 | + Ok((periph, reg, address)) |
| 148 | +} |
| 149 | + |
| 150 | +/// Build the module-relative path to a register's spec type, e.g. |
| 151 | +/// `cpu::ccp::CCP_SPEC`. The impls are emitted at the top level of the device |
| 152 | +/// module where all peripheral modules are siblings, so no crate-level prefix |
| 153 | +/// is needed. |
| 154 | +fn register_spec_path(periph: &Peripheral, reg: &Register, config: &Config) -> TokenStream { |
| 155 | + let span = Span::call_site(); |
| 156 | + let periph_mod = util::ident(&periph.name, config, "peripheral_mod", span); |
| 157 | + let reg_mod = util::ident(®.name, config, "register_mod", span); |
| 158 | + let reg_spec = util::ident(®.name, config, "register_spec", span); |
| 159 | + quote! { #periph_mod::#reg_mod::#reg_spec } |
| 160 | +} |
0 commit comments