nrf-rs / nrf-hal

A Rust HAL for the nRF family of devices
Apache License 2.0
503 stars 139 forks source link

Thoughts on timer API #352

Open David-OConnor opened 3 years ago

David-OConnor commented 3 years ago

Hi. I recently started using several modules from this HAL, and noticed the Timer Module's API was a bit opaque. I wrote a new one, here. If you think code from this would be a good addition (Ie adding some of the methods), I'll make a PR, or a maintainer could copy+paste+commit.

Missing functionality such as PWM, and input capture. May need modifications to work on timers other than 0, 1, and 2.

Example syntax:


    let mut timer = Timer::new(dp.TIMER1, TimerMode::Timer, 1., 0);  // ie 1Hz, on compare 0.
    timer.shortcut(TimerShortcut::Clear, 0);  // Clear the timer when it generates an event.
    timer.enable_interrupt(0);
    timer.start();

   // ...

#[interrupt]
fn TIMER1() {
    free(|cs| {
        // (Code to access the timer struct from an interrupt)
        timer.clear_interrupt(0);
    )};
}