Skip to content

Commit

Permalink
read and write in send
Browse files Browse the repository at this point in the history
  • Loading branch information
sajattack committed May 12, 2019
1 parent 1bcea74 commit e0c47d0
Showing 1 changed file with 8 additions and 52 deletions.
60 changes: 8 additions & 52 deletions src/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ where
mosi: Mosi,
sck: Sck,
timer: Timer,
read_val: u8,
}

impl <Miso, Mosi, Sck, Timer> SPI<Miso, Mosi, Sck, Timer>
Expand All @@ -44,6 +45,7 @@ where
mosi: mosi,
sck: sck,
timer: timer,
read_val: 0,
}
}
}
Expand All @@ -58,56 +60,7 @@ 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();
}
}
}
Ok(data_in)
Ok(self.read_val)
}

fn send(&mut self, byte: u8) -> nb::Result<(), Self::Error> {
Expand All @@ -119,6 +72,11 @@ where
} else {
self.mosi.set_low();
}
if self.miso.is_high() {
self.read_val = (self.read_val << 1) | 1
} else {
self.read_val = self.read_val << 1
}
if self.mode.phase == CaptureOnFirstTransition {
if self.mode.polarity == IdleLow {
block!(self.timer.wait()).ok();
Expand Down Expand Up @@ -167,5 +125,3 @@ where
Sck: OutputPin,
Timer: CountDown + Periodic
{}


0 comments on commit e0c47d0

Please sign in to comment.