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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#[doc(hidden)]
#[macro_export]
macro_rules! __internal_backend_methods {
{
mode = $mode:ty;
rtic_int = $rtic_int:ty;
rtc_pac = $rtc_pac: ident;
init_compares = $init_compares:block
statics = $statics:block
enable_interrupts = $enable_interrupts:block
}
=> {
/// RTC interrupt handler called before control passes to the
/// [`TimerQueue`
/// handler`](rtic_time::timer_queue::TimerQueue::on_monotonic_interrupt).
///
/// # Safety
/// This should only be called from the RTC interrupt handler.
#[inline]
pub unsafe fn interrupt_handler() {
let rtc = unsafe {pac::Rtc::steal() };
/// Returns whether a < b, taking wrapping into account and assuming
/// that the difference is less than a half period.
#[inline]
fn less_than_with_wrap(
a: <$mode as RtcMode>::Count,
b: <$mode as RtcMode>::Count,
) -> bool {
let d = a.wrapping_sub(b);
d >= <$mode>::HALF_PERIOD
}
// Ensure that the COUNT is at least the compare value
// Due to syncing delay this may not be the case initially
// Note that this has to be done here because RTIC will clear the cmp0 flag
// before `RtcBackend::on_interrupt` is called.
if <$mode>::check_interrupt_flag::<$rtic_int>(&rtc) {
let compare = <$mode>::get_compare(&rtc, 0);
while less_than_with_wrap(<$mode>::count(&rtc), compare) {}
}
unsafe { Self::timer_queue().on_monotonic_interrupt(); }
}
/// Starts the clock.
///
/// **Do not use this function directly.**
///
/// Use the crate level macros instead, then call `start` on the monotonic.
pub fn _start($rtc_pac: pac::Rtc) {
// Disable the RTC.
<$mode>::disable(&$rtc_pac);
// Reset RTC back to initial settings, which disables it and enters mode 0.
<$mode>::reset(&$rtc_pac);
// Set the RTC mode
<$mode>::set_mode(&$rtc_pac);
$init_compares
// Timing critical, make sure we don't get interrupted.
critical_section::with(|_| {
// Start the timer and initialize it
<$mode>::start_and_initialize(&$rtc_pac);
// Clear the triggered compare flag
<$mode>::clear_interrupt_flag::<$rtic_int>(&$rtc_pac);
// Enable the compare interrupt
<$mode>::enable_interrupt::<$rtic_int>(&$rtc_pac);
$statics
$enable_interrupts
// Enable the RTC interrupt in the NVIC and set its priority.
// 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::rtc::rtic::set_monotonic_prio(pac::Interrupt::RTC);
pac::NVIC::unmask(pac::Interrupt::RTC);
}
});
}
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! __internal_basic_backend {
($name:ident, $mode:ty, $mode_num:literal, $rtic_int:ty) => {
use atsamd_hal_macros::hal_cfg;
use rtic_time::timer_queue::{TimerQueue, TimerQueueBackend};
use $crate::pac;
use $crate::rtc::modes::RtcMode;
#[doc = concat!("Basic RTC-based [`TimerQueueBackend`] without period counting that uses the RTC in mode ", stringify!($mode_num), ".")]
pub struct $name;
static RTC_TQ: TimerQueue<$name> = TimerQueue::new();
impl $name {
$crate::__internal_backend_methods! {
mode = $mode;
rtic_int = $rtic_int;
rtc_pac = rtc;
init_compares = {
// Set the the initial compare
<$mode>::set_compare(&rtc, 0, 0);
}
statics = {
// Initialize the timer queue
RTC_TQ.initialize(Self);
}
enable_interrupts = {
// Enable the compare interrupt
<$mode>::enable_interrupt::<$rtic_int>(&rtc);
}
}
}
impl TimerQueueBackend for $name {
type Ticks = <$mode as RtcMode>::Count;
fn now() -> Self::Ticks {
<$mode>::count(unsafe { &pac::Rtc::steal() })
}
fn enable_timer() {
<$mode>::enable(unsafe { &pac::Rtc::steal() });
}
fn disable_timer() {
<$mode>::disable(unsafe { &pac::Rtc::steal() });
}
fn on_interrupt() {
// There is nothing we need to do here
}
fn set_compare(mut instant: Self::Ticks) {
let rtc = unsafe { pac::Rtc::steal() };
// Evidently the compare interrupt will not trigger if the instant is within a
// couple of ticks, so delay it a bit if it is too close.
// This is not mentioned in the documentation or errata, but is known to be an
// issue for other microcontrollers as well (e.g. nRF family).
if instant.saturating_sub(Self::now())
< <$mode>::MIN_COMPARE_TICKS
{
instant = instant.wrapping_add(<$mode>::MIN_COMPARE_TICKS)
}
unsafe { <$mode>::set_compare(&rtc, 0, instant) };
}
fn clear_compare_flag() {
<$mode>::clear_interrupt_flag::<$rtic_int>(unsafe { &pac::Rtc::steal() });
}
fn pend_interrupt() {
pac::NVIC::pend(pac::Interrupt::RTC);
}
fn timer_queue() -> &'static TimerQueue<Self> {
&RTC_TQ
}
}
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! __internal_half_period_counting_backend {
($name:ident, $mode:ty, $mode_num:literal, $rtic_int:ty, $half_period_int:ty, $overflow_int:ty) => {
use atsamd_hal_macros::hal_cfg;
use core::sync::atomic::Ordering;
use portable_atomic::AtomicU64;
use rtic_time::{
half_period_counter::calculate_now,
timer_queue::{TimerQueue, TimerQueueBackend},
};
use $crate::pac;
struct TimerValue(<$mode as RtcMode>::Count);
impl rtic_time::half_period_counter::TimerValue for TimerValue {
const BITS: u32 = <$mode as RtcMode>::Count::BITS;
}
impl From<TimerValue> for u64 {
fn from(value: TimerValue) -> Self {
Self::from(value.0)
}
}
#[doc = concat!("An RTC-based [`TimerQueueBackend`] using [half-period counting](rtic_time::half_period_counter) that uses the RTC in mode ", stringify!($mode_num), ".")]
pub struct $name;
static RTC_PERIOD_COUNT: AtomicU64 = AtomicU64::new(0);
static RTC_TQ: TimerQueue<$name> = TimerQueue::new();
impl $name {
$crate::__internal_backend_methods! {
mode = $mode;
rtic_int = $rtic_int;
rtc_pac = rtc;
init_compares = {
// Configure the compare registers
<$mode>::set_compare(&rtc, 0, 0);
<$mode>::set_compare(&rtc, 1, <$mode>::HALF_PERIOD);
}
statics = {
// Make sure period counter is synced with the timer value
RTC_PERIOD_COUNT.store(0, Ordering::SeqCst);
// Initialize the timer queue
RTC_TQ.initialize(Self);
}
enable_interrupts = {
// Enable the compare and overflow interrupts.
<$mode>::enable_interrupt::<$rtic_int>(&rtc);
<$mode>::enable_interrupt::<$half_period_int>(&rtc);
<$mode>::enable_interrupt::<$overflow_int>(&rtc);
}
}
}
impl TimerQueueBackend for RtcBackend {
type Ticks = u64;
fn now() -> Self::Ticks {
let rtc = unsafe { pac::Rtc::steal() };
calculate_now(
|| RTC_PERIOD_COUNT.load(Ordering::Relaxed),
|| TimerValue(<$mode>::count(&rtc)),
)
}
fn enable_timer() {
let rtc = unsafe { pac::Rtc::steal() };
<$mode>::enable(&rtc);
}
fn disable_timer() {
let rtc = unsafe { pac::Rtc::steal() };
<$mode>::disable(&rtc);
}
fn on_interrupt() {
let rtc: pac::Rtc = unsafe { pac::Rtc::steal() };
// NOTE: The cmp0 flag is cleared when RTIC calls `clear_compare_flag`.
if <$mode>::check_interrupt_flag::<$half_period_int>(&rtc) {
<$mode>::clear_interrupt_flag::<$half_period_int>(&rtc);
let prev = RTC_PERIOD_COUNT.fetch_add(1, Ordering::Relaxed);
assert!(prev % 2 == 0, "Monotonic must have skipped an interrupt!");
// Ensure that the COUNT has crossed
// Due to syncing delay this may not be the case initially
while <$mode>::count(&rtc) < <$mode>::HALF_PERIOD {}
}
if <$mode>::check_interrupt_flag::<$overflow_int>(&rtc) {
<$mode>::clear_interrupt_flag::<$overflow_int>(&rtc);
let prev = RTC_PERIOD_COUNT.fetch_add(1, Ordering::Relaxed);
assert!(prev % 2 == 1, "Monotonic must have skipped an interrupt!");
// Ensure that the COUNT has wrapped
// Due to syncing delay this may not be the case initially
while <$mode>::count(&rtc) > <$mode>::HALF_PERIOD {}
}
}
fn set_compare(mut instant: Self::Ticks) {
let rtc = unsafe { pac::Rtc::steal() };
const MAX: u64 = <$mode as RtcMode>::Count::MAX as u64;
// Disable interrupts because this section is timing critical.
// We rely on the fact that this entire section runs within one
// RTC clock tick. (which it will do easily if it doesn't get
// interrupted)
critical_section::with(|_| {
let now = Self::now();
// Wrapping_sub deals with the u64 overflow corner case
let diff = instant.wrapping_sub(now);
let val = if diff <= MAX {
// Now we know `instant` will happen within one `MAX` time duration.
// Evidently the compare interrupt will not trigger if the instant is within a
// couple of ticks, so delay it a bit if it is too close.
// This is not mentioned in the documentation or errata, but is known to be an
// issue for other microcontrollers as well (e.g. nRF family).
if diff < <$mode>::MIN_COMPARE_TICKS.into() {
instant = instant
.wrapping_add(<$mode>::MIN_COMPARE_TICKS.into());
}
(instant & MAX) as <$mode as RtcMode>::Count
} else {
// Just wait a full hardware counter period
<$mode>::count(&rtc).wrapping_sub(1)
};
<$mode>::set_compare(&rtc, 0, val);
});
}
fn clear_compare_flag() {
let rtc = unsafe { pac::Rtc::steal() };
<$mode>::clear_interrupt_flag::<$rtic_int>(&rtc);
}
fn pend_interrupt() {
pac::NVIC::pend(pac::Interrupt::RTC);
}
fn timer_queue() -> &'static TimerQueue<Self> {
&RTC_TQ
}
}
};
}