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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//! A high level interface for RTC peripherals

use core::ops::Deref;

use crate::target::{rtc0, Interrupt, NVIC, RTC0, RTC1};

#[cfg(not(feature = "52810"))]
use crate::target::RTC2;

// Zero Size Type State structs

/// The RTC has been stopped
pub struct Stopped;
/// The RTC has been started
pub struct Started;

/// An opaque high level interface to an RTC peripheral
pub struct Rtc<T, M> {
    periph: T,
    _mode: M,
}

/// An extension trait for constructing the high level interface
pub trait RtcExt: Deref<Target = rtc0::RegisterBlock> + Sized {
    // The interrupt that belongs to this RTC instance.
    const INTERRUPT: Interrupt;

    fn constrain(self) -> Rtc<Self, Stopped>;
}

macro_rules! impl_rtc_ext {
    ($($rtc:ident,)*) => {
        $(
            impl RtcExt for $rtc {
                const INTERRUPT: Interrupt = Interrupt::$rtc;

                fn constrain(self) -> Rtc<$rtc, Stopped> {
                    Rtc {
                        periph: self,
                        _mode: Stopped,
                    }
                }
            }
        )+
    }
}

impl_rtc_ext!(RTC0, RTC1,);

#[cfg(not(feature = "52810"))]
impl_rtc_ext!(RTC2,);

/// Interrupts/Events that can be generated by the RTCn peripheral
pub enum RtcInterrupt {
    Tick,
    Overflow,
    Compare0,
    Compare1,
    Compare2,
    Compare3,
}

/// Compare registers available on the RTCn
pub enum RtcCompareReg {
    Compare0,
    Compare1,
    Compare2,
    Compare3,
}

impl<T, M> Rtc<T, M>
where
    T: RtcExt,
{
    /// Enable/start the Real Time Counter
    pub fn enable_counter(self) -> Rtc<T, Started> {
        unsafe {
            self.periph.tasks_start.write(|w| w.bits(1));
        }
        Rtc {
            periph: self.periph,
            _mode: Started,
        }
    }

    /// Disable/stop the Real Time Counter
    pub fn disable_counter(self) -> Rtc<T, Stopped> {
        unsafe {
            self.periph.tasks_stop.write(|w| w.bits(1));
        }
        Rtc {
            periph: self.periph,
            _mode: Stopped,
        }
    }

    /// Enable the generation of a hardware interrupt from a given stimulus
    pub fn enable_interrupt(&mut self, int: RtcInterrupt, nvic: &mut NVIC) {
        match int {
            RtcInterrupt::Tick => self.periph.intenset.write(|w| w.tick().set()),
            RtcInterrupt::Overflow => self.periph.intenset.write(|w| w.ovrflw().set()),
            RtcInterrupt::Compare0 => self.periph.intenset.write(|w| w.compare0().set()),
            RtcInterrupt::Compare1 => self.periph.intenset.write(|w| w.compare1().set()),
            RtcInterrupt::Compare2 => self.periph.intenset.write(|w| w.compare2().set()),
            RtcInterrupt::Compare3 => self.periph.intenset.write(|w| w.compare3().set()),
        }
        nvic.enable(T::INTERRUPT);
    }

    /// Disable the generation of a hardware interrupt from a given stimulus
    pub fn disable_interrupt(&mut self, int: RtcInterrupt, nvic: &mut NVIC) {
        match int {
            RtcInterrupt::Tick => self.periph.intenclr.write(|w| w.tick().clear()),
            RtcInterrupt::Overflow => self.periph.intenclr.write(|w| w.ovrflw().clear()),
            RtcInterrupt::Compare0 => self.periph.intenclr.write(|w| w.compare0().clear()),
            RtcInterrupt::Compare1 => self.periph.intenclr.write(|w| w.compare1().clear()),
            RtcInterrupt::Compare2 => self.periph.intenclr.write(|w| w.compare2().clear()),
            RtcInterrupt::Compare3 => self.periph.intenclr.write(|w| w.compare3().clear()),
        }
        nvic.disable(T::INTERRUPT);
    }

    /// Enable the generation of a hardware event from a given stimulus
    pub fn enable_event(&mut self, evt: RtcInterrupt) {
        match evt {
            RtcInterrupt::Tick => self.periph.evtenset.write(|w| w.tick().set()),
            RtcInterrupt::Overflow => self.periph.evtenset.write(|w| w.ovrflw().set()),
            RtcInterrupt::Compare0 => self.periph.evtenset.write(|w| w.compare0().set()),
            RtcInterrupt::Compare1 => self.periph.evtenset.write(|w| w.compare1().set()),
            RtcInterrupt::Compare2 => self.periph.evtenset.write(|w| w.compare2().set()),
            RtcInterrupt::Compare3 => self.periph.evtenset.write(|w| w.compare3().set()),
        }
    }

    /// Disables the generation of a hardware event from a given stimulus
    pub fn disable_event(&mut self, evt: RtcInterrupt) {
        match evt {
            RtcInterrupt::Tick => self.periph.evtenclr.write(|w| w.tick().clear()),
            RtcInterrupt::Overflow => self.periph.evtenclr.write(|w| w.ovrflw().clear()),
            RtcInterrupt::Compare0 => self.periph.evtenclr.write(|w| w.compare0().clear()),
            RtcInterrupt::Compare1 => self.periph.evtenclr.write(|w| w.compare1().clear()),
            RtcInterrupt::Compare2 => self.periph.evtenclr.write(|w| w.compare2().clear()),
            RtcInterrupt::Compare3 => self.periph.evtenclr.write(|w| w.compare3().clear()),
        }
    }

    /// Obtain the state of a given interrupt/event, and optionally clear the event
    /// if it is set
    pub fn get_event_triggered(&mut self, evt: RtcInterrupt, clear_on_read: bool) -> bool {
        let mut orig = 0;
        let set_val = if clear_on_read { 0 } else { 1 };
        match evt {
            RtcInterrupt::Tick => self.periph.events_tick.modify(|r, w| {
                orig = r.bits();
                unsafe { w.bits(set_val) }
            }),
            RtcInterrupt::Overflow => self.periph.events_ovrflw.modify(|r, w| {
                orig = r.bits();
                unsafe { w.bits(set_val) }
            }),
            RtcInterrupt::Compare0 => self.periph.events_compare[0].modify(|r, w| {
                orig = r.bits();
                unsafe { w.bits(set_val) }
            }),
            RtcInterrupt::Compare1 => self.periph.events_compare[1].modify(|r, w| {
                orig = r.bits();
                unsafe { w.bits(set_val) }
            }),
            RtcInterrupt::Compare2 => self.periph.events_compare[2].modify(|r, w| {
                orig = r.bits();
                unsafe { w.bits(set_val) }
            }),
            RtcInterrupt::Compare3 => self.periph.events_compare[3].modify(|r, w| {
                orig = r.bits();
                unsafe { w.bits(set_val) }
            }),
        };

        orig == 1
    }

    /// Set the compare value of a given register. The compare registers have a width
    /// of 24 bits
    pub fn set_compare(&mut self, reg: RtcCompareReg, val: u32) -> Result<(), Error> {
        if val >= (1 << 24) {
            return Err(Error::CompareOutOfRange);
        }

        let reg = match reg {
            RtcCompareReg::Compare0 => 0,
            RtcCompareReg::Compare1 => 1,
            RtcCompareReg::Compare2 => 2,
            RtcCompareReg::Compare3 => 3,
        };

        unsafe {
            self.periph.cc[reg].write(|w| w.bits(val));
        }

        Ok(())
    }

    /// Obtain the current value of the Real Time Counter, 24 bits of range
    pub fn get_counter(&self) -> u32 {
        self.periph.counter.read().bits()
    }

    /// Destructure the high level interface. Does not reset any configuration made
    /// to the given RTC peripheral
    pub fn release(self) -> T {
        self.periph
    }
}

/// Error types associated with the RTC peripheral interface
#[derive(Debug, PartialEq, Eq)]
pub enum Error {
    PrescalerOutOfRange,
    CompareOutOfRange,
}

impl<T> Rtc<T, Stopped>
where
    T: RtcExt,
{
    /// Set the prescaler for the RTC peripheral. 12 bits of range.
    /// fRTC = 32_768 / (`prescaler` + 1 )
    pub fn set_prescaler(&mut self, prescaler: u32) -> Result<(), Error> {
        if prescaler >= (1 << 12) {
            return Err(Error::PrescalerOutOfRange);
        }

        unsafe { self.periph.prescaler.write(|w| w.bits(prescaler)) };

        Ok(())
    }
}