aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2024-03-19 21:33:20 +0100
committerDario Nieuwenhuis <[email protected]>2024-03-19 22:10:59 +0100
commitd90abb8ac91440602386b807edb221cae59e82f9 (patch)
treed7100839a83f3f9f546be8b8e17baa040059b188
parentdaa64bd5400c4db7da25e3f538b4ee0c31435029 (diff)
stm32/usb: assert usb clock is okay.
-rw-r--r--embassy-stm32/src/usb/mod.rs13
1 files changed, 13 insertions, 0 deletions
diff --git a/embassy-stm32/src/usb/mod.rs b/embassy-stm32/src/usb/mod.rs
index 130c728e6..788f61f16 100644
--- a/embassy-stm32/src/usb/mod.rs
+++ b/embassy-stm32/src/usb/mod.rs
@@ -10,6 +10,19 @@ use crate::rcc::sealed::RccPeripheral;
10 10
11/// clock, power initialization stuff that's common for USB and OTG. 11/// clock, power initialization stuff that's common for USB and OTG.
12fn common_init<T: Instance>() { 12fn common_init<T: Instance>() {
13 // Check the USB clock is enabled and running at exactly 48 MHz.
14 // frequency() will panic if not enabled
15 let freq = T::frequency();
16 // Check frequency is within the 0.25% tolerance allowed by the spec.
17 // Clock might not be exact 48Mhz due to rounding errors in PLL calculation, or if the user
18 // has tight clock restrictions due to something else (like audio).
19 if freq.0.abs_diff(48_000_000) > 120_000 {
20 panic!(
21 "USB clock should be 48Mhz but is {} Hz. Please double-check your RCC settings.",
22 freq.0
23 )
24 }
25
13 #[cfg(any(stm32l4, stm32l5, stm32wb))] 26 #[cfg(any(stm32l4, stm32l5, stm32wb))]
14 critical_section::with(|_| crate::pac::PWR.cr2().modify(|w| w.set_usv(true))); 27 critical_section::with(|_| crate::pac::PWR.cr2().modify(|w| w.set_usv(true)));
15 28