diff options
| author | Ben Gamari <[email protected]> | 2021-08-31 01:48:22 -0400 |
|---|---|---|
| committer | Ben Gamari <[email protected]> | 2021-09-28 21:19:10 -0400 |
| commit | 794798e225cbe9cf690938aaf802ad258dc996e2 (patch) | |
| tree | 608fce856069f881f7dee68a1ed3c89c6855bc33 | |
| parent | aa4069fe1032c68977bea9c006a3be83304d8427 (diff) | |
stm32g0: Add support for HSI divider
| -rw-r--r-- | embassy-stm32/src/rcc/g0/mod.rs | 42 |
1 files changed, 37 insertions, 5 deletions
diff --git a/embassy-stm32/src/rcc/g0/mod.rs b/embassy-stm32/src/rcc/g0/mod.rs index 863db709d..745779175 100644 --- a/embassy-stm32/src/rcc/g0/mod.rs +++ b/embassy-stm32/src/rcc/g0/mod.rs | |||
| @@ -18,10 +18,38 @@ pub const LSI_FREQ: u32 = 32_000; | |||
| 18 | #[derive(Clone, Copy)] | 18 | #[derive(Clone, Copy)] |
| 19 | pub enum ClockSrc { | 19 | pub enum ClockSrc { |
| 20 | HSE(Hertz), | 20 | HSE(Hertz), |
| 21 | HSI16, | 21 | HSI16(HSI16Prescaler), |
| 22 | LSI, | 22 | LSI, |
| 23 | } | 23 | } |
| 24 | 24 | ||
| 25 | #[derive(Clone, Copy)] | ||
| 26 | pub enum HSI16Prescaler { | ||
| 27 | NotDivided, | ||
| 28 | Div2, | ||
| 29 | Div4, | ||
| 30 | Div8, | ||
| 31 | Div16, | ||
| 32 | Div32, | ||
| 33 | Div64, | ||
| 34 | Div128, | ||
| 35 | } | ||
| 36 | |||
| 37 | impl Into<u8> for HSI16Prescaler { | ||
| 38 | fn into(self) -> u8 { | ||
| 39 | match self { | ||
| 40 | HSI16Prescaler::NotDivided => 0x00, | ||
| 41 | HSI16Prescaler::Div2 => 0x01, | ||
| 42 | HSI16Prescaler::Div4 => 0x02, | ||
| 43 | HSI16Prescaler::Div8 => 0x03, | ||
| 44 | HSI16Prescaler::Div16 => 0x04, | ||
| 45 | HSI16Prescaler::Div32 => 0x05, | ||
| 46 | HSI16Prescaler::Div64 => 0x06, | ||
| 47 | HSI16Prescaler::Div128 => 0x07, | ||
| 48 | } | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | |||
| 25 | impl Into<u8> for APBPrescaler { | 53 | impl Into<u8> for APBPrescaler { |
| 26 | fn into(self) -> u8 { | 54 | fn into(self) -> u8 { |
| 27 | match self { | 55 | match self { |
| @@ -61,7 +89,7 @@ impl Default for Config { | |||
| 61 | #[inline] | 89 | #[inline] |
| 62 | fn default() -> Config { | 90 | fn default() -> Config { |
| 63 | Config { | 91 | Config { |
| 64 | mux: ClockSrc::HSI16, | 92 | mux: ClockSrc::HSI16(HSI16Prescaler::NotDivided), |
| 65 | ahb_pre: AHBPrescaler::NotDivided, | 93 | ahb_pre: AHBPrescaler::NotDivided, |
| 66 | apb_pre: APBPrescaler::NotDivided, | 94 | apb_pre: APBPrescaler::NotDivided, |
| 67 | } | 95 | } |
| @@ -119,14 +147,18 @@ impl RccExt for RCC { | |||
| 119 | fn freeze(self, cfgr: Config) -> Clocks { | 147 | fn freeze(self, cfgr: Config) -> Clocks { |
| 120 | let rcc = pac::RCC; | 148 | let rcc = pac::RCC; |
| 121 | let (sys_clk, sw) = match cfgr.mux { | 149 | let (sys_clk, sw) = match cfgr.mux { |
| 122 | ClockSrc::HSI16 => { | 150 | ClockSrc::HSI16(div) => { |
| 123 | // Enable HSI16 | 151 | // Enable HSI16 |
| 152 | let div: u8 = div.into(); | ||
| 124 | unsafe { | 153 | unsafe { |
| 125 | rcc.cr().write(|w| w.set_hsion(true)); | 154 | rcc.cr().write(|w| { |
| 155 | w.set_hsidiv(div); | ||
| 156 | w.set_hsion(true) | ||
| 157 | }); | ||
| 126 | while !rcc.cr().read().hsirdy() {} | 158 | while !rcc.cr().read().hsirdy() {} |
| 127 | } | 159 | } |
| 128 | 160 | ||
| 129 | (HSI_FREQ, 0x00) | 161 | (HSI_FREQ >> div, 0x00) |
| 130 | } | 162 | } |
| 131 | ClockSrc::HSE(freq) => { | 163 | ClockSrc::HSE(freq) => { |
| 132 | // Enable HSE | 164 | // Enable HSE |
