esp_hal/rtc_cntl/sleep/mod.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
//! # RTC Control Sleep Module
//!
//! ## Overview
//! The `sleep` module allows configuring various wakeup sources and setting up
//! the sleep behavior based on those sources. The supported wakeup sources
//! include:
//! * `GPIO` pins - light sleep only
//! * timers
//! * `SDIO (Secure Digital Input/Output) - light sleep only`
//! * `MAC (Media Access Control)` wake - light sleep only
//! * `UART0` - light sleep only
//! * `UART1` - light sleep only
//! * `touch`
//! * `ULP (Ultra-Low Power)` wake
//! * `BT (Bluetooth) wake` - light sleep only
use core::cell::RefCell;
#[cfg(any(esp32, esp32c3, esp32s3, esp32c6, esp32c2))]
use core::time::Duration;
#[cfg(any(esp32, esp32s3))]
use crate::gpio::RtcPin as RtcIoWakeupPinType;
#[cfg(any(esp32c3, esp32c6, esp32c2))]
use crate::gpio::RtcPinWithResistors as RtcIoWakeupPinType;
use crate::rtc_cntl::Rtc;
#[cfg(any(esp32, esp32s3))]
use crate::{
into_ref,
peripheral::{Peripheral, PeripheralRef},
};
#[cfg_attr(esp32, path = "esp32.rs")]
#[cfg_attr(esp32s3, path = "esp32s3.rs")]
#[cfg_attr(esp32c3, path = "esp32c3.rs")]
#[cfg_attr(esp32c6, path = "esp32c6.rs")]
#[cfg_attr(esp32c2, path = "esp32c2.rs")]
mod sleep_impl;
pub use sleep_impl::*;
#[derive(Debug, Default, Clone, Copy, PartialEq)]
/// Level at which a wake-up event is triggered
pub enum WakeupLevel {
/// The wake-up event is triggered when the pin is low.
Low,
#[default]
/// The wake-up event is triggered when the pin is high.
High,
}
/// Represents a timer wake-up source, triggering an event after a specified
/// duration.
///
/// ```rust, no_run
#[doc = crate::before_snippet!()]
/// # use core::time::Duration;
/// # use esp_hal::delay::Delay;
/// # use esp_hal::rtc_cntl::{reset_reason, sleep::TimerWakeupSource, wakeup_cause, Rtc, SocResetReason};
/// # use esp_hal::system::Cpu;
///
/// let delay = Delay::new();
/// let mut rtc = Rtc::new(peripherals.LPWR);
///
/// let reason = reset_reason(Cpu::ProCpu);
/// let wake_reason = wakeup_cause();
///
/// println!("{:?} {?}", reason, wake_reason);
///
/// let timer = TimerWakeupSource::new(Duration::from_secs(5));
/// delay.delay_millis(100);
/// rtc.sleep_deep(&[&timer]);
///
/// # }
/// ```
#[derive(Debug, Default, Clone, Copy)]
#[cfg(any(esp32, esp32c3, esp32s3, esp32c6, esp32c2))]
pub struct TimerWakeupSource {
/// The duration after which the wake-up event is triggered.
duration: Duration,
}
#[cfg(any(esp32, esp32c3, esp32s3, esp32c6, esp32c2))]
impl TimerWakeupSource {
/// Creates a new timer wake-up source with the specified duration.
pub fn new(duration: Duration) -> Self {
Self { duration }
}
}
/// Errors that can occur when configuring RTC wake-up sources.
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Error {
/// The selected pin is not a valid RTC pin.
NotRtcPin,
/// The maximum number of wake-up sources has been exceeded.
TooManyWakeupSources,
}
/// External wake-up source (Ext0).
///
/// ```rust, no_run
#[doc = crate::before_snippet!()]
/// # use core::time::Duration;
/// # use esp_hal::delay::Delay;
/// # use esp_hal::rtc_cntl::{reset_reason, sleep::{Ext0WakeupSource, TimerWakeupSource, WakeupLevel}, wakeup_cause, Rtc, SocResetReason};
/// # use esp_hal::system::Cpu;
/// # use esp_hal::gpio::{Input, InputConfig, Pull};
///
/// let delay = Delay::new();
/// let mut rtc = Rtc::new(peripherals.LPWR);
///
/// let config = InputConfig::default().with_pull(Pull::None);
/// let mut pin_4 = peripherals.GPIO4;
/// let pin_4_input = Input::new(&mut pin_4, config);
///
/// let reason = reset_reason(Cpu::ProCpu);
/// let wake_reason = wakeup_cause();
///
/// println!("{:?} {?}", reason, wake_reason);
///
/// let timer = TimerWakeupSource::new(Duration::from_secs(30));
///
/// core::mem::drop(pin_4_input);
/// let ext0 = Ext0WakeupSource::new(&mut pin_4, WakeupLevel::High);
///
/// delay.delay_millis(100);
/// rtc.sleep_deep(&[&timer, &ext0]);
///
/// # }
/// ```
#[cfg(any(esp32, esp32s3))]
pub struct Ext0WakeupSource<'a, P: RtcIoWakeupPinType> {
/// The pin used as the wake-up source.
pin: RefCell<PeripheralRef<'a, P>>,
/// The level at which the wake-up event is triggered.
level: WakeupLevel,
}
#[cfg(any(esp32, esp32s3))]
impl<'a, P: RtcIoWakeupPinType> Ext0WakeupSource<'a, P> {
/// Creates a new external wake-up source (Ext0``) with the specified pin
/// and wake-up level.
pub fn new(pin: impl Peripheral<P = P> + 'a, level: WakeupLevel) -> Self {
into_ref!(pin);
Self {
pin: RefCell::new(pin),
level,
}
}
}
/// External wake-up source (Ext1).
///
/// ```rust, no_run
#[doc = crate::before_snippet!()]
/// # use core::time::Duration;
/// # use esp_hal::delay::Delay;
/// # use esp_hal::rtc_cntl::{reset_reason, sleep::{Ext1WakeupSource, TimerWakeupSource, WakeupLevel}, wakeup_cause, Rtc, SocResetReason};
/// # use esp_hal::system::Cpu;
/// # use esp_hal::gpio::{Input, InputConfig, Pull, RtcPin};
/// # use esp_hal::peripheral::Peripheral;
///
/// let delay = Delay::new();
/// let mut rtc = Rtc::new(peripherals.LPWR);
///
/// let config = InputConfig::default().with_pull(Pull::None);
/// let mut pin_2 = peripherals.GPIO2;
/// let mut pin_4 = peripherals.GPIO4;
/// let pin_4_driver = Input::new(&mut pin_4, config);
///
/// let reason = reset_reason(Cpu::ProCpu);
/// let wake_reason = wakeup_cause();
///
/// println!("{:?} {?}", reason, wake_reason);
///
/// let timer = TimerWakeupSource::new(Duration::from_secs(30));
///
/// // Drop the driver to access `pin_4`
/// core::mem::drop(pin_4_driver);
///
/// let mut wakeup_pins: [&mut dyn RtcPin; 2] = [&mut pin_4, &mut pin_2];
///
/// let ext1 = Ext1WakeupSource::new(&mut wakeup_pins, WakeupLevel::High);
///
/// delay.delay_millis(100);
/// rtc.sleep_deep(&[&timer, &ext1]);
///
/// # }
/// ```
#[cfg(any(esp32, esp32s3))]
pub struct Ext1WakeupSource<'a, 'b> {
/// A collection of pins used as wake-up sources.
pins: RefCell<&'a mut [&'b mut dyn RtcIoWakeupPinType]>,
/// The level at which the wake-up event is triggered across all pins.
level: WakeupLevel,
}
#[cfg(any(esp32, esp32s3))]
impl<'a, 'b> Ext1WakeupSource<'a, 'b> {
/// Creates a new external wake-up source (Ext1) with the specified pins and
/// wake-up level.
pub fn new(pins: &'a mut [&'b mut dyn RtcIoWakeupPinType], level: WakeupLevel) -> Self {
Self {
pins: RefCell::new(pins),
level,
}
}
}
/// External wake-up source (Ext1).
/// ```rust, no_run
#[doc = crate::before_snippet!()]
/// # use core::time::Duration;
/// # use esp_hal::delay::Delay;
/// # use esp_hal::rtc_cntl::{reset_reason, sleep::{Ext1WakeupSource, TimerWakeupSource, WakeupLevel}, wakeup_cause, Rtc, SocResetReason};
/// # use esp_hal::system::Cpu;
/// # use esp_hal::gpio::{Input, InputConfig, Pull, RtcPinWithResistors};
/// # use esp_hal::peripheral::Peripheral;
///
/// let delay = Delay::new();
/// let mut rtc = Rtc::new(peripherals.LPWR);
///
/// let config = InputConfig::default().with_pull(Pull::None);
/// let mut pin2 = peripherals.GPIO2;
/// let mut pin3 = peripherals.GPIO3;
/// let mut pin2_input = Input::new(&mut pin2, config);
///
/// let reason = reset_reason(Cpu::ProCpu);
/// let wake_reason = wakeup_cause();
///
/// println!("{:?} {?}", reason, wake_reason);
///
/// let timer = TimerWakeupSource::new(Duration::from_secs(30));
///
/// core::mem::drop(pin2_input);
///
/// let wakeup_pins: &mut [(&mut dyn RtcPinWithResistors, WakeupLevel)] =
/// &mut [
/// (&mut pin2, WakeupLevel::Low),
/// (&mut pin3, WakeupLevel::High),
/// ];
///
/// let ext1 = Ext1WakeupSource::new(wakeup_pins);
///
/// delay.delay_millis(100);
/// rtc.sleep_deep(&[&timer, &ext1]);
///
/// # }
/// ```
#[cfg(esp32c6)]
pub struct Ext1WakeupSource<'a, 'b> {
pins: RefCell<&'a mut [(&'b mut dyn RtcIoWakeupPinType, WakeupLevel)]>,
}
#[cfg(esp32c6)]
impl<'a, 'b> Ext1WakeupSource<'a, 'b> {
/// Creates a new external wake-up source (Ext1) with the specified pins and
/// wake-up level.
pub fn new(pins: &'a mut [(&'b mut dyn RtcIoWakeupPinType, WakeupLevel)]) -> Self {
Self {
pins: RefCell::new(pins),
}
}
}
/// RTC_IO wakeup source
///
/// RTC_IO wakeup allows configuring any combination of RTC_IO pins with
/// arbitrary wakeup levels to wake up the chip from sleep. This wakeup source
/// can be used to wake up from both light and deep sleep.
///
/// ```rust, no_run
#[doc = crate::before_snippet!()]
/// # use core::time::Duration;
/// # use esp_hal::delay::Delay;
/// # use esp_hal::gpio::{self, Input, InputConfig, Pull};
/// # use esp_hal::rtc_cntl::{reset_reason, sleep::{RtcioWakeupSource, TimerWakeupSource, WakeupLevel}, wakeup_cause, Rtc, SocResetReason};
/// # use esp_hal::system::Cpu;
/// # use esp_hal::peripheral::Peripheral;
///
/// let mut rtc = Rtc::new(peripherals.LPWR);
///
/// let reason = reset_reason(Cpu::ProCpu);
/// let wake_reason = wakeup_cause();
///
/// println!("{:?} {?}", reason, wake_reason);
///
/// let delay = Delay::new();
/// let timer = TimerWakeupSource::new(Duration::from_secs(10));
#[cfg_attr(any(esp32c3, esp32c2), doc = "let mut pin_0 = peripherals.GPIO2;")]
#[cfg_attr(any(esp32c3, esp32c2), doc = "let mut pin_1 = peripherals.GPIO3;")]
#[cfg_attr(esp32s3, doc = "let mut pin_0 = peripherals.GPIO17;")]
#[cfg_attr(esp32s3, doc = "let mut pin_1 = peripherals.GPIO18;")]
#[cfg_attr(
any(esp32c3, esp32c2),
doc = "let wakeup_pins: &mut [(&mut dyn gpio::RtcPinWithResistors, WakeupLevel)] = &mut ["
)]
#[cfg_attr(
esp32s3,
doc = "let wakeup_pins: &mut [(&mut dyn gpio::RtcPin, WakeupLevel)] = &mut ["
)]
/// (&mut pin_0, WakeupLevel::Low),
/// (&mut pin_1, WakeupLevel::High),
/// ];
///
/// let rtcio = RtcioWakeupSource::new(wakeup_pins);
/// delay.delay_millis(100);
/// rtc.sleep_deep(&[&timer, &rtcio]);
///
/// # }
/// ```
#[cfg(any(esp32c3, esp32s3, esp32c2))]
pub struct RtcioWakeupSource<'a, 'b> {
pins: RefCell<&'a mut [(&'b mut dyn RtcIoWakeupPinType, WakeupLevel)]>,
}
#[cfg(any(esp32c3, esp32s3, esp32c2))]
impl<'a, 'b> RtcioWakeupSource<'a, 'b> {
/// Creates a new external wake-up source (Ext1).
pub fn new(pins: &'a mut [(&'b mut dyn RtcIoWakeupPinType, WakeupLevel)]) -> Self {
Self {
pins: RefCell::new(pins),
}
}
}
/// LP Core wakeup source
///
/// Wake up from LP core. This wakeup source
/// can be used to wake up from both light and deep sleep.
#[cfg(esp32c6)]
pub struct WakeFromLpCoreWakeupSource {}
#[cfg(esp32c6)]
impl WakeFromLpCoreWakeupSource {
/// Create a new instance of `WakeFromLpCoreWakeupSource`
pub fn new() -> Self {
Self {}
}
}
#[cfg(esp32c6)]
impl Default for WakeFromLpCoreWakeupSource {
fn default() -> Self {
Self::new()
}
}
/// GPIO wakeup source
///
/// Wake up from GPIO high or low level. Any pin can be used with this wake up
/// source. Configure the pin for wake up via
/// [crate::gpio::Input::wakeup_enable].
///
/// This wakeup source can be used to wake up from light sleep only.
pub struct GpioWakeupSource {}
impl GpioWakeupSource {
/// Create a new instance of [GpioWakeupSource]
pub fn new() -> Self {
Self {}
}
}
impl Default for GpioWakeupSource {
fn default() -> Self {
Self::new()
}
}
impl WakeSource for GpioWakeupSource {
fn apply(
&self,
_rtc: &Rtc<'_>,
triggers: &mut WakeTriggers,
_sleep_config: &mut RtcSleepConfig,
) {
triggers.set_gpio(true);
}
}
macro_rules! uart_wakeup_impl {
($num:literal) => {
paste::paste! {
#[doc = concat!("UART", $num, " wakeup source")]
///
/// The chip can be woken up by reverting RXD for multiple cycles until the
/// number of rising edges is equal to or greater than the given value.
///
/// Note that the character which triggers wakeup (and any characters before
/// it) will not be received by the UART after wakeup. This means that the
/// external device typically needs to send an extra character to trigger
/// wakeup before sending the data.
///
/// After waking-up from UART, you should send some extra data through the UART
/// port in Active mode, so that the internal wakeup indication signal can be
/// cleared. Otherwise, the next UART wake-up would trigger with two less
/// rising edges than the configured threshold value.
///
/// Wakeup from light sleep takes some time, so not every character sent to the
/// UART can be received by the application.
///
/// This wakeup source can be used to wake up from light sleep only.
pub struct [< Uart $num WakeupSource >] {
threshold: u16,
}
impl [< Uart $num WakeupSource >] {
#[doc = concat!("Create a new instance of UART", $num, " wakeup source>") ]
///
/// # Panics
///
/// Panics if `threshold` is out of bounds.
pub fn new(threshold: u16) -> Self {
if threshold > 1023 {
panic!("Invalid threshold");
}
Self { threshold }
}
}
impl WakeSource for [< Uart $num WakeupSource >] {
fn apply(&self, _rtc: &Rtc<'_>, triggers: &mut WakeTriggers, _sleep_config: &mut RtcSleepConfig) {
triggers.[< set_uart $num >](true);
let uart = crate::peripherals::[< UART $num >]::regs();
#[cfg(any(esp32, esp32s2, esp32s3, esp32c2, esp32c3))]
uart.sleep_conf()
.modify(|_, w| unsafe { w.active_threshold().bits(self.threshold) });
#[cfg(not(any(esp32, esp32s2, esp32s3, esp32c2, esp32c3)))]
uart.sleep_conf2().modify(|_, w| unsafe {
w.wk_mode_sel()
.bits(0)
.active_threshold()
.bits(self.threshold)
});
}
}
}
};
}
uart_wakeup_impl!(0);
uart_wakeup_impl!(1);
#[cfg(not(pmu))]
bitfield::bitfield! {
/// Represents the wakeup triggers.
#[derive(Default, Clone, Copy)]
pub struct WakeTriggers(u16);
impl Debug;
/// EXT0 GPIO wakeup
pub ext0, set_ext0: 0;
/// EXT1 GPIO wakeup
pub ext1, set_ext1: 1;
/// GPIO wakeup (light sleep only)
pub gpio, set_gpio: 2;
/// Timer wakeup
pub timer, set_timer: 3;
/// SDIO wakeup (light sleep only)
pub sdio, set_sdio: 4;
/// MAC wakeup (light sleep only)
pub mac, set_mac: 5;
/// UART0 wakeup (light sleep only)
pub uart0, set_uart0: 6;
/// UART1 wakeup (light sleep only)
pub uart1, set_uart1: 7;
/// Touch wakeup
pub touch, set_touch: 8;
/// ULP wakeup
pub ulp, set_ulp: 9;
/// BT wakeup (light sleep only)
pub bt, set_bt: 10;
}
#[cfg(pmu)]
bitfield::bitfield! {
/// Represents the wakeup triggers.
#[derive(Default, Clone, Copy)]
pub struct WakeTriggers(u16);
impl Debug;
/// EXT0 GPIO wakeup
pub ext0, set_ext0: 0;
/// EXT1 GPIO wakeup
pub ext1, set_ext1: 1;
/// GPIO wakeup
pub gpio, set_gpio: 2;
/// WiFi beacon wakeup
pub wifi_beacon, set_wifi_beacon: 3;
/// Timer wakeup
pub timer, set_timer: 4;
/// WiFi SoC wakeup
pub wifi_soc, set_wifi_soc: 5;
/// UART0 wakeup
pub uart0, set_uart0: 6;
/// UART1 wakeup
pub uart1, set_uart1: 7;
/// SDIO wakeup
pub sdio, set_sdio: 8;
/// BT wakeup
pub bt, set_bt: 10;
/// LP core wakeup
pub lp_core, set_lp_core: 11;
/// USB wakeup
pub usb, set_usb: 14;
}
/// Trait representing a wakeup source.
pub trait WakeSource {
/// Configures the RTC and applies the wakeup triggers.
fn apply(&self, rtc: &Rtc<'_>, triggers: &mut WakeTriggers, sleep_config: &mut RtcSleepConfig);
}