embedded_hal_bus/
util.rs

1//! Utilities shared by all bus types.
2
3use core::cell::UnsafeCell;
4
5/// Cell type used by [`spi::AtomicDevice`](crate::spi::AtomicDevice) and [`i2c::AtomicDevice`](crate::i2c::AtomicDevice).
6///
7/// To use `AtomicDevice`, you must wrap the bus with this struct, and then
8/// construct multiple `AtomicDevice` instances with references to it.
9pub struct AtomicCell<BUS> {
10    pub(crate) bus: UnsafeCell<BUS>,
11    pub(crate) busy: portable_atomic::AtomicBool,
12}
13
14unsafe impl<BUS: Send> Send for AtomicCell<BUS> {}
15unsafe impl<BUS: Send> Sync for AtomicCell<BUS> {}
16
17impl<BUS> AtomicCell<BUS> {
18    /// Create a new `AtomicCell`
19    pub fn new(bus: BUS) -> Self {
20        Self {
21            bus: UnsafeCell::new(bus),
22            busy: portable_atomic::AtomicBool::from(false),
23        }
24    }
25}