Skip to content

Commit 6de379d

Browse files
Merge #204
204: Move maximum clock frequencies into public constants r=TeXitoi a=therealprof Signed-off-by: Daniel Egger <[email protected]> Co-authored-by: Daniel Egger <[email protected]>
2 parents 49b821b + fc502c4 commit 6de379d

File tree

1 file changed

+95
-89
lines changed

1 file changed

+95
-89
lines changed

src/rcc.rs

Lines changed: 95 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,93 @@ pub struct Rcc {
2929
pub cfgr: CFGR,
3030
}
3131

32-
const HSI: u32 = 16_000_000; // Hz
32+
/// Built-in high speed clock frequency
33+
pub const HSI: u32 = 16_000_000; // Hz
34+
35+
#[cfg(any(
36+
feature = "stm32f401",
37+
feature = "stm32f405",
38+
feature = "stm32f407",
39+
feature = "stm32f410",
40+
feature = "stm32f411",
41+
feature = "stm32f412",
42+
feature = "stm32f413",
43+
feature = "stm32f415",
44+
feature = "stm32f417",
45+
feature = "stm32f423",
46+
feature = "stm32f427",
47+
feature = "stm32f429",
48+
feature = "stm32f437",
49+
feature = "stm32f439",
50+
feature = "stm32f469",
51+
feature = "stm32f479"
52+
))]
53+
/// Minimum system clock frequency
54+
pub const SYSCLK_MIN: u32 = 24_000_000;
55+
56+
#[cfg(any(feature = "stm32f446"))]
57+
/// Minimum system clock frequency
58+
pub const SYSCLK_MIN: u32 = 12_500_000;
59+
60+
#[cfg(feature = "stm32f401")]
61+
/// Maximum system clock frequency
62+
pub const SYSCLK_MAX: u32 = 84_000_000;
63+
64+
#[cfg(any(
65+
feature = "stm32f405",
66+
feature = "stm32f407",
67+
feature = "stm32f415",
68+
feature = "stm32f417"
69+
))]
70+
/// Maximum system clock frequency
71+
pub const SYSCLK_MAX: u32 = 168_000_000;
72+
73+
#[cfg(any(
74+
feature = "stm32f410",
75+
feature = "stm32f411",
76+
feature = "stm32f412",
77+
feature = "stm32f413",
78+
feature = "stm32f423"
79+
))]
80+
/// Maximum system clock frequency
81+
pub const SYSCLK_MAX: u32 = 100_000_000;
82+
83+
#[cfg(any(
84+
feature = "stm32f427",
85+
feature = "stm32f429",
86+
feature = "stm32f437",
87+
feature = "stm32f439",
88+
feature = "stm32f446",
89+
feature = "stm32f469",
90+
feature = "stm32f479"
91+
))]
92+
/// Maximum system clock frequency
93+
pub const SYSCLK_MAX: u32 = 180_000_000;
94+
95+
#[cfg(any(
96+
feature = "stm32f401",
97+
feature = "stm32f410",
98+
feature = "stm32f411",
99+
feature = "stm32f412",
100+
feature = "stm32f413",
101+
feature = "stm32f423"
102+
))]
103+
/// Maximum APB2 peripheral clock frequency
104+
pub const PCLK2_MAX: u32 = SYSCLK_MAX;
105+
106+
#[cfg(not(any(
107+
feature = "stm32f401",
108+
feature = "stm32f410",
109+
feature = "stm32f411",
110+
feature = "stm32f412",
111+
feature = "stm32f413",
112+
feature = "stm32f423"
113+
)))]
114+
/// Maximum APB2 peripheral clock frequency
115+
pub const PCLK2_MAX: u32 = SYSCLK_MAX / 2;
116+
117+
/// Maximum APB1 peripheral clock frequency
118+
pub const PCLK1_MAX: u32 = PCLK2_MAX / 2;
33119

34120
pub struct CFGR {
35121
hse: Option<u32>,
@@ -88,6 +174,7 @@ impl CFGR {
88174
self
89175
}
90176

177+
#[inline(always)]
91178
fn pll_setup(&self) -> (bool, bool, u32, Option<Hertz>) {
92179
let pllsrcclk = self.hse.unwrap_or(HSI);
93180
let sysclk = self.sysclk.unwrap_or(pllsrcclk);
@@ -203,61 +290,7 @@ impl CFGR {
203290

204291
let (use_pll, sysclk_on_pll, sysclk, pll48clk) = self.pll_setup();
205292

206-
#[cfg(any(
207-
feature = "stm32f401",
208-
feature = "stm32f405",
209-
feature = "stm32f407",
210-
feature = "stm32f410",
211-
feature = "stm32f411",
212-
feature = "stm32f412",
213-
feature = "stm32f413",
214-
feature = "stm32f415",
215-
feature = "stm32f417",
216-
feature = "stm32f423",
217-
feature = "stm32f427",
218-
feature = "stm32f429",
219-
feature = "stm32f437",
220-
feature = "stm32f439",
221-
feature = "stm32f469",
222-
feature = "stm32f479"
223-
))]
224-
let sysclk_min = 24_000_000;
225-
226-
#[cfg(any(feature = "stm32f446"))]
227-
let sysclk_min = 12_500_000;
228-
229-
#[cfg(feature = "stm32f401")]
230-
let sysclk_max = 84_000_000;
231-
232-
#[cfg(any(
233-
feature = "stm32f405",
234-
feature = "stm32f407",
235-
feature = "stm32f415",
236-
feature = "stm32f417"
237-
))]
238-
let sysclk_max = 168_000_000;
239-
240-
#[cfg(any(
241-
feature = "stm32f410",
242-
feature = "stm32f411",
243-
feature = "stm32f412",
244-
feature = "stm32f413",
245-
feature = "stm32f423"
246-
))]
247-
let sysclk_max = 100_000_000;
248-
249-
#[cfg(any(
250-
feature = "stm32f427",
251-
feature = "stm32f429",
252-
feature = "stm32f437",
253-
feature = "stm32f439",
254-
feature = "stm32f446",
255-
feature = "stm32f469",
256-
feature = "stm32f479"
257-
))]
258-
let sysclk_max = 180_000_000;
259-
260-
assert!(!sysclk_on_pll || sysclk <= sysclk_max && sysclk >= sysclk_min);
293+
assert!(!sysclk_on_pll || sysclk <= SYSCLK_MAX && sysclk >= SYSCLK_MIN);
261294

262295
let hclk = self.hclk.unwrap_or(sysclk);
263296
let (hpre_bits, hpre_div) = match (sysclk + hclk - 1) / hclk {
@@ -276,36 +309,9 @@ impl CFGR {
276309
// Calculate real AHB clock
277310
let hclk = sysclk / hpre_div;
278311

279-
#[cfg(any(
280-
feature = "stm32f401",
281-
feature = "stm32f405",
282-
feature = "stm32f407",
283-
feature = "stm32f415",
284-
feature = "stm32f417"
285-
))]
286-
let (pclk1_max, pclk2_max) = (42_000_000, 84_000_000);
287-
#[cfg(any(
288-
feature = "stm32f427",
289-
feature = "stm32f429",
290-
feature = "stm32f437",
291-
feature = "stm32f439",
292-
feature = "stm32f446",
293-
feature = "stm32f469",
294-
feature = "stm32f479"
295-
))]
296-
let (pclk1_max, pclk2_max) = (45_000_000, 90_000_000);
297-
#[cfg(any(
298-
feature = "stm32f410",
299-
feature = "stm32f411",
300-
feature = "stm32f412",
301-
feature = "stm32f413",
302-
feature = "stm32f423"
303-
))]
304-
let (pclk1_max, pclk2_max) = (50_000_000, 100_000_000);
305-
306312
let pclk1 = self
307313
.pclk1
308-
.unwrap_or_else(|| core::cmp::min(pclk1_max, hclk));
314+
.unwrap_or_else(|| core::cmp::min(PCLK1_MAX, hclk));
309315
let (ppre1_bits, ppre1) = match (hclk + pclk1 - 1) / pclk1 {
310316
0 => unreachable!(),
311317
1 => (0b000, 1),
@@ -318,11 +324,11 @@ impl CFGR {
318324
// Calculate real APB1 clock
319325
let pclk1 = hclk / u32::from(ppre1);
320326

321-
assert!(pclk1 <= pclk1_max);
327+
assert!(pclk1 <= PCLK1_MAX);
322328

323329
let pclk2 = self
324330
.pclk2
325-
.unwrap_or_else(|| core::cmp::min(pclk2_max, hclk));
331+
.unwrap_or_else(|| core::cmp::min(PCLK2_MAX, hclk));
326332
let (ppre2_bits, ppre2) = match (hclk + pclk2 - 1) / pclk2 {
327333
0 => unreachable!(),
328334
1 => (0b000, 1),
@@ -335,7 +341,7 @@ impl CFGR {
335341
// Calculate real APB2 clock
336342
let pclk2 = hclk / u32::from(ppre2);
337343

338-
assert!(pclk2 <= pclk2_max);
344+
assert!(pclk2 <= PCLK2_MAX);
339345

340346
Self::flash_setup(sysclk);
341347

@@ -470,9 +476,9 @@ impl Clocks {
470476
/// Returns true if the PLL48 clock is within USB
471477
/// specifications. It is required to use the USB functionality.
472478
pub fn is_pll48clk_valid(&self) -> bool {
473-
// USB specification allow +-0.25%
479+
// USB specification allows +-0.25%
474480
self.pll48clk
475-
.map(|freq| (48_000_000 - freq.0 as i32).abs() <= 48_000_000 * 25 / 10000)
481+
.map(|freq| (48_000_000 - freq.0 as i32).abs() <= 120_000)
476482
.unwrap_or(false)
477483
}
478484
}

0 commit comments

Comments
 (0)