Quellcode durchsuchen

Add SPI frame format config method

 Fixes #279
Wesley Norris vor 3 Jahren
Ursprung
Commit
a6dde78048
2 geänderte Dateien mit 21 neuen und 0 gelöschten Zeilen
  1. 4 0
      CHANGELOG.md
  2. 17 0
      src/spi.rs

+ 4 - 0
CHANGELOG.md

@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
 
 ## [Unreleased]
 
+### Added
+
+- LSB/MSB bit format selection for `SPI`
+
 ## [v0.7.0]- 2020-10-17
 
 ### Breaking changes

+ 17 - 0
src/spi.rs

@@ -116,6 +116,15 @@ pub struct Spi<SPI, REMAP, PINS, FRAMESIZE> {
     _framesize: PhantomData<FRAMESIZE>,
 }
 
+/// The bit format to send the data in
+#[derive(Debug, Clone, Copy)]
+pub enum SpiBitFormat {
+    /// Least significant bit first
+    LsbFirst,
+    /// Most significant bit first
+    MsbFirst,
+}
+
 /// A filler type for when the SCK pin is unnecessary
 pub struct NoSck;
 /// A filler type for when the Miso pin is unnecessary
@@ -309,6 +318,14 @@ where
     pub fn release(self) -> (SPI, PINS) {
         (self.spi, self.pins)
     }
+
+    /// Select which frame format is used for data transfers
+    pub fn bit_format(&mut self, format: SpiBitFormat) {
+        match format {
+            SpiBitFormat::LsbFirst => self.spi.cr1.modify(|_, w| w.lsbfirst().set_bit()),
+            SpiBitFormat::MsbFirst => self.spi.cr1.modify(|_, w| w.lsbfirst().clear_bit()),
+        }
+    }
 }
 
 impl<SPI, REMAP, PINS> Spi<SPI, REMAP, PINS, u8>