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

Commit

Permalink
k20::spi: Add hal::SPI impl
Browse files Browse the repository at this point in the history
  • Loading branch information
bgamari committed Aug 28, 2014
1 parent 723044d commit 9b2e93b
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/hal/k20/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 9b2e93b

Please sign in to comment.