1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
//! Serial Peripheral Interface use nb; /// Full duplex (master mode) /// /// # Notes /// /// - It's the task of the user of this interface to manage the slave select lines /// /// - Due to how full duplex SPI works each `try_read` call must be preceded by a `try_send` call. /// /// - `try_read` calls only return the data received with the last `try_send` call. /// Previously received data is discarded /// /// - Data is only guaranteed to be clocked out when the `try_read` call succeeds. /// The slave select line shouldn't be released before that. /// /// - Some SPIs can work with 8-bit *and* 16-bit words. You can overload this trait with different /// `Word` types to allow operation in both modes. pub trait FullDuplex<Word> { /// An enumeration of SPI errors type Error; /// Reads the word stored in the shift register /// /// **NOTE** A word must be sent to the slave before attempting to call this /// method. fn try_read(&mut self) -> nb::Result<Word, Self::Error>; /// Sends a word to the slave fn try_send(&mut self, word: Word) -> nb::Result<(), Self::Error>; } /// Clock polarity #[derive(Clone, Copy, PartialEq, Eq)] pub enum Polarity { /// Clock signal low when idle IdleLow, /// Clock signal high when idle IdleHigh, } /// Clock phase #[derive(Clone, Copy, PartialEq, Eq)] pub enum Phase { /// Data in "captured" on the first clock transition CaptureOnFirstTransition, /// Data in "captured" on the second clock transition CaptureOnSecondTransition, } /// SPI mode #[derive(Clone, Copy, PartialEq, Eq)] pub struct Mode { /// Clock polarity pub polarity: Polarity, /// Clock phase pub phase: Phase, } /// Helper for CPOL = 0, CPHA = 0 pub const MODE_0: Mode = Mode { polarity: Polarity::IdleLow, phase: Phase::CaptureOnFirstTransition, }; /// Helper for CPOL = 0, CPHA = 1 pub const MODE_1: Mode = Mode { polarity: Polarity::IdleLow, phase: Phase::CaptureOnSecondTransition, }; /// Helper for CPOL = 1, CPHA = 0 pub const MODE_2: Mode = Mode { polarity: Polarity::IdleHigh, phase: Phase::CaptureOnFirstTransition, }; /// Helper for CPOL = 1, CPHA = 1 pub const MODE_3: Mode = Mode { polarity: Polarity::IdleHigh, phase: Phase::CaptureOnSecondTransition, };