Skip to content

Commit 14e1940

Browse files
committed
Use the MSI as default/fallback clock source for sysclk
On the one hand, this allows the user to also configure slower sysclk speeds, on the other hand, this also matches ST's default. It is still not possible to bypass the PLL when HSI or HSE should be used as clock source for sysclk but the same frequency configurations can be achieved by an appropriate PLL configuration which seems like a better trade-off than not offering slower clock speeds like before.
1 parent 0e34084 commit 14e1940

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/rcc.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,11 @@ impl CFGR {
634634
self.pll_config
635635
};
636636

637-
let sysclk = self.sysclk.unwrap_or(HSI);
637+
let sysclk = match (self.sysclk, self.msi) {
638+
(Some(sysclk), _) => sysclk,
639+
(None, Some(msi)) => msi.to_hertz().0,
640+
(None, None) => MsiFreq::RANGE4M.to_hertz().0,
641+
};
638642

639643
assert!(sysclk <= 80_000_000);
640644

@@ -711,6 +715,7 @@ impl CFGR {
711715
}
712716

713717
let sysclk_src_bits;
718+
let mut msi = self.msi;
714719
if let Some(pllconf) = pllconf {
715720
// Sanity-checks per RM0394, 6.4.4 PLL configuration register (RCC_PLLCFGR)
716721
let r = pllconf.r.to_division_factor();
@@ -763,13 +768,13 @@ impl CFGR {
763768
.bits(sysclk_src_bits)
764769
});
765770
} else {
766-
// use HSI as source
767-
sysclk_src_bits = 0b01;
768-
769-
rcc.cr.write(|w| w.hsion().set_bit());
770-
while rcc.cr.read().hsirdy().bit_is_clear() {}
771+
// use MSI as fallback source for sysclk
772+
sysclk_src_bits = 0b00;
773+
if msi.is_none() {
774+
msi = Some(MsiFreq::RANGE4M);
775+
}
771776

772-
// SW: HSI selected as system clock
777+
// SW: MSI selected as system clock
773778
rcc.cfgr.write(|w| unsafe {
774779
w.ppre2()
775780
.bits(ppre2_bits)
@@ -789,7 +794,7 @@ impl CFGR {
789794
//
790795

791796
// MSI always starts on reset
792-
if self.msi.is_none() {
797+
if msi.is_none() {
793798
rcc.cr
794799
.modify(|_, w| w.msion().clear_bit().msipllen().clear_bit())
795800
}
@@ -802,7 +807,7 @@ impl CFGR {
802807
hclk: Hertz(hclk),
803808
lsi: lsi_used,
804809
lse: self.lse.is_some(),
805-
msi: self.msi,
810+
msi,
806811
hsi48: self.hsi48,
807812
pclk1: Hertz(pclk1),
808813
pclk2: Hertz(pclk2),

0 commit comments

Comments
 (0)