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..7a503c1ede 100644 --- a/hil-test/tests/embassy_interrupt_executor.rs +++ b/hil-test/tests/embassy_interrupt_executor.rs @@ -57,7 +57,7 @@ mod test { let peripherals = esp_hal::init(esp_hal::Config::default()); let timg0 = TimerGroup::new(peripherals.TIMG0); - esp_hal_embassy::init(timg0.timer0); + esp_hal_embassy::init([timg0.timer0, timg0.timer1]); let sw_ints = SoftwareInterruptControl::new(peripherals.SW_INTERRUPT); Context {