Skip to content

Commit 5d29ea5

Browse files
committed
Derive USDHC1_FREQUENCY from named constants
USDHC1_FREQUENCY was a hand-maintained literal. Derive it from PLL2_PFD2_FREQUENCY (itself derived from ccm::analog::pll2::FREQUENCY and the bootloader-set PFD2 fractional divider) and a named USDHC1_PODF divider. Add a compile-time assert to catch any future drift.
1 parent 994205d commit 5d29ea5

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

src/clock_power.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -297,12 +297,20 @@ pub fn prepare_clocks_and_power(
297297
.for_each(|locator| locator.set(ccm, clock_gate::ON));
298298
}
299299

300+
/// PLL2_PFD2 fractional divider. This value is set by the Teensy bootloader
301+
/// and assumed to be stable before user code runs.
302+
const PLL2_PFD2_FRAC: u32 = 24;
303+
const PLL2_PFD2_FREQUENCY: u32 = ccm::analog::pll2::FREQUENCY / PLL2_PFD2_FRAC * 18;
304+
305+
const USDHC1_CLK_DIVISOR: u32 = 2;
306+
300307
/// USDHC1 root clock frequency (Hz).
301308
///
302-
/// PLL2_PFD2 (396 MHz) / 2 = 198 MHz. The actual SD bus clock is
303-
/// further divided by the USDHC1 peripheral's internal SDCLKFS and
304-
/// DVS dividers during card initialization.
305-
pub const USDHC1_FREQUENCY: u32 = 198_000_000;
309+
/// Derived from PLL2_PFD2 (396 MHz) divided by [`USDHC1_CLK_DIVISOR`]. The actual
310+
/// SD bus clock is further divided by the USDHC1 peripheral's internal
311+
/// SDCLKFS and DVS dividers during card initialization.
312+
pub const USDHC1_FREQUENCY: u32 = PLL2_PFD2_FREQUENCY / USDHC1_CLK_DIVISOR;
313+
const _: () = assert!(USDHC1_FREQUENCY == 198_000_000);
306314

307315
/// Configure the USDHC1 clock root for the SD card slot.
308316
///
@@ -314,5 +322,5 @@ pub const USDHC1_FREQUENCY: u32 = 198_000_000;
314322
fn setup_usdhc1_clk(ccm: &mut ral::ccm::CCM) {
315323
clock_gate::usdhc::<1>().set(ccm, clock_gate::OFF);
316324
ral::modify_reg!(ral::ccm, ccm, CSCMR1, USDHC1_CLK_SEL: 0); // PLL2_PFD2
317-
ral::modify_reg!(ral::ccm, ccm, CSCDR1, USDHC1_PODF: 1); // divide by 2
325+
ral::modify_reg!(ral::ccm, ccm, CSCDR1, USDHC1_PODF: USDHC1_CLK_DIVISOR - 1);
318326
}

0 commit comments

Comments
 (0)