|
| 1 | +use super::clock::ClockMask; |
| 2 | + |
| 3 | +#[derive(Copy, Clone)] |
| 4 | +pub enum BitOrder { |
| 5 | + /// The least significant bit is sent first. |
| 6 | + LeastSignificantBit, |
| 7 | + /// The most significant bit is sent first. |
| 8 | + MostSignificantBit, |
| 9 | +} |
| 10 | + |
| 11 | +#[derive(Copy, Clone)] |
| 12 | +pub enum ClockPhase { |
| 13 | + LeadingEdge, |
| 14 | + TrailingEdge, |
| 15 | +} |
| 16 | + |
| 17 | +/// SPI settings. |
| 18 | +#[derive(Copy, Clone)] |
| 19 | +pub struct Settings { |
| 20 | + /// Whether the SPI module is enabled. |
| 21 | + enabled: bool, |
| 22 | + /// Whether to be configured as a master or slave. |
| 23 | + master: bool, |
| 24 | + /// The clock speed. |
| 25 | + clock: u32, |
| 26 | + /// The bit ordering. |
| 27 | + bit_order: BitOrder, |
| 28 | + /// The clock phase. |
| 29 | + clock_phase: ClockPhase, |
| 30 | + /// Whether interrupts should be enabled. |
| 31 | + enable_interrupts: bool, |
| 32 | +} |
| 33 | + |
| 34 | +impl Settings { |
| 35 | + /// Gets the default settings for the master. |
| 36 | + pub fn master() -> Self { |
| 37 | + Settings { |
| 38 | + master: true, |
| 39 | + ..Default::default() |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /// Gets the default settings for the slave. |
| 44 | + pub fn slave() -> Self { |
| 45 | + Settings { |
| 46 | + master: false, |
| 47 | + ..Default::default() |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + pub fn control_register_bits(self) -> u8 { |
| 52 | + let mut bits = 0; |
| 53 | + |
| 54 | + bits |= self.clock().control_register_mask(); |
| 55 | + |
| 56 | + if self.enable_interrupts { |
| 57 | + bits |= control_register::INTERRUPT_ENABLE |
| 58 | + } |
| 59 | + if self.enabled { |
| 60 | + bits |= control_register::ENABLE |
| 61 | + } |
| 62 | + if let ClockPhase::LeadingEdge = self.clock_phase { |
| 63 | + bits |= control_register::CPHA; |
| 64 | + } |
| 65 | + |
| 66 | + if let BitOrder::LeastSignificantBit = self.bit_order { |
| 67 | + bits |= control_register::DATA_ORDER_LSB; |
| 68 | + } |
| 69 | + bits |
| 70 | + } |
| 71 | + |
| 72 | + pub fn status_register_bits(self) -> u8 { |
| 73 | + let mut bits = 0; |
| 74 | + |
| 75 | + bits |= self.clock().status_register_mask(); |
| 76 | + bits |
| 77 | + } |
| 78 | + |
| 79 | + fn clock(self) -> ClockMask { |
| 80 | + ClockMask::with_clock(self.clock) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +impl Default for Settings { |
| 85 | + fn default() -> Settings { |
| 86 | + Settings { |
| 87 | + enabled: true, |
| 88 | + master: true, |
| 89 | + // same as Arduino default in `SPI.h`. |
| 90 | + clock: 4_000_000, |
| 91 | + bit_order: BitOrder::MostSignificantBit, |
| 92 | + clock_phase: ClockPhase::LeadingEdge, |
| 93 | + enable_interrupts: false, |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +/// Constants for the control register. |
| 99 | +pub mod control_register { |
| 100 | + /// Set if interrupts are enabled. |
| 101 | + pub const INTERRUPT_ENABLE: u8 = 1<<7; |
| 102 | + /// Set if the SPI module is enabled. |
| 103 | + pub const ENABLE: u8 = 1<<6; |
| 104 | + /// Set if data is sent in LSB format. |
| 105 | + pub const DATA_ORDER_LSB: u8 = 1<<5; |
| 106 | + /// Set if we are configuring a master. |
| 107 | + pub const MASTER: u8 = 1<<4; |
| 108 | + /// Clock polarity. |
| 109 | + pub const CPOL: u8 = 1<<3; |
| 110 | + /// Clock phase. |
| 111 | + pub const CPHA: u8 = 1<<2; |
| 112 | + /// Clock rate select 1. |
| 113 | + pub const SPR1: u8 = 1<<1; |
| 114 | + /// Clock rate select 2. |
| 115 | + pub const SPR0: u8 = 1<<0; |
| 116 | +} |
| 117 | + |
| 118 | +/// Constants for the status register. |
| 119 | +pub mod status_register { |
| 120 | + /// SPI interrupt flag. |
| 121 | + pub const SPIF: u8 = 1<<7; |
| 122 | + /// Write collision flag. |
| 123 | + pub const WCOL: u8 = 1<<6; |
| 124 | + /// SPI double speed mode. |
| 125 | + pub const SPI2X: u8 = 1<<0; |
| 126 | +} |
| 127 | + |
0 commit comments