Skip to content

Commit 47f1816

Browse files
committed
Support HSE bypass and CSS
1 parent 1d075f8 commit 47f1816

File tree

1 file changed

+36
-10
lines changed

1 file changed

+36
-10
lines changed

src/rcc.rs

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,7 @@ impl RccExt for RCC {
2121
apb1: APB1 { _0: () },
2222
apb2: APB2 { _0: () },
2323
bdcr: BDCR { _0: () },
24-
cfgr: CFGR {
25-
hse: None,
26-
hclk: None,
27-
pclk1: None,
28-
pclk2: None,
29-
sysclk: None,
30-
},
24+
cfgr: CFGR::default(),
3125
}
3226
}
3327
}
@@ -214,8 +208,11 @@ impl BDCR {
214208
/// let rcc = dp.RCC.constrain();
215209
/// use_cfgr(&mut rcc.cfgr)
216210
/// ```
211+
#[derive(Default)]
217212
pub struct CFGR {
218213
hse: Option<u32>,
214+
hse_bypass: bool,
215+
css: bool,
219216
hclk: Option<u32>,
220217
pclk1: Option<u32>,
221218
pclk2: Option<u32>,
@@ -286,13 +283,34 @@ fn into_pre_div(div: u8) -> cfgr2::PREDIV_A {
286283
}
287284

288285
impl CFGR {
289-
/// Uses HSE (external oscillator) instead of HSI (internal RC oscillator) as the clock source.
286+
/// Enable HSE (external clock) in crystal mode.
287+
/// Uses external oscillator instead of HSI (internal RC oscillator) as the clock source.
290288
/// Will result in a hang if an external oscillator is not connected or it fails to start.
291289
pub fn use_hse<F>(mut self, freq: F) -> Self
292290
where
293291
F: Into<Hertz>,
294292
{
295293
self.hse = Some(freq.into().0);
294+
self.hse_bypass = false;
295+
self
296+
}
297+
298+
/// Enable HSE (external clock) in bypass mode.
299+
/// Uses user provided clock instead of HSI (internal RC oscillator) as the clock source.
300+
/// Will result in a hang if an external clock source is not connected.
301+
pub fn use_hse_bypass<F>(mut self, freq: F) -> Self
302+
where
303+
F: Into<Hertz>,
304+
{
305+
self.hse = Some(freq.into().0);
306+
self.hse_bypass = true;
307+
self
308+
}
309+
310+
/// Enable CSS (Clock Security System).
311+
/// No effect if HSE is not enabled.
312+
pub fn enable_css(mut self) -> Self {
313+
self.css = true;
296314
self
297315
}
298316

@@ -584,9 +602,17 @@ impl CFGR {
584602

585603
let rcc = unsafe { &*RCC::ptr() };
586604

605+
// enable HSE and wait for it to be ready
587606
if self.hse.is_some() {
588-
// enable HSE and wait for it to be ready
589-
rcc.cr.modify(|_, w| w.hseon().on());
607+
rcc.cr.modify(|_, w| {
608+
if self.css {
609+
w.csson().on();
610+
}
611+
if self.hse_bypass {
612+
w.hsebyp().bypassed();
613+
}
614+
w.hseon().on()
615+
});
590616

591617
while rcc.cr.read().hserdy().is_not_ready() {}
592618
}

0 commit comments

Comments
 (0)