Skip to content

Commit 1016f1a

Browse files
committed
Make internal delay functions private
1 parent b95c87e commit 1016f1a

File tree

1 file changed

+46
-51
lines changed

1 file changed

+46
-51
lines changed

src/lib.rs

+46-51
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#[inline(always)]
2828
pub fn delay_ms(ms: u32) {
2929
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);
3131
}
3232

3333
///delay for N microseconds
@@ -36,62 +36,57 @@ pub fn delay_ms(ms: u32) {
3636
#[inline(always)]
3737
pub fn delay_us(us: u32) {
3838
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);
4040
}
4141

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)
4345
#[inline(always)]
44-
#[deprecated(since = "0.3.3", note = "If necesarry, users can instead use internal_delay_functions::delay")]
4546
pub fn delay(count: u32) {
46-
internal_delay_functions::delay(u64::from(count));
47+
delay_impl(u64::from(count));
4748
}
4849

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);
7570
}
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\tbrne 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\tbrne 1b"
85+
: "=w" (count)
86+
: "0" (count)
87+
:
88+
: "volatile"
89+
);
9690
}
91+
// Allow compilation even on non-avr targets, for testing purposes
9792
}

0 commit comments

Comments
 (0)