diff options
| author | Mathias <[email protected]> | 2023-05-25 21:15:18 +0200 |
|---|---|---|
| committer | Mathias <[email protected]> | 2023-05-25 21:28:32 +0200 |
| commit | 181c4c5311bbc955c911eeaef5d9fd761fd42b9f (patch) | |
| tree | 22b965bc3d2c4b90e1130d7ba463e38a6ae22bc9 | |
| parent | 6efcc9acaa15c6fa696c67b224297f9086732a75 (diff) | |
Add RTC MUX selection to embassy-stm32 L4 family, to select and setup LSE and/or LSI
| -rw-r--r-- | embassy-stm32/src/rcc/l4.rs | 45 |
1 files changed, 42 insertions, 3 deletions
diff --git a/embassy-stm32/src/rcc/l4.rs b/embassy-stm32/src/rcc/l4.rs index c1bf7d0cd..f8c1a6e06 100644 --- a/embassy-stm32/src/rcc/l4.rs +++ b/embassy-stm32/src/rcc/l4.rs | |||
| @@ -1,12 +1,12 @@ | |||
| 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::vals::{Mcopre, Mcosel}; | 4 | use stm32_metapac::rcc::vals::{Lsedrv, Mcopre, Mcosel}; |
| 5 | 5 | ||
| 6 | use crate::gpio::sealed::AFType; | 6 | use crate::gpio::sealed::AFType; |
| 7 | use crate::gpio::Speed; | 7 | use crate::gpio::Speed; |
| 8 | use crate::pac::rcc::vals::{Hpre, Msirange, Pllsrc, Ppre, Sw}; | 8 | use crate::pac::rcc::vals::{Hpre, Msirange, Pllsrc, Ppre, Sw}; |
| 9 | use crate::pac::{FLASH, RCC}; | 9 | use crate::pac::{FLASH, PWR, RCC}; |
| 10 | use crate::rcc::{set_freqs, Clocks}; | 10 | use crate::rcc::{set_freqs, Clocks}; |
| 11 | use crate::time::Hertz; | 11 | use crate::time::Hertz; |
| 12 | use crate::{peripherals, Peripheral}; | 12 | use crate::{peripherals, Peripheral}; |
| @@ -289,6 +289,7 @@ pub struct Config { | |||
| 289 | )>, | 289 | )>, |
| 290 | #[cfg(not(any(stm32l471, stm32l475, stm32l476, stm32l486)))] | 290 | #[cfg(not(any(stm32l471, stm32l475, stm32l476, stm32l486)))] |
| 291 | pub hsi48: bool, | 291 | pub hsi48: bool, |
| 292 | pub rtc_mux: RtcClockSource, | ||
| 292 | } | 293 | } |
| 293 | 294 | ||
| 294 | impl Default for Config { | 295 | impl Default for Config { |
| @@ -302,10 +303,16 @@ impl Default for Config { | |||
| 302 | pllsai1: None, | 303 | pllsai1: None, |
| 303 | #[cfg(not(any(stm32l471, stm32l475, stm32l476, stm32l486)))] | 304 | #[cfg(not(any(stm32l471, stm32l475, stm32l476, stm32l486)))] |
| 304 | hsi48: false, | 305 | hsi48: false, |
| 306 | rtc_mux: RtcClockSource::LSI32, | ||
| 305 | } | 307 | } |
| 306 | } | 308 | } |
| 307 | } | 309 | } |
| 308 | 310 | ||
| 311 | pub enum RtcClockSource { | ||
| 312 | LSE32, | ||
| 313 | LSI32, | ||
| 314 | } | ||
| 315 | |||
| 309 | pub enum McoClock { | 316 | pub enum McoClock { |
| 310 | DIV1, | 317 | DIV1, |
| 311 | DIV2, | 318 | DIV2, |
| @@ -432,15 +439,47 @@ impl<'d, T: McoInstance> Mco<'d, T> { | |||
| 432 | } | 439 | } |
| 433 | 440 | ||
| 434 | pub(crate) unsafe fn init(config: Config) { | 441 | pub(crate) unsafe fn init(config: Config) { |
| 442 | match config.rtc_mux { | ||
| 443 | RtcClockSource::LSE32 => { | ||
| 444 | // 1. Unlock the backup domain | ||
| 445 | PWR.cr1().modify(|w| w.set_dbp(true)); | ||
| 446 | |||
| 447 | // 2. Setup the LSE | ||
| 448 | RCC.bdcr().modify(|w| { | ||
| 449 | // Enable LSE | ||
| 450 | w.set_lseon(true); | ||
| 451 | // Max drive strength | ||
| 452 | // TODO: should probably be settable | ||
| 453 | w.set_lsedrv(Lsedrv::HIGH); | ||
| 454 | }); | ||
| 455 | |||
| 456 | // Wait until LSE is running | ||
| 457 | while !RCC.bdcr().read().lserdy() {} | ||
| 458 | } | ||
| 459 | RtcClockSource::LSI32 => { | ||
| 460 | // Turn on the internal 32 kHz LSI oscillator | ||
| 461 | RCC.csr().modify(|w| w.set_lsion(true)); | ||
| 462 | |||
| 463 | // Wait until LSI is running | ||
| 464 | while !RCC.csr().read().lsirdy() {} | ||
| 465 | } | ||
| 466 | } | ||
| 467 | |||
| 435 | let (sys_clk, sw) = match config.mux { | 468 | let (sys_clk, sw) = match config.mux { |
| 436 | ClockSrc::MSI(range) => { | 469 | ClockSrc::MSI(range) => { |
| 437 | // Enable MSI | 470 | // Enable MSI |
| 438 | RCC.cr().write(|w| { | 471 | RCC.cr().write(|w| { |
| 439 | let bits: Msirange = range.into(); | 472 | let bits: Msirange = range.into(); |
| 440 | w.set_msirange(bits); | 473 | w.set_msirange(bits); |
| 441 | w.set_msipllen(false); | ||
| 442 | w.set_msirgsel(true); | 474 | w.set_msirgsel(true); |
| 443 | w.set_msion(true); | 475 | w.set_msion(true); |
| 476 | |||
| 477 | if let RtcClockSource::LSE32 = config.rtc_mux { | ||
| 478 | // If LSE is enabled, enable calibration of MSI | ||
| 479 | w.set_msipllen(true); | ||
| 480 | } else { | ||
| 481 | w.set_msipllen(false); | ||
| 482 | } | ||
| 444 | }); | 483 | }); |
| 445 | while !RCC.cr().read().msirdy() {} | 484 | while !RCC.cr().read().msirdy() {} |
| 446 | 485 | ||
