2023-10-30 18:36:52 +01:00
|
|
|
/// The STM32 line of microcontrollers support various deep-sleep modes which exploit clock-gating
|
|
|
|
/// to reduce power consumption. `embassy-stm32` provides a low-power executor, [`Executor`] which
|
|
|
|
/// can use knowledge of which peripherals are currently blocked upon to transparently and safely
|
|
|
|
/// enter such low-power modes (currently, only `STOP2`) when idle.
|
|
|
|
///
|
|
|
|
/// The executor determines which peripherals are active by their RCC state; consequently,
|
|
|
|
/// low-power states can only be entered if all peripherals have been `drop`'d. There are a few
|
|
|
|
/// exceptions to this rule:
|
|
|
|
///
|
|
|
|
/// * `GPIO`
|
|
|
|
/// * `RCC`
|
|
|
|
///
|
|
|
|
/// Since entering and leaving low-power modes typically incurs a significant latency, the
|
|
|
|
/// low-power executor will only attempt to enter when the next timer event is at least
|
|
|
|
/// [`time_driver::MIN_STOP_PAUSE`] in the future.
|
|
|
|
///
|
|
|
|
/// Currently there is no macro analogous to `embassy_executor::main` for this executor;
|
|
|
|
/// consequently one must define their entrypoint manually. Moveover, you must relinquish control
|
|
|
|
/// of the `RTC` peripheral to the executor. This will typically look like
|
|
|
|
///
|
|
|
|
/// ```rust,no_run
|
|
|
|
/// use embassy_executor::Spawner;
|
|
|
|
/// use embassy_stm32::low_power::Executor;
|
|
|
|
/// use embassy_stm32::rtc::{Rtc, RtcConfig};
|
|
|
|
/// use static_cell::make_static;
|
|
|
|
///
|
|
|
|
/// #[cortex_m_rt::entry]
|
|
|
|
/// fn main() -> ! {
|
|
|
|
/// Executor::take().run(|spawner| {
|
|
|
|
/// unwrap!(spawner.spawn(async_main(spawner)));
|
|
|
|
/// });
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// #[embassy_executor::task]
|
|
|
|
/// async fn async_main(spawner: Spawner) {
|
|
|
|
/// // initialize the platform...
|
|
|
|
/// let mut config = embassy_stm32::Config::default();
|
|
|
|
/// let p = embassy_stm32::init(config);
|
|
|
|
///
|
|
|
|
/// // give the RTC to the executor...
|
|
|
|
/// let mut rtc = Rtc::new(p.RTC, RtcConfig::default());
|
|
|
|
/// let rtc = make_static!(rtc);
|
|
|
|
/// embassy_stm32::low_power::stop_with_rtc(rtc);
|
|
|
|
///
|
|
|
|
/// // your application here...
|
|
|
|
/// }
|
|
|
|
/// ```
|
2023-08-23 00:31:40 +02:00
|
|
|
use core::arch::asm;
|
|
|
|
use core::marker::PhantomData;
|
2023-10-12 01:16:42 +02:00
|
|
|
use core::sync::atomic::{compiler_fence, Ordering};
|
2023-08-23 00:31:40 +02:00
|
|
|
|
2023-08-23 03:00:00 +02:00
|
|
|
use cortex_m::peripheral::SCB;
|
2023-08-23 00:31:40 +02:00
|
|
|
use embassy_executor::*;
|
2023-08-23 03:00:00 +02:00
|
|
|
|
|
|
|
use crate::interrupt;
|
2023-08-27 02:23:25 +02:00
|
|
|
use crate::time_driver::{get_driver, RtcDriver};
|
2023-08-23 00:31:40 +02:00
|
|
|
|
|
|
|
const THREAD_PENDER: usize = usize::MAX;
|
|
|
|
|
2023-08-27 02:23:25 +02:00
|
|
|
use crate::rtc::Rtc;
|
2023-08-23 00:00:00 +02:00
|
|
|
|
2023-08-26 01:41:51 +02:00
|
|
|
static mut EXECUTOR: Option<Executor> = None;
|
2023-08-23 00:00:00 +02:00
|
|
|
|
2023-08-23 03:00:00 +02:00
|
|
|
foreach_interrupt! {
|
|
|
|
(RTC, rtc, $block:ident, WKUP, $irq:ident) => {
|
|
|
|
#[interrupt]
|
|
|
|
unsafe fn $irq() {
|
2023-09-16 01:41:33 +02:00
|
|
|
EXECUTOR.as_mut().unwrap().on_wakeup_irq();
|
2023-08-23 03:00:00 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-09-16 01:41:33 +02:00
|
|
|
#[allow(dead_code)]
|
|
|
|
pub(crate) unsafe fn on_wakeup_irq() {
|
|
|
|
EXECUTOR.as_mut().unwrap().on_wakeup_irq();
|
|
|
|
}
|
2023-08-27 02:23:25 +02:00
|
|
|
|
2023-08-23 00:00:00 +02:00
|
|
|
pub fn stop_with_rtc(rtc: &'static Rtc) {
|
2023-08-26 01:41:51 +02:00
|
|
|
unsafe { EXECUTOR.as_mut().unwrap() }.stop_with_rtc(rtc)
|
2023-08-23 00:00:00 +02:00
|
|
|
}
|
|
|
|
|
2023-10-26 02:07:31 +02:00
|
|
|
pub fn stop_ready(stop_mode: StopMode) -> bool {
|
2023-11-04 19:49:54 +01:00
|
|
|
match unsafe { EXECUTOR.as_mut().unwrap() }.stop_mode() {
|
|
|
|
Some(StopMode::Stop2) => true,
|
|
|
|
Some(StopMode::Stop1) => stop_mode == StopMode::Stop1,
|
|
|
|
None => false,
|
|
|
|
}
|
2023-10-26 02:07:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[non_exhaustive]
|
2023-11-04 19:49:54 +01:00
|
|
|
#[derive(PartialEq)]
|
2023-10-26 02:07:31 +02:00
|
|
|
pub enum StopMode {
|
2023-11-04 19:49:54 +01:00
|
|
|
Stop1,
|
2023-10-26 02:07:31 +02:00
|
|
|
Stop2,
|
|
|
|
}
|
|
|
|
|
2023-08-23 00:31:40 +02:00
|
|
|
/// Thread mode executor, using WFE/SEV.
|
|
|
|
///
|
|
|
|
/// This is the simplest and most common kind of executor. It runs on
|
|
|
|
/// thread mode (at the lowest priority level), and uses the `WFE` ARM instruction
|
|
|
|
/// to sleep when it has no more work to do. When a task is woken, a `SEV` instruction
|
|
|
|
/// is executed, to make the `WFE` exit from sleep and poll the task.
|
|
|
|
///
|
|
|
|
/// This executor allows for ultra low power consumption for chips where `WFE`
|
|
|
|
/// triggers low-power sleep without extra steps. If your chip requires extra steps,
|
|
|
|
/// you may use [`raw::Executor`] directly to program custom behavior.
|
|
|
|
pub struct Executor {
|
|
|
|
inner: raw::Executor,
|
|
|
|
not_send: PhantomData<*mut ()>,
|
2023-08-26 01:41:51 +02:00
|
|
|
scb: SCB,
|
2023-08-27 02:23:25 +02:00
|
|
|
time_driver: &'static RtcDriver,
|
2023-08-23 00:31:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Executor {
|
|
|
|
/// Create a new Executor.
|
2023-08-27 02:23:25 +02:00
|
|
|
pub fn take() -> &'static mut Self {
|
2023-11-01 23:17:14 +01:00
|
|
|
critical_section::with(|_| unsafe {
|
2023-08-26 01:41:51 +02:00
|
|
|
assert!(EXECUTOR.is_none());
|
|
|
|
|
|
|
|
EXECUTOR = Some(Self {
|
|
|
|
inner: raw::Executor::new(THREAD_PENDER as *mut ()),
|
|
|
|
not_send: PhantomData,
|
|
|
|
scb: cortex_m::Peripherals::steal().SCB,
|
2023-08-27 02:23:25 +02:00
|
|
|
time_driver: get_driver(),
|
2023-08-26 01:41:51 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
EXECUTOR.as_mut().unwrap()
|
2023-11-01 23:17:14 +01:00
|
|
|
})
|
2023-08-23 00:31:40 +02:00
|
|
|
}
|
|
|
|
|
2023-08-26 01:41:51 +02:00
|
|
|
unsafe fn on_wakeup_irq(&mut self) {
|
2023-08-27 02:23:25 +02:00
|
|
|
self.time_driver.resume_time();
|
2023-10-03 01:11:03 +02:00
|
|
|
trace!("low power: resume");
|
2023-08-26 01:41:51 +02:00
|
|
|
}
|
2023-08-23 03:00:00 +02:00
|
|
|
|
2023-08-26 01:41:51 +02:00
|
|
|
pub(self) fn stop_with_rtc(&mut self, rtc: &'static Rtc) {
|
2023-08-27 02:23:25 +02:00
|
|
|
self.time_driver.set_rtc(rtc);
|
2023-08-25 02:29:11 +02:00
|
|
|
|
2023-08-28 22:30:29 +02:00
|
|
|
rtc.enable_wakeup_line();
|
2023-09-16 01:41:33 +02:00
|
|
|
|
|
|
|
trace!("low power: stop with rtc configured");
|
2023-08-23 03:00:00 +02:00
|
|
|
}
|
|
|
|
|
2023-11-04 19:49:54 +01:00
|
|
|
fn stop_mode(&self) -> Option<StopMode> {
|
|
|
|
if unsafe { crate::rcc::REFCOUNT_STOP2 == 0 } && unsafe { crate::rcc::REFCOUNT_STOP1 == 0 } {
|
|
|
|
Some(StopMode::Stop2)
|
|
|
|
} else if unsafe { crate::rcc::REFCOUNT_STOP1 == 0 } {
|
|
|
|
Some(StopMode::Stop1)
|
|
|
|
} else {
|
|
|
|
None
|
2023-10-26 02:07:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-04 19:49:54 +01:00
|
|
|
fn configure_stop(&mut self, _stop_mode: StopMode) {
|
|
|
|
// TODO: configure chip-specific settings for stop
|
|
|
|
}
|
|
|
|
|
2023-08-27 02:23:25 +02:00
|
|
|
fn configure_pwr(&mut self) {
|
|
|
|
self.scb.clear_sleepdeep();
|
|
|
|
|
2023-10-03 01:11:03 +02:00
|
|
|
compiler_fence(Ordering::SeqCst);
|
2023-08-27 02:23:25 +02:00
|
|
|
|
2023-11-04 19:49:54 +01:00
|
|
|
let stop_mode = self.stop_mode();
|
|
|
|
if stop_mode.is_none() {
|
2023-10-03 01:11:03 +02:00
|
|
|
trace!("low power: not ready to stop");
|
|
|
|
} else if self.time_driver.pause_time().is_err() {
|
|
|
|
trace!("low power: failed to pause time");
|
|
|
|
} else {
|
2023-11-04 19:49:54 +01:00
|
|
|
let stop_mode = stop_mode.unwrap();
|
|
|
|
match stop_mode {
|
|
|
|
StopMode::Stop1 => trace!("low power: stop 1"),
|
|
|
|
StopMode::Stop2 => trace!("low power: stop 2"),
|
|
|
|
}
|
|
|
|
self.configure_stop(stop_mode);
|
|
|
|
|
|
|
|
#[cfg(not(feature = "low-power-debug-with-sleep"))]
|
2023-10-03 01:11:03 +02:00
|
|
|
self.scb.set_sleepdeep();
|
|
|
|
}
|
2023-08-23 00:31:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Run the executor.
|
|
|
|
///
|
|
|
|
/// The `init` closure is called with a [`Spawner`] that spawns tasks on
|
|
|
|
/// this executor. Use it to spawn the initial task(s). After `init` returns,
|
|
|
|
/// the executor starts running the tasks.
|
|
|
|
///
|
|
|
|
/// To spawn more tasks later, you may keep copies of the [`Spawner`] (it is `Copy`),
|
|
|
|
/// for example by passing it as an argument to the initial tasks.
|
|
|
|
///
|
|
|
|
/// This function requires `&'static mut self`. This means you have to store the
|
|
|
|
/// Executor instance in a place where it'll live forever and grants you mutable
|
|
|
|
/// access. There's a few ways to do this:
|
|
|
|
///
|
|
|
|
/// - a [StaticCell](https://docs.rs/static_cell/latest/static_cell/) (safe)
|
|
|
|
/// - a `static mut` (unsafe)
|
|
|
|
/// - a local variable in a function you know never returns (like `fn main() -> !`), upgrading its lifetime with `transmute`. (unsafe)
|
|
|
|
///
|
|
|
|
/// This function never returns.
|
|
|
|
pub fn run(&'static mut self, init: impl FnOnce(Spawner)) -> ! {
|
2023-08-27 02:23:25 +02:00
|
|
|
init(unsafe { EXECUTOR.as_mut().unwrap() }.inner.spawner());
|
2023-08-23 00:31:40 +02:00
|
|
|
|
|
|
|
loop {
|
|
|
|
unsafe {
|
2023-08-27 02:23:25 +02:00
|
|
|
EXECUTOR.as_mut().unwrap().inner.poll();
|
2023-08-23 03:00:00 +02:00
|
|
|
self.configure_pwr();
|
2023-08-23 00:31:40 +02:00
|
|
|
asm!("wfe");
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|