|
11 | 11 | #![allow(unknown_lints)] |
12 | 12 | #![allow(unexpected_cfgs)] |
13 | 13 |
|
14 | | -#[cfg(any(esp32, esp32s2, esp32s3, esp32c2, esp32c3))] |
15 | 14 | use core::time::Duration; |
16 | | -// #[cfg(any(esp32, esp32s2, esp32s3))] |
17 | | -// use esp_idf_hal::gpio; |
18 | | -#[cfg(any(esp32c2, esp32c3))] |
19 | | -use esp_idf_hal::gpio::Level; |
20 | | -#[cfg(any(esp32, esp32s2, esp32s3, esp32c2, esp32c3))] |
21 | | -use esp_idf_hal::gpio::{PinDriver, Pull}; |
22 | | -#[cfg(any(esp32, esp32s2, esp32s3, esp32c2, esp32c3))] |
| 15 | + |
| 16 | +use esp_idf_hal::gpio::{Level, PinDriver, Pull}; |
23 | 17 | use esp_idf_hal::peripherals::Peripherals; |
24 | 18 | use esp_idf_hal::reset::{ResetReason, WakeupReason}; |
25 | | -#[cfg(any(esp32, esp32s2, esp32s3, esp32c2, esp32c3))] |
26 | 19 | use esp_idf_hal::sleep::*; |
27 | 20 |
|
28 | | -fn print_wakeup_result() { |
| 21 | +fn main() -> anyhow::Result<()> { |
| 22 | + esp_idf_hal::sys::link_patches(); |
| 23 | + |
29 | 24 | let reset_reason = ResetReason::get(); |
30 | 25 | let wakeup_reason = WakeupReason::get(); |
31 | | - println!( |
32 | | - "reset after {:?} wakeup due to {:?}", |
33 | | - reset_reason, wakeup_reason |
34 | | - ); |
35 | | -} |
| 26 | + println!("Reset after {reset_reason:?}; wakeup due to {wakeup_reason:?}"); |
36 | 27 |
|
37 | | -fn main() -> anyhow::Result<()> { |
38 | | - esp_idf_hal::sys::link_patches(); |
| 28 | + let peripherals = Peripherals::take()?; |
39 | 29 |
|
40 | | - print_wakeup_result(); |
41 | | - |
42 | | - #[cfg(any(esp32, esp32s2, esp32s3, esp32c2, esp32c3))] |
43 | | - let peripherals = Peripherals::take().unwrap(); |
44 | | - |
45 | | - // RTC wakeup definitions |
46 | | - #[cfg(esp32)] |
47 | | - let rtc0 = PinDriver::rtc_input(peripherals.pins.gpio26, Pull::Down)?; |
48 | | - #[cfg(esp32)] |
49 | | - let rtc1 = PinDriver::rtc_input(peripherals.pins.gpio27, Pull::Down)?; |
50 | | - |
51 | | - #[cfg(any(esp32s2, esp32s3))] |
52 | | - let rtc0 = PinDriver::rtc_input(peripherals.pins.gpio1, Pull::Down)?; |
53 | | - #[cfg(any(esp32s2, esp32s3))] |
54 | | - let rtc1 = PinDriver::rtc_input(peripherals.pins.gpio2, Pull::Down)?; |
55 | | - |
56 | | - #[cfg(any(esp32, esp32s2, esp32s3))] |
57 | | - let rtc_pin0 = &rtc0; |
58 | | - #[cfg(any(esp32, esp32s2, esp32s3))] |
59 | | - let rtc_pin1 = &rtc1; |
60 | | - #[cfg(any(esp32, esp32s2, esp32s3))] |
61 | | - let rtc_wakeup = Some(RtcWakeup { |
62 | | - pins: rtc_pin0.chain(rtc_pin1), |
63 | | - wake_level: RtcWakeLevel::AnyHigh, |
64 | | - }); |
65 | | - |
66 | | - #[cfg(any(esp32, esp32s2, esp32s3))] |
67 | | - let dsleep = make_deep_sleep_rtc_pins( |
68 | | - Some(TimerWakeup::new(Duration::from_secs(5))), |
69 | | - rtc_wakeup, |
70 | | - None, |
71 | | - None, |
72 | | - ); |
73 | | - |
74 | | - // GPIO wakeup definitions |
75 | | - #[cfg(any(esp32c2, esp32c3))] |
76 | | - let gpio0 = PinDriver::input(peripherals.pins.gpio0, Pull::Down)?; |
77 | | - #[cfg(any(esp32c2, esp32c3))] |
78 | | - let gpio1 = PinDriver::input(peripherals.pins.gpio1, Pull::Down)?; |
| 30 | + // Sample GPIO pins |
| 31 | + // GPIO pins can only be used in deep sleep on the esp32c2 and esp32c3 where there are no RTC pins |
| 32 | + // |
| 33 | + // Note also, that ONLY pins connected to the VDD3P3_RTC power domain can be used for deep-sleep wakeup, |
| 34 | + // which is not enforced in the deep-sleep API. |
79 | 35 |
|
80 | 36 | #[cfg(any(esp32c2, esp32c3))] |
81 | | - let gpio_pin0 = GpioWakeupPin { |
82 | | - pindriver: &gpio0, |
83 | | - wake_level: Level::High, |
84 | | - }; |
85 | | - #[cfg(any(esp32c2, esp32c3))] |
86 | | - let gpio_pin1 = GpioWakeupPin { |
87 | | - pindriver: &gpio1, |
88 | | - wake_level: Level::Low, |
89 | | - }; |
| 37 | + let gpio0 = PinDriver::input(peripherals.pins.gpio5, Pull::Down)?; |
90 | 38 | #[cfg(any(esp32c2, esp32c3))] |
91 | | - let gpio_wakeup = Some(GpioDeepWakeup { |
92 | | - pins: EmptyGpioWakeupPins::chain(gpio_pin0).chain(gpio_pin1), |
93 | | - }); |
| 39 | + let gpio1 = PinDriver::input(peripherals.pins.gpio6, Pull::Down)?; |
| 40 | + |
| 41 | + // Sample RTC pins |
| 42 | + // No RTC on esp32c2 and esp32c3 |
| 43 | + |
| 44 | + #[cfg(not(any(esp32c2, esp32c3)))] |
| 45 | + let rtc0 = PinDriver::rtc_input(peripherals.pins.gpio2, Pull::Down)?; |
| 46 | + #[cfg(not(any(esp32c2, esp32c3)))] |
| 47 | + let rtc1 = PinDriver::rtc_input(peripherals.pins.gpio4, Pull::Down)?; |
| 48 | + |
| 49 | + // Assemble the wakeup sources now |
| 50 | + |
| 51 | + let wakeup = Empty |
| 52 | + // Add timer wakeup |
| 53 | + .chain(TimerWakeup::new(Duration::from_secs(5))); |
94 | 54 |
|
95 | 55 | #[cfg(any(esp32c2, esp32c3))] |
96 | | - let dsleep = |
97 | | - make_deep_sleep_gpio_pins(Some(TimerWakeup::new(Duration::from_secs(5))), gpio_wakeup); |
98 | | - |
99 | | - #[cfg(any(esp32, esp32s2, esp32s3, esp32c2, esp32c3))] |
100 | | - println!("Deep sleep with: {:?}", dsleep); |
101 | | - #[cfg(any(esp32, esp32s2, esp32s3, esp32c2, esp32c3))] |
102 | | - dsleep.prepare()?; |
103 | | - #[cfg(any(esp32, esp32s2, esp32s3, esp32c2, esp32c3))] |
104 | | - dsleep.sleep(); |
105 | | - #[cfg(not(any(esp32, esp32s2, esp32s3, esp32c2, esp32c3)))] |
| 56 | + let wakeup = wakeup |
| 57 | + // Add GPIO wakeup |
| 58 | + .chain(PinsWakeup::<_, Gpio>::new( |
| 59 | + Empty |
| 60 | + .chain((&gpio0, Level::High)) |
| 61 | + .chain((&gpio1, Level::High)), |
| 62 | + )); |
| 63 | + |
| 64 | + #[cfg(not(any(esp32c2, esp32c3)))] |
| 65 | + let wakeup = wakeup |
| 66 | + // Add RTC wakeup |
| 67 | + .chain(PinsWakeup::<_, Rtc>::new( |
| 68 | + Empty |
| 69 | + .chain((&rtc0, Level::High)) |
| 70 | + .chain((&rtc1, Level::High)), |
| 71 | + )); |
| 72 | + |
| 73 | + println!("Deep sleep with wakeup: {wakeup:?}"); |
| 74 | + |
| 75 | + let e = deep_sleep(wakeup); |
| 76 | + |
| 77 | + println!("Failed to enter deep sleep: {e:?}"); |
| 78 | + |
106 | 79 | Ok(()) |
107 | 80 | } |
0 commit comments