Skip to content

Commit 4f97bef

Browse files
authored
Implement sleep and wakeup functionalities for ESP32C2 #1920 (#1922)
1 parent a2af2ac commit 4f97bef

File tree

8 files changed

+903
-17
lines changed

8 files changed

+903
-17
lines changed

esp-hal/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

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

1516
### Changed
1617
- Make saving and restoring SHA digest state an explicit operation (#2049)

esp-hal/src/gpio/mod.rs

+43-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ use procmacros::ram;
6060
#[cfg(any(adc, dac))]
6161
pub(crate) use crate::analog;
6262
pub(crate) use crate::gpio;
63-
#[cfg(any(xtensa, esp32c3))]
63+
#[cfg(any(xtensa, esp32c3, esp32c2))]
6464
pub(crate) use crate::rtc_pins;
6565
pub use crate::soc::gpio::*;
6666
use crate::{
@@ -236,7 +236,7 @@ pub trait RtcPin: Pin {
236236
///
237237
/// The `level` argument needs to be a valid setting for the
238238
/// `rtc_cntl.gpio_wakeup.gpio_pinX_int_type`.
239-
#[cfg(any(esp32c3, esp32c6))]
239+
#[cfg(any(esp32c3, esp32c2, esp32c6))]
240240
unsafe fn apply_wakeup(&mut self, wakeup: bool, level: u8);
241241
}
242242

@@ -1568,6 +1568,47 @@ macro_rules! rtc_pins {
15681568
( $( $pin_num:expr )+ ) => { $( $crate::gpio::rtc_pins!($pin_num); )+ };
15691569
}
15701570

1571+
#[cfg(esp32c2)]
1572+
#[doc(hidden)]
1573+
#[macro_export]
1574+
macro_rules! rtc_pins {
1575+
(
1576+
$pin_num:expr
1577+
) => {
1578+
impl $crate::gpio::RtcPin for GpioPin<$pin_num> {
1579+
unsafe fn apply_wakeup(&mut self, wakeup: bool, level: u8) {
1580+
let rtc_cntl = unsafe { &*$crate::peripherals::RTC_CNTL::ptr() };
1581+
paste::paste! {
1582+
rtc_cntl.cntl_gpio_wakeup().modify(|_, w| w.[< gpio_pin $pin_num _wakeup_enable >]().bit(wakeup));
1583+
rtc_cntl.cntl_gpio_wakeup().modify(|_, w| w.[< gpio_pin $pin_num _int_type >]().bits(level));
1584+
}
1585+
}
1586+
1587+
fn rtcio_pad_hold(&mut self, enable: bool) {
1588+
let rtc_cntl = unsafe { &*$crate::peripherals::RTC_CNTL::ptr() };
1589+
paste::paste! {
1590+
rtc_cntl.pad_hold().modify(|_, w| w.[< gpio_pin $pin_num _hold >]().bit(enable));
1591+
}
1592+
}
1593+
}
1594+
1595+
impl $crate::gpio::RtcPinWithResistors for GpioPin<$pin_num> {
1596+
fn rtcio_pullup(&mut self, enable: bool) {
1597+
let io_mux = unsafe { &*$crate::peripherals::IO_MUX::ptr() };
1598+
io_mux.gpio($pin_num).modify(|_, w| w.fun_wpu().bit(enable));
1599+
}
1600+
1601+
fn rtcio_pulldown(&mut self, enable: bool) {
1602+
let io_mux = unsafe { &*$crate::peripherals::IO_MUX::ptr() };
1603+
io_mux.gpio($pin_num).modify(|_, w| w.fun_wpd().bit(enable));
1604+
}
1605+
}
1606+
1607+
};
1608+
1609+
( $( $pin_num:expr )+ ) => { $( $crate::gpio::rtc_pins!($pin_num); )+ };
1610+
}
1611+
15711612
// Following code enables `into_analog`
15721613

15731614
#[doc(hidden)]

esp-hal/src/rtc_cntl/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ use crate::efuse::Efuse;
7979
use crate::peripherals::{LPWR, TIMG0};
8080
#[cfg(any(esp32c6, esp32h2))]
8181
use crate::peripherals::{LP_TIMER, LP_WDT};
82-
#[cfg(any(esp32, esp32s3, esp32c3, esp32c6))]
82+
#[cfg(any(esp32, esp32s3, esp32c3, esp32c6, esp32c2))]
8383
use crate::rtc_cntl::sleep::{RtcSleepConfig, WakeSource, WakeTriggers};
8484
use crate::{
8585
clock::Clock,
@@ -91,7 +91,7 @@ use crate::{
9191
InterruptConfigurable,
9292
};
9393
// only include sleep where its been implemented
94-
#[cfg(any(esp32, esp32s3, esp32c3, esp32c6))]
94+
#[cfg(any(esp32, esp32s3, esp32c3, esp32c6, esp32c2))]
9595
pub mod sleep;
9696

9797
#[cfg_attr(esp32, path = "rtc/esp32.rs")]
@@ -203,7 +203,7 @@ impl<'d> Rtc<'d> {
203203
swd: Swd::new(),
204204
};
205205

206-
#[cfg(any(esp32, esp32s3, esp32c3, esp32c6))]
206+
#[cfg(any(esp32, esp32s3, esp32c3, esp32c6, esp32c2))]
207207
RtcSleepConfig::base_settings(&this);
208208

209209
this
@@ -264,23 +264,23 @@ impl<'d> Rtc<'d> {
264264
}
265265

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

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

281281
/// Enter sleep with the provided `config` and wake with the provided
282282
/// `wake_sources`.
283-
#[cfg(any(esp32, esp32s3, esp32c3, esp32c6))]
283+
#[cfg(any(esp32, esp32s3, esp32c3, esp32c6, esp32c2))]
284284
pub fn sleep(&mut self, config: &RtcSleepConfig, wake_sources: &[&dyn WakeSource]) {
285285
let mut config = *config;
286286
let mut wakeup_triggers = WakeTriggers::default();

0 commit comments

Comments
 (0)