diff options
| author | Dario Nieuwenhuis <[email protected]> | 2020-12-29 15:32:23 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2020-12-29 15:32:23 +0100 |
| commit | 2bf9b14ef07c4d2a33ee8a45b2f07b4cdd050e9e (patch) | |
| tree | 8229727035f1aa26a9477fa702f02dd53592dd99 /embassy-nrf/src/rtc.rs | |
| parent | b01a88a839c2ac1eec68442f6cddf0c0a887cfa7 (diff) | |
| parent | 7dc81faa4ec46074c3500a868df18e0d123f0ba6 (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.rs | 36 |
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; | |||
| 2 | use core::ops::Deref; | 2 | use core::ops::Deref; |
| 3 | use core::sync::atomic::{AtomicU32, Ordering}; | 3 | use core::sync::atomic::{AtomicU32, Ordering}; |
| 4 | 4 | ||
| 5 | use embassy::time::Clock; | 5 | use embassy::time::{Clock, Instant}; |
| 6 | 6 | ||
| 7 | use crate::interrupt; | 7 | use crate::interrupt; |
| 8 | use crate::interrupt::{CriticalSection, Mutex}; | 8 | use crate::interrupt::{CriticalSection, Mutex, OwnedInterrupt}; |
| 9 | use crate::pac::{rtc0, Interrupt, RTC0, RTC1}; | 9 | use 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 | ||
| 57 | const ALARM_COUNT: usize = 3; | 57 | const ALARM_COUNT: usize = 3; |
| 58 | 58 | ||
| 59 | pub struct RTC<T> { | 59 | pub 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 | ||
| 78 | unsafe impl<T> Send for RTC<T> {} | 79 | unsafe impl<T: Instance> Send for RTC<T> {} |
| 79 | unsafe impl<T> Sync for RTC<T> {} | 80 | unsafe impl<T: Instance> Sync for RTC<T> {} |
| 80 | 81 | ||
| 81 | impl<T: Instance> RTC<T> { | 82 | impl<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. |
| 235 | pub trait Instance: Deref<Target = rtc0::RegisterBlock> + Sized + 'static { | 240 | pub 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 | ||
| 243 | macro_rules! impl_instance { | 248 | macro_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 | ||
| 264 | impl_instance!(RTC0, RTC0_INSTANCE); | 264 | impl_instance!(RTC0, interrupt::RTC0Interrupt, RTC0_INSTANCE); |
| 265 | impl_instance!(RTC1, RTC1_INSTANCE); | 265 | impl_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"))] |
| 268 | impl_instance!(RTC2, RTC2_INSTANCE); | 268 | impl_instance!(RTC2, interrupt::RTC2Interrupt, RTC2_INSTANCE); |
