@@ -27,6 +27,7 @@ impl Delay {
27
27
}
28
28
}
29
29
30
+ /// System timer (SysTick) as a delay provider.
30
31
impl DelayMs < u32 > for Delay {
31
32
fn delay_ms ( & mut self , ms : u32 ) {
32
33
self . delay_us ( ms * 1_000 ) ;
@@ -47,17 +48,29 @@ impl DelayMs<u8> for Delay {
47
48
48
49
impl DelayUs < u32 > for Delay {
49
50
fn delay_us ( & mut self , us : u32 ) {
50
- let rvr = us * ( self . clocks . sysclk ( ) . 0 / 1_000_000 ) ;
51
+ // The SysTick Reload Value register supports values between 1 and 0x00FFFFFF.
52
+ const MAX_RVR : u32 = 0x00FF_FFFF ;
51
53
52
- // assert!(rvr < (1 << 24)); //TODO fix this assertion
54
+ let mut total_rvr = us * ( self . clocks . sysclk ( ) . 0 / 1_000_000 ) ;
53
55
54
- self . syst . set_reload ( rvr) ;
55
- self . syst . clear_current ( ) ;
56
- self . syst . enable_counter ( ) ;
56
+ while total_rvr != 0 {
57
+ let current_rvr = if total_rvr <= MAX_RVR {
58
+ total_rvr
59
+ } else {
60
+ MAX_RVR
61
+ } ;
57
62
58
- while !self . syst . has_wrapped ( ) { }
63
+ self . syst . set_reload ( current_rvr) ;
64
+ self . syst . clear_current ( ) ;
65
+ self . syst . enable_counter ( ) ;
59
66
60
- self . syst . disable_counter ( ) ;
67
+ // Update the tracking variable while we are waiting...
68
+ total_rvr -= current_rvr;
69
+
70
+ while !self . syst . has_wrapped ( ) { }
71
+
72
+ self . syst . disable_counter ( ) ;
73
+ }
61
74
}
62
75
}
63
76
0 commit comments