embedded_hal_bus/spi/
mod.rs1use core::fmt::{self, Debug, Display, Formatter};
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::*;
14#[cfg(any(feature = "portable-atomic", target_has_atomic = "8"))]
15mod atomic;
16mod critical_section;
17mod shared;
18#[cfg(any(feature = "portable-atomic", target_has_atomic = "8"))]
19pub use atomic::*;
20
21#[cfg(feature = "alloc")]
22mod rc;
23#[cfg(feature = "alloc")]
24pub use rc::*;
25
26pub use self::critical_section::*;
27
28#[cfg(feature = "defmt-03")]
29use crate::defmt;
30
31#[derive(Copy, Clone, Eq, PartialEq, Debug)]
33#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
34pub enum DeviceError<BUS, CS> {
35 Spi(BUS),
37 Cs(CS),
39}
40
41impl<BUS: Display, CS: Display> Display for DeviceError<BUS, CS> {
42 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
43 match self {
44 Self::Spi(bus) => write!(f, "SPI bus error: {}", bus),
45 Self::Cs(cs) => write!(f, "SPI CS error: {}", cs),
46 }
47 }
48}
49
50impl<BUS: Debug + Display, CS: Debug + Display> core::error::Error for DeviceError<BUS, CS> {}
51
52impl<BUS, CS> Error for DeviceError<BUS, CS>
53where
54 BUS: Error + Debug,
55 CS: Debug,
56{
57 #[inline]
58 fn kind(&self) -> ErrorKind {
59 match self {
60 Self::Spi(e) => e.kind(),
61 Self::Cs(_) => ErrorKind::ChipSelectFault,
62 }
63 }
64}
65
66#[derive(Copy, Clone, Eq, PartialEq, Debug)]
68#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
69pub struct NoDelay;
70
71#[cold]
72fn no_delay_panic() {
73 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.");
74}
75
76impl embedded_hal::delay::DelayNs for NoDelay {
77 #[inline]
78 fn delay_ns(&mut self, _ns: u32) {
79 no_delay_panic();
80 }
81}
82
83#[cfg(feature = "async")]
84#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
85impl embedded_hal_async::delay::DelayNs for NoDelay {
86 #[inline]
87 async fn delay_ns(&mut self, _ns: u32) {
88 no_delay_panic();
89 }
90}