common: Add the spi module.
authorTilman Sauerbeck <tilman@code-monkey.de>
Sat, 4 Jan 2020 11:00:25 +0000 (12:00 +0100)
committerTilman Sauerbeck <tilman@code-monkey.de>
Mon, 6 Jan 2020 09:45:34 +0000 (10:45 +0100)
SConscript.libcommon
src/common/lib.rs
src/common/spi.rs [new file with mode: 0644]

index 4e5b06516785ae37b23901174d6f088794c47522..0ca199e275d42584ec584c94137fd012f06f6dbf 100644 (file)
@@ -10,6 +10,7 @@ source_files_rs = [
     'src/common/port.rs',
     'src/common/gpio.rs',
     'src/common/i2c.rs',
     'src/common/port.rs',
     'src/common/gpio.rs',
     'src/common/i2c.rs',
+    'src/common/spi.rs',
     'src/common/uart.rs',
     'src/common/watchdog.rs',
     'src/common/crc32.rs',
     'src/common/uart.rs',
     'src/common/watchdog.rs',
     'src/common/crc32.rs',
index 69cd7be4f8110e80219421339682585e92ed4a85..c74704c4ef5612b9f7803e31c146d6d0c030f2e8 100644 (file)
@@ -32,6 +32,7 @@ pub mod systick;
 pub mod port;
 pub mod gpio;
 pub mod i2c;
 pub mod port;
 pub mod gpio;
 pub mod i2c;
+pub mod spi;
 pub mod uart;
 pub mod watchdog;
 pub mod crc32;
 pub mod uart;
 pub mod watchdog;
 pub mod crc32;
diff --git a/src/common/spi.rs b/src/common/spi.rs
new file mode 100644 (file)
index 0000000..1dcaa22
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2020 Tilman Sauerbeck (tilman at code-monkey de)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+use register;
+
+type Reg8 = register::Register<u8>;
+type Reg32 = register::Register<u32>;
+
+const SPI_BASE: u32 = 0x40076000;
+
+pub const SPI0: u32 = SPI_BASE + 0x0000;
+pub const SPI1: u32 = SPI_BASE + 0x1000;
+
+const SIM_SCGC4: u32 = 0x40048034;
+
+const SIM_SCGC4_SPI0: u32 = 1 << 22;
+
+const SPI_S : u32 = 0x00;
+const SPI_BR: u32 = 0x01;
+const SPI_C2: u32 = 0x02;
+const SPI_C1: u32 = 0x03;
+const SPI_DL: u32 = 0x06;
+
+const SPI_S_SPTEF: u8 = 1 << 5;
+const SPI_S_SPRF : u8 = 1 << 7;
+
+const SPI_C1_MSTR: u8 = 1 << 4;
+const SPI_C1_SPE: u8 = 1 << 6;
+
+pub fn configure(spi: u32) {
+    {
+        let mut scgc4 = Reg32::new(SIM_SCGC4);
+
+        if spi == SPI0 {
+            scgc4.modify(|v| v | SIM_SCGC4_SPI0);
+        }
+    }
+
+    Reg8::new(spi + SPI_C1).write(SPI_C1_MSTR);
+    Reg8::new(spi + SPI_C2).write(0x0);
+    Reg8::new(spi + SPI_BR).write(0x0);
+    Reg8::new(spi + SPI_C1).modify(|v| v | SPI_C1_SPE);
+}
+
+pub fn tx8(spi: u32, v: u8) -> u8 {
+    let s = Reg8::new(spi + SPI_S);
+
+    // Wait for transmit buffer to become empty.
+    while (s.read() & SPI_S_SPTEF) == 0 {
+    }
+
+    let mut dl = Reg8::new(spi + SPI_DL);
+    dl.write(v);
+
+    // Wait for receive buffer to become full.
+    while (s.read() & SPI_S_SPRF) == 0 {
+    }
+
+    dl.read()
+}