diff options
| author | Ulf Lilleengen <[email protected]> | 2021-05-26 13:29:11 +0200 |
|---|---|---|
| committer | Ulf Lilleengen <[email protected]> | 2021-05-26 13:29:11 +0200 |
| commit | 9743c59ad48996c4c53f0bfdfc8070b67e513ff0 (patch) | |
| tree | f5bbb5529be4a71283752b1b2943c156df5c3982 | |
| parent | 9a21d742734a225f08762c6367b24af364de23c4 (diff) | |
Simplify
| -rw-r--r-- | embassy-stm32/src/rcc/l0/mod.rs | 175 |
1 files changed, 84 insertions, 91 deletions
diff --git a/embassy-stm32/src/rcc/l0/mod.rs b/embassy-stm32/src/rcc/l0/mod.rs index 8fbd6038e..c756fad10 100644 --- a/embassy-stm32/src/rcc/l0/mod.rs +++ b/embassy-stm32/src/rcc/l0/mod.rs | |||
| @@ -103,6 +103,83 @@ pub enum PLLSource { | |||
| 103 | /// HSI speed | 103 | /// HSI speed |
| 104 | pub const HSI_FREQ: u32 = 16_000_000; | 104 | pub const HSI_FREQ: u32 = 16_000_000; |
| 105 | 105 | ||
| 106 | impl Into<Pllmul> for PLLMul { | ||
| 107 | fn into(self) -> Pllmul { | ||
| 108 | match self { | ||
| 109 | PLLMul::Mul3 => Pllmul::MUL3, | ||
| 110 | PLLMul::Mul4 => Pllmul::MUL4, | ||
| 111 | PLLMul::Mul6 => Pllmul::MUL6, | ||
| 112 | PLLMul::Mul8 => Pllmul::MUL8, | ||
| 113 | PLLMul::Mul12 => Pllmul::MUL12, | ||
| 114 | PLLMul::Mul16 => Pllmul::MUL16, | ||
| 115 | PLLMul::Mul24 => Pllmul::MUL24, | ||
| 116 | PLLMul::Mul32 => Pllmul::MUL32, | ||
| 117 | PLLMul::Mul48 => Pllmul::MUL48, | ||
| 118 | } | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | impl Into<Plldiv> for PLLDiv { | ||
| 123 | fn into(self) -> Plldiv { | ||
| 124 | match self { | ||
| 125 | PLLDiv::Div2 => Plldiv::DIV2, | ||
| 126 | PLLDiv::Div3 => Plldiv::DIV3, | ||
| 127 | PLLDiv::Div4 => Plldiv::DIV4, | ||
| 128 | } | ||
| 129 | } | ||
| 130 | } | ||
| 131 | |||
| 132 | impl Into<Pllsrc> for PLLSource { | ||
| 133 | fn into(self) -> Pllsrc { | ||
| 134 | match self { | ||
| 135 | PLLSource::HSI16 => Pllsrc::HSI16, | ||
| 136 | PLLSource::HSE(_) => Pllsrc::HSE, | ||
| 137 | } | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | impl Into<Ppre> for APBPrescaler { | ||
| 142 | fn into(self) -> Ppre { | ||
| 143 | match self { | ||
| 144 | APBPrescaler::NotDivided => Ppre::DIV1, | ||
| 145 | APBPrescaler::Div2 => Ppre::DIV2, | ||
| 146 | APBPrescaler::Div4 => Ppre::DIV4, | ||
| 147 | APBPrescaler::Div8 => Ppre::DIV8, | ||
| 148 | APBPrescaler::Div16 => Ppre::DIV16, | ||
| 149 | } | ||
| 150 | } | ||
| 151 | } | ||
| 152 | |||
| 153 | impl Into<Hpre> for AHBPrescaler { | ||
| 154 | fn into(self) -> Hpre { | ||
| 155 | match self { | ||
| 156 | AHBPrescaler::NotDivided => Hpre::DIV1, | ||
| 157 | AHBPrescaler::Div2 => Hpre::DIV2, | ||
| 158 | AHBPrescaler::Div4 => Hpre::DIV4, | ||
| 159 | AHBPrescaler::Div8 => Hpre::DIV8, | ||
| 160 | AHBPrescaler::Div16 => Hpre::DIV16, | ||
| 161 | AHBPrescaler::Div64 => Hpre::DIV64, | ||
| 162 | AHBPrescaler::Div128 => Hpre::DIV128, | ||
| 163 | AHBPrescaler::Div256 => Hpre::DIV256, | ||
| 164 | AHBPrescaler::Div512 => Hpre::DIV512, | ||
| 165 | } | ||
| 166 | } | ||
| 167 | } | ||
| 168 | |||
| 169 | impl Into<Msirange> for MSIRange { | ||
| 170 | fn into(self) -> Msirange { | ||
| 171 | match self { | ||
| 172 | MSIRange::Range0 => Msirange::RANGE0, | ||
| 173 | MSIRange::Range1 => Msirange::RANGE1, | ||
| 174 | MSIRange::Range2 => Msirange::RANGE2, | ||
| 175 | MSIRange::Range3 => Msirange::RANGE3, | ||
| 176 | MSIRange::Range4 => Msirange::RANGE4, | ||
| 177 | MSIRange::Range5 => Msirange::RANGE5, | ||
| 178 | MSIRange::Range6 => Msirange::RANGE6, | ||
| 179 | } | ||
| 180 | } | ||
| 181 | } | ||
| 182 | |||
| 106 | /// Clocks configutation | 183 | /// Clocks configutation |
| 107 | pub struct Config { | 184 | pub struct Config { |
| 108 | mux: ClockSrc, | 185 | mux: ClockSrc, |
| @@ -192,11 +269,6 @@ impl Config { | |||
| 192 | } | 269 | } |
| 193 | } | 270 | } |
| 194 | 271 | ||
| 195 | /// RCC peripheral | ||
| 196 | pub struct Rcc { | ||
| 197 | clocks: Clocks, | ||
| 198 | } | ||
| 199 | |||
| 200 | /* | 272 | /* |
| 201 | impl Rcc { | 273 | impl Rcc { |
| 202 | pub fn enable_lse(&mut self, _: &PWR) -> LSE { | 274 | pub fn enable_lse(&mut self, _: &PWR) -> LSE { |
| @@ -266,7 +338,7 @@ impl Rcc { | |||
| 266 | 338 | ||
| 267 | /// Extension trait that freezes the `RCC` peripheral with provided clocks configuration | 339 | /// Extension trait that freezes the `RCC` peripheral with provided clocks configuration |
| 268 | pub trait RccExt { | 340 | pub trait RccExt { |
| 269 | fn freeze(self, config: Config) -> Rcc; | 341 | fn freeze(&mut self, config: Config) -> Clocks; |
| 270 | } | 342 | } |
| 271 | 343 | ||
| 272 | impl RccExt for RCC { | 344 | impl RccExt for RCC { |
| @@ -274,7 +346,7 @@ impl RccExt for RCC { | |||
| 274 | // marking this function and all `Config` constructors and setters as `#[inline]`. | 346 | // marking this function and all `Config` constructors and setters as `#[inline]`. |
| 275 | // This saves ~900 Bytes for the `pwr.rs` example. | 347 | // This saves ~900 Bytes for the `pwr.rs` example. |
| 276 | #[inline] | 348 | #[inline] |
| 277 | fn freeze(self, cfgr: Config) -> Rcc { | 349 | fn freeze(&mut self, cfgr: Config) -> Clocks { |
| 278 | let rcc = pac::RCC; | 350 | let rcc = pac::RCC; |
| 279 | let (sys_clk, sw) = match cfgr.mux { | 351 | let (sys_clk, sw) = match cfgr.mux { |
| 280 | ClockSrc::MSI(range) => { | 352 | ClockSrc::MSI(range) => { |
| @@ -409,7 +481,7 @@ impl RccExt for RCC { | |||
| 409 | } | 481 | } |
| 410 | }; | 482 | }; |
| 411 | 483 | ||
| 412 | let clocks = Clocks { | 484 | Clocks { |
| 413 | source: cfgr.mux, | 485 | source: cfgr.mux, |
| 414 | sys_clk: sys_clk.hz(), | 486 | sys_clk: sys_clk.hz(), |
| 415 | ahb_clk: ahb_freq.hz(), | 487 | ahb_clk: ahb_freq.hz(), |
| @@ -419,9 +491,7 @@ impl RccExt for RCC { | |||
| 419 | apb2_tim_clk: apb2_tim_freq.hz(), | 491 | apb2_tim_clk: apb2_tim_freq.hz(), |
| 420 | apb1_pre, | 492 | apb1_pre, |
| 421 | apb2_pre, | 493 | apb2_pre, |
| 422 | }; | 494 | } |
| 423 | |||
| 424 | Rcc { clocks } | ||
| 425 | } | 495 | } |
| 426 | } | 496 | } |
| 427 | 497 | ||
| @@ -506,83 +576,6 @@ pub struct MCOEnabled(()); | |||
| 506 | #[derive(Clone, Copy)] | 576 | #[derive(Clone, Copy)] |
| 507 | pub struct LSE(()); | 577 | pub struct LSE(()); |
| 508 | 578 | ||
| 509 | impl Into<Pllmul> for PLLMul { | ||
| 510 | fn into(self) -> Pllmul { | ||
| 511 | match self { | ||
| 512 | PLLMul::Mul3 => Pllmul::MUL3, | ||
| 513 | PLLMul::Mul4 => Pllmul::MUL4, | ||
| 514 | PLLMul::Mul6 => Pllmul::MUL6, | ||
| 515 | PLLMul::Mul8 => Pllmul::MUL8, | ||
| 516 | PLLMul::Mul12 => Pllmul::MUL12, | ||
| 517 | PLLMul::Mul16 => Pllmul::MUL16, | ||
| 518 | PLLMul::Mul24 => Pllmul::MUL24, | ||
| 519 | PLLMul::Mul32 => Pllmul::MUL32, | ||
| 520 | PLLMul::Mul48 => Pllmul::MUL48, | ||
| 521 | } | ||
| 522 | } | ||
| 523 | } | ||
| 524 | |||
| 525 | impl Into<Plldiv> for PLLDiv { | ||
| 526 | fn into(self) -> Plldiv { | ||
| 527 | match self { | ||
| 528 | PLLDiv::Div2 => Plldiv::DIV2, | ||
| 529 | PLLDiv::Div3 => Plldiv::DIV3, | ||
| 530 | PLLDiv::Div4 => Plldiv::DIV4, | ||
| 531 | } | ||
| 532 | } | ||
| 533 | } | ||
| 534 | |||
| 535 | impl Into<Pllsrc> for PLLSource { | ||
| 536 | fn into(self) -> Pllsrc { | ||
| 537 | match self { | ||
| 538 | PLLSource::HSI16 => Pllsrc::HSI16, | ||
| 539 | PLLSource::HSE(_) => Pllsrc::HSE, | ||
| 540 | } | ||
| 541 | } | ||
| 542 | } | ||
| 543 | |||
| 544 | impl Into<Ppre> for APBPrescaler { | ||
| 545 | fn into(self) -> Ppre { | ||
| 546 | match self { | ||
| 547 | APBPrescaler::NotDivided => Ppre::DIV1, | ||
| 548 | APBPrescaler::Div2 => Ppre::DIV2, | ||
| 549 | APBPrescaler::Div4 => Ppre::DIV4, | ||
| 550 | APBPrescaler::Div8 => Ppre::DIV8, | ||
| 551 | APBPrescaler::Div16 => Ppre::DIV16, | ||
| 552 | } | ||
| 553 | } | ||
| 554 | } | ||
| 555 | |||
| 556 | impl Into<Hpre> for AHBPrescaler { | ||
| 557 | fn into(self) -> Hpre { | ||
| 558 | match self { | ||
| 559 | AHBPrescaler::NotDivided => Hpre::DIV1, | ||
| 560 | AHBPrescaler::Div2 => Hpre::DIV2, | ||
| 561 | AHBPrescaler::Div4 => Hpre::DIV4, | ||
| 562 | AHBPrescaler::Div8 => Hpre::DIV8, | ||
| 563 | AHBPrescaler::Div16 => Hpre::DIV16, | ||
| 564 | AHBPrescaler::Div64 => Hpre::DIV64, | ||
| 565 | AHBPrescaler::Div128 => Hpre::DIV128, | ||
| 566 | AHBPrescaler::Div256 => Hpre::DIV256, | ||
| 567 | AHBPrescaler::Div512 => Hpre::DIV512, | ||
| 568 | } | ||
| 569 | } | ||
| 570 | } | ||
| 571 | |||
| 572 | impl Into<Msirange> for MSIRange { | ||
| 573 | fn into(self) -> Msirange { | ||
| 574 | match self { | ||
| 575 | MSIRange::Range0 => Msirange::RANGE0, | ||
| 576 | MSIRange::Range1 => Msirange::RANGE1, | ||
| 577 | MSIRange::Range2 => Msirange::RANGE2, | ||
| 578 | MSIRange::Range3 => Msirange::RANGE3, | ||
| 579 | MSIRange::Range4 => Msirange::RANGE4, | ||
| 580 | MSIRange::Range5 => Msirange::RANGE5, | ||
| 581 | MSIRange::Range6 => Msirange::RANGE6, | ||
| 582 | } | ||
| 583 | } | ||
| 584 | } | ||
| 585 | |||
| 586 | // We use TIM2 as SystemClock | 579 | // We use TIM2 as SystemClock |
| 587 | pub type SystemClock = Clock<TIM2>; | 580 | pub type SystemClock = Clock<TIM2>; |
| 588 | 581 | ||
| @@ -598,8 +591,8 @@ pub unsafe fn init(config: Config) -> SystemClock { | |||
| 598 | w.set_iophen(enabled); | 591 | w.set_iophen(enabled); |
| 599 | }); | 592 | }); |
| 600 | 593 | ||
| 601 | let r = <peripherals::RCC as embassy::util::Steal>::steal(); | 594 | let mut r = <peripherals::RCC as embassy::util::Steal>::steal(); |
| 602 | let r = r.freeze(config); | 595 | let clocks = r.freeze(config); |
| 603 | 596 | ||
| 604 | rcc.apb1enr().modify(|w| w.set_tim2en(Lptimen::ENABLED)); | 597 | rcc.apb1enr().modify(|w| w.set_tim2en(Lptimen::ENABLED)); |
| 605 | rcc.apb1rstr().modify(|w| w.set_tim2rst(true)); | 598 | rcc.apb1rstr().modify(|w| w.set_tim2rst(true)); |
| @@ -608,6 +601,6 @@ pub unsafe fn init(config: Config) -> SystemClock { | |||
| 608 | Clock::new( | 601 | Clock::new( |
| 609 | <peripherals::TIM2 as embassy::util::Steal>::steal(), | 602 | <peripherals::TIM2 as embassy::util::Steal>::steal(), |
| 610 | interrupt::take!(TIM2), | 603 | interrupt::take!(TIM2), |
| 611 | r.clocks.apb1_clk(), | 604 | clocks.apb1_clk(), |
| 612 | ) | 605 | ) |
| 613 | } | 606 | } |
