aboutsummaryrefslogtreecommitdiff
path: root/tests/rp/src/bin/rtc.rs
blob: e1def7b5b297b763d9dada6c047a3a6ac7e03c4d (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#![no_std]
#![no_main]
#[cfg(feature = "rp2040")]
teleprobe_meta::target!(b"rpi-pico");

use defmt::{assert, *};
use embassy_executor::Spawner;
use embassy_futures::select::{Either, select};
use embassy_rp::bind_interrupts;
use embassy_rp::rtc::{DateTime, DateTimeFilter, DayOfWeek, Rtc};
use embassy_time::{Duration, Instant, Timer};
use {defmt_rtt as _, panic_probe as _};

// Bind the RTC interrupt to the handler
bind_interrupts!(struct Irqs {
    RTC_IRQ => embassy_rp::rtc::InterruptHandler;
});

#[embassy_executor::main]
async fn main(_spawner: Spawner) {
    let p = embassy_rp::init(Default::default());
    let mut rtc = Rtc::new(p.RTC, Irqs);

    info!("RTC test started");

    // Initialize RTC if not running
    if !rtc.is_running() {
        info!("Starting RTC");
        let now = DateTime {
            year: 2000,
            month: 1,
            day: 1,
            day_of_week: DayOfWeek::Saturday,
            hour: 0,
            minute: 0,
            second: 0,
        };
        rtc.set_datetime(now).unwrap();
        Timer::after_millis(100).await;
    }

    // Test 1: Basic RTC functionality - read current time
    let initial_time = rtc.now().unwrap();
    info!(
        "Initial time: {}-{:02}-{:02} {}:{:02}:{:02}",
        initial_time.year,
        initial_time.month,
        initial_time.day,
        initial_time.hour,
        initial_time.minute,
        initial_time.second
    );

    // Test 2: Schedule and wait for alarm
    info!("Testing alarm scheduling");

    // Wait until we're at a predictable second, then schedule for a future second
    loop {
        let current = rtc.now().unwrap();
        if current.second <= 55 {
            break;
        }
        Timer::after_millis(100).await;
    }

    // Now schedule alarm for 3 seconds from current time
    let current_time = rtc.now().unwrap();
    let alarm_second = (current_time.second + 3) % 60;
    let alarm_filter = DateTimeFilter::default().second(alarm_second);

    info!("Scheduling alarm for second: {}", alarm_second);
    rtc.schedule_alarm(alarm_filter);

    // Verify alarm is scheduled
    let scheduled = rtc.alarm_scheduled();
    assert!(scheduled.is_some(), "Alarm should be scheduled");
    info!("Alarm scheduled successfully: {}", scheduled.unwrap());

    // Wait for alarm with timeout
    let alarm_start = Instant::now();
    match select(Timer::after_secs(5), rtc.wait_for_alarm()).await {
        Either::First(_) => {
            core::panic!("Alarm timeout - alarm should have triggered by now");
        }
        Either::Second(_) => {
            let alarm_duration = Instant::now() - alarm_start;
            info!("ALARM TRIGGERED after {:?}", alarm_duration);

            // Verify timing is reasonable (should be around 3 seconds)
            assert!(
                alarm_duration >= Duration::from_secs(2) && alarm_duration <= Duration::from_secs(4),
                "Alarm timing incorrect: {:?}",
                alarm_duration
            );
        }
    }

    // Test 3: Verify RTC is still running and time has advanced
    let final_time = rtc.now().unwrap();
    info!(
        "Final time: {}-{:02}-{:02} {}:{:02}:{:02}",
        final_time.year, final_time.month, final_time.day, final_time.hour, final_time.minute, final_time.second
    );

    // Verify time has advanced (allowing for minute/hour rollover)
    let time_diff = if final_time.second >= initial_time.second {
        final_time.second - initial_time.second
    } else {
        60 - initial_time.second + final_time.second
    };

    assert!(time_diff >= 3, "RTC should have advanced by at least 3 seconds");
    info!("Time advanced by {} seconds", time_diff);

    // Test 4: Verify alarm is no longer scheduled after triggering
    let post_alarm_scheduled = rtc.alarm_scheduled();
    assert!(
        post_alarm_scheduled.is_none(),
        "Alarm should not be scheduled after triggering"
    );
    info!("Alarm correctly cleared after triggering");

    info!("Test OK");
    cortex_m::asm::bkpt();
}