Skip to content

Commit 483a05f

Browse files
committed
PeripheralSpec
1 parent 87c2fc4 commit 483a05f

File tree

4 files changed

+48
-16
lines changed

4 files changed

+48
-16
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: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,33 @@
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<PER: PeripheralSpec> {
5+
_marker: marker::PhantomData<PER>,
66
}
77

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

10-
impl<RB, const A: usize> Periph<RB, A> {
24+
impl<PER: PeripheralSpec> Periph<PER> {
1125
///Pointer to the register block
12-
pub const PTR: *const RB = A as *const _;
26+
pub const PTR: *const PER::RB = PER::ADDRESS as *const _;
1327

1428
///Return the pointer to the register block
1529
#[inline(always)]
16-
pub const fn ptr() -> *const RB {
30+
pub const fn ptr() -> *const PER::RB {
1731
Self::PTR
1832
}
1933

@@ -37,8 +51,8 @@ impl<RB, const A: usize> Periph<RB, A> {
3751
}
3852
}
3953

40-
impl<RB, const A: usize> core::ops::Deref for Periph<RB, A> {
41-
type Target = RB;
54+
impl<PER: PeripheralSpec> core::ops::Deref for Periph<PER> {
55+
type Target = PER::RB;
4256

4357
#[inline(always)]
4458
fn deref(&self) -> &Self::Target {

src/generate/peripheral.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,25 @@ 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<#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 crate::PeripheralSpec for #pspec {
91+
type RB = #base::RegisterBlock;
92+
const ADDRESS: usize = #address;
93+
const NAME: &str = #name_str;
8994
}
9095
});
9196
};
@@ -113,6 +118,7 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
113118
&feature_attribute_n,
114119
&doc,
115120
&p_ty,
121+
&name_str,
116122
doc_alias,
117123
address,
118124
);
@@ -138,7 +144,15 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
138144
feature_attribute.extend(quote! { #[cfg(feature = #p_feature)] })
139145
};
140146
// Insert the peripheral structure
141-
per_to_tokens(&mut out, &feature_attribute, &doc, &p_ty, None, address);
147+
per_to_tokens(
148+
&mut out,
149+
&feature_attribute,
150+
&doc,
151+
&p_ty,
152+
&name_str,
153+
None,
154+
address,
155+
);
142156

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

0 commit comments

Comments
 (0)