Skip to content

Commit 04127d6

Browse files
committed
Fix PLL 48 MHz clock config.
1 parent 6c861b4 commit 04127d6

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

src/rcc.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -750,12 +750,23 @@ 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-
}
765+
766+
(Some(q), Some(pll48m1clk.Hz()))
767+
} else {
768+
(None, None)
769+
};
759770

760771
assert!(r <= 8); // Allowed max output divider
761772
assert!(pllconf.n >= 8); // Allowed min multiplier
@@ -783,7 +794,9 @@ impl CFGR {
783794
.plln()
784795
.bits(pllconf.n)
785796
.pllq()
786-
.bits(q as u8)
797+
.bits(q.unwrap_or(PllDivider::Div2).to_bits())
798+
.pllqen()
799+
.bit(q.is_some())
787800
});
788801

789802
rcc.cr.modify(|_, w| w.pllon().set_bit());
@@ -904,7 +917,8 @@ impl PllConfig {
904917
///
905918
/// PLL output = ((SourceClk / input_divider) * multiplier) / output_divider
906919
pub fn new(input_divider: u8, multiplier: u8, output_divider: PllDivider) -> Self {
907-
assert!(input_divider > 0);
920+
assert!(input_divider >= 1 && input_divider <= 8);
921+
assert!(multiplier >= 8 && multiplier <= 86);
908922

909923
PllConfig {
910924
m: input_divider - 1,

0 commit comments

Comments
 (0)