Skip to content

Commit 09c9770

Browse files
authored
Merge pull request #485 from chrisegerer/hse_bypass_bit
Allow to set HSE bypass bit in clock config
2 parents d50ccba + c2dc3be commit 09c9770

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1515
- Add dac
1616
- Fix flash error flag clearing
1717

18+
### Added
19+
20+
- Allow to set HSE bypass bit in `RCC` clock configuration register to use an external clock input on the `OSC_IN` pin
21+
1822
## [v0.10.0] - 2022-12-12
1923

2024
- `Timer`: adds `get_interrupt` to `Timer`

src/rcc.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ impl RccExt for RCC {
2222
Rcc {
2323
cfgr: CFGR {
2424
hse: None,
25+
hse_bypass: false,
2526
hclk: None,
2627
pclk1: None,
2728
pclk2: None,
@@ -110,6 +111,7 @@ const HSI: u32 = 8_000_000; // Hz
110111
#[derive(Debug, Default, PartialEq, Eq)]
111112
pub struct CFGR {
112113
hse: Option<u32>,
114+
hse_bypass: bool,
113115
hclk: Option<u32>,
114116
pclk1: Option<u32>,
115117
pclk2: Option<u32>,
@@ -127,6 +129,20 @@ impl CFGR {
127129
self
128130
}
129131

132+
/// Bypasses the high-speed external oscillator and uses an external clock input on the OSC_IN
133+
/// pin.
134+
///
135+
/// For this configuration, the OSC_IN pin should be connected to a clock source with a
136+
/// frequency specified in the call to use_hse(), and the OSC_OUT pin should not be connected.
137+
///
138+
/// This function has no effect unless use_hse() is also called.
139+
pub fn bypass_hse_oscillator(self) -> Self {
140+
Self {
141+
hse_bypass: true,
142+
..self
143+
}
144+
}
145+
130146
/// Sets the desired frequency for the HCLK clock
131147
#[inline(always)]
132148
pub fn hclk(mut self, freq: Hertz) -> Self {
@@ -208,7 +224,12 @@ impl CFGR {
208224
if cfg.hse.is_some() {
209225
// enable HSE and wait for it to be ready
210226

211-
rcc.cr.modify(|_, w| w.hseon().set_bit());
227+
rcc.cr.modify(|_, w| {
228+
if cfg.hse_bypass {
229+
w.hsebyp().bypassed();
230+
}
231+
w.hseon().set_bit()
232+
});
212233

213234
while rcc.cr.read().hserdy().bit_is_clear() {}
214235
}
@@ -478,6 +499,7 @@ pub trait Reset: RccBus {
478499
#[derive(Clone, Copy, Debug, PartialEq)]
479500
pub struct Config {
480501
pub hse: Option<u32>,
502+
pub hse_bypass: bool,
481503
pub pllmul: Option<u8>,
482504
pub hpre: HPre,
483505
pub ppre1: PPre,
@@ -491,6 +513,7 @@ impl Default for Config {
491513
fn default() -> Self {
492514
Self {
493515
hse: None,
516+
hse_bypass: false,
494517
pllmul: None,
495518
hpre: HPre::Div1,
496519
ppre1: PPre::Div1,
@@ -549,6 +572,7 @@ pub type AdcPre = rcc::cfgr::ADCPRE_A;
549572
impl Config {
550573
pub const fn from_cfgr(cfgr: CFGR) -> Self {
551574
let hse = cfgr.hse;
575+
let hse_bypass = cfgr.hse_bypass;
552576
let pllsrcclk = if let Some(hse) = hse { hse } else { HSI / 2 };
553577

554578
let pllmul = if let Some(sysclk) = cfgr.sysclk {
@@ -649,6 +673,7 @@ impl Config {
649673

650674
Self {
651675
hse,
676+
hse_bypass,
652677
pllmul: pllmul_bits,
653678
hpre: hpre_bits,
654679
ppre1: ppre1_bits,
@@ -730,6 +755,7 @@ fn rcc_config_usb() {
730755
let config = Config::from_cfgr(cfgr);
731756
let config_expected = Config {
732757
hse: Some(8_000_000),
758+
hse_bypass: false,
733759
pllmul: Some(4),
734760
hpre: HPre::Div1,
735761
ppre1: PPre::Div2,

0 commit comments

Comments
 (0)