-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bus: make AtomicDevice use a per-bus "busy" flag, not per-device.
Needed for soundness.
- Loading branch information
Showing
4 changed files
with
52 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,3 +18,4 @@ use defmt_03 as defmt; | |
|
||
pub mod i2c; | ||
pub mod spi; | ||
pub mod util; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//! Utilities shared by all bus types. | ||
|
||
use core::cell::UnsafeCell; | ||
|
||
/// Cell type used by [`spi::AtomicDevice`](crate::spi::AtomicDevice) and [`i2c::AtomicDevice`](crate::i2c::AtomicDevice). | ||
/// | ||
/// To use [`AtomicDevice`], you must wrap the bus with this struct, and then | ||
/// construct multiple [`AtomicDevice`] instances with references to it. | ||
pub struct AtomicCell<BUS> { | ||
pub(crate) bus: UnsafeCell<BUS>, | ||
pub(crate) busy: portable_atomic::AtomicBool, | ||
} | ||
|
||
unsafe impl<BUS: Send> Send for AtomicCell<BUS> {} | ||
unsafe impl<BUS: Send> Sync for AtomicCell<BUS> {} | ||
|
||
impl<BUS> AtomicCell<BUS> { | ||
/// Create a new `AtomicCell` | ||
pub fn new(bus: BUS) -> Self { | ||
Self { | ||
bus: UnsafeCell::new(bus), | ||
busy: portable_atomic::AtomicBool::from(false), | ||
} | ||
} | ||
} |