1
+ #![ no_std]
2
+ #![ no_main]
3
+ #![ feature( type_alias_impl_trait) ]
4
+
5
+ use embassy_executor:: Executor ;
6
+ use embassy_time:: { Duration , Timer } ;
7
+
8
+ use esp32c3_hal:: {
9
+ clock:: ClockControl ,
10
+ prelude:: * ,
11
+ timer:: TimerGroup ,
12
+ Rtc , embassy, peripherals:: Peripherals , IO ,
13
+ } ;
14
+ use esp_backtrace as _;
15
+ use esp_hal_common:: { PullDown , Bank0GpioRegisterAccess , InputOutputAnalogPinType , Input , GpioPin } ;
16
+ use static_cell:: StaticCell ;
17
+
18
+ use embedded_hal_async:: digital:: Wait ;
19
+
20
+ #[ embassy_executor:: task]
21
+ async fn ping ( mut pin : GpioPin < Input < PullDown > , Bank0GpioRegisterAccess , InputOutputAnalogPinType , 5 > ) {
22
+ loop {
23
+ esp_println:: println!( "Waiting..." ) ;
24
+ pin. wait_for_rising_edge ( ) . await . unwrap ( ) ;
25
+ esp_println:: println!( "Ping!" ) ;
26
+ Timer :: after ( Duration :: from_millis ( 100 ) ) . await ;
27
+ }
28
+ }
29
+
30
+ static EXECUTOR : StaticCell < Executor > = StaticCell :: new ( ) ;
31
+
32
+ #[ riscv_rt:: entry]
33
+ fn main ( ) -> ! {
34
+ esp_println:: println!( "Init!" ) ;
35
+ let peripherals = Peripherals :: take ( ) . unwrap ( ) ;
36
+ let system = peripherals. SYSTEM . split ( ) ;
37
+ let clocks = ClockControl :: boot_defaults ( system. clock_control ) . freeze ( ) ;
38
+
39
+ let mut rtc = Rtc :: new ( peripherals. RTC_CNTL ) ;
40
+ let timer_group0 = TimerGroup :: new ( peripherals. TIMG0 , & clocks) ;
41
+ let mut wdt0 = timer_group0. wdt ;
42
+ let timer_group1 = TimerGroup :: new ( peripherals. TIMG1 , & clocks) ;
43
+ let mut wdt1 = timer_group1. wdt ;
44
+
45
+ // Disable watchdog timers
46
+ rtc. swd . disable ( ) ;
47
+ rtc. rwdt . disable ( ) ;
48
+ wdt0. disable ( ) ;
49
+ wdt1. disable ( ) ;
50
+
51
+ #[ cfg( feature = "embassy-time-systick" ) ]
52
+ embassy:: init ( & clocks, esp32c3_hal:: systimer:: SystemTimer :: new ( peripherals. SYSTIMER ) ) ;
53
+
54
+ #[ cfg( feature = "embassy-time-timg0" ) ]
55
+ embassy:: init ( & clocks, timer_group0. timer0 ) ;
56
+
57
+ // Set GPIO5 as an output, and set its state high initially.
58
+ let io = IO :: new ( peripherals. GPIO , peripherals. IO_MUX ) ;
59
+ let input = io. pins . gpio5 . into_pull_down_input ( ) ;
60
+
61
+ esp32c3_hal:: interrupt:: enable (
62
+ esp32c3_hal:: pac:: Interrupt :: GPIO ,
63
+ esp32c3_hal:: interrupt:: Priority :: Priority1 ,
64
+ )
65
+ . unwrap ( ) ;
66
+
67
+ let executor = EXECUTOR . init ( Executor :: new ( ) ) ;
68
+ executor. run ( |spawner| {
69
+ spawner. spawn ( ping ( input) ) . ok ( ) ;
70
+ } ) ;
71
+ }
0 commit comments