@@ -27,7 +27,6 @@ use crate::gpio::{GPIOMode, InputPin, Level, PinDriver};
27
27
#[ cfg( any( esp32, esp32s2, esp32s3) ) ]
28
28
use crate :: gpio:: { RTCMode , RTCPin } ;
29
29
use crate :: uart:: UartDriver ;
30
- #[ cfg( not( any( esp32, esp32s2, esp32s3) ) ) ]
31
30
use core:: marker:: PhantomData ;
32
31
33
32
/// Will wake the CPU up after a given duration
@@ -274,6 +273,63 @@ where
274
273
}
275
274
}
276
275
276
+ #[ cfg( any( esp32c2, esp32c3) ) ]
277
+ pub struct GpioDeepWakeup < P >
278
+ where
279
+ P : GpioWakeupPins ,
280
+ {
281
+ pub pins : P ,
282
+ }
283
+
284
+ #[ cfg( any( esp32c2, esp32c3) ) ]
285
+ impl < P > GpioDeepWakeup < P >
286
+ where
287
+ P : GpioWakeupPins ,
288
+ {
289
+ fn mask ( & self , level : Level ) -> u64 {
290
+ let mut m: u64 = 0 ;
291
+ for pin in self . pins . iter ( ) {
292
+ if pin. 1 == level {
293
+ m |= 1 << pin. 0 ;
294
+ }
295
+ }
296
+ m
297
+ }
298
+
299
+ fn apply ( & self ) -> Result < ( ) , EspError > {
300
+ esp ! ( unsafe {
301
+ esp_deep_sleep_enable_gpio_wakeup(
302
+ self . mask( Level :: Low ) ,
303
+ esp_deepsleep_gpio_wake_up_mode_t_ESP_GPIO_WAKEUP_GPIO_LOW,
304
+ )
305
+ } ) ?;
306
+
307
+ esp ! ( unsafe {
308
+ esp_deep_sleep_enable_gpio_wakeup(
309
+ self . mask( Level :: High ) ,
310
+ esp_deepsleep_gpio_wake_up_mode_t_ESP_GPIO_WAKEUP_GPIO_HIGH,
311
+ )
312
+ } ) ?;
313
+
314
+ Ok ( ( ) )
315
+ }
316
+ }
317
+
318
+ #[ cfg( any( esp32c2, esp32c3) ) ]
319
+ impl < P > fmt:: Debug for GpioDeepWakeup < P >
320
+ where
321
+ P : GpioWakeupPins ,
322
+ {
323
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
324
+ write ! ( f, "GpioDeepWakeup {{ pins: [" ) ?;
325
+
326
+ for pin in self . pins . iter ( ) {
327
+ write ! ( f, "({} {:?}), " , pin. 0 , pin. 1 ) ?;
328
+ }
329
+ write ! ( f, "" )
330
+ }
331
+ }
332
+
277
333
pub trait GpioWakeupPinTrait {
278
334
fn pin ( & self ) -> i32 ;
279
335
fn wake_level ( & self ) -> Level ;
@@ -584,35 +640,41 @@ where
584
640
}
585
641
586
642
/// Struct for deep sleep. Add wakeup sources to this struct, and then call sleep().
587
- pub struct DeepSleep < R >
643
+ pub struct DeepSleep < R , P >
588
644
where
589
645
R : RtcWakeupPins ,
646
+ P : GpioWakeupPins ,
590
647
{
591
648
pub timer : Option < TimerWakeup > ,
592
649
#[ cfg( any( esp32, esp32s2, esp32s3) ) ]
593
650
pub rtc : Option < RtcWakeup < R > > ,
651
+ #[ cfg( any( esp32c2, esp32c3) ) ]
652
+ pub gpio : Option < GpioDeepWakeup < P > > ,
594
653
#[ cfg( any( esp32, esp32s2, esp32s3) ) ]
595
654
pub touch : Option < TouchWakeup > ,
596
655
#[ cfg( any( esp32, esp32s2, esp32s3) ) ]
597
656
pub ulp : Option < UlpWakeup > ,
598
657
#[ cfg( not( any( esp32, esp32s2, esp32s3) ) ) ]
599
658
pub _p : PhantomData < R > ,
659
+ #[ cfg( not( any( esp32c2, esp32c3) ) ) ]
660
+ pub _p : PhantomData < P > ,
600
661
}
601
662
602
663
pub fn make_deep_sleep_no_pins (
603
664
timer : Option < TimerWakeup > ,
604
665
#[ cfg( any( esp32, esp32s2, esp32s3) ) ] touch : Option < TouchWakeup > ,
605
666
#[ cfg( any( esp32, esp32s2, esp32s3) ) ] ulp : Option < UlpWakeup > ,
606
- ) -> DeepSleep < EmptyRtcWakeupPins > {
667
+ ) -> DeepSleep < EmptyRtcWakeupPins , EmptyGpioWakeupPins > {
607
668
DeepSleep {
608
669
timer,
609
670
#[ cfg( any( esp32, esp32s2, esp32s3) ) ]
610
671
rtc : None ,
672
+ #[ cfg( any( esp32c2, esp32c3) ) ]
673
+ gpio : None ,
611
674
#[ cfg( any( esp32, esp32s2, esp32s3) ) ]
612
675
touch,
613
676
#[ cfg( any( esp32, esp32s2, esp32s3) ) ]
614
677
ulp,
615
- #[ cfg( not( any( esp32, esp32s2, esp32s3) ) ) ]
616
678
_p : PhantomData ,
617
679
}
618
680
}
@@ -623,18 +685,32 @@ pub fn make_deep_sleep_rtc_pins<R: RtcWakeupPins>(
623
685
rtc : Option < RtcWakeup < R > > ,
624
686
touch : Option < TouchWakeup > ,
625
687
ulp : Option < UlpWakeup > ,
626
- ) -> DeepSleep < R > {
688
+ ) -> DeepSleep < R , EmptyGpioWakeupPins > {
627
689
DeepSleep {
628
690
timer,
629
691
rtc,
630
692
touch,
631
693
ulp,
694
+ _p : PhantomData ,
632
695
}
633
696
}
634
697
635
- impl < R > DeepSleep < R >
698
+ #[ cfg( any( esp32c2, esp32c3) ) ]
699
+ pub fn make_deep_sleep_gpio_pins < P : GpioWakeupPins > (
700
+ timer : Option < TimerWakeup > ,
701
+ gpio : Option < GpioDeepWakeup < P > > ,
702
+ ) -> DeepSleep < EmptyRtcWakeupPins , P > {
703
+ DeepSleep {
704
+ timer,
705
+ gpio,
706
+ _p : PhantomData ,
707
+ }
708
+ }
709
+
710
+ impl < R , P > DeepSleep < R , P >
636
711
where
637
712
R : RtcWakeupPins ,
713
+ P : GpioWakeupPins ,
638
714
{
639
715
pub fn prepare ( & self ) -> Result < ( ) , EspError > {
640
716
esp ! ( unsafe { esp_sleep_disable_wakeup_source( esp_sleep_source_t_ESP_SLEEP_WAKEUP_ALL) } ) ?;
@@ -648,6 +724,11 @@ where
648
724
rtc. apply ( ) ?;
649
725
}
650
726
727
+ #[ cfg( any( esp32c2, esp32c3) ) ]
728
+ if let Some ( gpio) = & self . gpio {
729
+ gpio. apply ( ) ?;
730
+ }
731
+
651
732
#[ cfg( any( esp32, esp32s2, esp32s3) ) ]
652
733
if let Some ( touch) = & self . touch {
653
734
touch. apply ( ) ?;
@@ -679,14 +760,17 @@ where
679
760
}
680
761
}
681
762
682
- impl < R > fmt:: Debug for DeepSleep < R >
763
+ impl < R , P > fmt:: Debug for DeepSleep < R , P >
683
764
where
684
765
R : RtcWakeupPins ,
766
+ P : GpioWakeupPins ,
685
767
{
686
768
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
687
769
write ! ( f, "DeepSleep: {{timer: {:?}, " , self . timer) ?;
688
770
#[ cfg( any( esp32, esp32s2, esp32s3) ) ]
689
771
write ! ( f, "rtc: {:?}, " , self . rtc) ?;
772
+ #[ cfg( any( esp32c2, esp32c3) ) ]
773
+ write ! ( f, "gpio: {:?}, " , self . gpio) ?;
690
774
#[ cfg( any( esp32, esp32s2, esp32s3) ) ]
691
775
write ! ( f, "touch: {:?}, " , self . touch) ?;
692
776
#[ cfg( any( esp32, esp32s2, esp32s3) ) ]
0 commit comments