Skip to content

Commit

Permalink
fix spi read
Browse files Browse the repository at this point in the history
  • Loading branch information
almindor committed Apr 1, 2024
1 parent 7ad7d13 commit 70bc940
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/spi/bus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,25 @@ where
SPI: SpiX,
{
fn read(&mut self, words: &mut [u8]) -> Result<(), Infallible> {
let rxdata = self.spi.rxdata.read();
// TODO: send 00 ??
let mut iwrite = 0;
let mut iread = 0;

// Ensure that RX FIFO is empty
self.wait_for_rxfifo();

for word in words {
if rxdata.empty().bit_is_set() {
break;
while iwrite < words.len() || iread < words.len() {
if iwrite < words.len() && self.spi.txdata.read().full().bit_is_clear() {
iwrite += 1;
self.spi.txdata.write(|w| unsafe { w.data().bits(0x00) });
}

*word = rxdata.data().bits();
if iread < iwrite {
let data = self.spi.rxdata.read();
if data.empty().bit_is_clear() {
unsafe { *words.get_unchecked_mut(iread) = data.data().bits() };
iread += 1;
}
}
}

Ok(())
Expand Down

0 comments on commit 70bc940

Please sign in to comment.