Skip to content

Commit

Permalink
inline Settings into Config
Browse files Browse the repository at this point in the history
  • Loading branch information
burrbull committed Nov 11, 2024
1 parent 74eee32 commit 4dfd16b
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 38 deletions.
18 changes: 16 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ pub struct Config {
pub ident_formats_theme: Option<IdentFormatsTheme>,
pub field_names_for_enums: bool,
pub base_address_shift: u64,
pub html_url: Option<url::Url>,
/// Path to YAML file with chip-specific settings
pub settings: Option<PathBuf>,
pub settings_file: Option<PathBuf>,
/// Chip-specific settings
pub settings: Settings,
}

#[allow(clippy::upper_case_acronyms)]
Expand Down Expand Up @@ -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<url::Url>,
/// RISC-V specific settings
pub riscv_config: Option<riscv::RiscvConfig>,
}

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;
27 changes: 3 additions & 24 deletions src/generate/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,6 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<Toke
}
};

let settings = match config.settings.as_ref() {
#[cfg(feature = "yaml")]
Some(settings) => {
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! {
Expand Down Expand Up @@ -203,7 +190,7 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<Toke

match config.target {
Target::RISCV => {
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(
Expand All @@ -214,12 +201,7 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<Toke
)?);
} else {
debug!("Rendering RISC-V specific code");
out.extend(riscv::render(
&d.peripherals,
device_x,
settings.as_ref().unwrap(),
config,
)?);
out.extend(riscv::render(&d.peripherals, device_x, config)?);
}
}
_ => {
Expand All @@ -241,10 +223,7 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<Toke
// Core peripherals are handled above
continue;
}
if config.target == Target::RISCV
&& settings.is_some()
&& riscv::is_riscv_peripheral(p, settings.as_ref().unwrap())
{
if config.target == Target::RISCV && riscv::is_riscv_peripheral(p, &config.settings) {
// RISC-V specific peripherals are handled above
continue;
}
Expand Down
5 changes: 2 additions & 3 deletions src/generate/riscv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ pub fn is_riscv_peripheral(p: &Peripheral, s: &Settings) -> bool {
pub fn render(
peripherals: &[Peripheral],
device_x: &mut String,
settings: &Settings,
config: &Config,
) -> Result<TokenStream> {
let mut mod_items = TokenStream::new();
Expand All @@ -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 */")?;
Expand Down Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,22 @@ pub fn generate(input: &str, config: &Config) -> Result<Generation> {
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(),
Expand Down
25 changes: 16 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 4dfd16b

Please sign in to comment.