aboutsummaryrefslogtreecommitdiff
path: root/examples/src/bin/rtc_alarm.rs
blob: 6f8a7710119d0b26e8e06e13407dc7bce3371a37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#![no_std]
#![no_main]

use embassy_executor::Spawner;
use embassy_mcxa as hal;
use hal::lpuart::{Config, Lpuart};
use hal::rtc::{RtcDateTime, RtcInterruptEnable};
use hal::InterruptExt;

type MyRtc = hal::rtc::Rtc<'static, hal::rtc::Rtc0>;

use embassy_mcxa::bind_interrupts;
use {defmt_rtt as _, panic_probe as _};

bind_interrupts!(struct Irqs {
    RTC => hal::rtc::RtcHandler;
});

#[used]
#[no_mangle]
static KEEP_RTC: unsafe extern "C" fn() = RTC;

#[embassy_executor::main]
async fn main(_spawner: Spawner) {
    let p = hal::init(hal::config::Config::default());

    // Create UART configuration
    let config = Config {
        baudrate_bps: 115_200,
        enable_tx: true,
        enable_rx: true,
        ..Default::default()
    };

    // Create UART instance using LPUART2 with P2_2 as TX and P2_3 as RX
    unsafe {
        embassy_mcxa_examples::init_uart2_pins();
    }
    let mut uart = Lpuart::new_blocking(
        p.LPUART2, // Peripheral
        p.P2_2,    // TX pin
        p.P2_3,    // RX pin
        config,
    )
    .unwrap();

    uart.write_str_blocking("\r\n=== RTC Alarm Example ===\r\n");

    let rtc_config = hal::rtc::get_default_config();

    let rtc = MyRtc::new(p.RTC0, rtc_config);

    let now = RtcDateTime {
        year: 2025,
        month: 10,
        day: 15,
        hour: 14,
        minute: 30,
        second: 0,
    };

    rtc.stop();

    uart.write_str_blocking("Time set to: 2025-10-15 14:30:00\r\n");
    rtc.set_datetime(now);

    let mut alarm = now;
    alarm.second += 10;

    rtc.set_alarm(alarm);
    uart.write_str_blocking("Alarm set for: 2025-10-15 14:30:10 (+10 seconds)\r\n");

    rtc.set_interrupt(RtcInterruptEnable::RTC_ALARM_INTERRUPT_ENABLE);

    unsafe {
        hal::interrupt::RTC.enable();
    }

    unsafe {
        cortex_m::interrupt::enable();
    }

    rtc.start();

    uart.write_str_blocking("RTC started, waiting for alarm...\r\n");

    loop {
        if rtc.is_alarm_triggered() {
            uart.write_str_blocking("\r\n*** ALARM TRIGGERED! ***\r\n");
            break;
        }
    }

    uart.write_str_blocking("Example complete - Test PASSED!\r\n");
}