Skip to content

Commit

Permalink
add register reader/writer links
Browse files Browse the repository at this point in the history
  • Loading branch information
burrbull committed Aug 4, 2023
1 parent d19c646 commit 46a93f7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
## [Unreleased]

- Fix when `atomics` features is generated but not enabled
- move hidden structs into module, add register reader/writer links into `SPEC` docs (#736)
- removed register writer & reader wrappers, generic `REG` in field writers (#731)
- Updated syn to version 2 (#732)
- Let readable field fetch doc from svd description (#734)
Expand Down
34 changes: 25 additions & 9 deletions src/generate/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use proc_macro2::{Ident, Punct, Spacing, Span, TokenStream};
use quote::{quote, ToTokens};
use std::borrow::Cow;
use std::collections::HashSet;
use std::fmt::Write;
use svd_parser::expand::{
derive_enumerated_values, derive_field, BlockPath, EnumPath, FieldPath, Index, RegisterPath,
};
Expand Down Expand Up @@ -138,19 +139,28 @@ pub fn render_register_mod(
);

let mut mod_items = TokenStream::new();
let mut methods = vec![];

let can_read = access.can_read();
let can_write = access.can_write();
let can_reset = properties.reset_value.is_some();

let mut doc = format!("{description}\n");

fn method(s: &str) -> String {
format!("[`{s}`](crate::generic::Reg::{s})")
}

if can_read {
let desc = format!("Register `{}` reader", register.name);
mod_items.extend(quote! {
#[doc = #desc]
pub type R = crate::R<#regspec_ident>;
});
methods.push("read");
write!(
doc,
"\nThis register you can {} with [`R`].",
method("read")
)?;
}

if can_write {
Expand All @@ -159,17 +169,29 @@ pub fn render_register_mod(
#[doc = #desc]
pub type W = crate::W<#regspec_ident>;
});
let mut methods = Vec::new();
methods.push("write_with_zero");
if can_reset {
methods.push("reset");
methods.push("write");
}
write!(
doc,
"\nThis register you can {} with [`W`].",
methods
.iter()
.map(|m| method(m))
.collect::<Vec<_>>()
.join(", ")
)?;
}

if can_read && can_write {
methods.push("modify");
write!(doc, "\nAnd {} with [`R`] and [`W`].", method("modify"))?;
}

doc.push_str("\nSee [API](https://docs.rs/svd2rust/#read--modify--write-api).");

let mut r_impl_items = TokenStream::new();
let mut r_debug_impl = TokenStream::new();
let mut w_impl_items = TokenStream::new();
Expand Down Expand Up @@ -310,12 +332,6 @@ pub fn render_register_mod(
close.to_tokens(&mut mod_items);
}

let methods = methods
.iter()
.map(|s| format!("[`{s}`](crate::generic::Reg::{s})"))
.collect::<Vec<_>>();
let mut doc = format!("{description}\n\nThis register you can {}. See [API](https://docs.rs/svd2rust/#read--modify--write-api).", methods.join(", "));

if name_snake_case != "cfg" {
doc += format!(
"\n\nFor information about available fields see [{name_snake_case}](index.html) module"
Expand Down

0 comments on commit 46a93f7

Please sign in to comment.