Struct stm32wlxx_hal::rtc::Rtc

source ·
pub struct Rtc { /* private fields */ }
Available on crate feature chrono only.
Expand description

Real-time clock driver.

Implementations§

source§

impl Rtc

source

pub fn new(rtc: RTC, clk: Clk, pwr: &mut PWR, rcc: &mut RCC) -> Rtc

Create a new real-time clock driver.

This will not setup the source clock.

Safety

This function could be considered unsafe because it is not a pure function. The RTC is in the backup domain; system resets will not reset the RTC. You are responsible for resetting the backup domain if required.

Panics
  • (debug) clock source is not ready.
Example

LSE clock source (this depends on HW, example valid for NUCLEO board):

use stm32wlxx_hal::{
    pac,
    rcc::pulse_reset_backup_domain,
    rtc::{Clk, Rtc},
};

let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();

unsafe { pulse_reset_backup_domain(&mut dp.RCC, &mut dp.PWR) };
dp.PWR.cr1.modify(|_, w| w.dbp().enabled());
dp.RCC.bdcr.modify(|_, w| w.lseon().on());
while dp.RCC.bdcr.read().lserdy().is_not_ready() {}

let rtc: Rtc = Rtc::new(dp.RTC, Clk::Lse, &mut dp.PWR, &mut dp.RCC);

LSI clock source:

use stm32wlxx_hal::{
    pac,
    rcc::{enable_lsi, pulse_reset_backup_domain},
    rtc::{Clk, Rtc},
};

let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();

unsafe { pulse_reset_backup_domain(&mut dp.RCC, &mut dp.PWR) };
enable_lsi(&mut dp.RCC);

let rtc: Rtc = Rtc::new(dp.RTC, Clk::Lsi, &mut dp.PWR, &mut dp.RCC);

HSE clock source (this depends on your hardware):

use stm32wlxx_hal::{
    pac,
    rcc::pulse_reset_backup_domain,
    rtc::{Clk, Rtc},
};

let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();

unsafe { pulse_reset_backup_domain(&mut dp.RCC, &mut dp.PWR) };
dp.RCC
    .cr
    .modify(|_, w| w.hseon().enabled().hsebyppwr().vddtcxo());
while dp.RCC.cr.read().hserdy().is_not_ready() {}

let rtc: Rtc = Rtc::new(dp.RTC, Clk::Hse, &mut dp.PWR, &mut dp.RCC);
source

pub unsafe fn renew(rtc: RTC, pwr: &mut PWR, rcc: &mut RCC) -> Rtc

Create a new real-time clock driver preserving backup domain values.

Unlike new this will enable the LSE clock source if not already enabled. This function assumes the LSE clock source will be used because it is the only clock source that is preserved in the shutdown mode.

The RTC calendar may not be initialized, this can occur if this function is called after power loss or after a backup domain reset.

Safety
  1. This function relies on global hardware state in the backup domain. The backup domain is not reset with normal system resets. Reset the backup domain before calling this function if determinism is required.
source

pub fn hz(rcc: &RCC) -> u32

Source clock frequency in hertz.

source

pub fn status() -> R

Read the RTC status (interrupt) register.

source

pub fn masked_status() -> R

Read the RTC masked status (interrupt) register.

source

pub fn clear_status(mask: u32)

Clear status (interrupt) flags.

Status flag masks can be found in stat.

source

pub fn set_date_time(&mut self, date_time: NaiveDateTime)

Set the date and time.

The value will take some duration to apply after this function returns:

  • LPCAL=0: the counting restarts after 4 RTCCLK clock cycles
  • LPCAL=1: the counting restarts after up to 2 RTCCLK + 1 ck_apre
Panics
  • Year is greater than or equal to 2100.
  • Year is less than 2000.
  • Backup domain write protection is enabled.
source

pub fn calendar_initialized(&self) -> Option<()>

Returns None if the calendar is uninitialized.

source

pub fn calibrate_lp(&mut self, calibration: i16)

Calibrate the RTC using the low-power mode.

This does not poll for completion, use recalibration_pending if you need to wait for completion.

The calibration argument is in units of 0.9537 ppm. The calibration range is -487.1 ppm to +488.5 ppm (-511 to 512), values outside of this range will saturate.

source

pub fn recalibration_pending(&self) -> bool

Returns true if recalibration is pending.

source

pub fn date(&self) -> Option<NaiveDate>

Calendar Date

Returns None if the calendar has not been initialized.

source

pub fn time(&self) -> Option<NaiveTime>

Current Time

Returns None if the calendar has not been initialized.

source

pub fn date_time(&self) -> Option<NaiveDateTime>

Calendar Date and Time

Returns None if the calendar has not been initialized.

source

pub fn setup_wakeup_timer(&mut self, sec: u32, irq_en: bool)

Setup the periodic wakeup timer for sec + 1 seconds.

sec can only go up to 217 (36 hours), values greater than this will be set to the maximum.

Example

Setup the wakeup timer to go off in 1 hour, without interrupts.

rtc.setup_wakeup_timer(3599, false);
source

pub fn is_wakeup_timer_en(&self) -> bool

Returns true if the wakeup timer is enabled.

source

pub fn disable_wakeup_timer(&mut self)

Disable the wakeup timer.

source

pub fn wakeup_period_cycles(&self) -> u32

Returns the value of the wakeup timer as calculated by the RTC logic in RTC cycles

source

pub fn set_alarm_a(&mut self, alarm: &Alarm)

Set alarm A.

This will disable the alarm if previously enabled.

This will not enable the alarm after setup. To enable the alarm use set_alarm_a_en.

source

pub fn is_alarm_a_en(&self) -> bool

Returns true if alarm A is enabled.

source

pub fn alarm_a(&self) -> Alarm

Get the value of alarm A.

source

pub fn set_alarm_a_en(&mut self, en: bool, irq_en: bool)

Set the alarm A enable, and alarm A interrupt enable.

source

pub fn set_alarm_b(&mut self, alarm: &Alarm)

Set alarm B.

This will disable the alarm if previously enabled.

This will not enable the alarm after setup. To enable the alarm use set_alarm_b_en.

source

pub fn is_alarm_b_en(&self) -> bool

Returns true if alarm B is enabled.

source

pub fn alarm_b(&self) -> Alarm

Get the value of alarm B.

source

pub fn set_alarm_b_en(&mut self, en: bool, irq_en: bool)

Set the alarm B enable, and alarm B interrupt enable.

source

pub fn disable_write_protect(&mut self)

Disable the RTC write protection.

source

pub unsafe fn enable_write_protect(&mut self)

Enable the RTC write protection.

Safety

Trait Implementations§

source§

impl Debug for Rtc

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl RefUnwindSafe for Rtc

§

impl Send for Rtc

§

impl !Sync for Rtc

§

impl Unpin for Rtc

§

impl UnwindSafe for Rtc

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.