aboutsummaryrefslogtreecommitdiff
path: root/embassy-nrf/src/rtc.rs
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2021-01-13 03:17:42 +0100
committerDario Nieuwenhuis <[email protected]>2021-01-13 03:17:42 +0100
commit5229a1991c0ac0a4eafa8a321a80aee21af96a2e (patch)
tree5168179b5ba9e172790b480c3cae962a40e5f3df /embassy-nrf/src/rtc.rs
parent7b94e06306a4bc0a1a31ded7de00eb30ab14ac25 (diff)
nrf/rtc: use interrupt handler context for instance ptr
Diffstat (limited to 'embassy-nrf/src/rtc.rs')
-rw-r--r--embassy-nrf/src/rtc.rs57
1 files changed, 28 insertions, 29 deletions
diff --git a/embassy-nrf/src/rtc.rs b/embassy-nrf/src/rtc.rs
index fb59faa31..f431339ff 100644
--- a/embassy-nrf/src/rtc.rs
+++ b/embassy-nrf/src/rtc.rs
@@ -2,14 +2,12 @@ use core::cell::Cell;
2use core::ops::Deref; 2use core::ops::Deref;
3use core::sync::atomic::{AtomicU32, Ordering}; 3use core::sync::atomic::{AtomicU32, Ordering};
4 4
5use embassy::time::{Clock, Instant}; 5use embassy::time::Clock;
6 6
7use crate::fmt::*;
7use crate::interrupt; 8use crate::interrupt;
8use crate::interrupt::{CriticalSection, Mutex, OwnedInterrupt}; 9use crate::interrupt::{CriticalSection, Mutex, OwnedInterrupt};
9use crate::pac::{rtc0, Interrupt, RTC0, RTC1}; 10use crate::pac::rtc0;
10
11#[cfg(any(feature = "52832", feature = "52833", feature = "52840"))]
12use crate::pac::RTC2;
13 11
14fn calc_now(period: u32, counter: u32) -> u64 { 12fn calc_now(period: u32, counter: u32) -> u64 {
15 let shift = ((period & 1) << 23) + 0x400000; 13 let shift = ((period & 1) << 23) + 0x400000;
@@ -104,10 +102,12 @@ impl<T: Instance> RTC<T> {
104 // Wait for clear 102 // Wait for clear
105 while self.rtc.counter.read().bits() != 0 {} 103 while self.rtc.counter.read().bits() != 0 {}
106 104
107 T::set_rtc_instance(self);
108 self.irq.set_handler( 105 self.irq.set_handler(
109 |_| T::get_rtc_instance().on_interrupt(), 106 |ptr| unsafe {
110 core::ptr::null_mut(), 107 let this = &*(ptr as *const () as *const Self);
108 this.on_interrupt();
109 },
110 self as *const _ as *mut _,
111 ); 111 );
112 self.irq.unpend(); 112 self.irq.unpend();
113 self.irq.enable(); 113 self.irq.enable();
@@ -238,33 +238,32 @@ impl<T: Instance> embassy::time::Alarm for Alarm<T> {
238 } 238 }
239} 239}
240 240
241mod sealed {
242 pub trait Instance {}
243
244 impl Instance for crate::pac::RTC0 {}
245 impl Instance for crate::pac::RTC1 {}
246 #[cfg(any(feature = "52832", feature = "52833", feature = "52840"))]
247 impl Instance for crate::pac::RTC2 {}
248}
249
241/// Implemented by all RTC instances. 250/// Implemented by all RTC instances.
242pub trait Instance: Deref<Target = rtc0::RegisterBlock> + Sized + 'static { 251pub trait Instance:
252 sealed::Instance + Deref<Target = rtc0::RegisterBlock> + Sized + 'static
253{
243 /// The interrupt associated with this RTC instance. 254 /// The interrupt associated with this RTC instance.
244 type Interrupt: OwnedInterrupt; 255 type Interrupt: OwnedInterrupt;
245
246 fn set_rtc_instance(rtc: &'static RTC<Self>);
247 fn get_rtc_instance() -> &'static RTC<Self>;
248} 256}
249 257
250macro_rules! impl_instance { 258impl Instance for crate::pac::RTC0 {
251 ($name:ident, $irq_name:path, $static_name:ident) => { 259 type Interrupt = interrupt::RTC0Interrupt;
252 static mut $static_name: Option<&'static RTC<$name>> = None;
253
254 impl Instance for $name {
255 type Interrupt = $irq_name;
256 fn set_rtc_instance(rtc: &'static RTC<Self>) {
257 unsafe { $static_name = Some(rtc) }
258 }
259 fn get_rtc_instance() -> &'static RTC<Self> {
260 unsafe { $static_name.unwrap() }
261 }
262 }
263 };
264} 260}
265 261
266impl_instance!(RTC0, interrupt::RTC0Interrupt, RTC0_INSTANCE); 262impl Instance for crate::pac::RTC1 {
267impl_instance!(RTC1, interrupt::RTC1Interrupt, RTC1_INSTANCE); 263 type Interrupt = interrupt::RTC1Interrupt;
264}
268 265
269#[cfg(any(feature = "52832", feature = "52833", feature = "52840"))] 266#[cfg(any(feature = "52832", feature = "52833", feature = "52840"))]
270impl_instance!(RTC2, interrupt::RTC2Interrupt, RTC2_INSTANCE); 267impl Instance for crate::pac::RTC2 {
268 type Interrupt = interrupt::RTC2Interrupt;
269}