-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from lord-ne/rerewrite
Rewrite: Prevent overflow from public functions Hopefully are the underlaying commits preserved. Yes, I do fear that "github.com" does `git merge --squash`. What "lord-ne" wrote: This is the rewrite discussed in #17. Its main purpose is to ensure that public functions cannot overflow, by having them delegate to private functions that accept larger parameters. Some work was also put in to mitigating the effects of the delays not being inlined at compile time. Additional a link to #22 are is being asked for good commit messages.
- Loading branch information
Showing
2 changed files
with
81 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#[allow(unused_imports)] | ||
use core::arch::asm; | ||
|
||
/// Internal function to implement a variable busy-wait loop. | ||
/// # Arguments | ||
/// * 'count' - an u32, the number of times to cycle the loop (4 clock cycles per loop). | ||
#[inline(always)] | ||
pub fn delay_count_32(count: u32) { | ||
let mut outer_count: u16 = (count >> 16) as u16; | ||
let inner_count: u16 = count as u16; | ||
if inner_count != 0 { | ||
delay_loop_4_cycles(inner_count); | ||
} | ||
while outer_count != 0 { | ||
delay_loop_4_cycles(0); | ||
outer_count -= 1; | ||
} | ||
} | ||
|
||
/// Internal function to implement a variable busy-wait loop. | ||
/// # Arguments | ||
/// * 'count' - an u64, the number of times to cycle the loop (4 clock cycles per loop). *The top 16 bits are ignored.* | ||
#[inline(always)] | ||
pub fn delay_count_48(count: u64) { | ||
let mut outer_count: u32 = (count >> 16) as u32; | ||
let inner_count: u16 = count as u16; | ||
if inner_count != 0 { | ||
delay_loop_4_cycles(inner_count); | ||
} | ||
while outer_count != 0 { | ||
delay_loop_4_cycles(0); | ||
outer_count -= 1; | ||
} | ||
} | ||
|
||
/// Internal function to implement a 16-bit busy-wait loop in assembly. | ||
/// Delays for 4 cycles per iteration, not including setup overhead. | ||
/// Up to 2^16 iterations (the value 2^16 would have to be passed as 0). | ||
/// # Arguments | ||
/// * 'cycles' - an u16, the number of times to cycle the loop. | ||
#[inline(always)] | ||
#[allow(unused_variables, unused_mut, unused_assignments, dead_code)] | ||
pub fn delay_loop_4_cycles(mut cycles: u16) { | ||
#[cfg(target_arch = "avr")] | ||
unsafe { | ||
asm!("1: sbiw {i}, 1", | ||
"brne 1b", | ||
i = inout(reg_iw) cycles => _, | ||
) | ||
} | ||
// Allow compilation even on non-avr targets, for testing purposes | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters