Skip to content

Commit

Permalink
Implement sleep and wakeup functionalities for ESP32C2 #1920 (#1922)
Browse files Browse the repository at this point in the history
  • Loading branch information
M4tsuri authored Sep 5, 2024
1 parent a2af2ac commit 4f97bef
Show file tree
Hide file tree
Showing 8 changed files with 903 additions and 17 deletions.
1 change: 1 addition & 0 deletions esp-hal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Implement `embedded-hal` output pin traits for `DummyPin` (#2019)
- Added `esp_hal::init` to simplify HAL initialisation (#1970, #1999)
- Added sleep and wakeup support for esp32c2 (#1922)

### Changed
- Make saving and restoring SHA digest state an explicit operation (#2049)
Expand Down
45 changes: 43 additions & 2 deletions esp-hal/src/gpio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,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 @@ -236,7 +236,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 @@ -1568,6 +1568,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 @@ -79,7 +79,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 @@ -91,7 +91,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 @@ -203,7 +203,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 @@ -264,23 +264,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 4f97bef

Please sign in to comment.