stm32f1xx_hal/spi/
hal_1.rs

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