Skip to content

Fix integer overflow in microseconds delay, typos #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@

/// Internal function to implement a variable busy-wait loop.
/// # Arguments
/// * 'count' - an i32, the number of times to cycle the loop.
pub fn delay(count: u32) {
/// * 'count' - a u64, the number of times to cycle the loop.
pub fn delay(count: u64) {
// Our asm busy-wait takes a 16 bit word as an argument,
// so the max number of loops is 2^16
let outer_count = count / 65536;
Expand All @@ -46,23 +46,21 @@ pub fn delay(count: u32) {
:)}
}

///delay for N miliseconds
///delay for N milliseconds
/// # Arguments
/// * 'ms' - an u32, number of milliseconds to busy-wait
pub fn delay_ms(ms: u32) {
/// * 'ms' - a u64, number of milliseconds to busy-wait
pub fn delay_ms(ms: u64) {
// microseconds
let us = ms * 1000;
delay_us(us);
}

///delay for N microseconds
/// # Arguments
/// * 'ms' - an u32, number of microseconds to busy-wait
pub fn delay_us(us: u32) {
// picoseconds
let ps = us * 1000;
let ps_lp = 1000000000 / (avr_config::CPU_FREQUENCY_HZ / 4);
let loops = (ps / ps_lp) as u32;
/// * 'us' - a u64, number of microseconds to busy-wait
pub fn delay_us(us: u64) {
let us_in_loop = (avr_config::CPU_FREQUENCY_HZ / 1000000 / 4) as u64;
let loops = us * us_in_loop;
delay(loops);
}

Expand Down