Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/).

## [Unreleased]

- Use marker struct instead of address in `Periph` with `PeripheralSpec` trait

## [v0.37.0] - 2025-08-14

- Fix new `mismatched-lifetime-syntaxes` lint warnings
Expand Down
6 changes: 4 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,12 @@ impl IdentFormats {
("register".into(), pascal.clone()),
("cluster".into(), pascal.clone()),
("register_spec".into(), pascal.clone().suffix("Spec")),
("peripheral".into(), pascal),
("peripheral".into(), pascal.clone()),
(
"peripheral_singleton".into(),
IdentFormat::default().snake_case(),
),
("peripheral_spec".into(), pascal.suffix("Spec")),
]);

map
Expand All @@ -309,7 +310,8 @@ impl IdentFormats {
("register".into(), constant.clone()),
("register_spec".into(), constant.clone().suffix("_SPEC")),
("peripheral".into(), constant.clone()),
("peripheral_singleton".into(), constant),
("peripheral_singleton".into(), constant.clone()),
("peripheral_spec".into(), constant.suffix("_SPEC")),
]);

map
Expand Down
32 changes: 24 additions & 8 deletions src/generate/generic.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
use core::marker;

/// Generic peripheral accessor
pub struct Periph<RB, const A: usize> {
_marker: marker::PhantomData<RB>,
pub struct Periph<PER: PeripheralSpec> {
_marker: marker::PhantomData<PER>,
}

unsafe impl<RB, const A: usize> Send for Periph<RB, A> {}
/// Peripheral data
pub trait PeripheralSpec {
/// RegisterBlock associated with peripheral
type RB;
/// Address of the register block
const ADDRESS: usize;
/// Debug name
const NAME: &'static str;
}

unsafe impl<PER: PeripheralSpec> Send for Periph<PER> {}

impl<PER: PeripheralSpec> core::fmt::Debug for Periph<PER> {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
f.debug_struct(PER::NAME).finish()
}
}

impl<RB, const A: usize> Periph<RB, A> {
impl<PER: PeripheralSpec> Periph<PER> {
///Pointer to the register block
pub const PTR: *const RB = A as *const _;
pub const PTR: *const PER::RB = PER::ADDRESS as *const _;

///Return the pointer to the register block
#[inline(always)]
pub const fn ptr() -> *const RB {
pub const fn ptr() -> *const PER::RB {
Self::PTR
}

Expand All @@ -37,8 +53,8 @@ impl<RB, const A: usize> Periph<RB, A> {
}
}

impl<RB, const A: usize> core::ops::Deref for Periph<RB, A> {
type Target = RB;
impl<PER: PeripheralSpec> core::ops::Deref for Periph<PER> {
type Target = PER::RB;

#[inline(always)]
fn deref(&self) -> &Self::Target {
Expand Down
26 changes: 20 additions & 6 deletions src/generate/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,25 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
feature_attribute: &TokenStream,
doc: &str,
p_ty: &Ident,
name_str: &str,
doc_alias: Option<TokenStream>,
address: LitInt| {
let pspec = ident(name_str, config, "peripheral_spec", Span::call_site());
out.extend(quote! {
#[doc = #doc]
#phtml
#doc_alias
#feature_attribute
pub type #p_ty = crate::Periph<#base::RegisterBlock, #address>;
pub type #p_ty = crate::Periph<#pspec>;

#feature_attribute
impl core::fmt::Debug for #p_ty {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
f.debug_struct(#name_str).finish()
}
pub struct #pspec;

#feature_attribute
impl crate::PeripheralSpec for #pspec {
type RB = #base::RegisterBlock;
const ADDRESS: usize = #address;
const NAME: &'static str = #name_str;
}
});
};
Expand Down Expand Up @@ -113,6 +118,7 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
&feature_attribute_n,
&doc,
&p_ty,
&name_str,
doc_alias,
address,
);
Expand All @@ -138,7 +144,15 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
feature_attribute.extend(quote! { #[cfg(feature = #p_feature)] })
};
// Insert the peripheral structure
per_to_tokens(&mut out, &feature_attribute, &doc, &p_ty, None, address);
per_to_tokens(
&mut out,
&feature_attribute,
&doc,
&p_ty,
&name_str,
None,
address,
);

// Derived peripherals may not require re-implementation, and will instead
// use a single definition of the non-derived version.
Expand Down
Loading