embassy_agb/
executor.rs

1//! Embassy executor with automatic power management
2//!
3//! Uses `HALTCNT` (0x4000301) to enter Halt mode when idle, waking on interrupts.
4//! - Halt (bit 7=0): CPU pauses until interrupt, hardware continues
5//! - Stop (bit 7=1): Everything pauses (not used by executor)
6
7use core::marker::PhantomData;
8
9use embassy_executor::raw;
10pub use embassy_executor::Spawner;
11
12/// Embassy executor with automatic Halt mode when idle
13pub struct Executor {
14    inner: raw::Executor,
15    not_send: PhantomData<*mut ()>,
16}
17
18impl Default for Executor {
19    fn default() -> Self {
20        Self::new()
21    }
22}
23
24impl Executor {
25    /// Create a new executor for GBA
26    pub fn new() -> Self {
27        Self {
28            inner: raw::Executor::new(core::ptr::null_mut()),
29            not_send: PhantomData,
30        }
31    }
32
33    /// Run the executor (never returns)
34    ///
35    /// Polls tasks continuously, entering Halt mode when idle to save power.
36    pub fn run(&'static mut self, init: impl FnOnce(Spawner)) -> ! {
37        // Initialize time driver if enabled
38        #[cfg(feature = "_time-driver")]
39        crate::time_driver::init();
40
41        // Call the init function with our spawner
42        init(self.inner.spawner());
43
44        // Main executor loop - poll tasks continuously
45        loop {
46            unsafe {
47                self.inner.poll();
48            }
49
50            // Halt until interrupt when idle (power saving)
51            agb::halt();
52        }
53    }
54}