aboutsummaryrefslogtreecommitdiff
path: root/tests/stm32/src/bin/rtc.rs
blob: 5c80eb250f1babc679698462fd81b5e2b402bc9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// required-features: chrono

#![no_std]
#![no_main]
#[path = "../common.rs"]
mod common;

use chrono::{NaiveDate, NaiveDateTime};
use common::*;
use defmt::assert;
use embassy_executor::Spawner;
#[cfg(feature = "stop")]
use embassy_stm32::low_power::reconfigure_rtc;
use embassy_stm32::rcc::LsConfig;
#[cfg(feature = "stop")]
use embassy_stm32::rtc::RtcTimeProvider;
#[cfg(not(feature = "stop"))]
use embassy_stm32::rtc::{Rtc, RtcConfig};
use embassy_time::Timer;
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
    let mut config = config();
    config.rcc.ls = LsConfig::default_lse();

    let p = init_with_config(config);
    info!("Hello World!");

    let now = NaiveDate::from_ymd_opt(2020, 5, 15)
        .unwrap()
        .and_hms_opt(10, 30, 15)
        .unwrap();

    #[cfg(not(feature = "stop"))]
    let mut rtc = Rtc::new(p.RTC, RtcConfig::default());

    #[cfg(feature = "stop")]
    let time_provider = RtcTimeProvider::new(p.RTC);

    #[cfg(not(feature = "stop"))]
    rtc.set_datetime(now.into()).expect("datetime not set");

    #[cfg(feature = "stop")]
    reconfigure_rtc(|rtc| rtc.set_datetime(now.into()).expect("datetime not set"));

    info!("Waiting 5 seconds");
    Timer::after_millis(5000).await;

    #[cfg(not(feature = "stop"))]
    let then: NaiveDateTime = rtc.now().unwrap().into();
    #[cfg(feature = "stop")]
    let then: NaiveDateTime = time_provider.now().unwrap().into();

    let seconds = (then - now).num_seconds();

    info!("measured = {}", seconds);

    assert!(seconds > 3 && seconds < 7);

    info!("Test OK");
    cortex_m::asm::bkpt();
}