aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarkus Kasten <[email protected]>2025-01-17 12:07:43 +0100
committerMarkus Kasten <[email protected]>2025-01-17 12:07:43 +0100
commit5d26bca2e7b67097da54d4b5973931c6c9f63f03 (patch)
tree2a81cc2cd91c082a93c72ac829a4de1cbde685fd
parent4fbe1beefeee9dd6a3486739cacd7377f8785808 (diff)
stm32/rcc: add HSISYS support for g0
adds support to divide HSI clock, which allows running in low power mode on HSI
-rw-r--r--embassy-stm32/src/rcc/g0.rs35
1 files changed, 24 insertions, 11 deletions
diff --git a/embassy-stm32/src/rcc/g0.rs b/embassy-stm32/src/rcc/g0.rs
index 5da33720c..f55b18290 100644
--- a/embassy-stm32/src/rcc/g0.rs
+++ b/embassy-stm32/src/rcc/g0.rs
@@ -1,8 +1,8 @@
1use crate::pac::flash::vals::Latency; 1use crate::pac::flash::vals::Latency;
2pub use crate::pac::pwr::vals::Vos as VoltageRange; 2pub use crate::pac::pwr::vals::Vos as VoltageRange;
3pub use crate::pac::rcc::vals::{ 3pub use crate::pac::rcc::vals::{
4 Hpre as AHBPrescaler, Pllm as PllPreDiv, Plln as PllMul, Pllp as PllPDiv, Pllq as PllQDiv, Pllr as PllRDiv, 4 Hpre as AHBPrescaler, Hsidiv as HsiSysDiv, Pllm as PllPreDiv, Plln as PllMul, Pllp as PllPDiv, Pllq as PllQDiv,
5 Pllsrc as PllSource, Ppre as APBPrescaler, Sw as Sysclk, 5 Pllr as PllRDiv, Pllsrc as PllSource, Ppre as APBPrescaler, Sw as Sysclk,
6}; 6};
7use crate::pac::{FLASH, PWR, RCC}; 7use crate::pac::{FLASH, PWR, RCC};
8use crate::time::Hertz; 8use crate::time::Hertz;
@@ -28,6 +28,12 @@ pub struct Hse {
28 pub mode: HseMode, 28 pub mode: HseMode,
29} 29}
30 30
31#[derive(Clone, Copy, Eq, PartialEq)]
32pub struct Hsi {
33 /// Division factor for HSISYS clock. Default is 1.
34 pub sys_div: HsiSysDiv,
35}
36
31/// PLL Configuration 37/// PLL Configuration
32/// 38///
33/// Use this struct to configure the PLL source, input frequency, multiplication factor, and output 39/// Use this struct to configure the PLL source, input frequency, multiplication factor, and output
@@ -58,8 +64,8 @@ pub struct Pll {
58#[non_exhaustive] 64#[non_exhaustive]
59#[derive(Clone, Copy)] 65#[derive(Clone, Copy)]
60pub struct Config { 66pub struct Config {
61 /// HSI Enable 67 /// HSI Configuration
62 pub hsi: bool, 68 pub hsi: Option<Hsi>,
63 69
64 /// HSE Configuration 70 /// HSE Configuration
65 pub hse: Option<Hse>, 71 pub hse: Option<Hse>,
@@ -94,7 +100,9 @@ impl Default for Config {
94 #[inline] 100 #[inline]
95 fn default() -> Config { 101 fn default() -> Config {
96 Config { 102 Config {
97 hsi: true, 103 hsi: Some(Hsi {
104 sys_div: HsiSysDiv::DIV1,
105 }),
98 hse: None, 106 hse: None,
99 sys: Sysclk::HSI, 107 sys: Sysclk::HSI,
100 #[cfg(crs)] 108 #[cfg(crs)]
@@ -119,7 +127,12 @@ pub struct PllFreq {
119 127
120pub(crate) unsafe fn init(config: Config) { 128pub(crate) unsafe fn init(config: Config) {
121 // Turn on the HSI 129 // 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 });
123 while !RCC.cr().read().hsirdy() {} 136 while !RCC.cr().read().hsirdy() {}
124 137
125 // Use the HSI clock as system clock during the actual clock setup 138 // Use the HSI clock as system clock during the actual clock setup
@@ -127,9 +140,9 @@ pub(crate) unsafe fn init(config: Config) {
127 while RCC.cfgr().read().sws() != Sysclk::HSI {} 140 while RCC.cfgr().read().sws() != Sysclk::HSI {}
128 141
129 // Configure HSI 142 // Configure HSI
130 let hsi = match config.hsi { 143 let (hsi, hsisys) = match config.hsi {
131 false => None, 144 None => (None, None),
132 true => Some(HSI_FREQ), 145 Some(hsi) => (Some(HSI_FREQ), Some(HSI_FREQ / hsi.sys_div)),
133 }; 146 };
134 147
135 // Configure HSE 148 // Configure HSE
@@ -222,7 +235,7 @@ pub(crate) unsafe fn init(config: Config) {
222 .unwrap_or_default(); 235 .unwrap_or_default();
223 236
224 let sys = match config.sys { 237 let sys = match config.sys {
225 Sysclk::HSI => unwrap!(hsi), 238 Sysclk::HSI => unwrap!(hsisys),
226 Sysclk::HSE => unwrap!(hse), 239 Sysclk::HSE => unwrap!(hse),
227 Sysclk::PLL1_R => unwrap!(pll.pll_r), 240 Sysclk::PLL1_R => unwrap!(pll.pll_r),
228 _ => unreachable!(), 241 _ => unreachable!(),
@@ -264,7 +277,7 @@ pub(crate) unsafe fn init(config: Config) {
264 while RCC.cfgr().read().sws() != config.sys {} 277 while RCC.cfgr().read().sws() != config.sys {}
265 278
266 // Disable HSI if not used 279 // Disable HSI if not used
267 if !config.hsi { 280 if config.hsi.is_none() {
268 RCC.cr().modify(|w| w.set_hsion(false)); 281 RCC.cr().modify(|w| w.set_hsion(false));
269 } 282 }
270 283