Rtc

Struct Rtc 

Source
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>

Source

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.

Source

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>

Source

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.

Source

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>

Source

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.

Source

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>

Source

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

Source

pub fn set_time(&mut self, counter_value: u32)

Set the current RTC counter value to the specified amount

Source

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

Source

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.

Source

pub fn unlisten_alarm(&mut self)

Stops the RTC alarm from triggering the RTC and RTCALARM interrupts

Source

pub fn current_time(&self) -> u32

Reads the current counter

Source

pub fn listen_seconds(&mut self)

Enables triggering the RTC interrupt every time the RTC counter is increased

Source

pub fn unlisten_seconds(&mut self)

Disables the RTC second interrupt

Source

pub fn clear_second_flag(&mut self)

Clears the RTC second interrupt flag

Source

pub fn clear_alarm_flag(&mut self)

Clears the RTC alarm interrupt flag

Source

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();

Auto Trait Implementations§

§

impl<CS> Freeze for Rtc<CS>

§

impl<CS = RtcClkLse> !RefUnwindSafe for Rtc<CS>

§

impl<CS> Send for Rtc<CS>
where CS: Send,

§

impl<CS = RtcClkLse> !Sync for Rtc<CS>

§

impl<CS> Unpin for Rtc<CS>
where CS: Unpin,

§

impl<CS> UnwindSafe for Rtc<CS>
where CS: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, const R: u8> RFrom<T, R> for T

Source§

fn rfrom(value: T) -> T

Source§

impl<S, T, const R: u8> RInto<T, R> for S
where T: RFrom<S, R>,

Source§

fn rinto(self) -> T

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.