Struct embedded_hal_bus::spi::CriticalSectionDevice
source · pub struct CriticalSectionDevice<'a, BUS, CS, D> { /* private fields */ }Expand description
critical-section-based shared bus SpiDevice implementation.
This allows for sharing an SpiBus, obtaining multiple SpiDevice instances,
each with its own CS pin.
Sharing is implemented with a critical-section Mutex. A critical section is taken for
the entire duration of a transaction. This allows sharing a single bus across multiple threads (interrupt priority levels).
The downside is critical sections typically require globally disabling interrupts, so CriticalSectionDevice will likely
negatively impact real-time properties, such as interrupt latency. If you can, prefer using
RefCellDevice instead, which does not require taking critical sections.
Implementations§
source§impl<'a, BUS, CS, D> CriticalSectionDevice<'a, BUS, CS, D>
impl<'a, BUS, CS, D> CriticalSectionDevice<'a, BUS, CS, D>
sourcepub fn new(bus: &'a Mutex<RefCell<BUS>>, cs: CS, delay: D) -> Self
pub fn new(bus: &'a Mutex<RefCell<BUS>>, cs: CS, delay: D) -> Self
Create a new CriticalSectionDevice.
source§impl<'a, BUS, CS> CriticalSectionDevice<'a, BUS, CS, NoDelay>
impl<'a, BUS, CS> CriticalSectionDevice<'a, BUS, CS, NoDelay>
sourcepub fn new_no_delay(bus: &'a Mutex<RefCell<BUS>>, cs: CS) -> Self
pub fn new_no_delay(bus: &'a Mutex<RefCell<BUS>>, cs: CS) -> Self
Create a new CriticalSectionDevice without support for in-transaction delays.
Warning: The returned instance technically doesn’t comply with the SpiDevice
contract, which mandates delay support. It is relatively rare for drivers to use
in-transaction delays, so you might still want to use this method because it’s more practical.
Note that a future version of the driver might start using delays, causing your
code to panic. This wouldn’t be considered a breaking change from the driver side, because
drivers are allowed to assume SpiDevice implementations comply with the contract.
If you feel this risk outweighs the convenience of having cargo automatically upgrade
the driver crate, you might want to pin the driver’s version.
Panics
The returned device will panic if you try to execute a transaction
that contains any operations of type Operation::DelayNs.