aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoonas Javanainen <[email protected]>2022-04-09 17:40:04 +0300
committerJoonas Javanainen <[email protected]>2022-04-29 17:51:18 +0300
commit0cfe1dc9df9966ed36021869e9bf247aedfabc27 (patch)
treecfe9d8965a3a8905bfaa5f7c5831d22a32daa412
parent2f43969dd4ce295e38280e517043a6b674862f4d (diff)
Move HSE config out of main clock mux
This makes the configuration more flexible and closer to the underlying configuration register structure. For example, we could use HSI for the system clock, but use HSE to output a clock with MCO.
-rw-r--r--embassy-stm32/src/rcc/f2.rs32
1 files changed, 24 insertions, 8 deletions
diff --git a/embassy-stm32/src/rcc/f2.rs b/embassy-stm32/src/rcc/f2.rs
index bece046f8..e44613926 100644
--- a/embassy-stm32/src/rcc/f2.rs
+++ b/embassy-stm32/src/rcc/f2.rs
@@ -9,10 +9,16 @@ use crate::time::Hertz;
9/// HSI speed 9/// HSI speed
10pub const HSI: Hertz = Hertz(16_000_000); 10pub const HSI: Hertz = Hertz(16_000_000);
11 11
12#[derive(Clone, Copy)]
13pub struct HSEConfig {
14 pub frequency: Hertz,
15 pub source: HSESrc,
16}
17
12/// System clock mux source 18/// System clock mux source
13#[derive(Clone, Copy)] 19#[derive(Clone, Copy)]
14pub enum ClockSrc { 20pub enum ClockSrc {
15 HSE(Hertz, HSESrc), 21 HSE,
16 HSI, 22 HSI,
17} 23}
18 24
@@ -206,6 +212,7 @@ impl VoltageRange {
206 212
207/// Clocks configuration 213/// Clocks configuration
208pub struct Config { 214pub struct Config {
215 pub hse: Option<HSEConfig>,
209 pub mux: ClockSrc, 216 pub mux: ClockSrc,
210 pub voltage: VoltageRange, 217 pub voltage: VoltageRange,
211 pub ahb_pre: AHBPrescaler, 218 pub ahb_pre: AHBPrescaler,
@@ -217,6 +224,7 @@ impl Default for Config {
217 #[inline] 224 #[inline]
218 fn default() -> Config { 225 fn default() -> Config {
219 Config { 226 Config {
227 hse: None,
220 voltage: VoltageRange::Min1V8, 228 voltage: VoltageRange::Min1V8,
221 mux: ClockSrc::HSI, 229 mux: ClockSrc::HSI,
222 ahb_pre: AHBPrescaler::NotDivided, 230 ahb_pre: AHBPrescaler::NotDivided,
@@ -238,18 +246,26 @@ unsafe fn enable_hse(source: HSESrc) {
238 while !RCC.cr().read().hserdy() {} 246 while !RCC.cr().read().hserdy() {}
239} 247}
240 248
249#[inline]
250unsafe fn enable_hsi() {
251 RCC.cr().write(|w| w.set_hsion(true));
252 while !RCC.cr().read().hsirdy() {}
253}
254
241pub(crate) unsafe fn init(config: Config) { 255pub(crate) unsafe fn init(config: Config) {
256 if let Some(hse_config) = config.hse {
257 enable_hse(hse_config.source);
258 }
242 let (sys_clk, sw) = match config.mux { 259 let (sys_clk, sw) = match config.mux {
243 ClockSrc::HSI => { 260 ClockSrc::HSI => {
244 // Enable HSI 261 enable_hsi();
245 RCC.cr().write(|w| w.set_hsion(true));
246 while !RCC.cr().read().hsirdy() {}
247
248 (HSI, Sw::HSI) 262 (HSI, Sw::HSI)
249 } 263 }
250 ClockSrc::HSE(freq, source) => { 264 ClockSrc::HSE => {
251 enable_hse(source); 265 let hse_config = config
252 (freq, Sw::HSE) 266 .hse
267 .expect("HSE must be configured to be used as system clock");
268 (hse_config.frequency, Sw::HSE)
253 } 269 }
254 }; 270 };
255 // RM0033 Figure 9. Clock tree suggests max SYSCLK/HCLK is 168 MHz, but datasheet specifies PLL 271 // RM0033 Figure 9. Clock tree suggests max SYSCLK/HCLK is 168 MHz, but datasheet specifies PLL