aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew W. Samsonoff <[email protected]>2022-03-02 18:35:53 -0500
committerDario Nieuwenhuis <[email protected]>2022-03-04 18:03:55 +0100
commit047ff9a2f2b1f0c4b24fe093fe94fbb5730905e4 (patch)
tree0f46942cf6758e43041e435e4f38fff169e3ae17
parent35636953b2048394733eaff95fad17970788d08a (diff)
Use new stm32-data registers and fix AHB clock calculation
The original code for calculating the AHB clock did not account for the gap in prescaler values (32 is not an available value.) The bit shifting and math has been replaced by a `match`.
-rw-r--r--embassy-stm32/src/rcc/g0.rs87
1 files changed, 46 insertions, 41 deletions
diff --git a/embassy-stm32/src/rcc/g0.rs b/embassy-stm32/src/rcc/g0.rs
index 71d44fd3c..b78cfdb4c 100644
--- a/embassy-stm32/src/rcc/g0.rs
+++ b/embassy-stm32/src/rcc/g0.rs
@@ -1,4 +1,5 @@
1use crate::pac::{PWR, RCC}; 1use crate::pac::{PWR, RCC};
2use crate::pac::rcc::vals::{Hsidiv, Hpre, Ppre, Sw};
2use crate::rcc::{set_freqs, Clocks}; 3use crate::rcc::{set_freqs, Clocks};
3use crate::time::Hertz; 4use crate::time::Hertz;
4use crate::time::U32Ext; 5use crate::time::U32Ext;
@@ -29,17 +30,17 @@ pub enum HSI16Prescaler {
29 Div128, 30 Div128,
30} 31}
31 32
32impl Into<u8> for HSI16Prescaler { 33impl Into<Hsidiv> for HSI16Prescaler {
33 fn into(self) -> u8 { 34 fn into(self) -> Hsidiv {
34 match self { 35 match self {
35 HSI16Prescaler::NotDivided => 0x00, 36 HSI16Prescaler::NotDivided => Hsidiv::DIV1,
36 HSI16Prescaler::Div2 => 0x01, 37 HSI16Prescaler::Div2 => Hsidiv::DIV2,
37 HSI16Prescaler::Div4 => 0x02, 38 HSI16Prescaler::Div4 => Hsidiv::DIV4,
38 HSI16Prescaler::Div8 => 0x03, 39 HSI16Prescaler::Div8 => Hsidiv::DIV8,
39 HSI16Prescaler::Div16 => 0x04, 40 HSI16Prescaler::Div16 => Hsidiv::DIV16,
40 HSI16Prescaler::Div32 => 0x05, 41 HSI16Prescaler::Div32 => Hsidiv::DIV32,
41 HSI16Prescaler::Div64 => 0x06, 42 HSI16Prescaler::Div64 => Hsidiv::DIV64,
42 HSI16Prescaler::Div128 => 0x07, 43 HSI16Prescaler::Div128 => Hsidiv::DIV128,
43 } 44 }
44 } 45 }
45} 46}
@@ -68,30 +69,30 @@ pub enum APBPrescaler {
68 Div16, 69 Div16,
69} 70}
70 71
71impl Into<u8> for APBPrescaler { 72impl Into<Ppre> for APBPrescaler {
72 fn into(self) -> u8 { 73 fn into(self) -> Ppre {
73 match self { 74 match self {
74 APBPrescaler::NotDivided => 1, 75 APBPrescaler::NotDivided => Ppre::DIV1,
75 APBPrescaler::Div2 => 0x04, 76 APBPrescaler::Div2 => Ppre::DIV2,
76 APBPrescaler::Div4 => 0x05, 77 APBPrescaler::Div4 => Ppre::DIV4,
77 APBPrescaler::Div8 => 0x06, 78 APBPrescaler::Div8 => Ppre::DIV8,
78 APBPrescaler::Div16 => 0x07, 79 APBPrescaler::Div16 => Ppre::DIV16,
79 } 80 }
80 } 81 }
81} 82}
82 83
83impl Into<u8> for AHBPrescaler { 84impl Into<Hpre> for AHBPrescaler {
84 fn into(self) -> u8 { 85 fn into(self) -> Hpre {
85 match self { 86 match self {
86 AHBPrescaler::NotDivided => 1, 87 AHBPrescaler::NotDivided => Hpre::DIV1,
87 AHBPrescaler::Div2 => 0x08, 88 AHBPrescaler::Div2 => Hpre::DIV2,
88 AHBPrescaler::Div4 => 0x09, 89 AHBPrescaler::Div4 => Hpre::DIV4,
89 AHBPrescaler::Div8 => 0x0a, 90 AHBPrescaler::Div8 => Hpre::DIV8,
90 AHBPrescaler::Div16 => 0x0b, 91 AHBPrescaler::Div16 => Hpre::DIV16,
91 AHBPrescaler::Div64 => 0x0c, 92 AHBPrescaler::Div64 => Hpre::DIV64,
92 AHBPrescaler::Div128 => 0x0d, 93 AHBPrescaler::Div128 => Hpre::DIV128,
93 AHBPrescaler::Div256 => 0x0e, 94 AHBPrescaler::Div256 => Hpre::DIV256,
94 AHBPrescaler::Div512 => 0x0f, 95 AHBPrescaler::Div512 => Hpre::DIV512,
95 } 96 }
96 } 97 }
97} 98}
@@ -120,27 +121,27 @@ pub(crate) unsafe fn init(config: Config) {
120 let (sys_clk, sw) = match config.mux { 121 let (sys_clk, sw) = match config.mux {
121 ClockSrc::HSI16(div) => { 122 ClockSrc::HSI16(div) => {
122 // Enable HSI16 123 // Enable HSI16
123 let div: u8 = div.into(); 124 let div: Hsidiv = div.into();
124 RCC.cr().write(|w| { 125 RCC.cr().write(|w| {
125 w.set_hsidiv(div); 126 w.set_hsidiv(div);
126 w.set_hsion(true) 127 w.set_hsion(true)
127 }); 128 });
128 while !RCC.cr().read().hsirdy() {} 129 while !RCC.cr().read().hsirdy() {}
129 130
130 (HSI_FREQ >> div, 0x00) 131 (HSI_FREQ >> div.0, Sw::HSI)
131 } 132 }
132 ClockSrc::HSE(freq) => { 133 ClockSrc::HSE(freq) => {
133 // Enable HSE 134 // Enable HSE
134 RCC.cr().write(|w| w.set_hseon(true)); 135 RCC.cr().write(|w| w.set_hseon(true));
135 while !RCC.cr().read().hserdy() {} 136 while !RCC.cr().read().hserdy() {}
136 137
137 (freq.0, 0x01) 138 (freq.0, Sw::HSE)
138 } 139 }
139 ClockSrc::LSI => { 140 ClockSrc::LSI => {
140 // Enable LSI 141 // Enable LSI
141 RCC.csr().write(|w| w.set_lsion(true)); 142 RCC.csr().write(|w| w.set_lsion(true));
142 while !RCC.csr().read().lsirdy() {} 143 while !RCC.csr().read().lsirdy() {}
143 (LSI_FREQ, 0x03) 144 (LSI_FREQ, Sw::LSI)
144 } 145 }
145 }; 146 };
146 147
@@ -150,20 +151,24 @@ pub(crate) unsafe fn init(config: Config) {
150 w.set_ppre(config.apb_pre.into()); 151 w.set_ppre(config.apb_pre.into());
151 }); 152 });
152 153
153 let ahb_freq: u32 = match config.ahb_pre { 154 let ahb_div = match config.ahb_pre {
154 AHBPrescaler::NotDivided => sys_clk, 155 AHBPrescaler::NotDivided => 1,
155 pre => { 156 AHBPrescaler::Div2 => 2,
156 let pre: u8 = pre.into(); 157 AHBPrescaler::Div4 => 4,
157 let pre = 1 << (pre as u32 - 7); 158 AHBPrescaler::Div8 => 8,
158 sys_clk / pre 159 AHBPrescaler::Div16 => 16,
159 } 160 AHBPrescaler::Div64 => 64,
161 AHBPrescaler::Div128 => 128,
162 AHBPrescaler::Div256 => 256,
163 AHBPrescaler::Div512 => 512,
160 }; 164 };
165 let ahb_freq = sys_clk / ahb_div;
161 166
162 let (apb_freq, apb_tim_freq) = match config.apb_pre { 167 let (apb_freq, apb_tim_freq) = match config.apb_pre {
163 APBPrescaler::NotDivided => (ahb_freq, ahb_freq), 168 APBPrescaler::NotDivided => (ahb_freq, ahb_freq),
164 pre => { 169 pre => {
165 let pre: u8 = pre.into(); 170 let pre: Ppre = pre.into();
166 let pre: u8 = 1 << (pre - 3); 171 let pre: u8 = 1 << (pre.0 - 3);
167 let freq = ahb_freq / pre as u32; 172 let freq = ahb_freq / pre as u32;
168 (freq, freq * 2) 173 (freq, freq * 2)
169 } 174 }