Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GPIO as wake-up source #1724

Merged
merged 4 commits into from
Jun 28, 2024
Merged

Add GPIO as wake-up source #1724

merged 4 commits into from
Jun 28, 2024

Conversation

bjoernQ
Copy link
Contributor

@bjoernQ bjoernQ commented Jun 27, 2024

Thank you for your contribution!

We appreciate the time and effort you've put into this pull request.
To help us review it efficiently, please ensure you've gone through the following checklist:

Submission Checklist 📝

  • I have updated existing examples or added new ones (if applicable).
  • I have used cargo xtask fmt-packages command to ensure that all changed code is formatted correctly.
  • My changes were added to the CHANGELOG.md in the proper section.
  • My changes are in accordance to the esp-rs API guidelines

Extra:

Pull Request Details 📖

Description

This adds GPIO as a wake-up source for light sleep.

The function to configure wake-up for a GPIO is added for all chips. We could make this conditional only for the chips we already have support for sleep added but there shouldn't be any problem with having it available for all chips.

It works fine to wake-up ESP32-C6 but all other sleep implementations (ESP32, ESP32-S3, ESP32-C3) have problems with light sleep in general (i.e. they wake-up and seem to enter light-sleep again but then things go wrong - most probably clock related problems). Since that problem is also reproducible with e.g. the already existing timer wake-up source these problems need to be addresses separately

Testing

Use this example to wake-up from light-sleep by pushing the boot button

//! Demonstrates light sleep with GPIO wake-up

//% CHIPS: esp32 esp32c3 esp32c6 esp32s3

#![no_std]
#![no_main]

use esp_backtrace as _;
use esp_hal::{
    clock::ClockControl,
    delay::Delay,
    entry,
    gpio::{Input, Io, Pull},
    peripherals::Peripherals,
    rtc_cntl::{get_reset_reason, get_wakeup_cause, sleep::GpioWakeupSource, Rtc, SocResetReason},
    system::SystemControl,
    Cpu,
};
use esp_println::println;

#[entry]
fn main() -> ! {
    let peripherals = Peripherals::take();
    let system = SystemControl::new(peripherals.SYSTEM);
    let clocks = ClockControl::boot_defaults(system.clock_control).freeze();

    let mut delay = Delay::new(&clocks);
    let mut rtc = Rtc::new(peripherals.LPWR, None);

    let io = Io::new(peripherals.GPIO, peripherals.IO_MUX);

    #[cfg(any(feature = "esp32", feature = "esp32s2", feature = "esp32s3"))]
    let button = io.pins.gpio0;
    #[cfg(not(any(feature = "esp32", feature = "esp32s2", feature = "esp32s3")))]
    let button = io.pins.gpio9;

    let mut pin = Input::new(button, Pull::Up);
    pin.wakeup_enable(true, esp_hal::gpio::WakeEvent::LowLevel);

    println!("up and runnning!");
    let reason = get_reset_reason(Cpu::ProCpu).unwrap_or(SocResetReason::ChipPowerOn);
    println!("reset reason: {:?}", reason);
    let wake_reason = get_wakeup_cause();
    println!("wake reason: {:?}", wake_reason);

    loop {
        let gpio_wakeup = GpioWakeupSource::new();
        println!("sleeping!");
        rtc.sleep_light(&[&gpio_wakeup], &mut delay);

        println!("welcome back!!!!!");
        delay.delay_millis(250);
    }
}

@bjoernQ bjoernQ mentioned this pull request Jun 27, 2024
5 tasks
Copy link
Member

@jessebraham jessebraham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, LGTM!

Copy link
Member

@SergioGasquez SergioGasquez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks

@bjoernQ bjoernQ force-pushed the gpio-wakeup-source branch from cc67bee to 8eef58c Compare June 28, 2024 07:55
@bjoernQ bjoernQ added this pull request to the merge queue Jun 28, 2024
Merged via the queue into esp-rs:main with commit 8cb921f Jun 28, 2024
30 checks passed
@bjoernQ bjoernQ deleted the gpio-wakeup-source branch November 26, 2024 08:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants