@@ -105,6 +105,7 @@ const HSI: u32 = 8_000_000; // Hz
105
105
///
106
106
/// **NOTE**: Currently, it is not guaranteed that the exact frequencies selected will be
107
107
/// used, only frequencies close to it.
108
+ #[ derive( Debug , Default , PartialEq ) ]
108
109
pub struct CFGR {
109
110
hse : Option < u32 > ,
110
111
hclk : Option < u32 > ,
@@ -228,6 +229,19 @@ impl CFGR {
228
229
while rcc. cr . read ( ) . hserdy ( ) . bit_is_clear ( ) { }
229
230
}
230
231
232
+ if let Some ( pllmul_bits) = cfg. pllmul {
233
+ // enable PLL and wait for it to be ready
234
+
235
+ #[ allow( unused_unsafe) ]
236
+ rcc. cfgr . modify ( |_, w| unsafe {
237
+ w. pllmul ( ) . bits ( pllmul_bits) . pllsrc ( ) . bit ( cfg. hse . is_some ( ) )
238
+ } ) ;
239
+
240
+ rcc. cr . modify ( |_, w| w. pllon ( ) . set_bit ( ) ) ;
241
+
242
+ while rcc. cr . read ( ) . pllrdy ( ) . bit_is_clear ( ) { }
243
+ }
244
+
231
245
// set prescalers and clock source
232
246
#[ cfg( feature = "connectivity" ) ]
233
247
rcc. cfgr . modify ( |_, w| unsafe {
@@ -335,7 +349,7 @@ impl BKP {
335
349
///
336
350
/// let clocks = rcc.cfgr.freeze(&mut flash.acr);
337
351
/// ```
338
- #[ derive( Clone , Copy ) ]
352
+ #[ derive( Clone , Copy , Debug , PartialEq ) ]
339
353
pub struct Clocks {
340
354
hclk : Hertz ,
341
355
pclk1 : Hertz ,
@@ -702,3 +716,39 @@ impl Config {
702
716
}
703
717
}
704
718
}
719
+
720
+ #[ test]
721
+ fn rcc_config_usb ( ) {
722
+ use crate :: time:: U32Ext ;
723
+ let cfgr = CFGR :: default ( )
724
+ . use_hse ( 8 . mhz ( ) )
725
+ . sysclk ( 48 . mhz ( ) )
726
+ . pclk1 ( 24 . mhz ( ) ) ;
727
+
728
+ let config = Config :: from_cfgr ( cfgr) ;
729
+ let config_expected = Config {
730
+ hse : Some ( 8_000_000 ) ,
731
+ pllmul : Some ( 4 ) ,
732
+ hpre : HPre :: DIV1 ,
733
+ ppre1 : PPre :: DIV2 ,
734
+ ppre2 : PPre :: DIV1 ,
735
+ #[ cfg( any( feature = "stm32f103" , feature = "connectivity" ) ) ]
736
+ usbpre : UsbPre :: DIV1 ,
737
+ adcpre : AdcPre :: DIV8 ,
738
+ } ;
739
+ assert_eq ! ( config, config_expected) ;
740
+
741
+ let clocks = config. get_clocks ( ) ;
742
+ let clocks_expected = Clocks {
743
+ hclk : 48 . mhz ( ) . into ( ) ,
744
+ pclk1 : 24 . mhz ( ) . into ( ) ,
745
+ pclk2 : 48 . mhz ( ) . into ( ) ,
746
+ ppre1 : 2 ,
747
+ ppre2 : 1 ,
748
+ sysclk : 48 . mhz ( ) . into ( ) ,
749
+ adcclk : 6 . mhz ( ) . into ( ) ,
750
+ #[ cfg( any( feature = "stm32f103" , feature = "connectivity" ) ) ]
751
+ usbclk_valid : true ,
752
+ } ;
753
+ assert_eq ! ( clocks, clocks_expected) ;
754
+ }
0 commit comments