Skip to content

Commit

Permalink
Implement sleep and wakeup functionalities for ESP32C2 esp-rs#1920
Browse files Browse the repository at this point in the history
  • Loading branch information
M4tsuri committed Aug 11, 2024
1 parent 2e8937a commit e60dfe3
Show file tree
Hide file tree
Showing 6 changed files with 850 additions and 16 deletions.
45 changes: 43 additions & 2 deletions esp-hal/src/gpio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ use procmacros::ram;
#[cfg(any(adc, dac))]
pub(crate) use crate::analog;
pub(crate) use crate::gpio;
#[cfg(any(xtensa, esp32c3))]
#[cfg(any(xtensa, esp32c3, esp32c2))]
pub(crate) use crate::rtc_pins;
pub use crate::soc::gpio::*;
use crate::{
Expand Down Expand Up @@ -217,7 +217,7 @@ pub trait RtcPin: Pin {
///
/// The `level` argument needs to be a valid setting for the
/// `rtc_cntl.gpio_wakeup.gpio_pinX_int_type`.
#[cfg(any(esp32c3, esp32c6))]
#[cfg(any(esp32c3, esp32c2, esp32c6))]
unsafe fn apply_wakeup(&mut self, wakeup: bool, level: u8);
}

Expand Down Expand Up @@ -1564,6 +1564,47 @@ macro_rules! rtc_pins {
( $( $pin_num:expr )+ ) => { $( $crate::gpio::rtc_pins!($pin_num); )+ };
}

#[cfg(esp32c2)]
#[doc(hidden)]
#[macro_export]
macro_rules! rtc_pins {
(
$pin_num:expr
) => {
impl $crate::gpio::RtcPin for GpioPin<$pin_num> {
unsafe fn apply_wakeup(&mut self, wakeup: bool, level: u8) {
let rtc_cntl = unsafe { &*$crate::peripherals::RTC_CNTL::ptr() };
paste::paste! {
rtc_cntl.cntl_gpio_wakeup().modify(|_, w| w.[< gpio_pin $pin_num _wakeup_enable >]().bit(wakeup));
rtc_cntl.cntl_gpio_wakeup().modify(|_, w| w.[< gpio_pin $pin_num _int_type >]().bits(level));
}
}

fn rtcio_pad_hold(&mut self, enable: bool) {
let rtc_cntl = unsafe { &*$crate::peripherals::RTC_CNTL::ptr() };
paste::paste! {
rtc_cntl.pad_hold().modify(|_, w| w.[< gpio_pin $pin_num _hold >]().bit(enable));
}
}
}

impl $crate::gpio::RtcPinWithResistors for GpioPin<$pin_num> {
fn rtcio_pullup(&mut self, enable: bool) {
let io_mux = unsafe { &*$crate::peripherals::IO_MUX::ptr() };
io_mux.gpio($pin_num).modify(|_, w| w.fun_wpu().bit(enable));
}

fn rtcio_pulldown(&mut self, enable: bool) {
let io_mux = unsafe { &*$crate::peripherals::IO_MUX::ptr() };
io_mux.gpio($pin_num).modify(|_, w| w.fun_wpd().bit(enable));
}
}

};

( $( $pin_num:expr )+ ) => { $( $crate::gpio::rtc_pins!($pin_num); )+ };
}

// Following code enables `into_analog`

#[doc(hidden)]
Expand Down
12 changes: 6 additions & 6 deletions esp-hal/src/rtc_cntl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ use crate::efuse::Efuse;
use crate::peripherals::{LPWR, TIMG0};
#[cfg(any(esp32c6, esp32h2))]
use crate::peripherals::{LP_TIMER, LP_WDT};
#[cfg(any(esp32, esp32s3, esp32c3, esp32c6))]
#[cfg(any(esp32, esp32s3, esp32c3, esp32c6, esp32c2))]
use crate::rtc_cntl::sleep::{RtcSleepConfig, WakeSource, WakeTriggers};
use crate::{
clock::Clock,
Expand All @@ -94,7 +94,7 @@ use crate::{
InterruptConfigurable,
};
// only include sleep where its been implemented
#[cfg(any(esp32, esp32s3, esp32c3, esp32c6))]
#[cfg(any(esp32, esp32s3, esp32c3, esp32c6, esp32c2))]
pub mod sleep;

#[cfg_attr(esp32, path = "rtc/esp32.rs")]
Expand Down Expand Up @@ -204,7 +204,7 @@ impl<'d> Rtc<'d> {
swd: Swd::new(),
};

#[cfg(any(esp32, esp32s3, esp32c3, esp32c6))]
#[cfg(any(esp32, esp32s3, esp32c3, esp32c6, esp32c2))]
RtcSleepConfig::base_settings(&this);

this
Expand Down Expand Up @@ -265,23 +265,23 @@ impl<'d> Rtc<'d> {
}

/// Enter deep sleep and wake with the provided `wake_sources`.
#[cfg(any(esp32, esp32s3, esp32c3, esp32c6))]
#[cfg(any(esp32, esp32s3, esp32c3, esp32c6, esp32c2))]
pub fn sleep_deep(&mut self, wake_sources: &[&dyn WakeSource]) -> ! {
let config = RtcSleepConfig::deep();
self.sleep(&config, wake_sources);
unreachable!();
}

/// Enter light sleep and wake with the provided `wake_sources`.
#[cfg(any(esp32, esp32s3, esp32c3, esp32c6))]
#[cfg(any(esp32, esp32s3, esp32c3, esp32c6, esp32c2))]
pub fn sleep_light(&mut self, wake_sources: &[&dyn WakeSource]) {
let config = RtcSleepConfig::default();
self.sleep(&config, wake_sources);
}

/// Enter sleep with the provided `config` and wake with the provided
/// `wake_sources`.
#[cfg(any(esp32, esp32s3, esp32c3, esp32c6))]
#[cfg(any(esp32, esp32s3, esp32c3, esp32c6, esp32c2))]
pub fn sleep(&mut self, config: &RtcSleepConfig, wake_sources: &[&dyn WakeSource]) {
let mut config = *config;
let mut wakeup_triggers = WakeTriggers::default();
Expand Down
Loading

0 comments on commit e60dfe3

Please sign in to comment.