27
27
#[ inline( always) ]
28
28
pub fn delay_ms ( ms : u32 ) {
29
29
let ticks: u64 = ( u64:: from ( avr_config:: CPU_FREQUENCY_HZ ) * u64:: from ( ms) ) / 4_000 ;
30
- internal_delay_functions :: delay ( ticks) ;
30
+ delay_impl ( ticks) ;
31
31
}
32
32
33
33
///delay for N microseconds
@@ -36,62 +36,57 @@ pub fn delay_ms(ms: u32) {
36
36
#[ inline( always) ]
37
37
pub fn delay_us ( us : u32 ) {
38
38
let ticks: u64 = ( u64:: from ( avr_config:: CPU_FREQUENCY_HZ ) * u64:: from ( us) ) / 4_000_000 ;
39
- internal_delay_functions :: delay ( ticks) ;
39
+ delay_impl ( ticks) ;
40
40
}
41
41
42
- /// Deprecated: If necesarry, use internal_delay_functions::delay instead
42
+ ///delay for N*4 clock cycles, not including setup overhead
43
+ /// # Arguments
44
+ /// * 'count' - a u32, number of iterations to busy-wait (4 cycles per iteration)
43
45
#[ inline( always) ]
44
- #[ deprecated( since = "0.3.3" , note = "If necesarry, users can instead use internal_delay_functions::delay" ) ]
45
46
pub fn delay ( count : u32 ) {
46
- internal_delay_functions :: delay ( u64:: from ( count) ) ;
47
+ delay_impl ( u64:: from ( count) ) ;
47
48
}
48
49
49
- /// Functions used internally for our busy-wait loop.
50
- ///
51
- /// These are not primarily intended to be used in user code, but are left public for
52
- /// use in making longer or more precise delays.
53
- pub mod internal_delay_functions {
54
- /// Internal function to implement a variable busy-wait loop.
55
- /// Delays for up up to 2^48 iterations, not including setup overhead.
56
- /// # Arguments
57
- /// * 'count' - a u64, the number of times to iterate the loop.
58
- /// The top 16 bits of count are ignored.
59
- ///
60
- #[ inline( always) ]
61
- pub fn delay ( count : u64 ) {
62
- // Casting truncates, so this is equivalent to `(count & 0xFFFF) as u16`
63
- let remaining_count: u16 = count as u16 ;
64
- // if remaining_count is zero, calling delay_loop_4_cycles would delay for 2^16 cycles,
65
- // so we don't loop in this case
66
- if remaining_count != 0 {
67
- delay_loop_4_cycles ( remaining_count) ;
68
- }
69
- // Casting truncates, so this is equivalent to `((count >> 16) & 0xFFFF_FFFF) as u32`
70
- let outer_count: u32 = ( count >> 16 ) as u32 ;
71
- for _ in 0 ..outer_count {
72
- // Each loop through should be 4 cycles, so we're getting 262,144 cycles per outer loop.
73
- delay_loop_4_cycles ( 0 ) ;
74
- }
50
+
51
+ /// Internal function to implement a variable busy-wait loop.
52
+ /// Delays for up up to 2^48 iterations, not including setup overhead.
53
+ /// # Arguments
54
+ /// * 'count' - a u64, the number of times to iterate the loop.
55
+ /// The top 16 bits of count are ignored.
56
+ #[ inline( always) ]
57
+ fn delay_impl ( count : u64 ) {
58
+ // Casting truncates, so this is equivalent to `(count & 0xFFFF) as u16`
59
+ let remaining_count: u16 = count as u16 ;
60
+ // if remaining_count is zero, calling delay_loop_4_cycles would delay for 2^16 cycles,
61
+ // so we don't loop in this case
62
+ if remaining_count != 0 {
63
+ delay_loop_4_cycles ( remaining_count) ;
64
+ }
65
+ // Casting truncates, so this is equivalent to `((count >> 16) & 0xFFFF_FFFF) as u32`
66
+ let outer_count: u32 = ( count >> 16 ) as u32 ;
67
+ for _ in 0 ..outer_count {
68
+ // Each loop through should be 4 cycles, so we're getting 262,144 cycles per outer loop.
69
+ delay_loop_4_cycles ( 0 ) ;
75
70
}
76
-
77
- /// Internal function to implement a 16-bit busy-wait loop in assembly.
78
- /// Delays for 4 cycles per iteration, not including setup overhead.
79
- /// Up to 2^16 iterations (the value 2^16 would have to be passed as 0).
80
- /// # Arguments
81
- /// * 'count' - a u16, the number of times to cycle the loop.
82
- #[ inline( always) ]
83
- #[ allow( unused_variables, unused_mut, unused_assignments) ]
84
- fn delay_loop_4_cycles ( mut count : u16 ) {
85
- #[ cfg( target_arch = "avr" ) ]
86
- unsafe {
87
- llvm_asm ! (
88
- "1: sbiw $0,1\n \t brne 1b"
89
- : "=w" ( count)
90
- : "0" ( count)
91
- :
92
- : "volatile"
93
- ) ;
94
- }
95
- // Allow compilation even on non-avr targets, for testing purposes
71
+ }
72
+
73
+ /// Internal function to implement a 16-bit busy-wait loop in assembly.
74
+ /// Delays for 4 cycles per iteration, not including setup overhead.
75
+ /// Up to 2^16 iterations (the value 2^16 would have to be passed as 0).
76
+ /// # Arguments
77
+ /// * 'count' - a u16, the number of times to cycle the loop.
78
+ #[ inline( always) ]
79
+ #[ allow( unused_variables, unused_mut, unused_assignments) ]
80
+ fn delay_loop_4_cycles ( mut count : u16 ) {
81
+ #[ cfg( target_arch = "avr" ) ]
82
+ unsafe {
83
+ llvm_asm ! (
84
+ "1: sbiw $0,1\n \t brne 1b"
85
+ : "=w" ( count)
86
+ : "0" ( count)
87
+ :
88
+ : "volatile"
89
+ ) ;
96
90
}
91
+ // Allow compilation even on non-avr targets, for testing purposes
97
92
}
0 commit comments