pub struct Rtc<CS = RtcClkLse> { /* private fields */ }
Expand description
Real time clock
A continuously running clock that counts seconds¹. It is part of the backup domain which means that the counter is not affected by system resets or standby mode. If Vbat is connected, it is not reset even if the rest of the device is powered off. This allows it to be used to wake the CPU when it is in low power mode.
See examples/rtc.rs and examples/blinky_rtc.rs for usage examples.
1: Unless configured to another frequency using select_frequency
Implementations§
Source§impl Rtc<RtcClkLse>
impl Rtc<RtcClkLse>
Sourcepub fn new(regs: RTC, bkp: &mut BackupDomain, rcc: &mut RCC) -> Self
pub fn new(regs: RTC, bkp: &mut BackupDomain, rcc: &mut RCC) -> Self
Initialises the RTC with low-speed external crystal source (lse).
The BackupDomain
struct is created by Rcc.bkp.constrain()
.
The frequency is set to 1 Hz.
Since the RTC is part of the backup domain, The RTC counter is not reset by normal resets or power cycles where (VBAT) still has power. Use set_time if you want to reset the counter.
In case application is running of a battery on VBAT,
this method will reset the RTC every time, leading to lost time,
you may want to use
restore_or_new
instead.
Sourcepub fn restore_or_new(
regs: RTC,
bkp: &mut BackupDomain,
rcc: &mut RCC,
) -> RestoredOrNewRtc<RtcClkLse>
pub fn restore_or_new( regs: RTC, bkp: &mut BackupDomain, rcc: &mut RCC, ) -> RestoredOrNewRtc<RtcClkLse>
Tries to obtain currently running RTC to prevent a reset in case it was running from VBAT. If the RTC is not running, or is not LSE, it will be reinitialized.
§Examples
let rtc = match Rtc::restore_or_new(p.RTC, &mut backup_domain) {
Restored(rtc) => rtc, // The rtc is restored from previous configuration. You may verify the frequency you want if needed.
New(rtc) => { // The rtc was just initialized, the clock source selected, frequency is 1.Hz()
// Initialize rtc with desired parameters
rtc.select_frequency(2u16.Hz()); // Set the frequency to 2 Hz. This will stay same after reset
rtc
}
};
Source§impl Rtc<RtcClkLsi>
impl Rtc<RtcClkLsi>
Sourcepub fn new_lsi(regs: RTC, bkp: &mut BackupDomain) -> Self
pub fn new_lsi(regs: RTC, bkp: &mut BackupDomain) -> Self
Initialises the RTC with low-speed internal oscillator source (lsi).
The BackupDomain
struct is created by Rcc.bkp.constrain()
.
The frequency is set to 1 Hz.
Since the RTC is part of the backup domain, The RTC counter is not reset by normal resets or power cycles where (VBAT) still has power. Use set_time if you want to reset the counter.
In case application is running of a battery on VBAT,
this method will reset the RTC every time, leading to lost time,
you may want to use
restore_or_new_lsi
instead.
Sourcepub fn restore_or_new_lsi(
regs: RTC,
bkp: &mut BackupDomain,
) -> RestoredOrNewRtc<RtcClkLsi>
pub fn restore_or_new_lsi( regs: RTC, bkp: &mut BackupDomain, ) -> RestoredOrNewRtc<RtcClkLsi>
Tries to obtain currently running RTC to prevent reset in case it was running from VBAT. If the RTC is not running, or is not LSI, it will be reinitialized.
Source§impl Rtc<RtcClkHseDiv128>
impl Rtc<RtcClkHseDiv128>
Sourcepub fn new_hse(regs: RTC, bkp: &mut BackupDomain, hse: Hertz) -> Self
pub fn new_hse(regs: RTC, bkp: &mut BackupDomain, hse: Hertz) -> Self
Initialises the RTC with high-speed external oscillator source (hse)
divided by 128.
The BackupDomain
struct is created by Rcc.bkp.constrain()
.
The frequency is set to 1 Hz.
Since the RTC is part of the backup domain, The RTC counter is not reset by normal resets or power cycles where (VBAT) still has power. Use set_time if you want to reset the counter.
In case application is running of a battery on VBAT,
this method will reset the RTC every time, leading to lost time,
you may want to use
restore_or_new_hse
instead.
Sourcepub fn restore_or_new_hse(
regs: RTC,
bkp: &mut BackupDomain,
hse: Hertz,
) -> RestoredOrNewRtc<RtcClkHseDiv128>
pub fn restore_or_new_hse( regs: RTC, bkp: &mut BackupDomain, hse: Hertz, ) -> RestoredOrNewRtc<RtcClkHseDiv128>
Tries to obtain currently running RTC to prevent reset in case it was running from VBAT. If the RTC is not running, or is not HSE, it will be reinitialized.
Source§impl<CS> Rtc<CS>
impl<CS> Rtc<CS>
Sourcepub fn select_frequency(&mut self, frequency: Hertz)
pub fn select_frequency(&mut self, frequency: Hertz)
Selects the frequency of the RTC Timer NOTE: Maximum frequency of 16384 Hz using the internal LSE
Sourcepub fn set_time(&mut self, counter_value: u32)
pub fn set_time(&mut self, counter_value: u32)
Set the current RTC counter value to the specified amount
Sourcepub fn set_alarm(&mut self, counter_value: u32)
pub fn set_alarm(&mut self, counter_value: u32)
Sets the time at which an alarm will be triggered
This also clears the alarm flag if it is set
Sourcepub fn listen_alarm(&mut self)
pub fn listen_alarm(&mut self)
Enables the RTC interrupt to trigger when the counter reaches the alarm value. In addition, if the EXTI controller has been set up correctly, this function also enables the RTCALARM interrupt.
Sourcepub fn unlisten_alarm(&mut self)
pub fn unlisten_alarm(&mut self)
Stops the RTC alarm from triggering the RTC and RTCALARM interrupts
Sourcepub fn current_time(&self) -> u32
pub fn current_time(&self) -> u32
Reads the current counter
Sourcepub fn listen_seconds(&mut self)
pub fn listen_seconds(&mut self)
Enables triggering the RTC interrupt every time the RTC counter is increased
Sourcepub fn unlisten_seconds(&mut self)
pub fn unlisten_seconds(&mut self)
Disables the RTC second interrupt
Sourcepub fn clear_second_flag(&mut self)
pub fn clear_second_flag(&mut self)
Clears the RTC second interrupt flag
Sourcepub fn clear_alarm_flag(&mut self)
pub fn clear_alarm_flag(&mut self)
Clears the RTC alarm interrupt flag
Sourcepub fn wait_alarm(&mut self) -> Result<(), Infallible>
pub fn wait_alarm(&mut self) -> Result<(), Infallible>
Return Ok(())
if the alarm flag is set, Err(nb::WouldBlock)
otherwise.
use nb::block;
rtc.set_alarm(rtc.read_counts() + 5);
// NOTE: Safe unwrap because Infallible can't be returned
block!(rtc.wait_alarm()).unwrap();