Skip to content

Commit 222be1f

Browse files
authored
Merge pull request #723 from JalonWong/feature/steal
Add steal() for each peripheral.
2 parents f1f28e9 + cd4ef26 commit 222be1f

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
1212
- removed register writer & reader wrappers, generic `REG` in field writers (#731)
1313
- Updated syn to version 2 (#732)
1414
- Let readable field fetch doc from svd description (#734)
15+
- Add `steal()` for each peripheral
1516

1617
## [v0.29.0] - 2023-06-05
1718

src/generate/peripheral.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,25 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
5353
feature_attribute.extend(quote! { #[cfg(feature = #feature_name)] });
5454
};
5555

56+
let steal_fn = quote! {
57+
/// Steal an instance of this peripheral
58+
///
59+
/// # Safety
60+
///
61+
/// Ensure that the new instance of the peripheral cannot be used in a way
62+
/// that may race with any existing instances, for example by only
63+
/// accessing read-only or write-only registers, or by consuming the
64+
/// original peripheral and using critical sections to coordinate
65+
/// access between multiple new instances.
66+
///
67+
/// Additionally, other software such as HALs may rely on only one
68+
/// peripheral instance existing to ensure memory safety; ensure
69+
/// no stolen instances are passed to such software.
70+
pub unsafe fn steal() -> Self {
71+
Self { _marker: PhantomData }
72+
}
73+
};
74+
5675
match &p {
5776
Peripheral::Array(p, dim) => {
5877
let names: Vec<Cow<str>> = names(p, dim).map(|n| n.into()).collect();
@@ -91,6 +110,8 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
91110
pub const fn ptr() -> *const #base::RegisterBlock {
92111
Self::PTR
93112
}
113+
114+
#steal_fn
94115
}
95116

96117
#feature_attribute_n
@@ -150,6 +171,8 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
150171
pub const fn ptr() -> *const #base::RegisterBlock {
151172
Self::PTR
152173
}
174+
175+
#steal_fn
153176
}
154177

155178
#feature_attribute

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,9 @@
186186
//! use the implementation provided by the target crate like `cortex-m`, `riscv` and `*-hal` crates.
187187
//! See more details in the [`critical-section`](https://crates.io/crates/critical-section) crate documentation.
188188
//!
189-
//! The singleton property can be *unsafely* bypassed using the `ptr` static method which is
190-
//! available on all the peripheral types. This method is useful for implementing safe higher
191-
//! level abstractions.
189+
//! The singleton property can be *unsafely* bypassed using the `ptr` or `steal` static methods
190+
//! which are available on all the peripheral types. This method is useful for implementing safe
191+
//! higher level abstractions.
192192
//!
193193
//! ```ignore
194194
//! struct PA0 { _0: () }

0 commit comments

Comments
 (0)