@@ -101,8 +101,14 @@ const HSI: u32 = 8_000_000; // Hz
101
101
// some microcontrollers do not have USB
102
102
#[ cfg( any( feature = "stm32f301" , feature = "stm32f334" , ) ) ]
103
103
mod usb_clocking {
104
- pub fn has_usb ( ) -> bool {
105
- false
104
+ use crate :: stm32:: rcc:: cfgr;
105
+
106
+ pub fn is_valid (
107
+ _sysclk : & u32 ,
108
+ _hse : & Option < u32 > ,
109
+ _pll_options : & Option < ( cfgr:: PLLMUL_A , cfgr:: PLLSRC_A ) > ,
110
+ ) -> ( bool , bool ) {
111
+ ( false , false )
106
112
}
107
113
108
114
pub fn set_usbpre < W > ( w : & mut W , _: bool ) -> & mut W {
@@ -121,19 +127,29 @@ mod usb_clocking {
121
127
feature = "stm32f398" ,
122
128
) ) ]
123
129
mod usb_clocking {
124
- use crate :: stm32:: rcc;
130
+ use crate :: stm32:: rcc:: cfgr ;
125
131
126
- pub fn has_usb ( ) -> bool {
127
- true
132
+ pub fn is_valid (
133
+ sysclk : & u32 ,
134
+ hse : & Option < u32 > ,
135
+ pll_options : & Option < ( cfgr:: PLLMUL_A , cfgr:: PLLSRC_A ) > ,
136
+ ) -> ( cfgr:: USBPRE_A , bool ) {
137
+ // the USB clock is only valid if an external crystal is used, the PLL is enabled, and the
138
+ // PLL output frequency is a supported one.
139
+ // usbpre == false: divide clock by 1.5, otherwise no division
140
+ let usb_ok = hse. is_some ( ) && pll_options. is_some ( ) ;
141
+ match ( usb_ok, sysclk) {
142
+ ( true , 72_000_000 ) => ( cfgr:: USBPRE_A :: DIV1_5 , true ) ,
143
+ ( true , 48_000_000 ) => ( cfgr:: USBPRE_A :: DIV1 , true ) ,
144
+ _ => ( cfgr:: USBPRE_A :: DIV1 , false ) ,
145
+ }
128
146
}
129
147
130
- pub fn set_usbpre ( w : & mut rcc :: cfgr:: W , bit : bool ) -> & mut rcc :: cfgr:: W {
131
- w. usbpre ( ) . bit ( bit )
148
+ pub fn set_usbpre ( w : & mut cfgr:: W , usb_prescale : cfgr :: USBPRE_A ) -> & mut cfgr:: W {
149
+ w. usbpre ( ) . variant ( usb_prescale )
132
150
}
133
151
}
134
152
135
- use self :: usb_clocking:: { has_usb, set_usbpre} ;
136
-
137
153
/// Clock configuration
138
154
pub struct CFGR {
139
155
hse : Option < u32 > ,
@@ -334,15 +350,7 @@ impl CFGR {
334
350
} )
335
351
}
336
352
337
- // the USB clock is only valid if an external crystal is used, the PLL is enabled, and the
338
- // PLL output frequency is a supported one.
339
- // usbpre == false: divide clock by 1.5, otherwise no division
340
- let usb_ok = has_usb ( ) && self . hse . is_some ( ) && pll_options. is_some ( ) ;
341
- let ( usbpre, usbclk_valid) = match ( usb_ok, sysclk) {
342
- ( true , 72_000_000 ) => ( false , true ) ,
343
- ( true , 48_000_000 ) => ( true , true ) ,
344
- _ => ( true , false ) ,
345
- } ;
353
+ let ( usbpre, usbclk_valid) = usb_clocking:: is_valid ( & sysclk, & self . hse , & pll_options) ;
346
354
347
355
let rcc = unsafe { & * RCC :: ptr ( ) } ;
348
356
0 commit comments