diff options
| author | everdrone <[email protected]> | 2025-09-27 16:00:39 +0200 |
|---|---|---|
| committer | everdrone <[email protected]> | 2025-09-27 16:00:39 +0200 |
| commit | 2674462a4d21b4901fceb32f6534160a44d1a564 (patch) | |
| tree | 5b26ac7b176ba7137cf14ad364821a7138fab96b | |
| parent | 791ce77dba0a9302ebb1ec0aaca0b121b6cf554e (diff) | |
Use nicer names in N6 RCC
| -rw-r--r-- | embassy-stm32/src/rcc/n6.rs | 81 |
1 files changed, 37 insertions, 44 deletions
diff --git a/embassy-stm32/src/rcc/n6.rs b/embassy-stm32/src/rcc/n6.rs index 08ea2fc4e..51fc8cdc8 100644 --- a/embassy-stm32/src/rcc/n6.rs +++ b/embassy-stm32/src/rcc/n6.rs | |||
| @@ -96,24 +96,14 @@ pub struct Msi { | |||
| 96 | } | 96 | } |
| 97 | 97 | ||
| 98 | #[derive(Clone, Copy, PartialEq)] | 98 | #[derive(Clone, Copy, PartialEq)] |
| 99 | pub struct PllOscillator { | ||
| 100 | pub source: Pllsel, | ||
| 101 | pub divm: Plldivm, | ||
| 102 | pub fractional: u32, | ||
| 103 | pub divn: u16, | ||
| 104 | pub divp1: Pllpdiv, | ||
| 105 | pub divp2: Pllpdiv, | ||
| 106 | } | ||
| 107 | |||
| 108 | #[derive(Clone, Copy, PartialEq)] | ||
| 109 | pub enum Pll { | 99 | pub enum Pll { |
| 110 | Oscillator { | 100 | Oscillator { |
| 111 | source: Pllsel, | 101 | source: Pllsel, |
| 112 | m: Plldivm, | 102 | divm: Plldivm, |
| 113 | fractional: u32, | 103 | fractional: u32, |
| 114 | n: u16, | 104 | divn: u16, |
| 115 | p1: Pllpdiv, | 105 | divp1: Pllpdiv, |
| 116 | p2: Pllpdiv, | 106 | divp2: Pllpdiv, |
| 117 | }, | 107 | }, |
| 118 | Bypass { | 108 | Bypass { |
| 119 | source: Pllsel, | 109 | source: Pllsel, |
| @@ -150,7 +140,10 @@ pub struct Config { | |||
| 150 | impl Config { | 140 | impl Config { |
| 151 | pub const fn new() -> Self { | 141 | pub const fn new() -> Self { |
| 152 | Self { | 142 | Self { |
| 153 | hsi: None, | 143 | hsi: Some(Hsi { |
| 144 | pre: HsiPrescaler::DIV1, | ||
| 145 | trim: HsiCalibration::from_bits(32), | ||
| 146 | }), | ||
| 154 | hse: None, | 147 | hse: None, |
| 155 | msi: None, | 148 | msi: None, |
| 156 | lsi: true, | 149 | lsi: true, |
| @@ -173,7 +166,7 @@ impl Config { | |||
| 173 | } | 166 | } |
| 174 | } | 167 | } |
| 175 | 168 | ||
| 176 | fn HAL_RCC_ClockConfig(config: Config) { | 169 | fn init_clocks(config: Config) { |
| 177 | // handle increasing dividers | 170 | // handle increasing dividers |
| 178 | debug!("configuring increasing pclk dividers"); | 171 | debug!("configuring increasing pclk dividers"); |
| 179 | RCC.cfgr2().modify(|w| { | 172 | RCC.cfgr2().modify(|w| { |
| @@ -203,7 +196,7 @@ fn HAL_RCC_ClockConfig(config: Config) { | |||
| 203 | match config.cpu { | 196 | match config.cpu { |
| 204 | CpuClk::Hse if !RCC.sr().read().hserdy() => panic!("HSE is not ready to be selected as CPU clock source"), | 197 | CpuClk::Hse if !RCC.sr().read().hserdy() => panic!("HSE is not ready to be selected as CPU clock source"), |
| 205 | CpuClk::Pll { source, divider } => { | 198 | CpuClk::Pll { source, divider } => { |
| 206 | if !RCC_IC_CheckPLLSources(RCC.iccfgr(0).read().icsel().to_bits(), source.to_bits()) { | 199 | if !pll_sources_ready(RCC.iccfgr(0).read().icsel().to_bits(), source.to_bits()) { |
| 207 | panic!("ICx clock switch requires both origin and destination clock source to be active") | 200 | panic!("ICx clock switch requires both origin and destination clock source to be active") |
| 208 | } | 201 | } |
| 209 | 202 | ||
| @@ -228,13 +221,13 @@ fn HAL_RCC_ClockConfig(config: Config) { | |||
| 228 | match config.sys { | 221 | match config.sys { |
| 229 | SysClk::Hse if !RCC.sr().read().hserdy() => panic!("HSE is not ready to be selected as CPU clock source"), | 222 | SysClk::Hse if !RCC.sr().read().hserdy() => panic!("HSE is not ready to be selected as CPU clock source"), |
| 230 | SysClk::Pll { ic2, ic6, ic11 } => { | 223 | SysClk::Pll { ic2, ic6, ic11 } => { |
| 231 | if !RCC_IC_CheckPLLSources(RCC.iccfgr(1).read().icsel().to_bits(), ic2.source.to_bits()) { | 224 | if !pll_sources_ready(RCC.iccfgr(1).read().icsel().to_bits(), ic2.source.to_bits()) { |
| 232 | panic!("IC2 clock switch requires both origin and destination clock source to be active") | 225 | panic!("IC2 clock switch requires both origin and destination clock source to be active") |
| 233 | } | 226 | } |
| 234 | if !RCC_IC_CheckPLLSources(RCC.iccfgr(5).read().icsel().to_bits(), ic6.source.to_bits()) { | 227 | if !pll_sources_ready(RCC.iccfgr(5).read().icsel().to_bits(), ic6.source.to_bits()) { |
| 235 | panic!("IC6 clock switch requires both origin and destination clock source to be active") | 228 | panic!("IC6 clock switch requires both origin and destination clock source to be active") |
| 236 | } | 229 | } |
| 237 | if !RCC_IC_CheckPLLSources(RCC.iccfgr(10).read().icsel().to_bits(), ic11.source.to_bits()) { | 230 | if !pll_sources_ready(RCC.iccfgr(10).read().icsel().to_bits(), ic11.source.to_bits()) { |
| 238 | panic!("IC11 clock switch requires both origin and destination clock source to be active") | 231 | panic!("IC11 clock switch requires both origin and destination clock source to be active") |
| 239 | } | 232 | } |
| 240 | 233 | ||
| @@ -292,7 +285,7 @@ fn HAL_RCC_ClockConfig(config: Config) { | |||
| 292 | }); | 285 | }); |
| 293 | } | 286 | } |
| 294 | 287 | ||
| 295 | fn RCC_PLL_Source_IsReady(source: u8) -> bool { | 288 | fn pll_source_ready(source: u8) -> bool { |
| 296 | match source { | 289 | match source { |
| 297 | 0x0 if !RCC.sr().read().pllrdy(0) && !RCC.pllcfgr1(0).read().pllbyp() => false, | 290 | 0x0 if !RCC.sr().read().pllrdy(0) && !RCC.pllcfgr1(0).read().pllbyp() => false, |
| 298 | 0x1 if !RCC.sr().read().pllrdy(1) && !RCC.pllcfgr1(1).read().pllbyp() => false, | 291 | 0x1 if !RCC.sr().read().pllrdy(1) && !RCC.pllcfgr1(1).read().pllbyp() => false, |
| @@ -302,8 +295,8 @@ fn RCC_PLL_Source_IsReady(source: u8) -> bool { | |||
| 302 | } | 295 | } |
| 303 | } | 296 | } |
| 304 | 297 | ||
| 305 | fn RCC_IC_CheckPLLSources(source1: u8, source2: u8) -> bool { | 298 | fn pll_sources_ready(source1: u8, source2: u8) -> bool { |
| 306 | RCC_PLL_Source_IsReady(source1) && RCC_PLL_Source_IsReady(source2) | 299 | pll_source_ready(source1) && pll_source_ready(source2) |
| 307 | } | 300 | } |
| 308 | 301 | ||
| 309 | impl Default for Config { | 302 | impl Default for Config { |
| @@ -325,7 +318,7 @@ fn power_supply_config(supply_config: SupplyConfig) { | |||
| 325 | while !PWR.voscr().read().actvosrdy() {} | 318 | while !PWR.voscr().read().actvosrdy() {} |
| 326 | } | 319 | } |
| 327 | 320 | ||
| 328 | fn pll_config(pll_config: Option<Pll>, pll_index: usize) { | 321 | fn init_pll(pll_config: Option<Pll>, pll_index: usize) { |
| 329 | let cfgr1 = RCC.pllcfgr1(pll_index); | 322 | let cfgr1 = RCC.pllcfgr1(pll_index); |
| 330 | let cfgr2 = RCC.pllcfgr2(pll_index); | 323 | let cfgr2 = RCC.pllcfgr2(pll_index); |
| 331 | let cfgr3 = RCC.pllcfgr3(pll_index); | 324 | let cfgr3 = RCC.pllcfgr3(pll_index); |
| @@ -333,11 +326,11 @@ fn pll_config(pll_config: Option<Pll>, pll_index: usize) { | |||
| 333 | match pll_config { | 326 | match pll_config { |
| 334 | Some(Pll::Oscillator { | 327 | Some(Pll::Oscillator { |
| 335 | source, | 328 | source, |
| 336 | m, | 329 | divm, |
| 337 | fractional, | 330 | fractional, |
| 338 | n, | 331 | divn, |
| 339 | p1, | 332 | divp1, |
| 340 | p2, | 333 | divp2, |
| 341 | }) => { | 334 | }) => { |
| 342 | // ensure pll is disabled | 335 | // ensure pll is disabled |
| 343 | RCC.ccr().write(|w| w.set_pllonc(pll_index, true)); | 336 | RCC.ccr().write(|w| w.set_pllonc(pll_index, true)); |
| @@ -350,12 +343,12 @@ fn pll_config(pll_config: Option<Pll>, pll_index: usize) { | |||
| 350 | // configure the pll clock source, mul and div factors | 343 | // configure the pll clock source, mul and div factors |
| 351 | cfgr1.modify(|w| { | 344 | cfgr1.modify(|w| { |
| 352 | w.set_pllsel(source); | 345 | w.set_pllsel(source); |
| 353 | w.set_plldivm(m); | 346 | w.set_plldivm(divm); |
| 354 | w.set_plldivn(n); | 347 | w.set_plldivn(divn); |
| 355 | }); | 348 | }); |
| 356 | cfgr3.modify(|w| { | 349 | cfgr3.modify(|w| { |
| 357 | w.set_pllpdiv1(p1); | 350 | w.set_pllpdiv1(divp1); |
| 358 | w.set_pllpdiv2(p2); | 351 | w.set_pllpdiv2(divp2); |
| 359 | }); | 352 | }); |
| 360 | // configure pll divnfrac | 353 | // configure pll divnfrac |
| 361 | cfgr2.modify(|w| w.set_plldivnfrac(fractional)); | 354 | cfgr2.modify(|w| w.set_plldivnfrac(fractional)); |
| @@ -380,7 +373,7 @@ fn pll_config(pll_config: Option<Pll>, pll_index: usize) { | |||
| 380 | } | 373 | } |
| 381 | Some(Pll::Bypass { source }) => { | 374 | Some(Pll::Bypass { source }) => { |
| 382 | // check if source is ready | 375 | // check if source is ready |
| 383 | if !RCC_PLL_Source_IsReady(source.to_bits()) { | 376 | if !pll_source_ready(source.to_bits()) { |
| 384 | panic!("PLL source is not ready") | 377 | panic!("PLL source is not ready") |
| 385 | } | 378 | } |
| 386 | 379 | ||
| @@ -405,7 +398,7 @@ fn pll_config(pll_config: Option<Pll>, pll_index: usize) { | |||
| 405 | } | 398 | } |
| 406 | } | 399 | } |
| 407 | 400 | ||
| 408 | fn HAL_RCC_OscConfig(config: Config) { | 401 | fn init_osc(config: Config) { |
| 409 | let (cpu_src, sys_src) = { | 402 | let (cpu_src, sys_src) = { |
| 410 | let reg = RCC.cfgr().read(); | 403 | let reg = RCC.cfgr().read(); |
| 411 | (reg.cpusws(), reg.syssws()) | 404 | (reg.cpusws(), reg.syssws()) |
| @@ -555,7 +548,7 @@ fn HAL_RCC_OscConfig(config: Config) { | |||
| 555 | debug!("configuring PLL{}", n + 1); | 548 | debug!("configuring PLL{}", n + 1); |
| 556 | let pll_ready = RCC.sr().read().pllrdy(n); | 549 | let pll_ready = RCC.sr().read().pllrdy(n); |
| 557 | 550 | ||
| 558 | if RCC_PLL_IsNewConfig(pll, 0) { | 551 | if is_new_pll_config(pll, 0) { |
| 559 | let ic1_src = RCC.iccfgr(0).read().icsel(); | 552 | let ic1_src = RCC.iccfgr(0).read().icsel(); |
| 560 | let ic2_src = RCC.iccfgr(1).read().icsel(); | 553 | let ic2_src = RCC.iccfgr(1).read().icsel(); |
| 561 | let ic6_src = RCC.iccfgr(5).read().icsel(); | 554 | let ic6_src = RCC.iccfgr(5).read().icsel(); |
| @@ -571,7 +564,7 @@ fn HAL_RCC_OscConfig(config: Config) { | |||
| 571 | panic!("PLL should not be disabled / reconfigured if used for IC2, IC6 or IC11 (sysclksrc)") | 564 | panic!("PLL should not be disabled / reconfigured if used for IC2, IC6 or IC11 (sysclksrc)") |
| 572 | } | 565 | } |
| 573 | 566 | ||
| 574 | pll_config(pll, 0); | 567 | init_pll(pll, 0); |
| 575 | } else if pll.is_some() && !pll_ready { | 568 | } else if pll.is_some() && !pll_ready { |
| 576 | debug!("PLL{} off", n + 1); | 569 | debug!("PLL{} off", n + 1); |
| 577 | RCC.csr().write(|w| w.pllons(n)); | 570 | RCC.csr().write(|w| w.pllons(n)); |
| @@ -580,7 +573,7 @@ fn HAL_RCC_OscConfig(config: Config) { | |||
| 580 | } | 573 | } |
| 581 | } | 574 | } |
| 582 | 575 | ||
| 583 | fn RCC_PLL_IsNewConfig(pll: Option<Pll>, pll_index: usize) -> bool { | 576 | fn is_new_pll_config(pll: Option<Pll>, pll_index: usize) -> bool { |
| 584 | let cfgr1 = RCC.pllcfgr1(pll_index).read(); | 577 | let cfgr1 = RCC.pllcfgr1(pll_index).read(); |
| 585 | let cfgr2 = RCC.pllcfgr2(pll_index).read(); | 578 | let cfgr2 = RCC.pllcfgr2(pll_index).read(); |
| 586 | let cfgr3 = RCC.pllcfgr3(pll_index).read(); | 579 | let cfgr3 = RCC.pllcfgr3(pll_index).read(); |
| @@ -603,11 +596,11 @@ fn RCC_PLL_IsNewConfig(pll: Option<Pll>, pll_index: usize) -> bool { | |||
| 603 | Some(Pll::Bypass { source }) => cfgr1.pllsel() != source, | 596 | Some(Pll::Bypass { source }) => cfgr1.pllsel() != source, |
| 604 | Some(Pll::Oscillator { | 597 | Some(Pll::Oscillator { |
| 605 | source, | 598 | source, |
| 606 | m, | 599 | divm: m, |
| 607 | fractional, | 600 | fractional, |
| 608 | n, | 601 | divn: n, |
| 609 | p1, | 602 | divp1: p1, |
| 610 | p2, | 603 | divp2: p2, |
| 611 | }) => { | 604 | }) => { |
| 612 | cfgr1.pllsel() != source | 605 | cfgr1.pllsel() != source |
| 613 | || cfgr1.plldivm() != m | 606 | || cfgr1.plldivm() != m |
| @@ -656,8 +649,8 @@ pub(crate) unsafe fn init(config: Config) { | |||
| 656 | 649 | ||
| 657 | power_supply_config(config.supply_config); | 650 | power_supply_config(config.supply_config); |
| 658 | 651 | ||
| 659 | HAL_RCC_OscConfig(config); | 652 | init_osc(config); |
| 660 | HAL_RCC_ClockConfig(config); | 653 | init_clocks(config); |
| 661 | 654 | ||
| 662 | let sys = match config.sys { | 655 | let sys = match config.sys { |
| 663 | SysClk::Hse => todo!(), | 656 | SysClk::Hse => todo!(), |
| @@ -680,7 +673,7 @@ pub(crate) unsafe fn init(config: Config) { | |||
| 680 | hclk5: None, | 673 | hclk5: None, |
| 681 | pclk1: None, | 674 | pclk1: None, |
| 682 | pclk2: None, | 675 | pclk2: None, |
| 683 | pclk2_tim: None, | 676 | pclk2_tim: Some(Hertz(1_000_000)), // FIXME: what is this?? |
| 684 | pclk4: None, | 677 | pclk4: None, |
| 685 | pclk5: None, | 678 | pclk5: None, |
| 686 | per: None, | 679 | per: None, |
