@@ -101,8 +101,14 @@ const HSI: u32 = 8_000_000; // Hz
101101// some microcontrollers do not have USB
102102#[ cfg( any( feature = "stm32f301" , feature = "stm32f334" , ) ) ]
103103mod 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 )
106112 }
107113
108114 pub fn set_usbpre < W > ( w : & mut W , _: bool ) -> & mut W {
@@ -121,19 +127,29 @@ mod usb_clocking {
121127 feature = "stm32f398" ,
122128) ) ]
123129mod usb_clocking {
124- use crate :: stm32:: rcc;
130+ use crate :: stm32:: rcc:: cfgr ;
125131
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+ }
128146 }
129147
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 )
132150 }
133151}
134152
135- use self :: usb_clocking:: { has_usb, set_usbpre} ;
136-
137153/// Clock configuration
138154pub struct CFGR {
139155 hse : Option < u32 > ,
@@ -334,15 +350,7 @@ impl CFGR {
334350 } )
335351 }
336352
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) ;
346354
347355 let rcc = unsafe { & * RCC :: ptr ( ) } ;
348356
0 commit comments