Skip to content

Commit 2f36ec2

Browse files
committed
Simple rtc example using the very inaccurate LSI clock source for the rtc
1 parent b649c3d commit 2f36ec2

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

examples/rtc.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#![no_main]
2+
#![no_std]
3+
4+
use hal::{
5+
delay::SYSTDelayExt,
6+
rcc::{Config, RccExt},
7+
stm32::Peripherals,
8+
time::{Time, Date, Year, Month, MonthDay, Hour, Minute, Second, ExtU32},
9+
hal::prelude::*, rtc::RtcExt
10+
};
11+
use stm32g4xx_hal as hal;
12+
13+
use cortex_m_rt::entry;
14+
15+
use utils::logger::info;
16+
17+
#[macro_use]
18+
mod utils;
19+
20+
#[entry]
21+
fn main() -> ! {
22+
utils::logger::init();
23+
24+
info!("start");
25+
26+
let dp = Peripherals::take().unwrap();
27+
let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");
28+
29+
info!("rcc");
30+
31+
let rcc = dp.RCC.constrain();
32+
let mut rcc = rcc.freeze(Config::hsi());
33+
34+
info!("Setup rtc");
35+
let mut delay = cp.SYST.delay(&rcc.clocks);
36+
let mut rtc = dp
37+
.RTC
38+
.constrain(&mut rcc);
39+
40+
info!("Setup date");
41+
rtc.set_date(&Date::new(Year(2023), Month(6), MonthDay(4)));
42+
rtc.set_time(&Time::new(18.hours(), 43.minutes(), 2.secs(), true));
43+
44+
info!("Enter Loop");
45+
46+
loop {
47+
info!("Timestamp: {} {}", rtc.get_date(), rtc.get_time());
48+
delay.delay_ms(500);
49+
}
50+
}

0 commit comments

Comments
 (0)