aboutsummaryrefslogtreecommitdiff
path: root/rtic-monotonics/src/nrf/rtc.rs
blob: 1f4e34f5accf15c9c4557501fcc1d1abecdf7270 (plain) (blame)
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
238
239
240
241
242
//! [`Monotonic`] implementation for the nRF Real Time Clocks (RTC).
//!
//! # Example
//!
//! ```
//! use rtic_monotonics::nrf::rtc::*;
//!
//! fn init() {
//!     # // This is normally provided by the selected PAC
//!     # let rtc = unsafe { core::mem::transmute(()) };
//!     // Generate the required token
//!     let token = rtic_monotonics::create_nrf_rtc0_monotonic_token!();
//!
//!     // Start the monotonic
//!     Rtc0::start(rtc, token);
//! }
//!
//! async fn usage() {
//!     loop {
//!          // Use the monotonic
//!          Rtc0::delay(100.millis()).await;
//!     }
//! }
//! ```

#[cfg(feature = "nrf52810")]
use nrf52810_pac::{self as pac, Interrupt, RTC0, RTC1};
#[cfg(feature = "nrf52811")]
use nrf52811_pac::{self as pac, Interrupt, RTC0, RTC1};
#[cfg(feature = "nrf52832")]
use nrf52832_pac::{self as pac, Interrupt, RTC0, RTC1, RTC2};
#[cfg(feature = "nrf52833")]
use nrf52833_pac::{self as pac, Interrupt, RTC0, RTC1, RTC2};
#[cfg(feature = "nrf52840")]
use nrf52840_pac::{self as pac, Interrupt, RTC0, RTC1, RTC2};
#[cfg(feature = "nrf5340-app")]
use nrf5340_app_pac::{self as pac, Interrupt, RTC0_NS as RTC0, RTC1_NS as RTC1};
#[cfg(feature = "nrf5340-net")]
use nrf5340_net_pac::{self as pac, Interrupt, RTC0_NS as RTC0, RTC1_NS as RTC1};
#[cfg(feature = "nrf9160")]
use nrf9160_pac::{self as pac, Interrupt, RTC0_NS as RTC0, RTC1_NS as RTC1};

use crate::{Monotonic, TimeoutError, TimerQueue};
use atomic_polyfill::{AtomicU32, Ordering};
use core::future::Future;
pub use fugit::{self, ExtU64, ExtU64Ceil};

#[doc(hidden)]
#[macro_export]
macro_rules! __internal_create_nrf_rtc_interrupt {
    ($mono_timer:ident, $rtc:ident, $rtc_token:ident) => {{
        #[no_mangle]
        #[allow(non_snake_case)]
        unsafe extern "C" fn $rtc() {
            $crate::nrf::rtc::$mono_timer::__tq().on_monotonic_interrupt();
        }

        pub struct $rtc_token;

        unsafe impl $crate::InterruptToken<$crate::nrf::rtc::$mono_timer> for $rtc_token {}

        $rtc_token
    }};
}

/// Register the Rtc0 interrupt for the monotonic.
#[macro_export]
macro_rules! create_nrf_rtc0_monotonic_token {
    () => {{
        $crate::__internal_create_nrf_rtc_interrupt!(Rtc0, RTC0, Rtc0Token)
    }};
}

/// Register the Rtc1 interrupt for the monotonic.
#[macro_export]
macro_rules! create_nrf_rtc1_monotonic_token {
    () => {{
        $crate::__internal_create_nrf_rtc_interrupt!(Rtc1, RTC1, Rtc1Token)
    }};
}

/// Register the Rtc2 interrupt for the monotonic.
#[cfg(any(feature = "nrf52832", feature = "nrf52833", feature = "nrf52840"))]
#[cfg_attr(
    docsrs,
    doc(cfg(any(feature = "nrf52832", feature = "nrf52833", feature = "nrf52840")))
)]
#[macro_export]
macro_rules! create_nrf_rtc2_monotonic_token {
    () => {{
        $crate::__internal_create_nrf_rtc_interrupt!(Rtc2, RTC2, Rtc2Token)
    }};
}

macro_rules! make_rtc {
    ($mono_name:ident, $rtc:ident, $overflow:ident, $tq:ident$(, doc: ($($doc:tt)*))?) => {
        /// Monotonic timer queue implementation.
        $(
            #[cfg_attr(docsrs, doc(cfg($($doc)*)))]
        )?
        pub struct $mono_name;

        static $overflow: AtomicU32 = AtomicU32::new(0);
        static $tq: TimerQueue<$mono_name> = TimerQueue::new();

        impl $mono_name {
            /// Start the timer monotonic.
            pub fn start(rtc: $rtc, _interrupt_token: impl crate::InterruptToken<Self>) {
                unsafe { rtc.prescaler.write(|w| w.bits(0)) };
                rtc.intenset.write(|w| w.compare0().set().ovrflw().set());
                rtc.evtenset.write(|w| w.compare0().set().ovrflw().set());

                rtc.tasks_clear.write(|w| unsafe { w.bits(1) });
                rtc.tasks_start.write(|w| unsafe { w.bits(1) });

                $tq.initialize(Self {});

                // SAFETY: We take full ownership of the peripheral and interrupt vector,
                // plus we are not using any external shared resources so we won't impact
                // basepri/source masking based critical sections.
                unsafe {
                    crate::set_monotonic_prio(pac::NVIC_PRIO_BITS, Interrupt::$rtc);
                    pac::NVIC::unmask(Interrupt::$rtc);
                }
            }

            /// Used to access the underlying timer queue
            #[doc(hidden)]
            pub fn __tq() -> &'static TimerQueue<$mono_name> {
                &$tq
            }

            #[inline(always)]
            fn is_overflow() -> bool {
                let rtc = unsafe { &*$rtc::PTR };
                rtc.events_ovrflw.read().bits() == 1
            }

            /// Timeout at a specific time.
            #[inline]
            pub async fn timeout_at<F: Future>(
                instant: <Self as Monotonic>::Instant,
                future: F,
            ) -> Result<F::Output, TimeoutError> {
                $tq.timeout_at(instant, future).await
            }

            /// Timeout after a specific duration.
            #[inline]
            pub async fn timeout_after<F: Future>(
                duration: <Self as Monotonic>::Duration,
                future: F,
            ) -> Result<F::Output, TimeoutError> {
                $tq.timeout_after(duration, future).await
            }

            /// Delay for some duration of time.
            #[inline]
            pub async fn delay(duration: <Self as Monotonic>::Duration) {
                $tq.delay(duration).await;
            }

            /// Delay to some specific time instant.
            #[inline]
            pub async fn delay_until(instant: <Self as Monotonic>::Instant) {
                $tq.delay_until(instant).await;
            }
        }


        rtic_time::embedded_hal_delay_impl_fugit64!($mono_name);

        #[cfg(feature = "embedded-hal-async")]
        rtic_time::embedded_hal_async_delay_impl_fugit64!($mono_name);

        impl Monotonic for $mono_name {
            const ZERO: Self::Instant = Self::Instant::from_ticks(0);
            const TICK_PERIOD: Self::Duration = Self::Duration::from_ticks(1);

            type Instant = fugit::TimerInstantU64<32_768>;
            type Duration = fugit::TimerDurationU64<32_768>;

            fn now() -> Self::Instant {
                // In a critical section to not get a race between overflow updates and reading it
                // and the flag here.
                critical_section::with(|_| {
                    let rtc = unsafe { &*$rtc::PTR };
                    let cnt = rtc.counter.read().bits();
                    // OVERFLOW HAPPENS HERE race needs to be handled
                    let ovf = if Self::is_overflow() {
                        $overflow.load(Ordering::Relaxed) + 1
                    } else {
                        $overflow.load(Ordering::Relaxed)
                    } as u64;

                    // Check and fix if above race happened
                    let new_cnt = rtc.counter.read().bits();
                    let cnt = if new_cnt >= cnt { cnt } else { new_cnt } as u64;

                    Self::Instant::from_ticks((ovf << 24) | cnt)
                })
            }

            fn on_interrupt() {
                let rtc = unsafe { &*$rtc::PTR };
                if Self::is_overflow() {
                    $overflow.fetch_add(1, Ordering::SeqCst);
                    rtc.events_ovrflw.write(|w| unsafe { w.bits(0) });
                }
            }

            // NOTE: To fix errata for RTC, if the release time is within 4 ticks
            // we release as the RTC will not generate a compare interrupt...
            fn should_dequeue_check(release_at: Self::Instant) -> bool {
                Self::now() + <Self as Monotonic>::Duration::from_ticks(4) >= release_at
            }

            fn enable_timer() {}

            fn disable_timer() {}

            fn set_compare(instant: Self::Instant) {
                let rtc = unsafe { &*$rtc::PTR };
                unsafe { rtc.cc[0].write(|w| w.bits(instant.ticks() as u32 & 0xffffff)) };
            }

            fn clear_compare_flag() {
                let rtc = unsafe { &*$rtc::PTR };
                unsafe { rtc.events_compare[0].write(|w| w.bits(0)) };
            }

            fn pend_interrupt() {
                pac::NVIC::pend(Interrupt::$rtc);
            }
        }
    };
}

make_rtc!(Rtc0, RTC0, RTC0_OVERFLOWS, RTC0_TQ);
make_rtc!(Rtc1, RTC1, RTC1_OVERFLOWS, RTC1_TQ);
#[cfg(any(feature = "nrf52832", feature = "nrf52833", feature = "nrf52840"))]
make_rtc!(Rtc2, RTC2, RTC2_OVERFLOWS, RTC2_TQ, doc: (any(feature = "nrf52832", feature = "nrf52833", feature = "nrf52840")));