Skip to content

Commit

Permalink
Merge pull request #2224 from cbruiz/stm32g0-usb_spi
Browse files Browse the repository at this point in the history
Add support for HSE Oscillator in stm32g0
  • Loading branch information
Dirbaio authored Dec 4, 2023
2 parents b867f9b + 09592ff commit 52fabbf
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions embassy-stm32/src/rcc/g0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,18 @@ use crate::time::Hertz;
/// HSI speed
pub const HSI_FREQ: Hertz = Hertz(16_000_000);

#[derive(Clone, Copy, Eq, PartialEq)]
pub enum HseMode {
/// crystal/ceramic oscillator (HSEBYP=0)
Oscillator,
/// external analog clock (low swing) (HSEBYP=1)
Bypass,
}

/// System clock mux source
#[derive(Clone, Copy)]
pub enum ClockSrc {
HSE(Hertz),
HSE(Hertz, HseMode),
HSI(HSIPrescaler),
PLL(PllConfig),
LSI,
Expand Down Expand Up @@ -61,7 +69,7 @@ impl Default for PllConfig {
#[derive(Clone, Copy, Eq, PartialEq)]
pub enum PllSource {
HSI,
HSE(Hertz),
HSE(Hertz, HseMode),
}

/// Clocks configutation
Expand Down Expand Up @@ -90,7 +98,7 @@ impl PllConfig {
pub(crate) fn init(self) -> Hertz {
let (src, input_freq) = match self.source {
PllSource::HSI => (vals::Pllsrc::HSI, HSI_FREQ),
PllSource::HSE(freq) => (vals::Pllsrc::HSE, freq),
PllSource::HSE(freq, _) => (vals::Pllsrc::HSE, freq),
};

let m_freq = input_freq / self.m;
Expand Down Expand Up @@ -125,8 +133,11 @@ impl PllConfig {
RCC.cr().write(|w| w.set_hsion(true));
while !RCC.cr().read().hsirdy() {}
}
PllSource::HSE(_) => {
RCC.cr().write(|w| w.set_hseon(true));
PllSource::HSE(_, mode) => {
RCC.cr().write(|w| {
w.set_hsebyp(mode != HseMode::Oscillator);
w.set_hseon(true);
});
while !RCC.cr().read().hserdy() {}
}
}
Expand Down Expand Up @@ -177,9 +188,12 @@ pub(crate) unsafe fn init(config: Config) {

(HSI_FREQ / div, Sw::HSI)
}
ClockSrc::HSE(freq) => {
ClockSrc::HSE(freq, mode) => {
// Enable HSE
RCC.cr().write(|w| w.set_hseon(true));
RCC.cr().write(|w| {
w.set_hseon(true);
w.set_hsebyp(mode != HseMode::Oscillator);
});
while !RCC.cr().read().hserdy() {}

(freq, Sw::HSE)
Expand Down

0 comments on commit 52fabbf

Please sign in to comment.