diff --git a/src/config.rs b/src/config.rs index 5a48105c..b206d075 100644 --- a/src/config.rs +++ b/src/config.rs @@ -35,9 +35,10 @@ pub struct Config { pub ident_formats_theme: Option, pub field_names_for_enums: bool, pub base_address_shift: u64, - pub html_url: Option, /// Path to YAML file with chip-specific settings - pub settings: Option, + pub settings_file: Option, + /// Chip-specific settings + pub settings: Settings, } #[allow(clippy::upper_case_acronyms)] @@ -320,8 +321,21 @@ pub enum IdentFormatsTheme { #[non_exhaustive] /// Chip-specific settings pub struct Settings { + /// Path to chip HTML generated by svdtools + pub html_url: Option, /// RISC-V specific settings pub riscv_config: Option, } +impl Settings { + pub fn update_from(&mut self, source: Self) { + if source.html_url.is_some() { + self.html_url = source.html_url; + } + if source.riscv_config.is_some() { + self.riscv_config = source.riscv_config; + } + } +} + pub mod riscv; diff --git a/src/generate/device.rs b/src/generate/device.rs index 677b7e8d..d2b3ee9c 100644 --- a/src/generate/device.rs +++ b/src/generate/device.rs @@ -28,19 +28,6 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result { - let file = std::fs::read_to_string(settings).context("could not read settings file")?; - Some(serde_yaml::from_str(&file).context("could not parse settings file")?) - } - #[cfg(not(feature = "yaml"))] - Some(_) => { - return Err(anyhow::anyhow!("Support for yaml config files is not available because svd2rust was compiled without the yaml feature")); - } - None => None, - }; - // make_mod option explicitly disables inner attributes. if config.target == Target::Msp430 && !config.make_mod { out.extend(quote! { @@ -203,7 +190,7 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result { - if settings.is_none() { + if config.settings.riscv_config.is_none() { warn!("No settings file provided for RISC-V target. Using legacy interrupts rendering"); warn!("Please, consider migrating your PAC to riscv 0.12.0 or later"); out.extend(interrupt::render( @@ -214,12 +201,7 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result { @@ -241,10 +223,7 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result bool { pub fn render( peripherals: &[Peripheral], device_x: &mut String, - settings: &Settings, config: &Config, ) -> Result { let mut mod_items = TokenStream::new(); @@ -30,7 +29,7 @@ pub fn render( .as_ref() .map(|feature| quote!(#[cfg_attr(feature = #feature, derive(defmt::Format))])); - if let Some(c) = settings.riscv_config.as_ref() { + if let Some(c) = config.settings.riscv_config.as_ref() { if !c.core_interrupts.is_empty() { debug!("Rendering target-specific core interrupts"); writeln!(device_x, "/* Core interrupt sources and trap handlers */")?; @@ -216,7 +215,7 @@ pub fn render( } let mut riscv_peripherals = TokenStream::new(); - if let Some(c) = &settings.riscv_config { + if let Some(c) = config.settings.riscv_config.as_ref() { let harts = match c.harts.is_empty() { true => vec![], false => c diff --git a/src/lib.rs b/src/lib.rs index edec886e..b5d5c4ab 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -628,6 +628,22 @@ pub fn generate(input: &str, config: &Config) -> Result { use std::fmt::Write; let mut config = config.clone(); + + match config.settings_file.as_ref() { + #[cfg(feature = "yaml")] + Some(settings) => { + let file = std::fs::read_to_string(settings).context("could not read settings file")?; + config + .settings + .update_from(serde_yaml::from_str(&file).context("could not parse settings file")?) + } + #[cfg(not(feature = "yaml"))] + Some(_) => { + return Err(anyhow::anyhow!("Support for yaml config files is not available because svd2rust was compiled without the yaml feature")); + } + None => {} + }; + let mut ident_formats = match config.ident_formats_theme { Some(IdentFormatsTheme::Legacy) => IdentFormats::legacy_theme(), _ => IdentFormats::default_theme(), diff --git a/src/main.rs b/src/main.rs index 880bb4ab..27607bfc 100755 --- a/src/main.rs +++ b/src/main.rs @@ -101,7 +101,7 @@ fn run() -> Result<()> { .value_name("TOML_FILE"), ) .arg( - Arg::new("settings") + Arg::new("settings_file") .long("settings") .help("Target-specific settings YAML file") .action(ArgAction::Set) @@ -276,14 +276,6 @@ Allowed cases are `unchanged` (''), `pascal` ('p'), `constant` ('c') and `snake` Useful for soft-cores where the peripheral address range isn't necessarily fixed. Ignore this option if you are not building your own FPGA based soft-cores."), ) - .arg( - Arg::new("html_url") - .long("html-url") - .alias("html_url") - .help("Path to chip HTML generated by svdtools") - .action(ArgAction::Set) - .value_name("URL"), - ) .arg( Arg::new("log_level") .long("log") @@ -330,6 +322,21 @@ Ignore this option if you are not building your own FPGA based soft-cores."), } } + match config.settings_file.as_ref() { + #[cfg(feature = "yaml")] + Some(settings) => { + let file = std::fs::read_to_string(settings).context("could not read settings file")?; + config + .settings + .update_from(serde_yaml::from_str(&file).context("could not parse settings file")?) + } + #[cfg(not(feature = "yaml"))] + Some(_) => { + return Err(anyhow::anyhow!("Support for yaml config files is not available because svd2rust was compiled without the yaml feature")); + } + None => {} + }; + if let Some(file) = config.input.as_ref() { config.source_type = SourceType::from_path(file) }