@@ -249,8 +249,12 @@ pub struct ClockControl {
249
249
dfs : dfs:: DFS ,
250
250
}
251
251
252
- /// Function only available once clock if frozen
253
- pub fn sleep < T : Into < NanoSeconds > > ( time : T ) {
252
+ /// Sleep by spinning
253
+ ///
254
+ /// *Note: Function only available once clock if frozen.embedded_hal*
255
+ ///
256
+ /// *Note: Maximum duration is 2e32-1 ns ~ 4.29s *
257
+ pub fn sleep < T : Into < NanoSecondsU64 > > ( time : T ) {
254
258
unsafe { CLOCK_CONTROL . as_ref ( ) . unwrap ( ) . delay ( time) } ;
255
259
}
256
260
@@ -440,7 +444,7 @@ impl ClockControl {
440
444
} ;
441
445
442
446
let estimated_time = ( Hertz ( 1_000_000 ) * ( slow_cycles as u32 ) / slow_freq) . us ( ) ;
443
- let estimated_cycle_count = 2 * self . time_to_cpu_cycles ( estimated_time) ;
447
+ let estimated_cycle_count = 2 * ( self . time_to_cpu_cycles ( estimated_time) as u32 ) ;
444
448
445
449
let max_cycle_count = 0x01FFFFFF ; // bit 7:31 = 25 bits
446
450
if estimated_cycle_count > max_cycle_count {
@@ -527,6 +531,7 @@ impl ClockControl {
527
531
fn init < T : Into < Hertz > + Copy > ( & mut self , xtal_frequency : T ) -> Result < & mut Self , Error > {
528
532
// if auto is selected check if the frequency has already been stored during
529
533
// a previous run in the scratch register
534
+
530
535
if xtal_frequency. into ( ) == XTAL_FREQUENCY_AUTO {
531
536
self . xtal_frequency = match self . xtal_frequency_from_scratch ( ) {
532
537
Ok ( frequency) => frequency,
@@ -585,14 +590,20 @@ impl ClockControl {
585
590
}
586
591
587
592
/// calculate the number of cpu cycles from a time at the current CPU frequency
588
- fn time_to_cpu_cycles < T : Into < NanoSeconds > > ( & self , time : T ) -> u32 {
589
- ( ( ( self . cpu_frequency / Hertz ( 1_000_000 ) ) as u64 ) * ( u32:: from ( time. into ( ) ) as u64 ) / 1000 )
590
- as u32
593
+ fn time_to_cpu_cycles < T : Into < NanoSecondsU64 > > ( & self , time : T ) -> u64 {
594
+ ( ( self . cpu_frequency / Hertz ( 1_000_000 ) ) as u64 ) * u64:: from ( time. into ( ) ) / 1000
591
595
}
592
596
593
597
/// delay a certain time by spinning
594
- fn delay < T : Into < NanoSeconds > > ( & self , time : T ) {
595
- delay ( self . time_to_cpu_cycles ( time) ) ;
598
+ fn delay < T : Into < NanoSecondsU64 > > ( & self , time : T ) {
599
+ let cycles = self . time_to_cpu_cycles ( time) ;
600
+ let lower = cycles as u32 ;
601
+ let upper = cycles >> 32 ;
602
+
603
+ delay ( lower) ;
604
+ for _ in 0 ..( upper * 2 ) {
605
+ delay ( u32:: MAX / 2 ) ;
606
+ }
596
607
}
597
608
598
609
/// Check if a value from RTC_XTAL_FREQ_REG or RTC_APB_FREQ_REG are valid clocks
@@ -626,17 +637,11 @@ impl ClockControl {
626
637
T2 : Into < Hertz > + Copy + PartialOrd ,
627
638
T3 : Into < Hertz > + Copy + PartialOrd ,
628
639
{
629
- match cpu_source_default {
630
- CPUSource :: APLL => return Err ( Error :: UnsupportedFreqConfig ) ,
631
- _ => { }
632
- }
633
- match cpu_source_locked {
634
- CPUSource :: APLL => return Err ( Error :: UnsupportedFreqConfig ) ,
635
- _ => { }
636
- }
637
- match cpu_source_apb_locked {
638
- CPUSource :: APLL => return Err ( Error :: UnsupportedFreqConfig ) ,
639
- _ => { }
640
+ if cpu_source_default == CPUSource :: APLL
641
+ || cpu_source_locked == CPUSource :: APLL
642
+ || cpu_source_apb_locked == CPUSource :: APLL
643
+ {
644
+ return Err ( Error :: UnsupportedFreqConfig ) ;
640
645
}
641
646
642
647
if cpu_frequency_default. into ( ) < CPU_FREQ_MIN
@@ -653,6 +658,12 @@ impl ClockControl {
653
658
return Err ( Error :: FrequencyTooHigh ) ;
654
659
}
655
660
661
+ if cpu_frequency_default. into ( ) > cpu_frequency_apb_locked. into ( )
662
+ || cpu_frequency_default. into ( ) > cpu_frequency_locked. into ( )
663
+ {
664
+ return Err ( Error :: UnsupportedFreqConfig ) ;
665
+ }
666
+
656
667
self . cpu_source_default = cpu_source_default;
657
668
self . cpu_frequency_default =
658
669
self . round_cpu_frequency ( cpu_source_default, cpu_frequency_default) ;
@@ -762,6 +773,10 @@ impl ClockControl {
762
773
frequency : T ,
763
774
keep_pll_enabled : bool ,
764
775
) -> Result < & mut Self , Error > {
776
+ if source == self . cpu_source && frequency. into ( ) == self . cpu_frequency {
777
+ return Ok ( self ) ;
778
+ }
779
+
765
780
match source {
766
781
CPUSource :: Xtal => self . set_cpu_frequency_to_xtal ( frequency) ?,
767
782
CPUSource :: PLL => self . set_cpu_frequency_to_pll ( frequency) ?,
0 commit comments