Skip to content

Commit e73fdc2

Browse files
authored
rtc: add methods to read alarms
1 parent 68e216c commit e73fdc2

File tree

5 files changed

+68
-3
lines changed

5 files changed

+68
-3
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [Unreleased]
8+
### Added
9+
- Added `Rtc::alarm_{a,b}` to get the alarm value.
10+
- Added `impl From<Alarm> for chrono::NaiveTime`.
11+
712
## [0.5.0] - 2022-05-08
813
### Added
914
- Added `set_sleep_clock` to GPIO ports to enable and disable clocks during sleep.

hal/src/rtc/alarm.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ impl From<chrono::NaiveTime> for Alarm {
5555
}
5656
}
5757

58+
impl From<Alarm> for chrono::NaiveTime {
59+
fn from(alarm: Alarm) -> Self {
60+
Self::from_hms(
61+
alarm.hours().into(),
62+
alarm.minutes().into(),
63+
alarm.seconds().into(),
64+
)
65+
}
66+
}
67+
5868
const fn const_min(a: u8, b: u8) -> u32 {
5969
if a < b {
6070
a as u32
@@ -506,3 +516,19 @@ impl Alarm {
506516
self.ss_mask
507517
}
508518
}
519+
520+
#[cfg(test)]
521+
mod tests {
522+
use super::Alarm;
523+
use chrono::NaiveTime;
524+
525+
#[test]
526+
fn chrono_convert() {
527+
let ndt: NaiveTime = NaiveTime::from_hms(12, 34, 56);
528+
let alarm: Alarm = ndt.into();
529+
assert_eq!(alarm.hours(), 12);
530+
assert_eq!(alarm.minutes(), 34);
531+
assert_eq!(alarm.seconds(), 56);
532+
assert_eq!(NaiveTime::from(alarm), ndt);
533+
}
534+
}

hal/src/rtc/mod.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,21 @@ impl Rtc {
547547
self.rtc.alrabinr.write(|w| w.ss().bits(alarm.ss));
548548
}
549549

550+
/// Get the value of alarm A.
551+
///
552+
/// Returns `None` if the alarm is not set.
553+
pub fn alarm_a(&self) -> Option<Alarm> {
554+
if self.rtc.cr.read().alrae().is_disabled() {
555+
None
556+
} else {
557+
Some(Alarm {
558+
val: self.rtc.alrmar.read().bits(),
559+
ss: self.rtc.alrabinr.read().ss().bits(),
560+
ss_mask: self.rtc.alrmassr.read().maskss().bits(),
561+
})
562+
}
563+
}
564+
550565
/// Set the alarm A enable, and alarm A interrupt enable.
551566
#[inline]
552567
pub fn set_alarm_a_en(&mut self, en: bool, irq_en: bool) {
@@ -568,6 +583,21 @@ impl Rtc {
568583
self.rtc.alrbbinr.write(|w| w.ss().bits(alarm.ss));
569584
}
570585

586+
/// Get the value of alarm B.
587+
///
588+
/// Returns `None` if the alarm is not set.
589+
pub fn alarm_b(&self) -> Option<Alarm> {
590+
if self.rtc.cr.read().alrbe().is_disabled() {
591+
None
592+
} else {
593+
Some(Alarm {
594+
val: self.rtc.alrmbr.read().bits(),
595+
ss: self.rtc.alrbbinr.read().ss().bits(),
596+
ss_mask: self.rtc.alrmbssr.read().maskss().bits(),
597+
})
598+
}
599+
}
600+
571601
/// Set the alarm B enable, and alarm B interrupt enable.
572602
#[inline]
573603
pub fn set_alarm_b_en(&mut self, en: bool, irq_en: bool) {

testsuite/src/aes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1732,9 +1732,9 @@ mod tests {
17321732
let start: u32 = DWT::cycle_count();
17331733
let cipher = aes_gcm::Aes128Gcm::new(key.as_ref().into());
17341734
let nonce = Nonce::from_slice(iv.as_ref().into());
1735-
let result_tag = cipher
1735+
let result_tag = unwrap!(cipher
17361736
.encrypt_in_place_detached(nonce, &gcm.aad, &mut buf[..gcm.pt.len()])
1737-
.unwrap();
1737+
.ok());
17381738
total_elapsed += DWT::cycle_count().wrapping_sub(start);
17391739

17401740
let result_tag: [u8; 16] = unwrap!(result_tag.try_into());

testsuite/src/rtc.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ mod tests {
198198
while ta.rcc.bdcr.read().lserdy().is_not_ready() {}
199199
let mut rtc: Rtc = test_set_date_time_with_clk(rtc::Clk::Lse);
200200

201+
defmt::assert_eq!(rtc.alarm_a(), None);
202+
201203
let alarm: Alarm = Alarm::from(unwrap!(rtc.time()) + Duration::seconds(1))
202204
.set_days_mask(true)
203205
.set_hours_mask(true)
@@ -214,11 +216,13 @@ mod tests {
214216
defmt::info!("elapsed: {=u32:us}", elapsed_micros);
215217
// 100ms tolerance
216218
defmt::assert!(elapsed_micros > 900_000 && elapsed_micros < 1_100_000);
217-
return;
219+
break;
218220
} else if elapsed_micros > 2 * 1000 * 1000 {
219221
defmt::info!("{:08X}", Rtc::status().bits());
220222
defmt::panic!("Timeout! Elapsed: {=u32:us}", elapsed_micros);
221223
}
222224
}
225+
226+
defmt::assert_eq!(rtc.alarm_a(), Some(alarm));
223227
}
224228
}

0 commit comments

Comments
 (0)