aboutsummaryrefslogtreecommitdiff
path: root/tests/stm32/src/bin/stop.rs
blob: 75dacbe7ed21facbb2b29aa3e5dbaf5ece8f15fb (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
62
63
64
65
66
67
68
69
70
71
// required-features: stop,chrono

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

use chrono::NaiveDate;
use common::*;
use embassy_executor::Spawner;
use embassy_stm32::low_power::{StopMode, stop_ready};
use embassy_stm32::rcc::LsConfig;
use embassy_stm32::rtc::Rtc;
use embassy_stm32::{Config, low_power};
use embassy_time::Timer;

#[embassy_executor::task]
async fn task_1() {
    for _ in 0..9 {
        info!("task 1: waiting for 500ms...");
        defmt::assert!(stop_ready(StopMode::Stop2));
        Timer::after_millis(500).await;
    }
}

#[embassy_executor::task]
async fn task_2() {
    for _ in 0..5 {
        info!("task 2: waiting for 1000ms...");
        defmt::assert!(stop_ready(StopMode::Stop2));
        Timer::after_millis(1000).await;
    }

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

#[embassy_executor::main(executor = "low_power::Executor")]
async fn async_main(spawner: Spawner) {
    let _ = config();

    let mut config = Config::default();
    config.rcc.ls = LsConfig::default_lse();
    config.rtc._disable_rtc = false;

    // System Clock seems cannot be greater than 16 MHz
    #[cfg(any(feature = "stm32h563zi", feature = "stm32h503rb"))]
    {
        use embassy_stm32::rcc::HSIPrescaler;
        config.rcc.hsi = Some(HSIPrescaler::DIV4); // 64 MHz HSI will need a /4
    }

    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();

    let (rtc, _time_provider) = Rtc::new(p.RTC);

    info!("set datetime");
    critical_section::with(|cs| {
        rtc.borrow_mut(cs).set_datetime(now.into()).expect("datetime not set");
    });

    info!("spawn");
    spawner.spawn(task_1().unwrap());
    spawner.spawn(task_2().unwrap());
}