aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMathias <[email protected]>2023-07-01 12:16:23 +0200
committerMathias <[email protected]>2023-07-01 12:16:23 +0200
commitd372df7ddb381571fd2964e32b486b6d1cd1ad03 (patch)
tree73f76375bb3061ef873e99fd0e9825e63e6dac3c
parentd690a1717fd03a1f3fdad509d6a638567f8a645a (diff)
L4: Switch to MSI to prevent problems with PLL configuration, and enable power to AHB bus clock to allow RTC to run
-rw-r--r--embassy-stm32/src/rcc/l4.rs23
-rw-r--r--examples/stm32l4/src/bin/rtc.rs50
2 files changed, 73 insertions, 0 deletions
diff --git a/embassy-stm32/src/rcc/l4.rs b/embassy-stm32/src/rcc/l4.rs
index f8c1a6e06..f7f3b9046 100644
--- a/embassy-stm32/src/rcc/l4.rs
+++ b/embassy-stm32/src/rcc/l4.rs
@@ -1,6 +1,7 @@
1use core::marker::PhantomData; 1use core::marker::PhantomData;
2 2
3use embassy_hal_common::into_ref; 3use embassy_hal_common::into_ref;
4use stm32_metapac::rcc::regs::Cfgr;
4use stm32_metapac::rcc::vals::{Lsedrv, Mcopre, Mcosel}; 5use stm32_metapac::rcc::vals::{Lsedrv, Mcopre, Mcosel};
5 6
6use crate::gpio::sealed::AFType; 7use crate::gpio::sealed::AFType;
@@ -439,6 +440,26 @@ impl<'d, T: McoInstance> Mco<'d, T> {
439} 440}
440 441
441pub(crate) unsafe fn init(config: Config) { 442pub(crate) unsafe fn init(config: Config) {
443 // Switch to MSI to prevent problems with PLL configuration.
444 if !RCC.cr().read().msion() {
445 // Turn on MSI and configure it to 4MHz.
446 RCC.cr().modify(|w| {
447 w.set_msirgsel(true); // MSI Range is provided by MSIRANGE[3:0].
448 w.set_msirange(MSIRange::default().into());
449 w.set_msipllen(false);
450 w.set_msion(true)
451 });
452
453 // Wait until MSI is running
454 while !RCC.cr().read().msirdy() {}
455 }
456 if RCC.cfgr().read().sws() != Sw::MSI {
457 // Set MSI as a clock source, reset prescalers.
458 RCC.cfgr().write_value(Cfgr::default());
459 // Wait for clock switch status bits to change.
460 while RCC.cfgr().read().sws() != Sw::MSI {}
461 }
462
442 match config.rtc_mux { 463 match config.rtc_mux {
443 RtcClockSource::LSE32 => { 464 RtcClockSource::LSE32 => {
444 // 1. Unlock the backup domain 465 // 1. Unlock the backup domain
@@ -660,6 +681,8 @@ pub(crate) unsafe fn init(config: Config) {
660 } 681 }
661 }; 682 };
662 683
684 RCC.apb1enr1().modify(|w| w.set_pwren(true));
685
663 set_freqs(Clocks { 686 set_freqs(Clocks {
664 sys: Hertz(sys_clk), 687 sys: Hertz(sys_clk),
665 ahb1: Hertz(ahb_freq), 688 ahb1: Hertz(ahb_freq),
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
5use chrono::{NaiveDate, NaiveDateTime};
6use defmt::*;
7use embassy_executor::Spawner;
8use embassy_stm32::rcc::{self, ClockSrc, PLLClkDiv, PLLMul, PLLSource, PLLSrcDiv};
9use embassy_stm32::rtc::{Rtc, RtcConfig};
10use embassy_stm32::time::Hertz;
11use embassy_stm32::Config;
12use embassy_time::{Duration, Timer};
13use {defmt_rtt as _, panic_probe as _};
14
15#[embassy_executor::main]
16async 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}