1 | //! SPI master mode traits using `nb`. |
2 | |
3 | pub use embedded_hal::spi::{ |
4 | Error, ErrorKind, ErrorType, Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3, |
5 | }; |
6 | |
7 | /// Full duplex SPI (master mode). |
8 | /// |
9 | /// # Notes |
10 | /// |
11 | /// - It's the task of the user of this interface to manage the slave select lines. |
12 | /// |
13 | /// - Due to how full duplex SPI works each `read` call must be preceded by a `write` call. |
14 | /// |
15 | /// - `read` calls only return the data received with the last `write` call. |
16 | /// Previously received data is discarded |
17 | /// |
18 | /// - Data is only guaranteed to be clocked out when the `read` call succeeds. |
19 | /// The slave select line shouldn't be released before that. |
20 | /// |
21 | /// - Some SPIs can work with 8-bit *and* 16-bit words. You can overload this trait with different |
22 | /// `Word` types to allow operation in both modes. |
23 | pub trait FullDuplex<Word: Copy = u8>: ErrorType { |
24 | /// Reads the word stored in the shift register |
25 | /// |
26 | /// **NOTE** A word must be sent to the slave before attempting to call this |
27 | /// method. |
28 | fn read(&mut self) -> nb::Result<Word, Self::Error>; |
29 | |
30 | /// Writes a word to the slave |
31 | fn write(&mut self, word: Word) -> nb::Result<(), Self::Error>; |
32 | } |
33 | |
34 | impl<T: FullDuplex<Word> + ?Sized, Word: Copy> FullDuplex<Word> for &mut T { |
35 | #[inline ] |
36 | fn read(&mut self) -> nb::Result<Word, Self::Error> { |
37 | T::read(self) |
38 | } |
39 | |
40 | #[inline ] |
41 | fn write(&mut self, word: Word) -> nb::Result<(), Self::Error> { |
42 | T::write(self, word) |
43 | } |
44 | } |
45 | |