diff options
| author | Mathias <[email protected]> | 2023-07-01 12:16:23 +0200 |
|---|---|---|
| committer | Mathias <[email protected]> | 2023-07-01 12:16:23 +0200 |
| commit | d372df7ddb381571fd2964e32b486b6d1cd1ad03 (patch) | |
| tree | 73f76375bb3061ef873e99fd0e9825e63e6dac3c /examples | |
| parent | d690a1717fd03a1f3fdad509d6a638567f8a645a (diff) | |
L4: Switch to MSI to prevent problems with PLL configuration, and enable power to AHB bus clock to allow RTC to run
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/stm32l4/src/bin/rtc.rs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/examples/stm32l4/src/bin/rtc.rs b/examples/stm32l4/src/bin/rtc.rs new file mode 100644 index 000000000..0de708950 --- /dev/null +++ b/examples/stm32l4/src/bin/rtc.rs | |||
| @@ -0,0 +1,50 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use chrono::{NaiveDate, NaiveDateTime}; | ||
| 6 | use defmt::*; | ||
| 7 | use embassy_executor::Spawner; | ||
| 8 | use embassy_stm32::rcc::{self, ClockSrc, PLLClkDiv, PLLMul, PLLSource, PLLSrcDiv}; | ||
| 9 | use embassy_stm32::rtc::{Rtc, RtcConfig}; | ||
| 10 | use embassy_stm32::time::Hertz; | ||
| 11 | use embassy_stm32::Config; | ||
| 12 | use embassy_time::{Duration, Timer}; | ||
| 13 | use {defmt_rtt as _, panic_probe as _}; | ||
| 14 | |||
| 15 | #[embassy_executor::main] | ||
| 16 | async fn main(_spawner: Spawner) { | ||
| 17 | let p = { | ||
| 18 | let mut config = Config::default(); | ||
| 19 | config.rcc.mux = ClockSrc::PLL( | ||
| 20 | PLLSource::HSE(Hertz::mhz(8)), | ||
| 21 | PLLClkDiv::Div2, | ||
| 22 | PLLSrcDiv::Div1, | ||
| 23 | PLLMul::Mul20, | ||
| 24 | None, | ||
| 25 | ); | ||
| 26 | config.rcc.rtc_mux = rcc::RtcClockSource::LSE32; | ||
| 27 | embassy_stm32::init(config) | ||
| 28 | }; | ||
| 29 | info!("Hello World!"); | ||
| 30 | |||
| 31 | let now = NaiveDate::from_ymd_opt(2020, 5, 15) | ||
| 32 | .unwrap() | ||
| 33 | .and_hms_opt(10, 30, 15) | ||
| 34 | .unwrap(); | ||
| 35 | |||
| 36 | let mut rtc = Rtc::new( | ||
| 37 | p.RTC, | ||
| 38 | RtcConfig::default().clock_config(embassy_stm32::rtc::RtcClockSource::LSE), | ||
| 39 | ); | ||
| 40 | info!("Got RTC! {:?}", now.timestamp()); | ||
| 41 | |||
| 42 | rtc.set_datetime(now.into()).expect("datetime not set"); | ||
| 43 | |||
| 44 | // In reality the delay would be much longer | ||
| 45 | Timer::after(Duration::from_millis(20000)).await; | ||
| 46 | |||
| 47 | let then: NaiveDateTime = rtc.now().unwrap().into(); | ||
| 48 | info!("Got RTC! {:?}", then.timestamp()); | ||
| 49 | |||
| 50 | } | ||
