From ac32f43c3dc915b78e71328855189b8aacfec8c3 Mon Sep 17 00:00:00 2001 From: 1-rafael-1 Date: Mon, 15 Sep 2025 21:10:22 +0200 Subject: alarm handling to poll hardware status directly; add ci test --- tests/rp/Cargo.toml | 6 +++ tests/rp/src/bin/rtc.rs | 113 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 tests/rp/src/bin/rtc.rs (limited to 'tests') diff --git a/tests/rp/Cargo.toml b/tests/rp/Cargo.toml index 809346bed..19461520a 100644 --- a/tests/rp/Cargo.toml +++ b/tests/rp/Cargo.toml @@ -64,6 +64,12 @@ name = "float" path = "src/bin/float.rs" required-features = [ "rp2040",] +# RTC is only available on RP2040 +[[bin]] +name = "rtc" +path = "src/bin/rtc.rs" +required-features = [ "rp2040",] + [profile.dev] debug = 2 debug-assertions = true diff --git a/tests/rp/src/bin/rtc.rs b/tests/rp/src/bin/rtc.rs new file mode 100644 index 000000000..8d1d4ee14 --- /dev/null +++ b/tests/rp/src/bin/rtc.rs @@ -0,0 +1,113 @@ +#![no_std] +#![no_main] +#[cfg(feature = "rp2040")] +teleprobe_meta::target!(b"rpi-pico"); + +use defmt::{assert, *}; +use embassy_executor::Spawner; +use embassy_futures::select::{select, Either}; +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(); + } + + // 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"); + + // Schedule alarm for 3 seconds from now + let alarm_second = (initial_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 within 5 seconds"); + } + Either::Second(_) => { + let alarm_duration = Instant::now() - alarm_start; + info!("ALARM TRIGGERED after {:?}", alarm_duration); + + // Verify timing is reasonable (should be around 3 seconds, allow some margin) + 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!("Test OK"); + cortex_m::asm::bkpt(); +} -- cgit