Skip to content

Commit

Permalink
fix(i2c): timeout support
Browse files Browse the repository at this point in the history
  • Loading branch information
andelf committed Nov 9, 2024
1 parent 7d5d975 commit 3ccf0c8
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 17 deletions.
1 change: 0 additions & 1 deletion src/exti.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,5 +391,4 @@ mod irq_impl {
}
}


pub(crate) use irq_impl::*;
26 changes: 13 additions & 13 deletions src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ pub enum Duty {
#[derive(Copy, Clone)]
pub struct Config {
/// Timeout.
#[cfg(feature = "time")]
#[cfg(feature = "embassy")]
pub timeout: embassy_time::Duration,
pub duty: Duty,
}

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,
}
Expand All @@ -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);
}

Expand All @@ -118,7 +118,7 @@ impl Timeout {

#[inline]
fn with<R>(self, fut: impl Future<Output = Result<R, Error>>) -> impl Future<Output = Result<R, Error>> {
#[cfg(feature = "time")]
#[cfg(feature = "embassy")]
{
use futures::FutureExt;

Expand All @@ -128,7 +128,7 @@ impl Timeout {
})
}

#[cfg(not(feature = "time"))]
#[cfg(not(feature = "embassy"))]
fut
}
}
Expand All @@ -137,8 +137,8 @@ impl Timeout {
pub struct I2c<'d, T: Instance, M: Mode> {
tx_dma: Option<ChannelAndRequest<'d>>,
rx_dma: Option<ChannelAndRequest<'d>>,
#[cfg(feature = "time")]
timeout: Duration,
#[cfg(feature = "embassy")]
timeout: embassy_time::Duration,
_phantom: PhantomData<(&'d mut T, M)>,
}

Expand Down Expand Up @@ -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,
};
Expand All @@ -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,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/otg_fs/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
4 changes: 2 additions & 2 deletions src/otg_fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit 3ccf0c8

Please sign in to comment.