Skip to content

Commit 56c743f

Browse files
authored
Merge pull request #156 from Piroro-hs/rcc-hse-bypass
Add support for HSE bypass and CSS
2 parents 92d9357 + bd9f858 commit 56c743f

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2121
- SPI4 peripheral for supported
2222
devices. ([#99](https://github.com/stm32-rs/stm32f3xx-hal/pull/99))
2323
- Support for I2C transfer of more than 255 bytes, and 0 byte write ([#154](https://github.com/stm32-rs/stm32f3xx-hal/pull/154))
24+
- Support for HSE bypass and CSS ([#156](https://github.com/stm32-rs/stm32f3xx-hal/pull/156))
2425

2526
### Changed
2627

src/rcc.rs

Lines changed: 34 additions & 14 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
}
@@ -87,7 +81,7 @@ impl AHB {
8781
/// ```
8882
/// let dp = pac::Peripherals::take().unwrap();
8983
/// let rcc = dp.RCC.constrain();
90-
/// use_ahb(&mut rcc.apb1)
84+
/// use_apb1(&mut rcc.apb1)
9185
/// ```
9286
pub struct APB1 {
9387
_0: (),
@@ -112,7 +106,7 @@ impl APB1 {
112106
/// ```
113107
/// let dp = pac::Peripherals::take().unwrap();
114108
/// let rcc = dp.RCC.constrain();
115-
/// use_ahb(&mut rcc.apb2)
109+
/// use_apb2(&mut rcc.apb2)
116110
/// ```
117111
pub struct APB2 {
118112
_0: (),
@@ -212,10 +206,13 @@ impl BDCR {
212206
/// ```
213207
/// let dp = pac::Peripherals::take().unwrap();
214208
/// let rcc = dp.RCC.constrain();
215-
/// use_ahb(&mut rcc.cfgr)
209+
/// 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>,
@@ -296,6 +293,24 @@ impl CFGR {
296293
self
297294
}
298295

296+
/// Enable HSE bypass.
297+
/// Uses user provided clock signal instead of an external oscillator.
298+
/// OSC_OUT pin is free and can be used as GPIO.
299+
/// No effect if HSE is not enabled.
300+
pub fn bypass_hse(mut self) -> Self {
301+
self.hse_bypass = true;
302+
self
303+
}
304+
305+
/// Enable CSS (Clock Security System).
306+
/// System clock is automatically switched to HSI and an interrupt (CSSI) is generated
307+
/// when HSE clock failure is detected.
308+
/// No effect if HSE is not enabled.
309+
pub fn enable_css(mut self) -> Self {
310+
self.css = true;
311+
self
312+
}
313+
299314
/// Sets a frequency for the AHB bus
300315
pub fn hclk<F>(mut self, freq: F) -> Self
301316
where
@@ -376,10 +391,11 @@ impl CFGR {
376391
}
377392

378393
// PLL_MUL maximal value is 16
379-
assert!(divisor <= 16);
380-
// PRE_DIV maximal value is 16
381394
assert!(multiplier <= 16);
382395

396+
// PRE_DIV maximal value is 16
397+
assert!(divisor <= 16);
398+
383399
(multiplier, Some(divisor))
384400
}
385401
// HSI division is always divided by 2 and has no adjustable division
@@ -583,9 +599,13 @@ impl CFGR {
583599

584600
let rcc = unsafe { &*RCC::ptr() };
585601

602+
// enable HSE and wait for it to be ready
586603
if self.hse.is_some() {
587-
// enable HSE and wait for it to be ready
588-
rcc.cr.modify(|_, w| w.hseon().on());
604+
rcc.cr.modify(|_, w| {
605+
w.hsebyp().bit(self.hse_bypass);
606+
w.csson().bit(self.css);
607+
w.hseon().on()
608+
});
589609

590610
while rcc.cr.read().hserdy().is_not_ready() {}
591611
}

0 commit comments

Comments
 (0)