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
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//! Generic `Timer` implementation that works with all 3 timers on the chip.

use {
    core::mem,
    nrf52810_hal::nrf52810_pac::{TIMER0, TIMER1, TIMER2},
    rubble::{
        link::NextUpdate,
        time::{Instant, Timer},
    },
};

/// Implements Rubble's `Timer` trait for the timers on the nRF chip.
pub struct BleTimer<T: NrfTimerExt> {
    inner: T,
    next: Instant,
    interrupt_enabled: bool,
}

impl<T: NrfTimerExt> BleTimer<T> {
    /// Initializes the timer.
    pub fn init(mut peripheral: T) -> Self {
        peripheral.init();
        Self {
            inner: peripheral,
            next: Instant::from_raw_micros(0),
            interrupt_enabled: false,
        }
    }

    /// Configures the timer interrupt to fire according to `next`.
    pub fn configure_interrupt(&mut self, next: NextUpdate) {
        match next {
            NextUpdate::Keep => {
                // Don't call `set_interrupt` when the interrupt is already configured, since that
                // might result in races (it resets the event)
                if !self.interrupt_enabled {
                    self.inner.set_interrupt(self.next);
                    self.interrupt_enabled = true;
                }
            }
            NextUpdate::Disable => {
                self.inner.clear_interrupt();
                self.interrupt_enabled = false;
            }
            NextUpdate::At(instant) => {
                self.inner.set_interrupt(instant);
                self.interrupt_enabled = true;
            }
        }
    }

    /// Checks whether this timer's interrupt is pending.
    ///
    /// This will return `true` when interrupt handler should execute. To prevent spurious wakeups,
    /// the handler *must* check that this is `true` when it gets executed. The handler should
    /// acknowledge the interrupt by calling [`clear_interrupt`], otherwise the handler will be run
    /// immediately after returning.
    ///
    /// [`clear_interrupt`]: #method.clear_interrupt
    pub fn is_interrupt_pending(&self) -> bool {
        self.inner.is_pending()
    }

    /// Clears a pending interrupt and disables generation of further interrupts.
    pub fn clear_interrupt(&mut self) {
        self.inner.clear_interrupt();
    }

    /// Provides access to the raw peripheral. Use with caution.
    pub fn inner(&mut self) -> &mut T {
        &mut self.inner
    }

    /// Creates a new `StampSource` using this timer.
    ///
    /// The `StampSource` can be used to obtain the current time, but can not do anything else. This
    /// restriction makes it safe to use even when the `BleTimer` it was created from is modified.
    pub fn create_stamp_source(&self) -> StampSource<T> {
        StampSource {
            inner: unsafe { self.inner.duplicate() },
        }
    }
}

impl<T: NrfTimerExt> Timer for BleTimer<T> {
    fn now(&self) -> Instant {
        self.inner.now()
    }
}

/// A timer interface that only allows reading the current time stamp.
pub struct StampSource<T: NrfTimerExt> {
    inner: T,
}

impl<T: NrfTimerExt> Timer for StampSource<T> {
    fn now(&self) -> Instant {
        self.inner.now()
    }
}

mod sealed {
    pub trait Sealed {}
}

/// Extension trait implemented for the nRF timer peripherals.
///
/// We use `CC[0]` to read the counter value, and `CC[1]` to set timer interrupts.
pub trait NrfTimerExt: sealed::Sealed {
    unsafe fn duplicate(&self) -> Self;

    /// Initialize the timer so that it counts at a rate of 1 MHz.
    fn init(&mut self);

    /// Configures the timer's interrupt to fire at the given `Instant`.
    fn set_interrupt(&mut self, at: Instant);

    /// Disables or acknowledges this timer's interrupt.
    fn clear_interrupt(&mut self);

    /// Returns whether a timer interrupt is currently pending.
    ///
    /// This must be called by the interrupt handler to avoid spurious timer events.
    fn is_pending(&self) -> bool;

    /// Obtains the current time as an `Instant`.
    fn now(&self) -> Instant;
}

macro_rules! impl_timer {
    ($ty:ident) => {
        impl NrfTimerExt for $ty {
            unsafe fn duplicate(&self) -> Self {
                mem::transmute_copy(self)
            }

            fn init(&mut self) {
                self.bitmode.write(|w| w.bitmode()._32bit());
                // 2^4 = 16
                // 16 MHz / 16 = 1 MHz = µs resolution
                self.prescaler.write(|w| unsafe { w.prescaler().bits(4) });
                self.tasks_clear.write(|w| w.tasks_clear().trigger());
                self.tasks_start.write(|w| w.tasks_start().trigger());
            }

            fn set_interrupt(&mut self, at: Instant) {
                // Not sure if an assertion is the right thing here, we might want to trigger the
                // interrupt immediately instead.
                at.duration_since(self.now());

                self.cc[1].write(|w| unsafe { w.bits(at.raw_micros()) });
                self.events_compare[1].reset();
                self.intenset.write(|w| w.compare1().set());
            }

            fn clear_interrupt(&mut self) {
                self.intenclr.write(|w| w.compare1().clear());
                self.events_compare[1].reset();
            }

            fn is_pending(&self) -> bool {
                self.events_compare[1]
                    .read()
                    .events_compare()
                    .is_generated()
            }

            fn now(&self) -> Instant {
                self.tasks_capture[0].write(|w| w.tasks_capture().trigger());
                let micros = self.cc[0].read().bits();
                Instant::from_raw_micros(micros)
            }
        }

        impl sealed::Sealed for $ty {}
    };
}

impl_timer!(TIMER0);
impl_timer!(TIMER1);
impl_timer!(TIMER2);