feat: log rtc time to terminal over rtt
This commit is contained in:
parent
7df4f90ae3
commit
058f01fd13
1 changed files with 53 additions and 0 deletions
53
examples/rtc-log.rs
Executable file
53
examples/rtc-log.rs
Executable 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()
|
||||
)
|
||||
}
|
Loading…
Add table
Reference in a new issue