2 * Copyright (c) 2020 Tilman Sauerbeck (tilman at code-monkey de)
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 type Reg8 = register::Register<u8>;
27 type Reg32 = register::Register<u32>;
29 const SPI_BASE: u32 = 0x40076000;
31 pub const SPI0: u32 = SPI_BASE + 0x0000;
32 pub const SPI1: u32 = SPI_BASE + 0x1000;
34 const SIM_SCGC4: u32 = 0x40048034;
36 const SIM_SCGC4_SPI0: u32 = 1 << 22;
38 const SPI_S : u32 = 0x00;
39 const SPI_BR: u32 = 0x01;
40 const SPI_C2: u32 = 0x02;
41 const SPI_C1: u32 = 0x03;
42 const SPI_DL: u32 = 0x06;
44 const SPI_S_SPTEF: u8 = 1 << 5;
45 const SPI_S_SPRF : u8 = 1 << 7;
47 const SPI_C1_MSTR: u8 = 1 << 4;
48 const SPI_C1_SPE: u8 = 1 << 6;
50 pub fn configure(spi: u32) {
52 let mut scgc4 = Reg32::new(SIM_SCGC4);
55 scgc4.modify(|v| v | SIM_SCGC4_SPI0);
59 Reg8::new(spi + SPI_C1).write(SPI_C1_MSTR);
60 Reg8::new(spi + SPI_C2).write(0x0);
61 Reg8::new(spi + SPI_BR).write(0x0);
62 Reg8::new(spi + SPI_C1).modify(|v| v | SPI_C1_SPE);
65 pub fn tx8(spi: u32, v: u8) -> u8 {
66 let s = Reg8::new(spi + SPI_S);
68 // Wait for transmit buffer to become empty.
69 while (s.read() & SPI_S_SPTEF) == 0 {
72 let mut dl = Reg8::new(spi + SPI_DL);
75 // Wait for receive buffer to become full.
76 while (s.read() & SPI_S_SPRF) == 0 {