diff --git a/hal/src/sercom/i2c/async_api.rs b/hal/src/sercom/i2c/async_api.rs index 6fe4923302af..1a46b6047df8 100644 --- a/hal/src/sercom/i2c/async_api.rs +++ b/hal/src/sercom/i2c/async_api.rs @@ -74,6 +74,38 @@ where pub type I2cFutureDma = I2cFuture>; +impl I2cFuture +where + C: AnyConfig, + S: Sercom, + N: InterruptNumber, +{ + async fn wait_flags(&mut self, flags_to_wait: Flags) { + core::future::poll_fn(|cx| { + // Scope maybe_pending so we don't forget to re-poll the register later down. + { + let maybe_pending = self.i2c.config.as_ref().registers.read_flags(); + if flags_to_wait.intersects(maybe_pending) { + return Poll::Ready(()); + } + } + + self.i2c.disable_interrupts(Flags::all()); + // By convention, I2C uses the sercom's RX waker. + S::rx_waker().register(cx.waker()); + self.i2c.enable_interrupts(flags_to_wait); + let maybe_pending = self.i2c.config.as_ref().registers.read_flags(); + + if !flags_to_wait.intersects(maybe_pending) { + Poll::Pending + } else { + Poll::Ready(()) + } + }) + .await; + } +} + impl I2cFuture where C: AnyConfig, @@ -167,31 +199,6 @@ where self.wait_flags(Flags::SB | Flags::ERROR).await; self.i2c.config.as_mut().registers.read_one() } - - async fn wait_flags(&mut self, flags_to_wait: Flags) { - core::future::poll_fn(|cx| { - // Scope maybe_pending so we don't forget to re-poll the register later down. - { - let maybe_pending = self.i2c.config.as_ref().registers.read_flags(); - if flags_to_wait.intersects(maybe_pending) { - return Poll::Ready(()); - } - } - - self.i2c.disable_interrupts(Flags::all()); - // By convention, I2C uses the sercom's RX waker. - S::rx_waker().register(cx.waker()); - self.i2c.enable_interrupts(flags_to_wait); - let maybe_pending = self.i2c.config.as_ref().registers.read_flags(); - - if !flags_to_wait.intersects(maybe_pending) { - Poll::Pending - } else { - Poll::Ready(()) - } - }) - .await; - } } // impl Drop for I2cFuture @@ -416,6 +423,7 @@ 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(()) }