diff options
| author | Dario Nieuwenhuis <[email protected]> | 2025-01-17 16:19:27 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-01-17 16:19:27 +0000 |
| commit | c39076724f052fed6781b056bb79c9fa576b87a3 (patch) | |
| tree | 15580a7500f92fe4702efcdacda0e257b566d1be | |
| parent | 169f9c27aa33a279aad51a92b52fc047a54b82af (diff) | |
| parent | 083f584f29b092a68f99120373dc6ec60fe6bc3d (diff) | |
Merge pull request #3781 from markus-k/stm32g0-hsisysdiv
stm32/rcc: add HSISYS support for g0
| -rw-r--r-- | embassy-stm32/src/rcc/g0.rs | 35 | ||||
| -rw-r--r-- | examples/stm32g0/src/bin/hf_timer.rs | 4 | ||||
| -rw-r--r-- | tests/stm32/src/common.rs | 4 |
3 files changed, 30 insertions, 13 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 @@ | |||
| 1 | use crate::pac::flash::vals::Latency; | 1 | use crate::pac::flash::vals::Latency; |
| 2 | pub use crate::pac::pwr::vals::Vos as VoltageRange; | 2 | pub use crate::pac::pwr::vals::Vos as VoltageRange; |
| 3 | pub use crate::pac::rcc::vals::{ | 3 | 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, | 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 | }; |
| 7 | use crate::pac::{FLASH, PWR, RCC}; | 7 | use crate::pac::{FLASH, PWR, RCC}; |
| 8 | use crate::time::Hertz; | 8 | use 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)] | ||
| 32 | pub 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)] |
| 60 | pub struct Config { | 66 | pub 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 | ||
| 120 | pub(crate) unsafe fn init(config: Config) { | 128 | pub(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 | ||
diff --git a/examples/stm32g0/src/bin/hf_timer.rs b/examples/stm32g0/src/bin/hf_timer.rs index 3ea06cdee..dfb6e0edc 100644 --- a/examples/stm32g0/src/bin/hf_timer.rs +++ b/examples/stm32g0/src/bin/hf_timer.rs | |||
| @@ -16,7 +16,9 @@ async fn main(_spawner: Spawner) { | |||
| 16 | let mut config = PeripheralConfig::default(); | 16 | let mut config = PeripheralConfig::default(); |
| 17 | { | 17 | { |
| 18 | use embassy_stm32::rcc::*; | 18 | use embassy_stm32::rcc::*; |
| 19 | config.rcc.hsi = true; | 19 | config.rcc.hsi = Some(Hsi { |
| 20 | sys_div: HsiSysDiv::DIV1, | ||
| 21 | }); | ||
| 20 | config.rcc.pll = Some(Pll { | 22 | config.rcc.pll = Some(Pll { |
| 21 | source: PllSource::HSI, | 23 | source: PllSource::HSI, |
| 22 | prediv: PllPreDiv::DIV1, | 24 | prediv: PllPreDiv::DIV1, |
diff --git a/tests/stm32/src/common.rs b/tests/stm32/src/common.rs index 935a41ed2..829f2cff0 100644 --- a/tests/stm32/src/common.rs +++ b/tests/stm32/src/common.rs | |||
| @@ -284,7 +284,9 @@ pub fn config() -> Config { | |||
| 284 | 284 | ||
| 285 | #[cfg(feature = "stm32g071rb")] | 285 | #[cfg(feature = "stm32g071rb")] |
| 286 | { | 286 | { |
| 287 | config.rcc.hsi = true; | 287 | config.rcc.hsi = Some(Hsi { |
| 288 | sys_div: HsiSysDiv::DIV1, | ||
| 289 | }); | ||
| 288 | config.rcc.pll = Some(Pll { | 290 | config.rcc.pll = Some(Pll { |
| 289 | source: PllSource::HSI, | 291 | source: PllSource::HSI, |
| 290 | prediv: PllPreDiv::DIV1, | 292 | prediv: PllPreDiv::DIV1, |
