|
| 1 | +//! Micro-Controller Out (MCO) pins |
| 2 | +
|
| 3 | +use super::Rcc; |
| 4 | +use crate::time::Hertz; |
| 5 | + |
| 6 | +pub use crate::stm32::rcc::cfgr1::MCO1SEL as MCO1; |
| 7 | +pub use crate::stm32::rcc::cfgr1::MCO2SEL as MCO2; |
| 8 | + |
| 9 | +/// Clock settings for Micro-Controller Out 1 (MCO1) |
| 10 | +pub struct MCO1Config { |
| 11 | + pub(super) source: MCO1, |
| 12 | + pub(super) frequency: Option<u32>, |
| 13 | +} |
| 14 | +impl Default for MCO1Config { |
| 15 | + fn default() -> MCO1Config { |
| 16 | + Self { |
| 17 | + source: MCO1::Hsi, |
| 18 | + frequency: None, |
| 19 | + } |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +/// Clock settings for Micro-Controller Out 2 (MCO2) |
| 24 | +pub struct MCO2Config { |
| 25 | + pub(super) source: MCO2, |
| 26 | + frequency: Option<u32>, |
| 27 | +} |
| 28 | +impl Default for MCO2Config { |
| 29 | + fn default() -> MCO2Config { |
| 30 | + Self { |
| 31 | + source: MCO2::Sysclk, |
| 32 | + frequency: None, |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +macro_rules! calculate_prescaler { |
| 38 | + () => { |
| 39 | + /// Calculates the prescaler and the resulting clock frequency |
| 40 | + pub(super) fn calculate_prescaler( |
| 41 | + &self, |
| 42 | + in_ck: u32, |
| 43 | + ) -> (u8, Option<Hertz>) { |
| 44 | + // Running? |
| 45 | + if let Some(freq) = self.frequency { |
| 46 | + // Calculate prescaler |
| 47 | + let prescaler = match (in_ck + freq - 1) / freq { |
| 48 | + 0 => unreachable!(), |
| 49 | + x @ 1..=15 => x, |
| 50 | + _ => { |
| 51 | + panic!("Clock is too fast to achieve {} Hz MCO!", freq) |
| 52 | + } |
| 53 | + }; |
| 54 | + |
| 55 | + (prescaler as u8, Some(Hertz::from_raw(in_ck / prescaler))) |
| 56 | + } else { |
| 57 | + // Disabled |
| 58 | + (0, None) |
| 59 | + } |
| 60 | + } |
| 61 | + }; |
| 62 | +} |
| 63 | +impl MCO1Config { |
| 64 | + calculate_prescaler!(); |
| 65 | +} |
| 66 | +impl MCO2Config { |
| 67 | + calculate_prescaler!(); |
| 68 | +} |
| 69 | + |
| 70 | +impl Rcc { |
| 71 | + /// Checks the MCO1 setup and sets further requirements in `config` if they |
| 72 | + /// are currently set to `None` |
| 73 | + /// |
| 74 | + /// # Panics |
| 75 | + /// |
| 76 | + /// Panics if the MCO1 setup is invalid, or if it is inconsistent with the |
| 77 | + /// rest of the `config` |
| 78 | + pub(super) fn mco1_setup(&mut self) { |
| 79 | + // HSI always runs |
| 80 | + |
| 81 | + // LSE unimplemented |
| 82 | + |
| 83 | + // HSE must be explicitly stated |
| 84 | + if self.config.mco1.source == MCO1::Hse { |
| 85 | + assert!( |
| 86 | + self.config.hse.is_some(), |
| 87 | + "HSE is required for MCO1. Explicitly state its frequency with `use_hse`" |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + // Set pll1_q_ck based on requirement |
| 92 | + if self.config.mco1.source == MCO1::Pll1Q |
| 93 | + && self.config.pll1.q_ck.is_none() |
| 94 | + { |
| 95 | + self.config.pll1.q_ck = self.config.mco1.frequency; |
| 96 | + } |
| 97 | + |
| 98 | + // HSI48 always runs |
| 99 | + } |
| 100 | + |
| 101 | + /// Checks the MCO2 setup and sets further requirements in `config` if they |
| 102 | + /// are currently set to `None` |
| 103 | + /// |
| 104 | + /// # Panics |
| 105 | + /// |
| 106 | + /// Panics if the MCO2 setup is invalid, or if it is inconsistent with the |
| 107 | + /// rest of the `config` |
| 108 | + pub(super) fn mco2_setup(&mut self) { |
| 109 | + // Set sysclk based on requirement |
| 110 | + if self.config.mco2.source == MCO2::Sysclk |
| 111 | + && self.config.sys_ck.is_none() |
| 112 | + { |
| 113 | + self.config.sys_ck = self.config.mco2.frequency; |
| 114 | + } |
| 115 | + |
| 116 | + // Set pll2_p_ck based on requirement |
| 117 | + if self.config.mco2.source == MCO2::Pll2P |
| 118 | + && self.config.pll2.p_ck.is_none() |
| 119 | + { |
| 120 | + self.config.pll2.p_ck = self.config.mco2.frequency; |
| 121 | + } |
| 122 | + |
| 123 | + // HSE must be explicitly stated |
| 124 | + if self.config.mco2.source == MCO2::Hse { |
| 125 | + assert!( |
| 126 | + self.config.hse.is_some(), |
| 127 | + "HSE is required for MCO2. Explicitly state its frequency with `use_hse`" |
| 128 | + ); |
| 129 | + } |
| 130 | + |
| 131 | + // Set pll1_p_ck based on requirement |
| 132 | + if self.config.mco2.source == MCO2::Pll1P |
| 133 | + && self.config.pll1.p_ck.is_none() |
| 134 | + { |
| 135 | + self.config.pll1.p_ck = self.config.mco2.frequency; |
| 136 | + } |
| 137 | + |
| 138 | + // CSI always runs |
| 139 | + |
| 140 | + // LSI unimplemented |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +macro_rules! mco1_setters { |
| 145 | + ($($mco_setter:ident: $source:ident $doc:expr),+) => { |
| 146 | + /// Setters for Micro-Controller Out 1 (MCO1) |
| 147 | + impl Rcc { |
| 148 | + $( |
| 149 | + /// Set the MCO1 output frequency. The clock is sourced from |
| 150 | + #[doc=$doc] |
| 151 | + /// |
| 152 | + /// This only enables the signal within the RCC block, it does |
| 153 | + /// not enable the MCO1 output pin itself (use the GPIO for |
| 154 | + /// that). |
| 155 | + #[must_use] |
| 156 | + pub fn $mco_setter(mut self, freq: Hertz) -> Self { |
| 157 | + self.config.mco1.source = MCO1::$source; |
| 158 | + self.config.mco1.frequency = Some(freq.raw()); |
| 159 | + self |
| 160 | + } |
| 161 | + )+ |
| 162 | + } |
| 163 | + } |
| 164 | +} |
| 165 | +mco1_setters! { |
| 166 | + mco1_from_hsi: Hsi "the HSI", |
| 167 | + //mco1_from_lse: Lse "the LSE", UNIMPLEMENTED |
| 168 | + mco1_from_hse: Hse "the HSE", |
| 169 | + mco1_from_pll1_q_ck: Pll1Q "pll1_q_ck", |
| 170 | + mco1_from_hsi48: Hsi48 "HSI48" |
| 171 | +} |
| 172 | + |
| 173 | +macro_rules! mco2_setters { |
| 174 | + ($($mco_setter:ident: $source:ident $doc:expr),+) => { |
| 175 | + /// Setters for Micro-Controller Out 2 (MCO2) |
| 176 | + impl Rcc { |
| 177 | + $( |
| 178 | + /// Set the MCO2 output frequency. The clock is sourced from |
| 179 | + #[doc=$doc] |
| 180 | + /// |
| 181 | + /// This only enables the signal within the RCC block, it does |
| 182 | + /// not enable the MCO2 output pin itself (use the GPIO for |
| 183 | + /// that). |
| 184 | + #[must_use] |
| 185 | + pub fn $mco_setter(mut self, freq: Hertz) -> Self { |
| 186 | + self.config.mco2.source = MCO2::$source; |
| 187 | + self.config.mco2.frequency = Some(freq.raw()); |
| 188 | + self |
| 189 | + } |
| 190 | + )+ |
| 191 | + } |
| 192 | + } |
| 193 | +} |
| 194 | +mco2_setters! { |
| 195 | + mco2_from_sys_ck: Sysclk "sys_ck", |
| 196 | + mco2_from_pll2_p_ck: Pll2P "pll2_p_ck", |
| 197 | + mco2_from_hse: Hse "the HSE", |
| 198 | + mco2_from_pll1_p_ck: Pll1P "pll1_p_ck", |
| 199 | + mco2_from_csi: Csi "CSI", |
| 200 | + mco2_from_lsi: Lsi "the LSI" |
| 201 | +} |
0 commit comments