diff --git a/esp-hal-embassy/src/time_driver.rs b/esp-hal-embassy/src/time_driver.rs index b46d5d0869..6b73f80010 100644 --- a/esp-hal-embassy/src/time_driver.rs +++ b/esp-hal-embassy/src/time_driver.rs @@ -100,25 +100,26 @@ impl Driver for EmbassyTimer { unsafe fn allocate_alarm(&self) -> Option { critical_section::with(|cs| { for (i, alarm) in self.alarms.borrow(cs).iter().enumerate() { - if !alarm.allocated.get() { - let mut timer = TIMERS.borrow_ref_mut(cs); - // `allocate_alarm` may be called before `esp_hal_embassy::init()`, so - // we need to check if we have timers. - if let Some(timer) = &mut *timer { - // If we do, bind the interrupt handler to the timer. - // This ensures that alarms allocated after init are correctly bound to the - // core that created the executor. - let timer = unwrap!( - timer.get_mut(i), - "There are not enough timers to allocate a new alarm. Call `esp_hal_embassy::init()` with the correct number of timers." - ); - timer.set_interrupt_handler(HANDLERS[i]); - } - - // set alarm so it is not overwritten - alarm.allocated.set(true); - return Some(AlarmHandle::new(i as u8)); + if alarm.allocated.get() { + continue; } + let mut timer = TIMERS.borrow_ref_mut(cs); + // `allocate_alarm` may be called before `esp_hal_embassy::init()`, so + // we need to check if we have timers. + if let Some(timer) = &mut *timer { + // If we do, bind the interrupt handler to the timer. + // This ensures that alarms allocated after init are correctly bound to the + // core that created the executor. + let timer = unwrap!( + timer.get_mut(i), + "There are not enough timers to allocate a new alarm. Call `esp_hal_embassy::init()` with the correct number of timers." + ); + timer.set_interrupt_handler(HANDLERS[i]); + } + + // set alarm so it is not overwritten + alarm.allocated.set(true); + return Some(AlarmHandle::new(i as u8)); } None }) diff --git a/hil-test/tests/embassy_interrupt_executor.rs b/hil-test/tests/embassy_interrupt_executor.rs index 4aad1a3865..042a17ff82 100644 --- a/hil-test/tests/embassy_interrupt_executor.rs +++ b/hil-test/tests/embassy_interrupt_executor.rs @@ -16,7 +16,7 @@ use esp_hal::{ software::{SoftwareInterrupt, SoftwareInterruptControl}, Priority, }, - timer::timg::TimerGroup, + timer::AnyTimer, }; use esp_hal_embassy::InterruptExecutor; use hil_test as _; @@ -56,8 +56,31 @@ mod test { fn init() -> Context { let peripherals = esp_hal::init(esp_hal::Config::default()); - let timg0 = TimerGroup::new(peripherals.TIMG0); - esp_hal_embassy::init(timg0.timer0); + cfg_if::cfg_if! { + if #[cfg(timg_timer1)] { + use esp_hal::timer::timg::TimerGroup; + let timg0 = TimerGroup::new(peripherals.TIMG0); + esp_hal_embassy::init([ + AnyTimer::from(timg0.timer0), + AnyTimer::from(timg0.timer1), + ]); + } else if #[cfg(timg1)] { + use esp_hal::timer::timg::TimerGroup; + let timg0 = TimerGroup::new(peripherals.TIMG0); + let timg1 = TimerGroup::new(peripherals.TIMG1); + esp_hal_embassy::init([ + AnyTimer::from(timg0.timer0), + AnyTimer::from(timg1.timer0), + ]); + } else if #[cfg(systimer)] { + use esp_hal::timer::systimer::{SystemTimer, Periodic}; + let systimer = SystemTimer::new(peripherals.SYSTIMER).split::(); + esp_hal_embassy::init([ + AnyTimer::from(systimer.alarm0), + AnyTimer::from(systimer.alarm1), + ]); + } + } let sw_ints = SoftwareInterruptControl::new(peripherals.SW_INTERRUPT); Context {