Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --mark_range flag #576

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/generate/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use proc_macro2::{Ident, Span, TokenStream};
use quote::{quote, ToTokens};

use log::debug;
use std::collections::HashSet;
use std::fs::File;
use std::io::Write;

Expand Down Expand Up @@ -245,6 +246,24 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<Toke
exprs.extend(quote!(#id: #id { _marker: PhantomData },));
}

debug!("Rendering markers");
let mut markers = TokenStream::new();
let unique_markers: HashSet<_> = config.mark_ranges.iter().map(|m| m.name.as_str()).collect();
for marker in unique_markers {
let marker = Ident::new(marker, Span::call_site());
markers.extend(quote! {
#[doc = "Marker trait"]
pub trait #marker {}
});
}

out.extend(quote! {
#[doc(hidden)]
pub mod markers {
#markers
}
});

let span = Span::call_site();
let take = match config.target {
Target::CortexM => Some(Ident::new("cortex_m", span)),
Expand Down
18 changes: 18 additions & 0 deletions src/generate/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use core::u64;
use log::warn;
use proc_macro2::{Ident, Punct, Spacing, Span, TokenStream};
use quote::{quote, ToTokens};
use std::collections::HashSet;

use crate::util::{self, Config, ToSanitizedSnakeCase, ToSanitizedUpperCase, U32Ext};
use anyhow::{anyhow, Result};
Expand Down Expand Up @@ -48,6 +49,7 @@ pub fn render(
let mut mod_items = TokenStream::new();
let mut r_impl_items = TokenStream::new();
let mut w_impl_items = TokenStream::new();
let mut marker_impl_items = TokenStream::new();
let mut methods = vec![];

let can_read = access.can_read();
Expand Down Expand Up @@ -267,6 +269,22 @@ pub fn render(
});
}

let address = peripheral.base_address + register.address_offset as u64;
let markers: HashSet<_> = config
.mark_ranges
.iter()
.filter(|m| m.start <= address && address < m.end)
.map(|m| m.name.as_str())
.collect();
for marker in markers {
let name = Ident::new(marker, span);
marker_impl_items.extend(quote! {
impl crate::markers::#name for #name_uc_spec {}
});
}

mod_items.extend(marker_impl_items);

out.extend(quote! {
#[doc = #description]
pub mod #name_sc #open
Expand Down
29 changes: 28 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use clap::{App, Arg};

use svd2rust::{
generate, load_from,
util::{build_rs, Config, SourceType, Target},
util::{build_rs, parse_address, Config, MarkRange, SourceType, Target},
};

fn run() -> Result<()> {
Expand Down Expand Up @@ -103,6 +103,20 @@ fn run() -> Result<()> {
.takes_value(true)
.possible_values(&["off", "error", "warn", "info", "debug", "trace"]),
)
.arg(
Arg::with_name("mark_range")
.long("mark_range")
.multiple(true)
.number_of_values(3)
.help("Mark registers in given range with a marker trait")
.long_help(
"Mark registers in given range with a marker trait.
The first argument after the flag sets the name for the marker trait.
The second and third argument are the start and end addresses of registers
which this trait is applied for.",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this deserves an example of how it could look.

)
.value_names(&["trait", "range start", "range end"]),
)
.version(concat!(
env!("CARGO_PKG_VERSION"),
include_str!(concat!(env!("OUT_DIR"), "/commit-info.txt"))
Expand Down Expand Up @@ -168,6 +182,18 @@ fn run() -> Result<()> {
source_type = SourceType::from_path(Path::new(file))
}

let mut mark_range_iter = cfg.values("mark_range", Filter::Arg).into_iter().flatten();
let mut mark_ranges = Vec::new();
while let (Some(name), Some(start), Some(end)) = (
mark_range_iter.next(),
mark_range_iter.next(),
mark_range_iter.next(),
) {
let start = parse_address(&start).context("Invalid range start")?;
let end = parse_address(&end).context("Invalid range end")?;
mark_ranges.push(MarkRange { name, start, end });
}

let config = Config {
target,
nightly,
Expand All @@ -179,6 +205,7 @@ fn run() -> Result<()> {
strict,
output_dir: path.clone(),
source_type,
mark_ranges,
};

info!("Parsing device from SVD file");
Expand Down
20 changes: 19 additions & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::borrow::Cow;
use std::{borrow::Cow, num::ParseIntError};

use crate::svd::{
Access, Cluster, Field, Register, RegisterCluster, RegisterInfo, RegisterProperties,
Expand Down Expand Up @@ -28,6 +28,7 @@ pub struct Config {
pub strict: bool,
pub output_dir: PathBuf,
pub source_type: SourceType,
pub mark_ranges: Vec<MarkRange>,
}

impl Default for Config {
Expand All @@ -43,10 +44,19 @@ impl Default for Config {
strict: false,
output_dir: PathBuf::from("."),
source_type: SourceType::default(),
mark_ranges: Vec::new(),
}
}
}

#[derive(Clone, PartialEq, Debug)]

pub struct MarkRange {
pub name: String,
pub start: u64,
pub end: u64,
}

#[allow(clippy::upper_case_acronyms)]
#[allow(non_camel_case_types)]
#[derive(Clone, Copy, PartialEq, Debug)]
Expand Down Expand Up @@ -435,3 +445,11 @@ impl FullName for RegisterInfo {
}
}
}

pub fn parse_address(addr: &str) -> Result<u64, ParseIntError> {
if addr.starts_with("0x") {
u64::from_str_radix(&addr[2..], 16)
} else {
u64::from_str_radix(addr, 10)
}
}