Skip to content

Commit

Permalink
Merge pull request #2 from sajattack/spi-fix
Browse files Browse the repository at this point in the history
read and write in send
  • Loading branch information
sajattack authored May 14, 2019
2 parents 1bcea74 + de66010 commit cb2f913
Showing 1 changed file with 18 additions and 52 deletions.
70 changes: 18 additions & 52 deletions src/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use nb::block;

#[derive(Debug)]
pub enum Error {
Unimplemented,
NoData,
}

/// A Full-Duplex SPI implementation, takes 3 pins, and a timer running at 2x
Expand All @@ -22,6 +22,7 @@ where
mosi: Mosi,
sck: Sck,
timer: Timer,
read_val: Option<u8>,
}

impl <Miso, Mosi, Sck, Timer> SPI<Miso, Mosi, Sck, Timer>
Expand All @@ -44,6 +45,15 @@ where
mosi: mosi,
sck: sck,
timer: timer,
read_val: None,
}
}

fn read_bit(&mut self) {
if self.miso.is_high() {
self.read_val = Some((self.read_val.unwrap_or(0) << 1) | 1);
} else {
self.read_val = Some(self.read_val.unwrap_or(0) << 1);
}
}
}
Expand All @@ -58,56 +68,10 @@ where
type Error = Error;

fn read(&mut self) -> nb::Result<u8, Error> {
self.mosi.set_low();
let mut data_in: u8 = 0;
for _bit in 0..8 {
if self.mode.phase == CaptureOnFirstTransition {
if self.mode.polarity == IdleLow {
block!(self.timer.wait()).ok();
self.sck.set_high();
if self.miso.is_high() {
data_in = (data_in << 1) | 1
} else {
data_in = data_in << 1
}
block!(self.timer.wait()).ok();
self.sck.set_low();
} else {
block!(self.timer.wait()).ok();
self.sck.set_low();
if self.miso.is_high() {
data_in = (data_in << 1) | 1
} else {
data_in = data_in << 1
}
block!(self.timer.wait()).ok();
self.sck.set_high();
}
} else {
if self.mode.polarity == IdleLow {
self.sck.set_high();
block!(self.timer.wait()).ok();
if self.miso.is_high() {
data_in = (data_in << 1) | 1
} else {
data_in = data_in << 1
}
self.sck.set_low();
block!(self.timer.wait()).ok();
} else {
self.sck.set_low();
block!(self.timer.wait()).ok();
if self.miso.is_high() {
data_in = (data_in << 1) | 1
} else {
data_in = data_in << 1
}
self.sck.set_high();
block!(self.timer.wait()).ok();
}
}
match self.read_val {
Some(val) => Ok(val),
None => Err(nb::Error::Other(Error::NoData))
}
Ok(data_in)
}

fn send(&mut self, byte: u8) -> nb::Result<(), Self::Error> {
Expand All @@ -123,23 +87,27 @@ where
if self.mode.polarity == IdleLow {
block!(self.timer.wait()).ok();
self.sck.set_high();
self.read_bit();
block!(self.timer.wait()).ok();
self.sck.set_low();
} else {
block!(self.timer.wait()).ok();
self.sck.set_low();
self.read_bit();
block!(self.timer.wait()).ok();
self.sck.set_high();
}
} else {
if self.mode.polarity == IdleLow {
self.sck.set_high();
block!(self.timer.wait()).ok();
self.read_bit();
self.sck.set_low();
block!(self.timer.wait()).ok();
} else {
self.sck.set_low();
block!(self.timer.wait()).ok();
self.read_bit();
self.sck.set_high();
block!(self.timer.wait()).ok();
}
Expand Down Expand Up @@ -167,5 +135,3 @@ where
Sck: OutputPin,
Timer: CountDown + Periodic
{}


0 comments on commit cb2f913

Please sign in to comment.