Skip to content

Commit 56a10f3

Browse files
committed
PeripheralSpec
1 parent 87c2fc4 commit 56a10f3

File tree

4 files changed

+44
-14
lines changed

4 files changed

+44
-14
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
77

88
## [Unreleased]
99

10+
- Use marker struct instead of address in `Periph` with `PeripheralSpec` trait
11+
1012
## [v0.37.0] - 2025-08-14
1113

1214
- Fix new `mismatched-lifetime-syntaxes` lint warnings

src/config.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,11 +284,12 @@ impl IdentFormats {
284284
("register".into(), pascal.clone()),
285285
("cluster".into(), pascal.clone()),
286286
("register_spec".into(), pascal.clone().suffix("Spec")),
287-
("peripheral".into(), pascal),
287+
("peripheral".into(), pascal.clone()),
288288
(
289289
"peripheral_singleton".into(),
290290
IdentFormat::default().snake_case(),
291291
),
292+
("peripheral_spec".into(), pascal.suffix("Spec")),
292293
]);
293294

294295
map
@@ -309,7 +310,8 @@ impl IdentFormats {
309310
("register".into(), constant.clone()),
310311
("register_spec".into(), constant.clone().suffix("_SPEC")),
311312
("peripheral".into(), constant.clone()),
312-
("peripheral_singleton".into(), constant),
313+
("peripheral_singleton".into(), constant.clone()),
314+
("peripheral_spec".into(), constant.suffix("_SPEC")),
313315
]);
314316

315317
map

src/generate/generic.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
11
use core::marker;
22

33
/// Generic peripheral accessor
4-
pub struct Periph<RB, const A: usize> {
5-
_marker: marker::PhantomData<RB>,
4+
pub struct Periph<RB, PER> {
5+
_marker: marker::PhantomData<(RB, PER)>,
66
}
77

8-
unsafe impl<RB, const A: usize> Send for Periph<RB, A> {}
8+
pub trait PeripheralSpec {
9+
///Pointer to the register block
10+
const ADDRESS: usize;
11+
///Debug name
12+
const NAME: &str;
13+
}
14+
15+
unsafe impl<RB, PER> Send for Periph<RB, PER> {}
16+
17+
impl<RB, PER: PeripheralSpec> core::fmt::Debug for Periph<RB, PER> {
18+
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
19+
f.debug_struct(PER::NAME).finish()
20+
}
21+
}
922

10-
impl<RB, const A: usize> Periph<RB, A> {
23+
impl<RB, PER: PeripheralSpec> Periph<RB, PER> {
1124
///Pointer to the register block
12-
pub const PTR: *const RB = A as *const _;
25+
pub const PTR: *const RB = PER::ADDRESS as *const _;
1326

1427
///Return the pointer to the register block
1528
#[inline(always)]
@@ -37,7 +50,7 @@ impl<RB, const A: usize> Periph<RB, A> {
3750
}
3851
}
3952

40-
impl<RB, const A: usize> core::ops::Deref for Periph<RB, A> {
53+
impl<RB, PER: PeripheralSpec> core::ops::Deref for Periph<RB, PER> {
4154
type Target = RB;
4255

4356
#[inline(always)]

src/generate/peripheral.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,24 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
7272
feature_attribute: &TokenStream,
7373
doc: &str,
7474
p_ty: &Ident,
75+
name_str: &str,
7576
doc_alias: Option<TokenStream>,
7677
address: LitInt| {
78+
let pspec = ident(name_str, config, "peripheral_spec", Span::call_site());
7779
out.extend(quote! {
7880
#[doc = #doc]
7981
#phtml
8082
#doc_alias
8183
#feature_attribute
82-
pub type #p_ty = crate::Periph<#base::RegisterBlock, #address>;
84+
pub type #p_ty = crate::Periph<#base::RegisterBlock, #pspec>;
8385

8486
#feature_attribute
85-
impl core::fmt::Debug for #p_ty {
86-
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
87-
f.debug_struct(#name_str).finish()
88-
}
87+
pub struct #pspec;
88+
89+
#feature_attribute
90+
impl PeripheralSpec for #pspec {
91+
const ADDRESS: usize = #address;
92+
const NAME: &str = #name_str;
8993
}
9094
});
9195
};
@@ -113,6 +117,7 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
113117
&feature_attribute_n,
114118
&doc,
115119
&p_ty,
120+
&name_str,
116121
doc_alias,
117122
address,
118123
);
@@ -138,7 +143,15 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
138143
feature_attribute.extend(quote! { #[cfg(feature = #p_feature)] })
139144
};
140145
// Insert the peripheral structure
141-
per_to_tokens(&mut out, &feature_attribute, &doc, &p_ty, None, address);
146+
per_to_tokens(
147+
&mut out,
148+
&feature_attribute,
149+
&doc,
150+
&p_ty,
151+
&name_str,
152+
None,
153+
address,
154+
);
142155

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

0 commit comments

Comments
 (0)