From 9b2e93b20c3e468e6721df05892fd28ac1ecd859 Mon Sep 17 00:00:00 2001 From: Ben Gamari Date: Thu, 28 Aug 2014 13:07:21 -0400 Subject: [PATCH] k20::spi: Add hal::SPI impl --- src/hal/k20/spi.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/hal/k20/spi.rs b/src/hal/k20/spi.rs index 345fc6b3..79c938d5 100644 --- a/src/hal/k20/spi.rs +++ b/src/hal/k20/spi.rs @@ -17,6 +17,48 @@ SPI peripheral */ +use hal::spi; + +#[path="../../lib/wait_for.rs"] mod wait_for; + +pub enum ChipSelect { + CS0 = 0, + CS1 = 1, + CS2 = 2, + CS3 = 3, + CS4 = 4, + CS5 = 5, +} + +/// An SPI peripheral instance +pub struct DSPI { + reg: &'static reg::DSPI, + cs: ChipSelect, +} + +impl DSPI { + fn new(reg: &'static reg::DSPI, cs: ChipSelect) -> DSPI { + reg.mcr.set_halt(false); + DSPI {reg: reg, cs: cs} + } +} + +impl spi::SPI for DSPI { + fn write(&self, value: u8) { + wait_for!(self.reg.sr.tfff()); + self.reg.sr.clear_tfff(); + // TODO(bgamari): Need to ensure this doesn't read + self.reg.pushr + .set_txdata(value as u32) + .set_pcs(self.cs as u32); + } + + fn read(&self) -> u8 { + wait_for!(self.reg.sr.rfdf()); + self.reg.popr.rxdata() as u8 + } +} + /// Registers pub mod reg { use lib::volatile_cell::VolatileCell;