embedded_hal_bus/spi/
mod.rs

1//! `SpiDevice` implementations.
2
3use core::fmt::Debug;
4use embedded_hal::spi::{Error, ErrorKind};
5
6mod exclusive;
7pub use exclusive::*;
8mod refcell;
9pub use refcell::*;
10#[cfg(feature = "std")]
11mod mutex;
12#[cfg(feature = "std")]
13pub use mutex::*;
14mod critical_section;
15mod shared;
16
17pub use self::critical_section::*;
18
19#[cfg(feature = "defmt-03")]
20use crate::defmt;
21
22/// Error type for [`ExclusiveDevice`] operations.
23#[derive(Copy, Clone, Eq, PartialEq, Debug)]
24#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
25pub enum DeviceError<BUS, CS> {
26    /// An inner SPI bus operation failed.
27    Spi(BUS),
28    /// Asserting or deasserting CS failed.
29    Cs(CS),
30}
31
32impl<BUS, CS> Error for DeviceError<BUS, CS>
33where
34    BUS: Error + Debug,
35    CS: Debug,
36{
37    #[inline]
38    fn kind(&self) -> ErrorKind {
39        match self {
40            Self::Spi(e) => e.kind(),
41            Self::Cs(_) => ErrorKind::ChipSelectFault,
42        }
43    }
44}
45
46/// Dummy [`DelayNs`](embedded_hal::delay::DelayNs) implementation that panics on use.
47#[derive(Copy, Clone, Eq, PartialEq, Debug)]
48#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
49pub struct NoDelay;
50
51#[cold]
52fn no_delay_panic() {
53    panic!("You've tried to execute a SPI transaction containing a `Operation::DelayNs` in a `SpiDevice` created with `new_no_delay()`. Create it with `new()` instead, passing a `DelayNs` implementation.");
54}
55
56impl embedded_hal::delay::DelayNs for NoDelay {
57    #[inline]
58    fn delay_ns(&mut self, _ns: u32) {
59        no_delay_panic();
60    }
61}
62
63#[cfg(feature = "async")]
64#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
65impl embedded_hal_async::delay::DelayNs for NoDelay {
66    #[inline]
67    async fn delay_ns(&mut self, _ns: u32) {
68        no_delay_panic();
69    }
70}