Skip to content

Commit b726d79

Browse files
committed
PeripheralSpec
1 parent 87c2fc4 commit b726d79

File tree

4 files changed

+50
-16
lines changed

4 files changed

+50
-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: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,35 @@
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+
/// Peripheral data
9+
pub trait PeripheralSpec {
10+
/// RegisterBlock associated with peripheral
11+
type RB;
12+
/// Address of the register block
13+
const ADDRESS: usize;
14+
/// Debug name
15+
const NAME: &'static str;
16+
}
17+
18+
unsafe impl<PER: PeripheralSpec> Send for Periph<PER> {}
19+
20+
impl<PER: PeripheralSpec> core::fmt::Debug for Periph<PER> {
21+
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
22+
f.debug_struct(PER::NAME).finish()
23+
}
24+
}
925

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

1430
///Return the pointer to the register block
1531
#[inline(always)]
16-
pub const fn ptr() -> *const RB {
32+
pub const fn ptr() -> *const PER::RB {
1733
Self::PTR
1834
}
1935

@@ -37,8 +53,8 @@ impl<RB, const A: usize> Periph<RB, A> {
3753
}
3854
}
3955

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

4359
#[inline(always)]
4460
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: &'static 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)