Skip to content
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

DelayUs should not be implemented with thread::sleep #6

Open
tafia opened this issue Apr 19, 2018 · 0 comments
Open

DelayUs should not be implemented with thread::sleep #6

tafia opened this issue Apr 19, 2018 · 0 comments

Comments

@tafia
Copy link

tafia commented Apr 19, 2018

I believe thread::sleep cannot provide microsecond precision on linux.

Failing example:

extern crate embedded_hal;
extern crate linux_embedded_hal as hal;

use embedded_hal::blocking::delay::DelayUs;

fn main() {
    let us = 10u8;
    let t = ::std::time::SystemTime::now();
    let mut delay = hal::Delay{};
    delay.delay_us(us);
    println!("Expecting {}us, got {}us", us, t.elapsed().unwrap().subsec_nanos() / 1000);
}

Prints

Running `target/release/test_hal`
Expecting 10us, got 84us

What is the recommended way to get this level of precision?

As an example this very bad loop works:

pub fn delay_us(us: u32) {
    let target = Instant::now() + Duration::new(0, us * 1000);
    while Instant::now() < target { }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant