Skip to content
This repository was archived by the owner on Jul 6, 2019. It is now read-only.

Commit ea47c0e

Browse files
committed
k20::spi: Add hal::SPI impl
1 parent d67d156 commit ea47c0e

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/hal/k20/spi.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,48 @@
1717
SPI peripheral
1818
*/
1919

20+
use hal::spi;
21+
22+
#[path="../../lib/wait_for.rs"] mod wait_for;
23+
24+
pub enum ChipSelect {
25+
CS0 = 0,
26+
CS1 = 1,
27+
CS2 = 2,
28+
CS3 = 3,
29+
CS4 = 4,
30+
CS5 = 5,
31+
}
32+
33+
/// An SPI peripheral instance
34+
pub struct DSPI {
35+
reg: &'static reg::DSPI,
36+
cs: ChipSelect,
37+
}
38+
39+
impl DSPI {
40+
fn new(reg: &'static reg::DSPI, cs: ChipSelect) -> DSPI {
41+
reg.mcr.set_halt(false);
42+
DSPI {reg: reg, cs: cs}
43+
}
44+
}
45+
46+
impl spi::SPI for DSPI {
47+
fn write(&self, value: u8) {
48+
wait_for!(self.reg.sr.tfff());
49+
self.reg.sr.clear_tfff();
50+
// TODO(bgamari): Need to ensure this doesn't read
51+
self.reg.pushr
52+
.set_txdata(value as u32)
53+
.set_pcs(self.cs as u32);
54+
}
55+
56+
fn read(&self) -> u8 {
57+
wait_for!(self.reg.sr.rfdf());
58+
self.reg.popr.rxdata() as u8
59+
}
60+
}
61+
2062
/// Registers
2163
pub mod reg {
2264
use lib::volatile_cell::VolatileCell;

0 commit comments

Comments
 (0)