Skip to content

Commit c390767

Browse files
authored
Merge pull request #3781 from markus-k/stm32g0-hsisysdiv
stm32/rcc: add HSISYS support for g0
2 parents 169f9c2 + 083f584 commit c390767

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

embassy-stm32/src/rcc/g0.rs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::pac::flash::vals::Latency;
22
pub use crate::pac::pwr::vals::Vos as VoltageRange;
33
pub use crate::pac::rcc::vals::{
4-
Hpre as AHBPrescaler, Pllm as PllPreDiv, Plln as PllMul, Pllp as PllPDiv, Pllq as PllQDiv, Pllr as PllRDiv,
5-
Pllsrc as PllSource, Ppre as APBPrescaler, Sw as Sysclk,
4+
Hpre as AHBPrescaler, Hsidiv as HsiSysDiv, Pllm as PllPreDiv, Plln as PllMul, Pllp as PllPDiv, Pllq as PllQDiv,
5+
Pllr as PllRDiv, Pllsrc as PllSource, Ppre as APBPrescaler, Sw as Sysclk,
66
};
77
use crate::pac::{FLASH, PWR, RCC};
88
use crate::time::Hertz;
@@ -28,6 +28,12 @@ pub struct Hse {
2828
pub mode: HseMode,
2929
}
3030

31+
#[derive(Clone, Copy, Eq, PartialEq)]
32+
pub struct Hsi {
33+
/// Division factor for HSISYS clock. Default is 1.
34+
pub sys_div: HsiSysDiv,
35+
}
36+
3137
/// PLL Configuration
3238
///
3339
/// Use this struct to configure the PLL source, input frequency, multiplication factor, and output
@@ -58,8 +64,8 @@ pub struct Pll {
5864
#[non_exhaustive]
5965
#[derive(Clone, Copy)]
6066
pub struct Config {
61-
/// HSI Enable
62-
pub hsi: bool,
67+
/// HSI Configuration
68+
pub hsi: Option<Hsi>,
6369

6470
/// HSE Configuration
6571
pub hse: Option<Hse>,
@@ -94,7 +100,9 @@ impl Default for Config {
94100
#[inline]
95101
fn default() -> Config {
96102
Config {
97-
hsi: true,
103+
hsi: Some(Hsi {
104+
sys_div: HsiSysDiv::DIV1,
105+
}),
98106
hse: None,
99107
sys: Sysclk::HSI,
100108
#[cfg(crs)]
@@ -119,17 +127,22 @@ pub struct PllFreq {
119127

120128
pub(crate) unsafe fn init(config: Config) {
121129
// Turn on the HSI
122-
RCC.cr().modify(|w| w.set_hsion(true));
130+
RCC.cr().modify(|w| {
131+
w.set_hsion(true);
132+
if let Some(hsi) = config.hsi {
133+
w.set_hsidiv(hsi.sys_div);
134+
}
135+
});
123136
while !RCC.cr().read().hsirdy() {}
124137

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

129142
// Configure HSI
130-
let hsi = match config.hsi {
131-
false => None,
132-
true => Some(HSI_FREQ),
143+
let (hsi, hsisys) = match config.hsi {
144+
None => (None, None),
145+
Some(hsi) => (Some(HSI_FREQ), Some(HSI_FREQ / hsi.sys_div)),
133146
};
134147

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

224237
let sys = match config.sys {
225-
Sysclk::HSI => unwrap!(hsi),
238+
Sysclk::HSI => unwrap!(hsisys),
226239
Sysclk::HSE => unwrap!(hse),
227240
Sysclk::PLL1_R => unwrap!(pll.pll_r),
228241
_ => unreachable!(),
@@ -264,7 +277,7 @@ pub(crate) unsafe fn init(config: Config) {
264277
while RCC.cfgr().read().sws() != config.sys {}
265278

266279
// Disable HSI if not used
267-
if !config.hsi {
280+
if config.hsi.is_none() {
268281
RCC.cr().modify(|w| w.set_hsion(false));
269282
}
270283

examples/stm32g0/src/bin/hf_timer.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ async fn main(_spawner: Spawner) {
1616
let mut config = PeripheralConfig::default();
1717
{
1818
use embassy_stm32::rcc::*;
19-
config.rcc.hsi = true;
19+
config.rcc.hsi = Some(Hsi {
20+
sys_div: HsiSysDiv::DIV1,
21+
});
2022
config.rcc.pll = Some(Pll {
2123
source: PllSource::HSI,
2224
prediv: PllPreDiv::DIV1,

tests/stm32/src/common.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,9 @@ pub fn config() -> Config {
284284

285285
#[cfg(feature = "stm32g071rb")]
286286
{
287-
config.rcc.hsi = true;
287+
config.rcc.hsi = Some(Hsi {
288+
sys_div: HsiSysDiv::DIV1,
289+
});
288290
config.rcc.pll = Some(Pll {
289291
source: PllSource::HSI,
290292
prediv: PllPreDiv::DIV1,

0 commit comments

Comments
 (0)