Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stm32/rcc: add HSISYS support for g0 #3781

Merged
merged 3 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions embassy-stm32/src/rcc/g0.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::pac::flash::vals::Latency;
pub use crate::pac::pwr::vals::Vos as VoltageRange;
pub use crate::pac::rcc::vals::{
Hpre as AHBPrescaler, Pllm as PllPreDiv, Plln as PllMul, Pllp as PllPDiv, Pllq as PllQDiv, Pllr as PllRDiv,
Pllsrc as PllSource, Ppre as APBPrescaler, Sw as Sysclk,
Hpre as AHBPrescaler, Hsidiv as HsiSysDiv, Pllm as PllPreDiv, Plln as PllMul, Pllp as PllPDiv, Pllq as PllQDiv,
Pllr as PllRDiv, Pllsrc as PllSource, Ppre as APBPrescaler, Sw as Sysclk,
};
use crate::pac::{FLASH, PWR, RCC};
use crate::time::Hertz;
Expand All @@ -28,6 +28,12 @@ pub struct Hse {
pub mode: HseMode,
}

#[derive(Clone, Copy, Eq, PartialEq)]
pub struct Hsi {
/// Division factor for HSISYS clock. Default is 1.
pub sys_div: HsiSysDiv,
}

/// PLL Configuration
///
/// Use this struct to configure the PLL source, input frequency, multiplication factor, and output
Expand Down Expand Up @@ -58,8 +64,8 @@ pub struct Pll {
#[non_exhaustive]
#[derive(Clone, Copy)]
pub struct Config {
/// HSI Enable
pub hsi: bool,
/// HSI Configuration
pub hsi: Option<Hsi>,

/// HSE Configuration
pub hse: Option<Hse>,
Expand Down Expand Up @@ -94,7 +100,9 @@ impl Default for Config {
#[inline]
fn default() -> Config {
Config {
hsi: true,
hsi: Some(Hsi {
sys_div: HsiSysDiv::DIV1,
}),
hse: None,
sys: Sysclk::HSI,
#[cfg(crs)]
Expand All @@ -119,17 +127,22 @@ pub struct PllFreq {

pub(crate) unsafe fn init(config: Config) {
// Turn on the HSI
RCC.cr().modify(|w| w.set_hsion(true));
RCC.cr().modify(|w| {
w.set_hsion(true);
if let Some(hsi) = config.hsi {
w.set_hsidiv(hsi.sys_div);
}
});
while !RCC.cr().read().hsirdy() {}

// Use the HSI clock as system clock during the actual clock setup
RCC.cfgr().modify(|w| w.set_sw(Sysclk::HSI));
while RCC.cfgr().read().sws() != Sysclk::HSI {}

// Configure HSI
let hsi = match config.hsi {
false => None,
true => Some(HSI_FREQ),
let (hsi, hsisys) = match config.hsi {
None => (None, None),
Some(hsi) => (Some(HSI_FREQ), Some(HSI_FREQ / hsi.sys_div)),
};

// Configure HSE
Expand Down Expand Up @@ -222,7 +235,7 @@ pub(crate) unsafe fn init(config: Config) {
.unwrap_or_default();

let sys = match config.sys {
Sysclk::HSI => unwrap!(hsi),
Sysclk::HSI => unwrap!(hsisys),
Sysclk::HSE => unwrap!(hse),
Sysclk::PLL1_R => unwrap!(pll.pll_r),
_ => unreachable!(),
Expand Down Expand Up @@ -264,7 +277,7 @@ pub(crate) unsafe fn init(config: Config) {
while RCC.cfgr().read().sws() != config.sys {}

// Disable HSI if not used
if !config.hsi {
if config.hsi.is_none() {
RCC.cr().modify(|w| w.set_hsion(false));
}

Expand Down
4 changes: 3 additions & 1 deletion examples/stm32g0/src/bin/hf_timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ async fn main(_spawner: Spawner) {
let mut config = PeripheralConfig::default();
{
use embassy_stm32::rcc::*;
config.rcc.hsi = true;
config.rcc.hsi = Some(Hsi {
sys_div: HsiSysDiv::DIV1,
});
config.rcc.pll = Some(Pll {
source: PllSource::HSI,
prediv: PllPreDiv::DIV1,
Expand Down
4 changes: 3 additions & 1 deletion tests/stm32/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,9 @@ pub fn config() -> Config {

#[cfg(feature = "stm32g071rb")]
{
config.rcc.hsi = true;
config.rcc.hsi = Some(Hsi {
sys_div: HsiSysDiv::DIV1,
});
config.rcc.pll = Some(Pll {
source: PllSource::HSI,
prediv: PllPreDiv::DIV1,
Expand Down
Loading