forked from stm32-rs/stm32h7xx-hal
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rtc.rs
87 lines (66 loc) · 2.13 KB
/
rtc.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//! Example of configuring the real-time clock using the internal LSI oscillator
#![no_main]
#![no_std]
use core::cell::RefCell;
use log::info;
use chrono::prelude::*;
use cortex_m::{asm, interrupt::Mutex};
use cortex_m_rt::entry;
use pac::interrupt;
use stm32h7xx_hal::{pac, prelude::*, rtc};
#[path = "utilities/logger.rs"]
mod logger;
static RTC: Mutex<RefCell<Option<rtc::Rtc>>> = Mutex::new(RefCell::new(None));
#[entry]
fn main() -> ! {
logger::init();
let mut dp = pac::Peripherals::take().unwrap();
// Constrain and Freeze power
info!("Setup PWR... ");
let pwr = dp.PWR.constrain();
let mut pwrcfg = pwr.freeze();
// Take the backup power domain
let backup = pwrcfg.backup().unwrap();
// Constrain and Freeze clock
info!("Setup RCC... ");
let rcc = dp.RCC.constrain();
let ccdr = rcc.sys_ck(100.mhz()).freeze(pwrcfg, &dp.SYSCFG);
info!("");
info!("stm32h7xx-hal example - RTC");
info!("");
let mut rtc = rtc::Rtc::open_or_init(
dp.RTC,
backup.RTC,
rtc::RtcClock::Lsi,
&ccdr.clocks,
);
// TODO: Get current time from some source
let now = NaiveDate::from_ymd(2001, 1, 1).and_hms(0, 0, 0);
rtc.set_date_time(now);
rtc.enable_wakeup(10);
rtc.listen(&mut dp.EXTI, rtc::Event::Wakeup);
unsafe {
pac::NVIC::unmask(interrupt::RTC_WKUP);
}
info!("Time: {}", rtc.date_time().unwrap());
cortex_m::interrupt::free(|cs| {
RTC.borrow(cs).replace(Some(rtc));
});
loop {
// Some debuggers have issues working when the MCU goes to standby/sleep.
// You may want to change this to asm::nop() or try to enable low-power debugging.
asm::wfi();
}
}
#[interrupt]
fn RTC_WKUP() {
let date_time = cortex_m::interrupt::free(|cs| {
let mut rc = RTC.borrow(cs).borrow_mut();
let rtc = rc.as_mut().unwrap();
let date_time = rtc.date_time().unwrap();
let exti = unsafe { &mut pac::Peripherals::steal().EXTI };
rtc.unpend(exti, rtc::Event::Wakeup);
date_time
});
info!("Time: {}", date_time);
}