44//! Prints wakeup reason and sleep time on wakeup.
55
66use core:: time:: Duration ;
7- use esp_idf_hal:: gpio:: { self , AnyInputPin , PinDriver } ;
7+ use esp_idf_hal:: gpio:: { self , PinDriver } ;
88use esp_idf_hal:: peripherals:: Peripherals ;
99use esp_idf_hal:: prelude:: * ;
1010use esp_idf_hal:: reset:: WakeupReason ;
@@ -31,46 +31,61 @@ fn main() -> anyhow::Result<()> {
3131 esp_idf_sys:: link_patches ( ) ;
3232
3333 // run in a thread with increased stack size to prevent overflow
34- let builder = std:: thread:: Builder :: new ( ) . stack_size ( 8 * 1024 ) ;
34+ let builder = std:: thread:: Builder :: new ( ) . stack_size ( 10 * 1024 ) ;
3535 let th = builder. spawn ( move || -> anyhow:: Result < ( ) > {
3636 let peripherals = Peripherals :: take ( ) . unwrap ( ) ;
3737
3838 // RTC wakeup definitions
3939 #[ cfg( esp32) ]
40- let rtc_pins = [
41- RtcWakeupPin :: new ( peripherals . pins . gpio26 . into ( ) , false , true ) ,
42- RtcWakeupPin :: new ( peripherals. pins . gpio27 . into ( ) , false , true ) ,
43- ] ;
40+ let rtc0 = PinDriver :: rtc_input ( peripherals . pins . gpio26 ) ? ;
41+ # [ cfg ( esp32 ) ]
42+ let rtc1 = PinDriver :: rtc_input ( peripherals. pins . gpio27 ) ? ;
43+
4444 #[ cfg( any( esp32s2, esp32s3) ) ]
45- let rtc_pins = [
46- RtcWakeupPin :: new ( peripherals . pins . gpio1 . into ( ) , false , true ) ,
47- RtcWakeupPin :: new ( peripherals. pins . gpio2 . into ( ) , false , true ) ,
48- ] ;
45+ let rtc0 = PinDriver :: rtc_input ( peripherals . pins . gpio1 ) ? ;
46+ # [ cfg ( any ( esp32s2 , esp32s3 ) ) ]
47+ let rtc1 = PinDriver :: rtc_input ( peripherals. pins . gpio2 ) ? ;
48+
4949 #[ cfg( any( esp32, esp32s2, esp32s3) ) ]
50- let rtc_wakeup = Some ( RtcWakeup :: new ( & rtc_pins, RtcWakeLevel :: AnyHigh ) ) ;
50+ let rtc_pin0 = RtcWakeupPin { pindriver : & rtc0 } ;
51+ #[ cfg( any( esp32, esp32s2, esp32s3) ) ]
52+ let rtc_pin1 = RtcWakeupPin { pindriver : & rtc1 } ;
53+ #[ cfg( any( esp32, esp32s2, esp32s3) ) ]
54+ let rtc_wakeup = Some ( RtcWakeup {
55+ pins : EmptyRtcWakeupPins :: chain ( rtc_pin0) . chain ( rtc_pin1) ,
56+ wake_level : RtcWakeLevel :: AnyHigh ,
57+ } ) ;
5158
5259 // GPIO wakeup definitions
5360 #[ cfg( esp32) ]
54- let gpio_pin1 = PinDriver :: input ( AnyInputPin :: from ( peripherals. pins . gpio16 ) ) ?;
61+ let gpio0 = PinDriver :: input ( peripherals. pins . gpio12 ) ?;
5562 #[ cfg( esp32) ]
56- let gpio_pin2 = PinDriver :: input ( AnyInputPin :: from ( peripherals. pins . gpio17 ) ) ?;
63+ let gpio1 = PinDriver :: input ( peripherals. pins . gpio14 ) ?;
5764
5865 #[ cfg( any( esp32s2, esp32s3) ) ]
59- let gpio_pin1 = PinDriver :: input ( AnyInputPin :: from ( peripherals. pins . gpio37 ) ) ?;
66+ let gpio0 = PinDriver :: input ( peripherals. pins . gpio37 ) ?;
6067 #[ cfg( any( esp32s2, esp32s3) ) ]
61- let gpio_pin2 = PinDriver :: input ( AnyInputPin :: from ( peripherals. pins . gpio38 ) ) ?;
68+ let gpio1 = PinDriver :: input ( peripherals. pins . gpio38 ) ?;
6269
6370 #[ cfg( esp32c3) ]
64- let gpio_pin1 = PinDriver :: input ( AnyInputPin :: from ( peripherals. pins . gpio6 ) ) ?;
71+ let gpio0 = PinDriver :: input ( peripherals. pins . gpio6 ) ?;
6572 #[ cfg( esp32c3) ]
66- let gpio_pin2 = PinDriver :: input ( AnyInputPin :: from ( peripherals. pins . gpio7 ) ) ?;
73+ let gpio1 = PinDriver :: input ( peripherals. pins . gpio7 ) ?;
6774
68- let gpio_pins = [
69- GpioWakeupPin :: new ( gpio_pin1, Level :: High ) ?,
70- GpioWakeupPin :: new ( gpio_pin2, Level :: High ) ?,
71- ] ;
7275 #[ cfg( any( esp32, esp32c3, esp32s2, esp32s3) ) ]
73- let gpio_wakeup = Some ( GpioWakeup :: new ( & gpio_pins) ) ;
76+ let gpio_pin0 = GpioWakeupPin {
77+ pindriver : & gpio0,
78+ wake_level : Level :: High ,
79+ } ;
80+ #[ cfg( any( esp32, esp32c3, esp32s2, esp32s3) ) ]
81+ let gpio_pin1 = GpioWakeupPin {
82+ pindriver : & gpio1,
83+ wake_level : Level :: High ,
84+ } ;
85+ #[ cfg( any( esp32, esp32c3, esp32s2, esp32s3) ) ]
86+ let gpio_wakeup = Some ( GpioWakeup {
87+ pins : EmptyGpioWakeupPins :: chain ( gpio_pin0) . chain ( gpio_pin1) ,
88+ } ) ;
7489 #[ cfg( not( any( esp32, esp32c3, esp32s2, esp32s3) ) ) ]
7590 let gpio_wakeup: Option < GpioWakeup > = None ;
7691
@@ -108,14 +123,22 @@ fn main() -> anyhow::Result<()> {
108123 #[ cfg( not( any( esp32, esp32s2, esp32s3, esp32c3) ) ) ]
109124 let uart_wakeup: Option < UartWakeup > = None ;
110125
111- let lsleep = LightSleep {
112- timer : Some ( TimerWakeup :: new ( Duration :: from_secs ( 5 ) ) ) ,
113- #[ cfg( any( esp32, esp32s2, esp32s3) ) ]
114- rtc : rtc_wakeup,
115- gpio : gpio_wakeup,
116- uart : uart_wakeup,
117- ..Default :: default ( )
118- } ;
126+ #[ cfg( any( esp32, esp32s2, esp32s3) ) ]
127+ let lsleep = make_light_sleep_rtc_gpio_pins (
128+ Some ( TimerWakeup :: new ( Duration :: from_secs ( 5 ) ) ) ,
129+ rtc_wakeup,
130+ gpio_wakeup,
131+ uart_wakeup,
132+ None ,
133+ None ,
134+ ) ;
135+
136+ #[ cfg( not( any( esp32, esp32s2, esp32s3) ) ) ]
137+ let lsleep = make_light_sleep_gpio_pins (
138+ Some ( TimerWakeup :: new ( Duration :: from_secs ( 5 ) ) ) ,
139+ gpio_wakeup,
140+ uart_wakeup,
141+ ) ;
119142
120143 loop {
121144 println ! ( "Light sleep with: {:?}" , lsleep) ;
@@ -124,6 +147,7 @@ fn main() -> anyhow::Result<()> {
124147
125148 let time_before = Instant :: now ( ) ;
126149 let err = lsleep. sleep ( ) ;
150+
127151 match err {
128152 Ok ( _) => print_wakeup_result ( time_before) ,
129153 Err ( e) => {
0 commit comments