embassy/examples/rp/src/bin/rtc.rs

53 lines
1.4 KiB
Rust
Raw Normal View History

2023-06-22 22:37:24 +02:00
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
use defmt::*;
use embassy_executor::Spawner;
use embassy_rp::pac::rtc::regs::{Rtc0, Rtc1};
use embassy_rp::rtc::{DateTime, DayOfWeek, Rtc};
2023-06-22 22:37:24 +02:00
use embassy_time::{Duration, Timer};
use {defmt_rtt as _, panic_probe as _};
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let p = embassy_rp::init(Default::default());
info!("Wait for 20s");
2023-06-22 22:37:24 +02:00
let mut watchdog = embassy_rp::watchdog::Watchdog::new(p.WATCHDOG);
let mut rtc = Rtc::new(p.RTC);
2023-06-22 22:37:24 +02:00
let rtc0 = Rtc0(watchdog.get_scratch0());
let rtc1 = Rtc1(watchdog.get_scratch1());
if rtc1.year() >= 2020 {
rtc.restore(rtc1, rtc0);
} else {
let now = DateTime {
year: 2020,
month: 5,
day: 15,
day_of_week: DayOfWeek::Monday,
hour: 10,
minute: 30,
second: 50,
};
rtc.set_datetime(now).unwrap();
2023-06-22 22:37:24 +02:00
}
Timer::after(Duration::from_millis(20000)).await;
if let Ok(dt) = rtc.now() {
info!(
"Now: {}-{:02}-{:02} {}:{:02}:{:02}",
dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second,
);
let (rtc1, rtc0) = rtc.save();
watchdog.set_scratch0(rtc0.0);
watchdog.set_scratch1(rtc1.0);
}
info!("Reboot.");
Timer::after(Duration::from_millis(200)).await;
cortex_m::peripheral::SCB::sys_reset();
2023-06-22 22:37:24 +02:00
}