aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxoviat <[email protected]>2023-09-05 23:13:57 +0000
committerGitHub <[email protected]>2023-09-05 23:13:57 +0000
commita80acf686e9e87aa27bc53054949df3cc2ea6efb (patch)
tree14c99598468c3b27c62825ea2fc203ce3f2f6229
parent9d76a6e933259be6783b2ba06b6cefa85f3a6732 (diff)
parent6770d8e8a690ca42a695105303784f4fc9796f6a (diff)
Merge pull request #1868 from MabezDev/f2-rtc-clocks
[F2] Allow the RTC clock source to be configured with the new RTC mechanism
-rw-r--r--embassy-stm32/src/rcc/f2.rs37
1 files changed, 36 insertions, 1 deletions
diff --git a/embassy-stm32/src/rcc/f2.rs b/embassy-stm32/src/rcc/f2.rs
index 9d9bc59fd..8b6556249 100644
--- a/embassy-stm32/src/rcc/f2.rs
+++ b/embassy-stm32/src/rcc/f2.rs
@@ -4,8 +4,10 @@ use core::ops::{Div, Mul};
4pub use super::bus::{AHBPrescaler, APBPrescaler}; 4pub use super::bus::{AHBPrescaler, APBPrescaler};
5use crate::pac::flash::vals::Latency; 5use crate::pac::flash::vals::Latency;
6use crate::pac::rcc::vals::{Pllp, Pllsrc, Sw}; 6use crate::pac::rcc::vals::{Pllp, Pllsrc, Sw};
7use crate::pac::{FLASH, RCC}; 7use crate::pac::{FLASH, PWR, RCC};
8use crate::rcc::bd::BackupDomain;
8use crate::rcc::{set_freqs, Clocks}; 9use crate::rcc::{set_freqs, Clocks};
10use crate::rtc::RtcClockSource;
9use crate::time::Hertz; 11use crate::time::Hertz;
10 12
11/// HSI speed 13/// HSI speed
@@ -288,6 +290,7 @@ pub struct Config {
288 pub pll_mux: PLLSrc, 290 pub pll_mux: PLLSrc,
289 pub pll: PLLConfig, 291 pub pll: PLLConfig,
290 pub mux: ClockSrc, 292 pub mux: ClockSrc,
293 pub rtc: Option<RtcClockSource>,
291 pub voltage: VoltageScale, 294 pub voltage: VoltageScale,
292 pub ahb_pre: AHBPrescaler, 295 pub ahb_pre: AHBPrescaler,
293 pub apb1_pre: APBPrescaler, 296 pub apb1_pre: APBPrescaler,
@@ -304,6 +307,7 @@ impl Default for Config {
304 pll: PLLConfig::default(), 307 pll: PLLConfig::default(),
305 voltage: VoltageScale::Scale3, 308 voltage: VoltageScale::Scale3,
306 mux: ClockSrc::HSI, 309 mux: ClockSrc::HSI,
310 rtc: None,
307 ahb_pre: AHBPrescaler::NotDivided, 311 ahb_pre: AHBPrescaler::NotDivided,
308 apb1_pre: APBPrescaler::NotDivided, 312 apb1_pre: APBPrescaler::NotDivided,
309 apb2_pre: APBPrescaler::NotDivided, 313 apb2_pre: APBPrescaler::NotDivided,
@@ -414,6 +418,37 @@ pub(crate) unsafe fn init(config: Config) {
414 RCC.cr().modify(|w| w.set_hsion(false)); 418 RCC.cr().modify(|w| w.set_hsion(false));
415 } 419 }
416 420
421 RCC.apb1enr().modify(|w| w.set_pwren(true));
422 PWR.cr().read();
423
424 match config.rtc {
425 Some(RtcClockSource::LSE) => {
426 // 1. Unlock the backup domain
427 PWR.cr().modify(|w| w.set_dbp(true));
428
429 // 2. Setup the LSE
430 RCC.bdcr().modify(|w| {
431 // Enable LSE
432 w.set_lseon(true);
433 });
434
435 // Wait until LSE is running
436 while !RCC.bdcr().read().lserdy() {}
437
438 BackupDomain::set_rtc_clock_source(RtcClockSource::LSE);
439 }
440 Some(RtcClockSource::LSI) => {
441 // Turn on the internal 32 kHz LSI oscillator
442 RCC.csr().modify(|w| w.set_lsion(true));
443
444 // Wait until LSI is running
445 while !RCC.csr().read().lsirdy() {}
446
447 BackupDomain::set_rtc_clock_source(RtcClockSource::LSI);
448 }
449 _ => todo!(),
450 }
451
417 set_freqs(Clocks { 452 set_freqs(Clocks {
418 sys: sys_clk, 453 sys: sys_clk,
419 ahb1: ahb_freq, 454 ahb1: ahb_freq,