use core::cell::RefCell;
#[cfg(any(esp32, esp32c3, esp32s3, esp32c6))]
use core::time::Duration;
#[cfg(any(esp32, esp32s3))]
use crate::gpio::RTCPin as RtcIoWakeupPinType;
#[cfg(any(esp32c3, esp32c6))]
use crate::gpio::RTCPinWithResistors as RtcIoWakeupPinType;
use crate::Rtc;
#[cfg_attr(esp32, path = "esp32.rs")]
#[cfg_attr(esp32s3, path = "esp32s3.rs")]
#[cfg_attr(esp32c3, path = "esp32c3.rs")]
#[cfg_attr(esp32c6, path = "esp32c6.rs")]
mod sleep_impl;
pub use sleep_impl::*;
#[derive(Debug, Default, Clone, Copy, PartialEq)]
pub enum WakeupLevel {
Low,
#[default]
High,
}
#[derive(Debug, Default, Clone, Copy)]
#[cfg(any(esp32, esp32c3, esp32s3, esp32c6))]
pub struct TimerWakeupSource {
duration: Duration,
}
#[cfg(any(esp32, esp32c3, esp32s3, esp32c6))]
impl TimerWakeupSource {
pub fn new(duration: Duration) -> Self {
Self { duration }
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Error {
NotRtcPin,
TooManyWakeupSources,
}
#[derive(Debug)]
#[cfg(any(esp32, esp32s3))]
pub struct Ext0WakeupSource<'a, P: RtcIoWakeupPinType> {
pin: RefCell<&'a mut P>,
level: WakeupLevel,
}
#[cfg(any(esp32, esp32s3))]
impl<'a, P: RtcIoWakeupPinType> Ext0WakeupSource<'a, P> {
pub fn new(pin: &'a mut P, level: WakeupLevel) -> Self {
Self {
pin: RefCell::new(pin),
level,
}
}
}
#[cfg(any(esp32, esp32s3))]
pub struct Ext1WakeupSource<'a, 'b> {
pins: RefCell<&'a mut [&'b mut dyn RtcIoWakeupPinType]>,
level: WakeupLevel,
}
#[cfg(any(esp32, esp32s3))]
impl<'a, 'b> Ext1WakeupSource<'a, 'b> {
pub fn new(pins: &'a mut [&'b mut dyn RtcIoWakeupPinType], level: WakeupLevel) -> Self {
Self {
pins: RefCell::new(pins),
level,
}
}
}
#[cfg(esp32c6)]
pub struct Ext1WakeupSource<'a, 'b> {
pins: RefCell<&'a mut [(&'b mut dyn RtcIoWakeupPinType, WakeupLevel)]>,
}
#[cfg(esp32c6)]
impl<'a, 'b> Ext1WakeupSource<'a, 'b> {
pub fn new(pins: &'a mut [(&'b mut dyn RtcIoWakeupPinType, WakeupLevel)]) -> Self {
Self {
pins: RefCell::new(pins),
}
}
}
#[cfg(any(esp32c3, esp32s3))]
pub struct RtcioWakeupSource<'a, 'b> {
pins: RefCell<&'a mut [(&'b mut dyn RtcIoWakeupPinType, WakeupLevel)]>,
}
#[cfg(any(esp32c3, esp32s3))]
impl<'a, 'b> RtcioWakeupSource<'a, 'b> {
pub fn new(pins: &'a mut [(&'b mut dyn RtcIoWakeupPinType, WakeupLevel)]) -> Self {
Self {
pins: RefCell::new(pins),
}
}
}
#[cfg(not(pmu))]
bitfield::bitfield! {
#[derive(Default, Clone, Copy)]
pub struct WakeTriggers(u16);
impl Debug;
pub ext0, set_ext0: 0;
pub ext1, set_ext1: 1;
pub gpio, set_gpio: 2;
pub timer, set_timer: 3;
pub sdio, set_sdio: 4;
pub mac, set_mac: 5;
pub uart0, set_uart0: 6;
pub uart1, set_uart1: 7;
pub touch, set_touch: 8;
pub ulp, set_ulp: 9;
pub bt, set_bt: 10;
}
#[cfg(pmu)]
bitfield::bitfield! {
#[derive(Default, Clone, Copy)]
pub struct WakeTriggers(u16);
impl Debug;
pub ext0, set_ext0: 0;
pub ext1, set_ext1: 1;
pub gpio, set_gpio: 2;
pub wifi_beacon, set_wifi_beacon: 3;
pub timer, set_timer: 4;
pub wifi_soc, set_wifi_soc: 5;
pub uart0, set_uart0: 6;
pub uart1, set_uart1: 7;
pub sdio, set_sdio: 8;
pub bt, set_bt: 10;
pub lp_core, set_lp_core: 11;
pub usb, set_usb: 14;
}
pub trait WakeSource {
fn apply(&self, rtc: &Rtc, triggers: &mut WakeTriggers, sleep_config: &mut RtcSleepConfig);
}