Skip to content

Commit 72b1597

Browse files
committed
Fix PLL 48 MHz clock config.
1 parent 6c861b4 commit 72b1597

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

src/rcc.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -750,16 +750,24 @@ impl CFGR {
750750
let vco = clock_speed * pllconf.n as u32;
751751
let pllclk = vco / r;
752752

753-
let q = (vco + 48_000_000 - 1) / 48_000_000;
754-
pll48m1clk = Some((vco / q).Hz());
755-
756-
if self.clk48_source == Some(Clk48Source::Pll) {
753+
let q;
754+
(q, pll48m1clk) = if self.clk48_source == Some(Clk48Source::Pll) {
755+
let q = match (vco + 48_000_000 - 1) / 48_000_000 {
756+
0..=2 => PllDivider::Div2,
757+
3..=4 => PllDivider::Div4,
758+
5..=6 => PllDivider::Div6,
759+
7.. => PllDivider::Div8,
760+
};
761+
762+
let pll48m1clk = vco / q.to_division_factor();
763+
// TODO: Assert with tolerance.
757764
assert_eq!(pll48m1clk, 48_000_000);
758-
}
759765

760-
assert!(r <= 8); // Allowed max output divider
761-
assert!(pllconf.n >= 8); // Allowed min multiplier
762-
assert!(pllconf.n <= 86); // Allowed max multiplier
766+
(Some(q), Some(pll48m1clk.Hz()))
767+
} else {
768+
(None, None)
769+
};
770+
763771
assert!(clock_speed >= 4_000_000); // VCO input clock min
764772
assert!(clock_speed <= 16_000_000); // VCO input clock max
765773
assert!(vco >= 64_000_000); // VCO output min
@@ -783,7 +791,9 @@ impl CFGR {
783791
.plln()
784792
.bits(pllconf.n)
785793
.pllq()
786-
.bits(q as u8)
794+
.bits(q.unwrap_or(PllDivider::Div2).to_bits())
795+
.pllqen()
796+
.bit(q.is_some())
787797
});
788798

789799
rcc.cr.modify(|_, w| w.pllon().set_bit());
@@ -904,7 +914,8 @@ impl PllConfig {
904914
///
905915
/// PLL output = ((SourceClk / input_divider) * multiplier) / output_divider
906916
pub fn new(input_divider: u8, multiplier: u8, output_divider: PllDivider) -> Self {
907-
assert!(input_divider > 0);
917+
assert!(input_divider >= 1 && input_divider <= 8);
918+
assert!(multiplier >= 8 && multiplier <= 86);
908919

909920
PllConfig {
910921
m: input_divider - 1,

0 commit comments

Comments
 (0)