stm32f4xx_hal/spi/
hal_1.rs

1pub use embedded_hal::spi::{Error, ErrorKind, ErrorType, Mode, Phase, Polarity};
2
3use super::Instance;
4
5impl From<Polarity> for super::Polarity {
6    fn from(p: Polarity) -> Self {
7        match p {
8            Polarity::IdleLow => Self::IdleLow,
9            Polarity::IdleHigh => Self::IdleHigh,
10        }
11    }
12}
13
14impl From<Phase> for super::Phase {
15    fn from(p: Phase) -> Self {
16        match p {
17            Phase::CaptureOnFirstTransition => Self::CaptureOnFirstTransition,
18            Phase::CaptureOnSecondTransition => Self::CaptureOnSecondTransition,
19        }
20    }
21}
22
23impl From<Mode> for super::Mode {
24    fn from(m: Mode) -> Self {
25        Self {
26            polarity: m.polarity.into(),
27            phase: m.phase.into(),
28        }
29    }
30}
31
32impl Error for super::Error {
33    fn kind(&self) -> ErrorKind {
34        match self {
35            Self::Overrun => ErrorKind::Overrun,
36            Self::ModeFault => ErrorKind::ModeFault,
37            Self::Crc => ErrorKind::Other,
38        }
39    }
40}
41
42impl<SPI: Instance, const BIDI: bool, W> ErrorType for super::Spi<SPI, BIDI, W> {
43    type Error = super::Error;
44}
45
46mod nb {
47    use super::super::{Error, FrameSize, Instance, Spi};
48    use embedded_hal_nb::spi::FullDuplex;
49
50    impl<SPI, const BIDI: bool, W: FrameSize> FullDuplex<W> for Spi<SPI, BIDI, W>
51    where
52        SPI: Instance,
53    {
54        fn read(&mut self) -> nb::Result<W, Error> {
55            self.read_nonblocking()
56        }
57
58        fn write(&mut self, byte: W) -> nb::Result<(), Error> {
59            self.write_nonblocking(byte)
60        }
61    }
62}
63
64mod blocking {
65    use super::super::{FrameSize, Instance, Spi};
66    use embedded_hal::spi::SpiBus;
67
68    impl<SPI, const BIDI: bool, W: FrameSize + 'static> SpiBus<W> for Spi<SPI, BIDI, W>
69    where
70        SPI: Instance,
71    {
72        fn transfer_in_place(&mut self, words: &mut [W]) -> Result<(), Self::Error> {
73            self.transfer_in_place(words)
74        }
75
76        fn transfer(&mut self, buff: &mut [W], data: &[W]) -> Result<(), Self::Error> {
77            self.transfer(buff, data)
78        }
79
80        fn read(&mut self, words: &mut [W]) -> Result<(), Self::Error> {
81            self.read(words)
82        }
83
84        fn write(&mut self, words: &[W]) -> Result<(), Self::Error> {
85            self.write(words)
86        }
87
88        fn flush(&mut self) -> Result<(), Self::Error> {
89            Ok(())
90        }
91    }
92}