From 71f91c7ede09143a803037e5cbfc90b34397fe1b Mon Sep 17 00:00:00 2001 From: Andelf Date: Sat, 9 Nov 2024 21:33:38 +0800 Subject: [PATCH] fix(i2c): timeout support --- src/exti.rs | 1 - src/i2c.rs | 26 +++++++++++++------------- src/otg_fs/endpoint.rs | 2 +- src/otg_fs/mod.rs | 4 ++-- 4 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/exti.rs b/src/exti.rs index d3cdf72..a2458d1 100644 --- a/src/exti.rs +++ b/src/exti.rs @@ -391,5 +391,4 @@ mod irq_impl { } } - pub(crate) use irq_impl::*; diff --git a/src/i2c.rs b/src/i2c.rs index 75007e4..284d81f 100644 --- a/src/i2c.rs +++ b/src/i2c.rs @@ -83,7 +83,7 @@ pub enum Duty { #[derive(Copy, Clone)] pub struct Config { /// Timeout. - #[cfg(feature = "time")] + #[cfg(feature = "embassy")] pub timeout: embassy_time::Duration, pub duty: Duty, } @@ -91,7 +91,7 @@ pub struct Config { impl Default for Config { fn default() -> Self { Self { - #[cfg(feature = "time")] + #[cfg(feature = "embassy")] timeout: embassy_time::Duration::from_millis(1000), duty: Duty::Duty2_1, } @@ -100,16 +100,16 @@ impl Default for Config { #[derive(Copy, Clone)] struct Timeout { - #[cfg(feature = "time")] - deadline: Instant, + #[cfg(feature = "embassy")] + deadline: embassy_time::Instant, } #[allow(dead_code)] impl Timeout { #[inline] fn check(self) -> Result<(), Error> { - #[cfg(feature = "time")] - if Instant::now() > self.deadline { + #[cfg(feature = "embassy")] + if embassy_time::Instant::now() > self.deadline { return Err(Error::Timeout); } @@ -118,7 +118,7 @@ impl Timeout { #[inline] fn with(self, fut: impl Future>) -> impl Future> { - #[cfg(feature = "time")] + #[cfg(feature = "embassy")] { use futures::FutureExt; @@ -128,7 +128,7 @@ impl Timeout { }) } - #[cfg(not(feature = "time"))] + #[cfg(not(feature = "embassy"))] fut } } @@ -137,8 +137,8 @@ impl Timeout { pub struct I2c<'d, T: Instance, M: Mode> { tx_dma: Option>, rx_dma: Option>, - #[cfg(feature = "time")] - timeout: Duration, + #[cfg(feature = "embassy")] + timeout: embassy_time::Duration, _phantom: PhantomData<(&'d mut T, M)>, } @@ -201,7 +201,7 @@ impl<'d, T: Instance, M: Mode> I2c<'d, T, M> { let mut this = Self { tx_dma, rx_dma, - #[cfg(feature = "time")] + #[cfg(feature = "embassy")] timeout: config.timeout, _phantom: PhantomData, }; @@ -213,8 +213,8 @@ impl<'d, T: Instance, M: Mode> I2c<'d, T, M> { fn timeout(&self) -> Timeout { Timeout { - #[cfg(feature = "time")] - deadline: Instant::now() + self.timeout, + #[cfg(feature = "embassy")] + deadline: embassy_time::Instant::now() + self.timeout, } } } diff --git a/src/otg_fs/endpoint.rs b/src/otg_fs/endpoint.rs index 897b407..23e3446 100644 --- a/src/otg_fs/endpoint.rs +++ b/src/otg_fs/endpoint.rs @@ -368,7 +368,7 @@ where v.set_mask_r_res(EpRxResponse::ACK); v.set_r_tog(true); }); - + // Expect the empty OUT token for status poll_fn(|ctx| { super::EP_WAKERS[0].register(ctx.waker()); diff --git a/src/otg_fs/mod.rs b/src/otg_fs/mod.rs index 60a10e5..5fd7727 100644 --- a/src/otg_fs/mod.rs +++ b/src/otg_fs/mod.rs @@ -33,10 +33,10 @@ use ch32_metapac::otg::vals::{EpRxResponse, EpTxResponse, UsbToken}; use embassy_sync::waitqueue::AtomicWaker; use embassy_usb_driver::{Direction, EndpointAddress, EndpointInfo, EndpointType, Event}; use endpoint::{ControlPipe, Endpoint}; -use crate::usb::{Dir, EndpointBufferAllocator, EndpointDataBuffer, In, Out}; use crate::gpio::{AFType, Speed}; use crate::interrupt::typelevel::Interrupt; +use crate::usb::{Dir, EndpointBufferAllocator, EndpointDataBuffer, In, Out}; use crate::{interrupt, peripherals, Peripheral, RccPeripheral}; pub mod endpoint; @@ -351,7 +351,7 @@ where } fn endpoint_set_enabled(&mut self, ep_addr: EndpointAddress, enabled: bool) { - #[cfg(feature="defmt")] + #[cfg(feature = "defmt")] trace!( "[USBFS] Endpoint: {}, {}: Set enable={}", ep_addr.index(),