Skip to content

Commit

Permalink
Merge pull request #723 from JalonWong/feature/steal
Browse files Browse the repository at this point in the history
Add steal() for each peripheral.
  • Loading branch information
burrbull authored Aug 15, 2023
2 parents f1f28e9 + cd4ef26 commit 222be1f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
- removed register writer & reader wrappers, generic `REG` in field writers (#731)
- Updated syn to version 2 (#732)
- Let readable field fetch doc from svd description (#734)
- Add `steal()` for each peripheral

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

Expand Down
23 changes: 23 additions & 0 deletions src/generate/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,25 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
feature_attribute.extend(quote! { #[cfg(feature = #feature_name)] });
};

let steal_fn = quote! {
/// Steal an instance of this peripheral
///
/// # Safety
///
/// Ensure that the new instance of the peripheral cannot be used in a way
/// that may race with any existing instances, for example by only
/// accessing read-only or write-only registers, or by consuming the
/// original peripheral and using critical sections to coordinate
/// access between multiple new instances.
///
/// Additionally, other software such as HALs may rely on only one
/// peripheral instance existing to ensure memory safety; ensure
/// no stolen instances are passed to such software.
pub unsafe fn steal() -> Self {
Self { _marker: PhantomData }
}
};

match &p {
Peripheral::Array(p, dim) => {
let names: Vec<Cow<str>> = names(p, dim).map(|n| n.into()).collect();
Expand Down Expand Up @@ -91,6 +110,8 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
pub const fn ptr() -> *const #base::RegisterBlock {
Self::PTR
}

#steal_fn
}

#feature_attribute_n
Expand Down Expand Up @@ -150,6 +171,8 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
pub const fn ptr() -> *const #base::RegisterBlock {
Self::PTR
}

#steal_fn
}

#feature_attribute
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@
//! use the implementation provided by the target crate like `cortex-m`, `riscv` and `*-hal` crates.
//! See more details in the [`critical-section`](https://crates.io/crates/critical-section) crate documentation.
//!
//! The singleton property can be *unsafely* bypassed using the `ptr` static method which is
//! available on all the peripheral types. This method is useful for implementing safe higher
//! level abstractions.
//! The singleton property can be *unsafely* bypassed using the `ptr` or `steal` static methods
//! which are available on all the peripheral types. This method is useful for implementing safe
//! higher level abstractions.
//!
//! ```ignore
//! struct PA0 { _0: () }
Expand Down

0 comments on commit 222be1f

Please sign in to comment.