Skip to content

Commit

Permalink
Address reviews: Remove duplicated/useless code and add HIL test for …
Browse files Browse the repository at this point in the history
…delay_us and delay_ms
  • Loading branch information
JurajSadel committed Sep 18, 2024
1 parent 68a9d40 commit 0b12115
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 45 deletions.
82 changes: 42 additions & 40 deletions esp-hal/src/timer/timg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,51 +303,54 @@ where
{
/// Construct a new instance of [`TimerGroup`] in asynchronous mode
pub fn new_async(_timer_group: impl Peripheral<P = T> + 'd) -> Self {
use crate::timer::timg::asynch::timg0_timer0_handler;
#[cfg(timg_timer1)]
use crate::timer::timg::asynch::timg0_timer1_handler;
#[cfg(timg1)]
use crate::timer::timg::asynch::timg1_timer0_handler;
#[cfg(all(timg1, timg_timer1))]
use crate::timer::timg::asynch::timg1_timer1_handler;

match T::id() {
0 => unsafe {
interrupt::bind_interrupt(Interrupt::TG0_T0_LEVEL, timg0_timer0_handler.handler());
interrupt::enable(Interrupt::TG0_T0_LEVEL, timg0_timer0_handler.priority())
.unwrap();

#[cfg(timg_timer1)]
{
interrupt::bind_interrupt(
Interrupt::TG0_T1_LEVEL,
timg0_timer1_handler.handler(),
);
interrupt::enable(Interrupt::TG0_T1_LEVEL, timg0_timer1_handler.priority())
.unwrap();
}
},
#[cfg(any(timg1, timg_timer1))]
1 => unsafe {
#[cfg(timg1)]
{
0 => {
use crate::timer::timg::asynch::timg0_timer0_handler;
unsafe {
interrupt::bind_interrupt(
Interrupt::TG1_T0_LEVEL,
timg1_timer0_handler.handler(),
Interrupt::TG0_T0_LEVEL,
timg0_timer0_handler.handler(),
);
interrupt::enable(Interrupt::TG1_T0_LEVEL, timg1_timer0_handler.priority())
interrupt::enable(Interrupt::TG0_T0_LEVEL, timg0_timer0_handler.priority())
.unwrap();

#[cfg(timg_timer1)]
{
use crate::timer::timg::asynch::timg0_timer1_handler;

interrupt::bind_interrupt(
Interrupt::TG0_T1_LEVEL,
timg0_timer1_handler.handler(),
);
interrupt::enable(Interrupt::TG0_T1_LEVEL, timg0_timer1_handler.priority())
.unwrap();
}
}
#[cfg(all(timg1, timg_timer1))]
{
interrupt::bind_interrupt(
Interrupt::TG1_T1_LEVEL,
timg1_timer1_handler.handler(),
);
interrupt::enable(Interrupt::TG1_T1_LEVEL, timg1_timer1_handler.priority())
.unwrap();
}
#[cfg(timg1)]
1 => {
use crate::timer::timg::asynch::timg1_timer0_handler;
unsafe {
{
interrupt::bind_interrupt(
Interrupt::TG1_T0_LEVEL,
timg1_timer0_handler.handler(),
);
interrupt::enable(Interrupt::TG1_T0_LEVEL, timg1_timer0_handler.priority())
.unwrap();
}
#[cfg(timg_timer1)]
{
use crate::timer::timg::asynch::timg1_timer1_handler;
interrupt::bind_interrupt(
Interrupt::TG1_T1_LEVEL,
timg1_timer1_handler.handler(),
);
interrupt::enable(Interrupt::TG1_T1_LEVEL, timg1_timer1_handler.priority())
.unwrap();
}
}
},
}
_ => unreachable!(),
}

Expand Down Expand Up @@ -1153,7 +1156,6 @@ mod asynch {
WAKERS[index as usize].register(ctx.waker());

if self.event_bit_is_clear() {
self.timer.clear_interrupt();
Poll::Ready(())
} else {
Poll::Pending
Expand Down
100 changes: 95 additions & 5 deletions hil-test/tests/delay_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,32 @@ async fn test_async_delay_ns(mut timer: impl DelayNs, duration: u32) {
);
}

async fn test_async_delay_us(mut timer: impl DelayNs, duration: u32) {
let t1 = esp_hal::time::now();
timer.delay_us(duration).await;
let t2 = esp_hal::time::now();

assert!(t2 > t1);
assert!(
(t2 - t1).to_micros() >= duration as u64,
"diff: {:?}",
(t2 - t1).to_micros()
);
}

async fn test_async_delay_ms(mut timer: impl DelayNs, duration: u32) {
let t1 = esp_hal::time::now();
timer.delay_ms(duration).await;
let t2 = esp_hal::time::now();

assert!(t2 > t1);
assert!(
(t2 - t1).to_millis() >= duration as u64,
"diff: {:?}",
(t2 - t1).to_millis()
);
}

#[cfg(test)]
#[embedded_test::tests(executor = esp_hal_embassy::Executor::new())]
mod tests {
Expand All @@ -51,17 +77,17 @@ mod tests {
let unit = FrozenUnit::new(&mut alarms.unit0);
let alarm0 = Alarm::new_async(alarms.comparator0, &unit).into_periodic();

test_async_delay_ns(alarm0, 600_000_000).await;
test_async_delay_ns(alarm0, 10_000_000).await;
}

#[test]
#[timeout(2)]
async fn test_timg0_async_delay_ns(ctx: Context) {
let timg0 = TimerGroup::new_async(ctx.peripherals.TIMG0);

test_async_delay_ns(timg0.timer0, 600_000_000).await;
test_async_delay_ns(timg0.timer0, 10_000_000).await;
#[cfg(timg_timer1)]
test_async_delay_ns(timg0.timer1, 600_000_000).await;
test_async_delay_ns(timg0.timer1, 10_000_000).await;
}

#[cfg(timg1)]
Expand All @@ -70,8 +96,72 @@ mod tests {
async fn test_timg1_async_delay_ns(ctx: Context) {
let timg1 = TimerGroup::new_async(ctx.peripherals.TIMG1);

test_async_delay_ns(timg1.timer0, 600_000_000).await;
test_async_delay_ns(timg1.timer0, 10_000_000).await;
#[cfg(timg_timer1)]
test_async_delay_ns(timg1.timer1, 10_000_000).await;
}

#[cfg(systimer)]
#[test]
#[timeout(2)]
async fn test_systimer_async_delay_us(ctx: Context) {
let mut alarms = SystemTimer::new(ctx.peripherals.SYSTIMER);
let unit = FrozenUnit::new(&mut alarms.unit0);
let alarm0 = Alarm::new_async(alarms.comparator0, &unit).into_periodic();

test_async_delay_us(alarm0, 10_000).await;
}

#[test]
#[timeout(2)]
async fn test_timg0_async_delay_us(ctx: Context) {
let timg0 = TimerGroup::new_async(ctx.peripherals.TIMG0);

test_async_delay_us(timg0.timer0, 10_000).await;
#[cfg(timg_timer1)]
test_async_delay_us(timg0.timer1, 10_000).await;
}

#[cfg(timg1)]
#[test]
#[timeout(2)]
async fn test_timg1_async_delay_us(ctx: Context) {
let timg1 = TimerGroup::new_async(ctx.peripherals.TIMG1);

test_async_delay_us(timg1.timer0, 10_000).await;
#[cfg(timg_timer1)]
test_async_delay_us(timg1.timer1, 10_000).await;
}

#[cfg(systimer)]
#[test]
#[timeout(2)]
async fn test_systimer_async_delay_ms(ctx: Context) {
let mut alarms = SystemTimer::new(ctx.peripherals.SYSTIMER);
let unit = FrozenUnit::new(&mut alarms.unit0);
let alarm0 = Alarm::new_async(alarms.comparator0, &unit).into_periodic();

test_async_delay_ms(alarm0, 10).await;
}

#[test]
#[timeout(2)]
async fn test_timg0_async_delay_ms(ctx: Context) {
let timg0 = TimerGroup::new_async(ctx.peripherals.TIMG0);

test_async_delay_ms(timg0.timer0, 10).await;
#[cfg(timg_timer1)]
test_async_delay_ms(timg0.timer1, 10).await;
}

#[cfg(timg1)]
#[test]
#[timeout(2)]
async fn test_timg1_async_delay_ms(ctx: Context) {
let timg1 = TimerGroup::new_async(ctx.peripherals.TIMG1);

test_async_delay_ms(timg1.timer0, 10).await;
#[cfg(timg_timer1)]
test_async_delay_ns(timg1.timer1, 600_000_000).await;
test_async_delay_ms(timg1.timer1, 10).await;
}
}

0 comments on commit 0b12115

Please sign in to comment.