1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use core::time::Duration;
use core::u32;

use nb::{Error, Result};
use hal::timer::{CountDown, Periodic};
use nrf51::TIMER0;

pub struct Timer(TIMER0);

impl Timer {
    pub fn new(timer: TIMER0) -> Timer {
        // 32bits @ 1MHz == max delay of ~1 hour 11 minutes
        timer.bitmode.write(|w| w.bitmode()._32bit());
        timer.prescaler.write(|w| unsafe { w.prescaler().bits(4) });
        timer.intenset.write(|w| w.compare0().set());
        timer.shorts.write(|w| w.compare0_clear().enabled());

        Timer(timer)
    }
}

impl CountDown for Timer {
    type Time = Duration;

    fn start<T>(&mut self, count: T)
    where
        T: Into<Self::Time>,
    {
        let duration = count.into();
        assert!(duration.as_secs() < ((u32::MAX - duration.subsec_micros()) / 1_000_000) as u64);

        let us = (duration.as_secs() as u32) * 1_000_000 + duration.subsec_micros();
        self.0.cc[0].write(|w| unsafe { w.bits(us) });

        self.0.events_compare[0].reset();
        self.0.tasks_clear.write(|w| unsafe { w.bits(1) });
        self.0.tasks_start.write(|w| unsafe { w.bits(1) });
    }

    fn wait(&mut self) -> Result<(), !> {
        if self.0.events_compare[0].read().bits() == 1 {
            self.0.events_compare[0].reset();
            Ok(())
        } else {
            Err(Error::WouldBlock)
        }
    }
}

impl Periodic for Timer {}