File tree 1 file changed +15
-2
lines changed 1 file changed +15
-2
lines changed Original file line number Diff line number Diff line change @@ -28,7 +28,10 @@ pub fn delay(count: u32) {
28
28
/// compile time, otherwise the delay may be much longer than specified.
29
29
#[ inline( always) ]
30
30
pub fn delay_ms ( ms : u32 ) {
31
- let ticks: u64 = ( u64:: from ( avr_config:: CPU_FREQUENCY_HZ ) * u64:: from ( ms) ) / 4_000 ;
31
+ const GCD : u32 = gcd ( avr_config:: CPU_FREQUENCY_HZ , 4_000 ) ;
32
+ const NUMERATOR : u32 = avr_config:: CPU_FREQUENCY_HZ / GCD ;
33
+ const DENOMINATOR : u32 = 4_000 / GCD ;
34
+ let ticks: u64 = ( u64:: from ( ms) * u64:: from ( NUMERATOR ) ) / u64:: from ( DENOMINATOR ) ;
32
35
delay_impl:: delay_count_48 ( ticks) ;
33
36
}
34
37
@@ -38,6 +41,16 @@ pub fn delay_ms(ms: u32) {
38
41
/// compile time, otherwise the delay may be much longer than specified.
39
42
#[ inline( always) ]
40
43
pub fn delay_us ( us : u32 ) {
41
- let ticks: u64 = ( u64:: from ( avr_config:: CPU_FREQUENCY_HZ ) * u64:: from ( us) ) / 4_000_000 ;
44
+ const GCD : u32 = gcd ( avr_config:: CPU_FREQUENCY_HZ , 4_000_000 ) ;
45
+ const NUMERATOR : u32 = avr_config:: CPU_FREQUENCY_HZ / GCD ;
46
+ const DENOMINATOR : u32 = 4_000_000 / GCD ;
47
+ let ticks: u64 = ( u64:: from ( us) * u64:: from ( NUMERATOR ) ) / u64:: from ( DENOMINATOR ) ;
42
48
delay_impl:: delay_count_48 ( ticks) ;
43
49
}
50
+
51
+ const fn gcd ( mut a : u32 , mut b : u32 ) -> u32 {
52
+ while b != 0 {
53
+ ( a, b) = ( b, a % b) ;
54
+ }
55
+ return a;
56
+ }
You can’t perform that action at this time.
0 commit comments