FluenTech / embedded-time

Time(ing) library (Instant/Duration/Clock/Timer/Period/Frequency) for bare-metal embedded systems
Apache License 2.0
87 stars 17 forks source link

Why does the `Clock` Trait require the user to define `const SCALING_FACTOR`? #96

Closed seanybaggins closed 3 years ago

seanybaggins commented 3 years ago

Why does the Clock Trait require the user to define...

const SCALING_FACTOR: Fraction

?

Would it be better if const SCALING_FACTOR was instead

fn frequency(&self) -> Rate;

The rational Suppose I am writing some hal or driver and I would like to implement the Clock trait so the user of my hal or driver doesn't need to. Its possible when writing these hal's or drivers that I cannot determine the frequency of hardware I am interfacing with at compile time but can determine the frequency at runtime.

Example of how this may be implemented for a hal or driver

struct StopWatch {
    inner: Hardware
}

impl Clock for StopWatch {
    ...
    fn frequency(&self) -> Rate {
        self.hardware.get_clock_frequency().MHz()
    }
    ...
}

I am still somewhat new to rust in an embedded context so my apologies if this is a horribly misguided suggestion/question.

korken89 commented 3 years ago

Hi this is to get compile time generation and optimization of the conversion math.

To set the speed of a HAL timer at compile time you can see an example here: https://github.com/rtic-rs/dwt-systick-monotonic/blob/master/src/lib.rs

seanybaggins commented 3 years ago

Thanks for the explanation. Closed the issue.