-
Notifications
You must be signed in to change notification settings - Fork 37
Enable RTC-based Time Alarm functionality #542
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
felipebalbi
merged 17 commits into
OpenDevicePartnership:main
from
Ctru14:connortruono/rtc-alarm
Jan 15, 2026
Merged
Changes from 11 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
7e5e158
Enable time alarm future and interrupt using the RTC peripheral
1b2e8af
Move RtcAlarm structure out of RTC peripheral to user defined space
aefae46
fmt fixes
11a3de9
Address Copilot PR feedback on set_alarm
0d7a96e
Disable interrupt within critical section in clear_alarm
1407181
Fix trailing whitespace
cb5e79d
Ban access to static waker via function, mark methods as mut, update …
2a8e309
Add separate APIs for setting the alarm at a time and at a relative o…
97bd30b
Fix doc formatting
8ca902f
Better documentation for get_waker
Ctru14 d53cb80
register_alarm_waker added to hide the static AtomicWaker
628d09b
Address PR feedback on return types, critical section, and ordering
1fda1f9
Clarify alarm documentation
3fc8ac9
Disable 1Hz alarm bit in interrupt handler
7bef2e4
DIsable RTC interrupt in alarm interrupt handler
c307c04
Remove Datetime conversions from set_alarm APIs
a341e2b
Fix doc return type
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| #![no_std] | ||
| #![no_main] | ||
|
|
||
| use core::task::Poll; | ||
|
|
||
| use defmt::info; | ||
| use embassy_executor::Spawner; | ||
| use embassy_imxrt::rtc::{Rtc, RtcDatetimeClock}; | ||
| use embassy_time::Timer; | ||
| use embedded_mcu_hal::time::{Datetime, DatetimeClock, DatetimeClockError, Month, UncheckedDatetime}; | ||
| use {defmt_rtt as _, embassy_imxrt_examples as _, panic_probe as _}; | ||
|
|
||
| /// RTC alarm struct to await the time alarm wakeup location | ||
| /// This should be implemented by the user to handle RTC peripheral synchronization | ||
| struct RtcAlarm<'r> { | ||
| expires_at: u64, | ||
| rtc: &'r RtcDatetimeClock<'r>, | ||
| } | ||
|
|
||
| impl<'r> Future for RtcAlarm<'r> { | ||
| type Output = Result<(), DatetimeClockError>; | ||
| fn poll(self: core::pin::Pin<&mut Self>, cx: &mut core::task::Context<'_>) -> Poll<Self::Output> { | ||
| match self.rtc.get_current_datetime() { | ||
| Ok(now) => { | ||
| if self.expires_at <= now.to_unix_time_seconds() { | ||
| Poll::Ready(Ok(())) | ||
| } else { | ||
| info!("Alarm pending at time {}, expires at {}", now, self.expires_at); | ||
|
|
||
| // Register our waker to be called by the interrupt handler | ||
| self.rtc.register_alarm_waker(cx.waker()); | ||
|
|
||
| Poll::Pending | ||
| } | ||
| } | ||
| Err(e) => Poll::Ready(Err(e)), | ||
| } | ||
| } | ||
| } | ||
| #[embassy_executor::main] | ||
| async fn main(_spawner: Spawner) { | ||
| const ALARM_SECONDS: u64 = 10; | ||
|
|
||
| let p = embassy_imxrt::init(Default::default()); | ||
| let mut r = Rtc::new(p.RTC); | ||
| let (dt_clock, _rtc_nvram) = r.split(); | ||
|
|
||
| // Initialize the system RTC | ||
| let datetime = Datetime::new(UncheckedDatetime { | ||
| year: 2026, | ||
| month: Month::January, | ||
| day: 12, | ||
| hour: 16, | ||
| ..Default::default() | ||
| }) | ||
| .unwrap(); | ||
| let ret = dt_clock.set_current_datetime(&datetime); | ||
| info!("RTC set time: {:?}", datetime); | ||
| assert!(ret.is_ok()); | ||
|
|
||
| // Show RTC functioning: Display current time before setting alarm | ||
| let current_time = dt_clock.get_current_datetime().unwrap(); | ||
| info!("Current time before alarm: {:?}", current_time); | ||
|
|
||
| info!("Waiting 5 seconds..."); | ||
| Timer::after_secs(5).await; // This timer uses the OsTimer peripheral | ||
|
|
||
| info!( | ||
| "Current time after waiting: {:?}", | ||
| dt_clock.get_current_datetime().unwrap() | ||
| ); | ||
|
|
||
| // Set an RTC alarm to trigger after ALARM_SECONDS | ||
| info!("Setting alarm to trigger in {} seconds...", ALARM_SECONDS); | ||
| let expires_at_secs = dt_clock.set_alarm_from_now(ALARM_SECONDS).expect("Failed to set alarm"); | ||
|
|
||
| let alarm = RtcAlarm { | ||
| expires_at: expires_at_secs, | ||
| rtc: dt_clock, | ||
| }; | ||
|
|
||
| // Wait for the alarm to trigger | ||
| info!("Waiting for alarm..."); | ||
| alarm.await.expect("Alarm failed"); | ||
|
|
||
| // Display time after alarm triggered | ||
| let wake_time = dt_clock.get_current_datetime().unwrap(); | ||
| info!("Alarm triggered! Wake time: {:?}", wake_time); | ||
|
|
||
| // Clear the alarm | ||
| dt_clock.clear_alarm().expect("Failed to clear alarm"); | ||
| info!("Alarm cleared"); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.