feat: log rtc time to terminal over rtt

This commit is contained in:
cscherr 2025-04-28 14:42:12 +02:00
parent 7df4f90ae3
commit 058f01fd13
Signed by: cscherrNT
GPG key ID: 8E2B45BC51A27EA7

53
examples/rtc-log.rs Executable file
View file

@ -0,0 +1,53 @@
#![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);
let mut rtc = Rtc::new(dp.RTC, &mut rcc, &pwr, None).unwrap(); // starts at time 0
#[allow(clippy::never_loop)]
loop {
led.set_high().unwrap();
delay.delay_ms(500_u16);
led.set_low().unwrap();
delay.delay_ms(500_u16);
ptime(&mut rtc);
}
}
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()
)
}