diff options
| author | Dario Nieuwenhuis <[email protected]> | 2023-07-05 14:17:35 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-07-05 14:17:35 +0000 |
| commit | 46a46009520036cc4b4dd52e7e3bdc5191b67ea0 (patch) | |
| tree | 2df192a43a60a47e78eb69182557132af0ca69cf | |
| parent | 27992d9c07738ecdd15a5ca3b25863ada51c5787 (diff) | |
| parent | 1255d8a8ce2c5b2ed9b33a6c3d2ee5e0d6b99ba1 (diff) | |
Merge pull request #1607 from MathiasKoch/embassy-stm32/rcc-rtc-l4
feature(embassy-stm32): L4 enable APB to allow RTC to work
| -rw-r--r-- | embassy-stm32/src/rcc/l4.rs | 23 | ||||
| -rw-r--r-- | examples/stm32l4/Cargo.toml | 3 | ||||
| -rw-r--r-- | examples/stm32l4/src/bin/rtc.rs | 49 |
3 files changed, 74 insertions, 1 deletions
diff --git a/embassy-stm32/src/rcc/l4.rs b/embassy-stm32/src/rcc/l4.rs index 20cb8c91c..8a9b4adbf 100644 --- a/embassy-stm32/src/rcc/l4.rs +++ b/embassy-stm32/src/rcc/l4.rs | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | use core::marker::PhantomData; | 1 | use core::marker::PhantomData; |
| 2 | 2 | ||
| 3 | use embassy_hal_common::into_ref; | 3 | use embassy_hal_common::into_ref; |
| 4 | use stm32_metapac::rcc::regs::Cfgr; | ||
| 4 | use stm32_metapac::rcc::vals::{Lsedrv, Mcopre, Mcosel}; | 5 | use stm32_metapac::rcc::vals::{Lsedrv, Mcopre, Mcosel}; |
| 5 | 6 | ||
| 6 | use crate::gpio::sealed::AFType; | 7 | use crate::gpio::sealed::AFType; |
| @@ -439,6 +440,26 @@ impl<'d, T: McoInstance> Mco<'d, T> { | |||
| 439 | } | 440 | } |
| 440 | 441 | ||
| 441 | pub(crate) unsafe fn init(config: Config) { | 442 | pub(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/Cargo.toml b/examples/stm32l4/Cargo.toml index 7d066bb82..dca052c2f 100644 --- a/examples/stm32l4/Cargo.toml +++ b/examples/stm32l4/Cargo.toml | |||
| @@ -9,7 +9,7 @@ embassy-sync = { version = "0.2.0", path = "../../embassy-sync", features = ["de | |||
| 9 | embassy-executor = { version = "0.2.0", path = "../../embassy-executor", features = ["nightly", "arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] } | 9 | embassy-executor = { version = "0.2.0", path = "../../embassy-executor", features = ["nightly", "arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] } |
| 10 | embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } | 10 | embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } |
| 11 | embassy-embedded-hal = { version = "0.1.0", path = "../../embassy-embedded-hal" } | 11 | embassy-embedded-hal = { version = "0.1.0", path = "../../embassy-embedded-hal" } |
| 12 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "unstable-pac", "stm32l4s5vi", "time-driver-any", "exti", "unstable-traits"] } | 12 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "unstable-pac", "stm32l4s5vi", "time-driver-any", "exti", "unstable-traits", "chrono"] } |
| 13 | embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] } | 13 | embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] } |
| 14 | 14 | ||
| 15 | defmt = "0.3" | 15 | defmt = "0.3" |
| @@ -23,5 +23,6 @@ embedded-hal-async = { version = "=0.2.0-alpha.2" } | |||
| 23 | panic-probe = { version = "0.3", features = ["print-defmt"] } | 23 | panic-probe = { version = "0.3", features = ["print-defmt"] } |
| 24 | futures = { version = "0.3.17", default-features = false, features = ["async-await"] } | 24 | futures = { version = "0.3.17", default-features = false, features = ["async-await"] } |
| 25 | heapless = { version = "0.7.5", default-features = false } | 25 | heapless = { version = "0.7.5", default-features = false } |
| 26 | chrono = { version = "^0.4", default-features = false } | ||
| 26 | 27 | ||
| 27 | micromath = "2.0.0" | 28 | micromath = "2.0.0" |
diff --git a/examples/stm32l4/src/bin/rtc.rs b/examples/stm32l4/src/bin/rtc.rs new file mode 100644 index 000000000..d72d5ddb6 --- /dev/null +++ b/examples/stm32l4/src/bin/rtc.rs | |||
| @@ -0,0 +1,49 @@ | |||
| 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 | } | ||
