nucleo-l053r8/examples/rtc-log.rs
2025-05-14 17:40:08 +02:00

57 lines
1.4 KiB
Rust
Executable file

#![no_main]
#![no_std]
use defmt::info;
use hal::pwr::PWR;
use hal::rtc::{Datelike, NaiveDateTime, Rtc, Timelike};
use panic_probe as _;
use defmt_rtt as _; // global logger
use cortex_m_rt::entry;
use hal::{pac, prelude::*, rcc::Config};
#[entry]
fn main() -> ! {
let dp = pac::Peripherals::take().unwrap();
let cp = cortex_m::Peripherals::take().unwrap();
// Configure the clock.
let mut rcc = dp.RCC.freeze(Config::hsi16());
let pwr = PWR::new(dp.PWR, &mut rcc);
let gpioa = dp.GPIOA.split(&mut rcc);
let mut led = gpioa.pa5.into_push_pull_output();
let mut delay = cp.SYST.delay(rcc.clocks);
// Setup the Real-Time-Clock of the Controller.
// starts at 0 (2001-01-01 00:00:00) and resets
// when the Controller is no longer powered.
let mut rtc = Rtc::new(dp.RTC, &mut rcc, &pwr, None).unwrap();
loop {
led.set_high().unwrap(); // light on
delay.delay_ms(500_u16); // wait
led.set_low().unwrap(); // light off
delay.delay_ms(500_u16); // wait
// print the current time from the RTC
ptime(&mut rtc);
}
}
/// prints the time to the "host" computer via RTT
fn ptime(rtc: &mut Rtc) {
let time: NaiveDateTime = rtc.now();
info!(
"Time: {:04}-{:02}-{:02} {:02}:{:02}:{:02}",
time.year(),
time.month(),
time.day(),
time.hour(),
time.minute(),
time.second()
)
}