Skip to content

Commit 6d11419

Browse files
committed
const fn best_divider
1 parent 0dee1a4 commit 6d11419

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/rcc/f4/pll.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ impl MainPll {
340340
}
341341
}
342342

343-
fn best_divider(
343+
const fn best_divider(
344344
vco_out: u32,
345345
min: u32,
346346
target: u32,
@@ -349,25 +349,43 @@ impl MainPll {
349349
max_div: u32,
350350
) -> Option<(u32, u32, u32)> {
351351
let div = (vco_out + target / 2) / target;
352-
let min_div = u32::max(
352+
let min_div = max_u32(
353353
min_div,
354354
if max != 0 {
355355
(vco_out + max - 1) / max
356356
} else {
357357
0
358358
},
359359
);
360-
let max_div = u32::min(max_div, if min != 0 { vco_out / min } else { u32::MAX });
360+
let max_div = min_u32(max_div, if min != 0 { vco_out / min } else { u32::MAX });
361361
if min_div > max_div {
362362
return None;
363363
}
364-
let div = u32::min(u32::max(div, min_div), max_div);
364+
let div = min_u32(max_u32(div, min_div), max_div);
365365
let output = vco_out / div;
366366
let error = (output as i32 - target as i32).abs() as u32;
367367
Some((div, output, error))
368368
}
369369
}
370370

371+
#[cfg(feature = "gpio-f410")]
372+
const fn max_u32(first: u32, second: u32) -> u32 {
373+
if second > first {
374+
second
375+
} else {
376+
first
377+
}
378+
}
379+
380+
#[cfg(feature = "gpio-f410")]
381+
const fn min_u32(first: u32, second: u32) -> u32 {
382+
if second < first {
383+
second
384+
} else {
385+
first
386+
}
387+
}
388+
371389
#[cfg(not(feature = "gpio-f410"))]
372390
pub struct I2sPll {
373391
pub use_pll: bool,

0 commit comments

Comments
 (0)