aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxoviat <[email protected]>2023-09-06 17:48:12 -0500
committerxoviat <[email protected]>2023-09-06 17:48:12 -0500
commitc21ad04c2ed2e5bd0c0e53ded4be8d1c4c4a1bc3 (patch)
tree98b282bab2ec43a06aa202917bc91dfdd6ae5184
parentd097c99719f31aa68afdedde09149d6de1d0b600 (diff)
stm32: extract lse/lsi into bd mod
-rw-r--r--embassy-stm32/src/rcc/bd.rs6
-rw-r--r--embassy-stm32/src/rcc/l4.rs35
-rw-r--r--embassy-stm32/src/rcc/wb.rs14
-rw-r--r--embassy-stm32/src/rcc/wl.rs33
4 files changed, 9 insertions, 79 deletions
diff --git a/embassy-stm32/src/rcc/bd.rs b/embassy-stm32/src/rcc/bd.rs
index b4d21c35f..34b88458f 100644
--- a/embassy-stm32/src/rcc/bd.rs
+++ b/embassy-stm32/src/rcc/bd.rs
@@ -95,7 +95,7 @@ impl BackupDomain {
95 } 95 }
96 96
97 #[allow(dead_code, unused_variables)] 97 #[allow(dead_code, unused_variables)]
98 #[cfg(any(rtc_v2f4, rtc_v2f7, rtc_v2h7, rtc_v2l0, rtc_v2l1, rtc_v2l4, rtc_v2wb))] 98 #[cfg(any(rtc_v2f4, rtc_v2f7, rtc_v2h7, rtc_v2l0, rtc_v2l1, rtc_v2l4, rtc_v2wb, rtc_v3))]
99 pub fn enable_lse(lse_drive: LseDrive) { 99 pub fn enable_lse(lse_drive: LseDrive) {
100 Self::modify(|w| { 100 Self::modify(|w| {
101 #[cfg(any(rtc_v2f7, rtc_v2h7, rtc_v2l0, rtc_v2l4))] 101 #[cfg(any(rtc_v2f7, rtc_v2h7, rtc_v2l0, rtc_v2l4))]
@@ -107,7 +107,7 @@ impl BackupDomain {
107 } 107 }
108 108
109 #[allow(dead_code)] 109 #[allow(dead_code)]
110 #[cfg(any(rtc_v2f4, rtc_v2f7, rtc_v2h7, rtc_v2l0, rtc_v2l1, rtc_v2l4, rtc_v2wb))] 110 #[cfg(any(rtc_v2f4, rtc_v2f7, rtc_v2h7, rtc_v2l0, rtc_v2l1, rtc_v2l4, rtc_v2wb, rtc_v3))]
111 pub fn enable_lsi() { 111 pub fn enable_lsi() {
112 let csr = crate::pac::RCC.csr(); 112 let csr = crate::pac::RCC.csr();
113 113
@@ -146,7 +146,7 @@ impl BackupDomain {
146 }); 146 });
147 } 147 }
148 148
149 #[cfg(any(rtc_v2f4, rtc_v2f7, rtc_v2h7, rtc_v2l0, rtc_v2l1, rtc_v2l4, rtc_v2wb))] 149 #[cfg(any(rtc_v2f4, rtc_v2f7, rtc_v2h7, rtc_v2l0, rtc_v2l1, rtc_v2l4, rtc_v2wb, rtc_v3))]
150 #[allow(dead_code, unused_variables)] 150 #[allow(dead_code, unused_variables)]
151 pub fn configure_rtc(clock_source: RtcClockSource, lse_drive: Option<LseDrive>) { 151 pub fn configure_rtc(clock_source: RtcClockSource, lse_drive: Option<LseDrive>) {
152 match clock_source { 152 match clock_source {
diff --git a/embassy-stm32/src/rcc/l4.rs b/embassy-stm32/src/rcc/l4.rs
index c6bccfd26..0083ae5bb 100644
--- a/embassy-stm32/src/rcc/l4.rs
+++ b/embassy-stm32/src/rcc/l4.rs
@@ -2,13 +2,13 @@ use core::marker::PhantomData;
2 2
3use embassy_hal_internal::into_ref; 3use embassy_hal_internal::into_ref;
4use stm32_metapac::rcc::regs::Cfgr; 4use stm32_metapac::rcc::regs::Cfgr;
5use stm32_metapac::rcc::vals::{Lsedrv, Mcopre, Mcosel}; 5use stm32_metapac::rcc::vals::{Mcopre, Mcosel};
6 6
7pub use super::bus::{AHBPrescaler, APBPrescaler}; 7pub use super::bus::{AHBPrescaler, APBPrescaler};
8use crate::gpio::sealed::AFType; 8use crate::gpio::sealed::AFType;
9use crate::gpio::Speed; 9use crate::gpio::Speed;
10use crate::pac::rcc::vals::{Hpre, Msirange, Pllsrc, Ppre, Sw}; 10use crate::pac::rcc::vals::{Hpre, Msirange, Pllsrc, Ppre, Sw};
11use crate::pac::{FLASH, PWR, RCC}; 11use crate::pac::{FLASH, RCC};
12use crate::rcc::bd::{BackupDomain, RtcClockSource}; 12use crate::rcc::bd::{BackupDomain, RtcClockSource};
13use crate::rcc::{set_freqs, Clocks}; 13use crate::rcc::{set_freqs, Clocks};
14use crate::time::Hertz; 14use crate::time::Hertz;
@@ -407,36 +407,7 @@ pub(crate) unsafe fn init(config: Config) {
407 407
408 RCC.apb1enr1().modify(|w| w.set_pwren(true)); 408 RCC.apb1enr1().modify(|w| w.set_pwren(true));
409 409
410 match config.rtc_mux { 410 BackupDomain::configure_rtc(config.rtc_mux, None);
411 RtcClockSource::LSE => {
412 // 1. Unlock the backup domain
413 PWR.cr1().modify(|w| w.set_dbp(true));
414
415 // 2. Setup the LSE
416 RCC.bdcr().modify(|w| {
417 // Enable LSE
418 w.set_lseon(true);
419 // Max drive strength
420 // TODO: should probably be settable
421 w.set_lsedrv(Lsedrv::HIGH);
422 });
423
424 // Wait until LSE is running
425 while !RCC.bdcr().read().lserdy() {}
426
427 BackupDomain::set_rtc_clock_source(RtcClockSource::LSE);
428 }
429 RtcClockSource::LSI => {
430 // Turn on the internal 32 kHz LSI oscillator
431 RCC.csr().modify(|w| w.set_lsion(true));
432
433 // Wait until LSI is running
434 while !RCC.csr().read().lsirdy() {}
435
436 BackupDomain::set_rtc_clock_source(RtcClockSource::LSI);
437 }
438 _ => unreachable!(),
439 }
440 411
441 let (sys_clk, sw) = match config.mux { 412 let (sys_clk, sw) = match config.mux {
442 ClockSrc::MSI(range) => { 413 ClockSrc::MSI(range) => {
diff --git a/embassy-stm32/src/rcc/wb.rs b/embassy-stm32/src/rcc/wb.rs
index 6496b41e1..efd964642 100644
--- a/embassy-stm32/src/rcc/wb.rs
+++ b/embassy-stm32/src/rcc/wb.rs
@@ -293,18 +293,6 @@ pub(crate) fn configure_clocks(config: &Config) {
293 while !rcc.cr().read().hsirdy() {} 293 while !rcc.cr().read().hsirdy() {}
294 } 294 }
295 295
296 let needs_lsi = if let Some(rtc_mux) = &config.rtc {
297 *rtc_mux == RtcClockSource::LSI
298 } else {
299 false
300 };
301
302 if needs_lsi {
303 rcc.csr().modify(|w| w.set_lsi1on(true));
304
305 while !rcc.csr().read().lsi1rdy() {}
306 }
307
308 match &config.lse { 296 match &config.lse {
309 Some(_) => { 297 Some(_) => {
310 rcc.cfgr().modify(|w| w.set_stopwuck(true)); 298 rcc.cfgr().modify(|w| w.set_stopwuck(true));
@@ -378,5 +366,5 @@ pub(crate) fn configure_clocks(config: &Config) {
378 366
379 config 367 config
380 .rtc 368 .rtc
381 .map(|clock_source| BackupDomain::set_rtc_clock_source(clock_source)); 369 .map(|clock_source| BackupDomain::configure_rtc(clock_source, None));
382} 370}
diff --git a/embassy-stm32/src/rcc/wl.rs b/embassy-stm32/src/rcc/wl.rs
index e33690d10..b3ddbae64 100644
--- a/embassy-stm32/src/rcc/wl.rs
+++ b/embassy-stm32/src/rcc/wl.rs
@@ -1,5 +1,5 @@
1pub use super::bus::{AHBPrescaler, APBPrescaler, VoltageScale}; 1pub use super::bus::{AHBPrescaler, APBPrescaler, VoltageScale};
2use crate::pac::{FLASH, PWR, RCC}; 2use crate::pac::{FLASH, RCC};
3use crate::rcc::bd::{BackupDomain, RtcClockSource}; 3use crate::rcc::bd::{BackupDomain, RtcClockSource};
4use crate::rcc::{set_freqs, Clocks}; 4use crate::rcc::{set_freqs, Clocks};
5use crate::time::Hertz; 5use crate::time::Hertz;
@@ -208,36 +208,7 @@ pub(crate) unsafe fn init(config: Config) {
208 208
209 while FLASH.acr().read().latency() != ws {} 209 while FLASH.acr().read().latency() != ws {}
210 210
211 match config.rtc_mux { 211 BackupDomain::configure_rtc(config.rtc_mux, None);
212 RtcClockSource::LSE => {
213 // 1. Unlock the backup domain
214 PWR.cr1().modify(|w| w.set_dbp(true));
215
216 // 2. Setup the LSE
217 RCC.bdcr().modify(|w| {
218 // Enable LSE
219 w.set_lseon(true);
220 // Max drive strength
221 // TODO: should probably be settable
222 w.set_lsedrv(Lsedrv::High as u8); //---// PAM - should not be commented
223 });
224
225 // Wait until LSE is running
226 while !RCC.bdcr().read().lserdy() {}
227
228 BackupDomain::set_rtc_clock_source(RtcClockSource::LSE);
229 }
230 RtcClockSource::LSI => {
231 // Turn on the internal 32 kHz LSI oscillator
232 RCC.csr().modify(|w| w.set_lsion(true));
233
234 // Wait until LSI is running
235 while !RCC.csr().read().lsirdy() {}
236
237 BackupDomain::set_rtc_clock_source(RtcClockSource::LSI);
238 }
239 _ => unreachable!(),
240 }
241 212
242 match config.mux { 213 match config.mux {
243 ClockSrc::HSI16 => { 214 ClockSrc::HSI16 => {