Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add steal() for each peripheral. #723

Merged
merged 6 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,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
Loading