Skip to content

Commit

Permalink
Assign enough timers for HIL test
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Nov 2, 2024
1 parent 261b153 commit 0f33775
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 21 deletions.
37 changes: 19 additions & 18 deletions esp-hal-embassy/src/time_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,25 +100,26 @@ impl Driver for EmbassyTimer {
unsafe fn allocate_alarm(&self) -> Option<AlarmHandle> {
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
})
Expand Down
29 changes: 26 additions & 3 deletions hil-test/tests/embassy_interrupt_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 _;
Expand Down Expand Up @@ -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::<Periodic>();
esp_hal_embassy::init([
AnyTimer::from(systimer.alarm0),
AnyTimer::from(systimer.alarm1),
]);
}
}

let sw_ints = SoftwareInterruptControl::new(peripherals.SW_INTERRUPT);
Context {
Expand Down

0 comments on commit 0f33775

Please sign in to comment.