From fca39892ac02673929af55bd44bd447ec524c957 Mon Sep 17 00:00:00 2001 From: Liam Kinne Date: Fri, 22 Nov 2024 19:06:44 +1000 Subject: [PATCH] allow selecting any clock source when freezing rcc configuration (#148) --- src/can.rs | 25 ------------------------- src/rcc/config.rs | 20 ++++++++++++++++++++ src/rcc/mod.rs | 6 ++++++ 3 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/can.rs b/src/can.rs index 697df9c4..398916a4 100644 --- a/src/can.rs +++ b/src/can.rs @@ -11,19 +11,6 @@ mod sealed { pub trait Rx {} } -/// Select an FDCAN Clock Source -#[allow(clippy::upper_case_acronyms)] -#[allow(dead_code)] -enum FdCanClockSource { - /// Select HSE as the FDCAN clock source - HSE = 0b00, - /// Select PLL "Q" clock as the FDCAN clock source - PLLQ = 0b01, - /// Select "P" clock as the FDCAN clock source - PCLK = 0b10, - //Reserved = 0b10, -} - /// Storage type for the CAN controller #[derive(Debug)] pub struct Can { @@ -55,18 +42,6 @@ where { Self::enable(&rcc.rb); - if rcc.rb.ccipr.read().fdcansel().is_hse() { - // Select P clock as FDCAN clock source - rcc.rb.ccipr.modify(|_, w| { - // This is sound, as `FdCanClockSource` only contains valid values for this field. - unsafe { - w.fdcansel().bits(FdCanClockSource::PCLK as u8); - } - - w - }); - } - self.fdcan_unchecked() } diff --git a/src/rcc/config.rs b/src/rcc/config.rs index a2ab3443..6998eafa 100644 --- a/src/rcc/config.rs +++ b/src/rcc/config.rs @@ -325,6 +325,18 @@ impl Default for PllConfig { } } +/// FDCAN Clock Source +#[allow(clippy::upper_case_acronyms)] +pub enum FdCanClockSource { + /// Select HSE as the FDCAN clock source + HSE = 0b00, + /// Select PLL "Q" clock as the FDCAN clock source + PLLQ = 0b01, + /// Select "P" clock as the FDCAN clock source + PCLK = 0b10, + //Reserved = 0b10, +} + /// Clocks configutation pub struct Config { pub(crate) sys_mux: SysClockSrc, @@ -335,6 +347,8 @@ pub struct Config { /// Required for f_sys > 150MHz pub(crate) enable_boost: bool, + + pub(crate) fdcansel: FdCanClockSource, } impl Config { @@ -379,6 +393,11 @@ impl Config { self.enable_boost = enable_boost; self } + + pub fn fdcan_src(mut self, mux: FdCanClockSource) -> Self { + self.fdcansel = mux; + self + } } impl Default for Config { @@ -390,6 +409,7 @@ impl Default for Config { apb1_psc: Prescaler::NotDivided, apb2_psc: Prescaler::NotDivided, enable_boost: false, + fdcansel: FdCanClockSource::HSE, } } } diff --git a/src/rcc/mod.rs b/src/rcc/mod.rs index c901ee7e..e5dab58c 100644 --- a/src/rcc/mod.rs +++ b/src/rcc/mod.rs @@ -216,6 +216,12 @@ impl Rcc { _ => apb2_freq * 2, }; + // Configure FDCAN clock source. + self.rb.ccipr.modify(|_, w| unsafe { + // This is sound, as `FdCanClockSource` only contains valid values for this field. + w.fdcansel().bits(rcc_cfg.fdcansel as u8) + }); + Rcc { rb: self.rb, clocks: Clocks {