aboutsummaryrefslogtreecommitdiff
path: root/embassy-nrf/src/rtc.rs
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2020-12-29 15:32:23 +0100
committerGitHub <[email protected]>2020-12-29 15:32:23 +0100
commit2bf9b14ef07c4d2a33ee8a45b2f07b4cdd050e9e (patch)
tree8229727035f1aa26a9477fa702f02dd53592dd99 /embassy-nrf/src/rtc.rs
parentb01a88a839c2ac1eec68442f6cddf0c0a887cfa7 (diff)
parent7dc81faa4ec46074c3500a868df18e0d123f0ba6 (diff)
Merge pull request #7 from akiles/irq
Owned IRQs
Diffstat (limited to 'embassy-nrf/src/rtc.rs')
-rw-r--r--embassy-nrf/src/rtc.rs36
1 files changed, 18 insertions, 18 deletions
diff --git a/embassy-nrf/src/rtc.rs b/embassy-nrf/src/rtc.rs
index 66e2f552d..d65b8d472 100644
--- a/embassy-nrf/src/rtc.rs
+++ b/embassy-nrf/src/rtc.rs
@@ -2,10 +2,10 @@ 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; 5use embassy::time::{Clock, Instant};
6 6
7use crate::interrupt; 7use crate::interrupt;
8use crate::interrupt::{CriticalSection, Mutex}; 8use crate::interrupt::{CriticalSection, Mutex, OwnedInterrupt};
9use crate::pac::{rtc0, Interrupt, RTC0, RTC1}; 9use crate::pac::{rtc0, Interrupt, RTC0, RTC1};
10 10
11#[cfg(any(feature = "52832", feature = "52833", feature = "52840"))] 11#[cfg(any(feature = "52832", feature = "52833", feature = "52840"))]
@@ -56,8 +56,9 @@ impl AlarmState {
56 56
57const ALARM_COUNT: usize = 3; 57const ALARM_COUNT: usize = 3;
58 58
59pub struct RTC<T> { 59pub struct RTC<T: Instance> {
60 rtc: T, 60 rtc: T,
61 irq: T::Interrupt,
61 62
62 /// Number of 2^23 periods elapsed since boot. 63 /// Number of 2^23 periods elapsed since boot.
63 /// 64 ///
@@ -75,13 +76,14 @@ pub struct RTC<T> {
75 alarms: Mutex<[AlarmState; ALARM_COUNT]>, 76 alarms: Mutex<[AlarmState; ALARM_COUNT]>,
76} 77}
77 78
78unsafe impl<T> Send for RTC<T> {} 79unsafe impl<T: Instance> Send for RTC<T> {}
79unsafe impl<T> Sync for RTC<T> {} 80unsafe impl<T: Instance> Sync for RTC<T> {}
80 81
81impl<T: Instance> RTC<T> { 82impl<T: Instance> RTC<T> {
82 pub fn new(rtc: T) -> Self { 83 pub fn new(rtc: T, irq: T::Interrupt) -> Self {
83 Self { 84 Self {
84 rtc, 85 rtc,
86 irq,
85 period: AtomicU32::new(0), 87 period: AtomicU32::new(0),
86 alarms: Mutex::new([AlarmState::new(), AlarmState::new(), AlarmState::new()]), 88 alarms: Mutex::new([AlarmState::new(), AlarmState::new(), AlarmState::new()]),
87 } 89 }
@@ -103,7 +105,10 @@ impl<T: Instance> RTC<T> {
103 while self.rtc.counter.read().bits() != 0 {} 105 while self.rtc.counter.read().bits() != 0 {}
104 106
105 T::set_rtc_instance(self); 107 T::set_rtc_instance(self);
106 interrupt::enable(T::INTERRUPT); 108 self.irq
109 .set_handler(|| T::get_rtc_instance().on_interrupt());
110 self.irq.unpend();
111 self.irq.enable();
107 } 112 }
108 113
109 fn on_interrupt(&self) { 114 fn on_interrupt(&self) {
@@ -234,18 +239,18 @@ impl<T: Instance> embassy::time::Alarm for Alarm<T> {
234/// Implemented by all RTC instances. 239/// Implemented by all RTC instances.
235pub trait Instance: Deref<Target = rtc0::RegisterBlock> + Sized + 'static { 240pub trait Instance: Deref<Target = rtc0::RegisterBlock> + Sized + 'static {
236 /// The interrupt associated with this RTC instance. 241 /// The interrupt associated with this RTC instance.
237 const INTERRUPT: Interrupt; 242 type Interrupt: OwnedInterrupt;
238 243
239 fn set_rtc_instance(rtc: &'static RTC<Self>); 244 fn set_rtc_instance(rtc: &'static RTC<Self>);
240 fn get_rtc_instance() -> &'static RTC<Self>; 245 fn get_rtc_instance() -> &'static RTC<Self>;
241} 246}
242 247
243macro_rules! impl_instance { 248macro_rules! impl_instance {
244 ($name:ident, $static_name:ident) => { 249 ($name:ident, $irq_name:path, $static_name:ident) => {
245 static mut $static_name: Option<&'static RTC<$name>> = None; 250 static mut $static_name: Option<&'static RTC<$name>> = None;
246 251
247 impl Instance for $name { 252 impl Instance for $name {
248 const INTERRUPT: Interrupt = Interrupt::$name; 253 type Interrupt = $irq_name;
249 fn set_rtc_instance(rtc: &'static RTC<Self>) { 254 fn set_rtc_instance(rtc: &'static RTC<Self>) {
250 unsafe { $static_name = Some(rtc) } 255 unsafe { $static_name = Some(rtc) }
251 } 256 }
@@ -253,16 +258,11 @@ macro_rules! impl_instance {
253 unsafe { $static_name.unwrap() } 258 unsafe { $static_name.unwrap() }
254 } 259 }
255 } 260 }
256
257 #[interrupt]
258 fn $name() {
259 $name::get_rtc_instance().on_interrupt();
260 }
261 }; 261 };
262} 262}
263 263
264impl_instance!(RTC0, RTC0_INSTANCE); 264impl_instance!(RTC0, interrupt::RTC0Interrupt, RTC0_INSTANCE);
265impl_instance!(RTC1, RTC1_INSTANCE); 265impl_instance!(RTC1, interrupt::RTC1Interrupt, RTC1_INSTANCE);
266 266
267#[cfg(any(feature = "52832", feature = "52833", feature = "52840"))] 267#[cfg(any(feature = "52832", feature = "52833", feature = "52840"))]
268impl_instance!(RTC2, RTC2_INSTANCE); 268impl_instance!(RTC2, interrupt::RTC2Interrupt, RTC2_INSTANCE);