Struct stm32wlxx_hal::rtc::Rtc
source · pub struct Rtc { /* private fields */ }
chrono
only.Expand description
Real-time clock driver.
Implementations§
source§impl Rtc
impl Rtc
sourcepub fn new(rtc: RTC, clk: Clk, pwr: &mut PWR, rcc: &mut RCC) -> Rtc
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);
sourcepub unsafe fn renew(rtc: RTC, pwr: &mut PWR, rcc: &mut RCC) -> Rtc
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
- 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.
sourcepub fn masked_status() -> R
pub fn masked_status() -> R
Read the RTC masked status (interrupt) register.
sourcepub fn clear_status(mask: u32)
pub fn clear_status(mask: u32)
Clear status (interrupt) flags.
Status flag masks can be found in stat
.
sourcepub fn set_date_time(&mut self, date_time: NaiveDateTime)
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.
sourcepub fn calendar_initialized(&self) -> Option<()>
pub fn calendar_initialized(&self) -> Option<()>
Returns None
if the calendar is uninitialized.
sourcepub fn calibrate_lp(&mut self, calibration: i16)
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.
sourcepub fn recalibration_pending(&self) -> bool
pub fn recalibration_pending(&self) -> bool
Returns true
if recalibration is pending.
sourcepub fn date(&self) -> Option<NaiveDate>
pub fn date(&self) -> Option<NaiveDate>
Calendar Date
Returns None
if the calendar has not been initialized.
sourcepub fn time(&self) -> Option<NaiveTime>
pub fn time(&self) -> Option<NaiveTime>
Current Time
Returns None
if the calendar has not been initialized.
sourcepub fn date_time(&self) -> Option<NaiveDateTime>
pub fn date_time(&self) -> Option<NaiveDateTime>
Calendar Date and Time
Returns None
if the calendar has not been initialized.
sourcepub fn setup_wakeup_timer(&mut self, sec: u32, irq_en: bool)
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);
sourcepub fn is_wakeup_timer_en(&self) -> bool
pub fn is_wakeup_timer_en(&self) -> bool
Returns true
if the wakeup timer is enabled.
sourcepub fn disable_wakeup_timer(&mut self)
pub fn disable_wakeup_timer(&mut self)
Disable the wakeup timer.
sourcepub fn wakeup_period_cycles(&self) -> u32
pub fn wakeup_period_cycles(&self) -> u32
Returns the value of the wakeup timer as calculated by the RTC logic in RTC cycles
sourcepub fn set_alarm_a(&mut self, alarm: &Alarm)
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
.
sourcepub fn is_alarm_a_en(&self) -> bool
pub fn is_alarm_a_en(&self) -> bool
Returns true
if alarm A is enabled.
sourcepub fn set_alarm_a_en(&mut self, en: bool, irq_en: bool)
pub fn set_alarm_a_en(&mut self, en: bool, irq_en: bool)
Set the alarm A enable, and alarm A interrupt enable.
sourcepub fn set_alarm_b(&mut self, alarm: &Alarm)
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
.
sourcepub fn is_alarm_b_en(&self) -> bool
pub fn is_alarm_b_en(&self) -> bool
Returns true
if alarm B is enabled.
sourcepub fn set_alarm_b_en(&mut self, en: bool, irq_en: bool)
pub fn set_alarm_b_en(&mut self, en: bool, irq_en: bool)
Set the alarm B enable, and alarm B interrupt enable.
sourcepub fn disable_write_protect(&mut self)
pub fn disable_write_protect(&mut self)
Disable the RTC write protection.
sourcepub unsafe fn enable_write_protect(&mut self)
pub unsafe fn enable_write_protect(&mut self)
Enable the RTC write protection.
Safety
- You must call
disable_write_protect
before using any other&mut self
RTC method.