diff options
Diffstat (limited to 'examples/mcxa/src/bin/rtc_alarm.rs')
| -rw-r--r-- | examples/mcxa/src/bin/rtc_alarm.rs | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/examples/mcxa/src/bin/rtc_alarm.rs b/examples/mcxa/src/bin/rtc_alarm.rs new file mode 100644 index 000000000..a7800a2d1 --- /dev/null +++ b/examples/mcxa/src/bin/rtc_alarm.rs | |||
| @@ -0,0 +1,74 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use embassy_executor::Spawner; | ||
| 5 | use embassy_mcxa as hal; | ||
| 6 | use hal::rtc::{RtcDateTime, RtcInterruptEnable}; | ||
| 7 | use hal::InterruptExt; | ||
| 8 | |||
| 9 | type MyRtc = hal::rtc::Rtc<'static, hal::rtc::Rtc0>; | ||
| 10 | |||
| 11 | use embassy_mcxa::bind_interrupts; | ||
| 12 | use {defmt_rtt as _, panic_probe as _}; | ||
| 13 | |||
| 14 | bind_interrupts!(struct Irqs { | ||
| 15 | RTC => hal::rtc::RtcHandler; | ||
| 16 | }); | ||
| 17 | |||
| 18 | #[used] | ||
| 19 | #[no_mangle] | ||
| 20 | static KEEP_RTC: unsafe extern "C" fn() = RTC; | ||
| 21 | |||
| 22 | #[embassy_executor::main] | ||
| 23 | async fn main(_spawner: Spawner) { | ||
| 24 | let p = hal::init(hal::config::Config::default()); | ||
| 25 | |||
| 26 | defmt::info!("=== RTC Alarm Example ==="); | ||
| 27 | |||
| 28 | let rtc_config = hal::rtc::get_default_config(); | ||
| 29 | |||
| 30 | let rtc = MyRtc::new(p.RTC0, rtc_config); | ||
| 31 | |||
| 32 | let now = RtcDateTime { | ||
| 33 | year: 2025, | ||
| 34 | month: 10, | ||
| 35 | day: 15, | ||
| 36 | hour: 14, | ||
| 37 | minute: 30, | ||
| 38 | second: 0, | ||
| 39 | }; | ||
| 40 | |||
| 41 | rtc.stop(); | ||
| 42 | |||
| 43 | defmt::info!("Time set to: 2025-10-15 14:30:00"); | ||
| 44 | rtc.set_datetime(now); | ||
| 45 | |||
| 46 | let mut alarm = now; | ||
| 47 | alarm.second += 10; | ||
| 48 | |||
| 49 | rtc.set_alarm(alarm); | ||
| 50 | defmt::info!("Alarm set for: 2025-10-15 14:30:10 (+10 seconds)"); | ||
| 51 | |||
| 52 | rtc.set_interrupt(RtcInterruptEnable::RTC_ALARM_INTERRUPT_ENABLE); | ||
| 53 | |||
| 54 | unsafe { | ||
| 55 | hal::interrupt::RTC.enable(); | ||
| 56 | } | ||
| 57 | |||
| 58 | unsafe { | ||
| 59 | cortex_m::interrupt::enable(); | ||
| 60 | } | ||
| 61 | |||
| 62 | rtc.start(); | ||
| 63 | |||
| 64 | defmt::info!("RTC started, waiting for alarm..."); | ||
| 65 | |||
| 66 | loop { | ||
| 67 | if rtc.is_alarm_triggered() { | ||
| 68 | defmt::info!("*** ALARM TRIGGERED! ***"); | ||
| 69 | break; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | defmt::info!("Example complete - Test PASSED!"); | ||
| 74 | } | ||
