Skip to content

Commit

Permalink
Fix nasty bug in I2C-DMA transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
jbeaurivage committed Dec 13, 2023
1 parent a0d7865 commit 51a62d5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
8 changes: 7 additions & 1 deletion hal/src/sercom/i2c/async_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,13 @@ mod dma {
.await
.map_err(i2c::Error::Dma)?;

// Unfortunately, gotta take a polling approach here as there is no interrupt
// source that can notify us of an IDLE bus state. Fortunately, it's usually not
// long. 8-10 times around the loop will do the trick.
while !self.i2c.read_status().is_idle() {
core::hint::spin_loop();
}
self.i2c.read_status().check_bus_error()?;
Ok(())
}

Expand Down Expand Up @@ -397,7 +404,6 @@ mod dma {
read_buf: &mut [u8],
) -> Result<(), i2c::Error> {
self.write(addr, write_buf).await?;
// TODO may need some sort of delay here??
self.read(addr, read_buf).await?;
Ok(())
}
Expand Down
4 changes: 4 additions & 0 deletions hal/src/sercom/i2c/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ impl Status {
Ok(())
}
}

pub fn is_idle(self) -> bool {
self.busstate() == BusState::Idle
}
}

/// Errors available for I2C transactions
Expand Down

0 comments on commit 51a62d5

Please sign in to comment.