diff options
| author | Dario Nieuwenhuis <[email protected]> | 2020-12-29 01:05:28 +0100 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2020-12-29 01:05:28 +0100 |
| commit | 4b8d8ba87ee26173b0a7743c606c76df2d171790 (patch) | |
| tree | 0e8106a564d3db25c340df170592b1e0a54d8c59 | |
| parent | 0750234fbead723138d6d1ebb0635a55c82923e0 (diff) | |
Update RTC for owned irqs
| -rw-r--r-- | embassy-nrf/src/lib.rs | 2 | ||||
| -rw-r--r-- | embassy-nrf/src/qspi.rs | 2 | ||||
| -rw-r--r-- | embassy-nrf/src/rtc.rs | 36 | ||||
| -rw-r--r-- | examples/src/bin/multiprio.rs | 19 | ||||
| -rw-r--r-- | examples/src/bin/rtc_async.rs | 8 | ||||
| -rw-r--r-- | examples/src/bin/rtc_raw.rs | 7 |
6 files changed, 40 insertions, 34 deletions
diff --git a/embassy-nrf/src/lib.rs b/embassy-nrf/src/lib.rs index ccfcc0681..c63bfd995 100644 --- a/embassy-nrf/src/lib.rs +++ b/embassy-nrf/src/lib.rs | |||
| @@ -56,6 +56,6 @@ pub(crate) mod fmt; | |||
| 56 | pub mod interrupt; | 56 | pub mod interrupt; |
| 57 | #[cfg(feature = "52840")] | 57 | #[cfg(feature = "52840")] |
| 58 | pub mod qspi; | 58 | pub mod qspi; |
| 59 | //pub mod rtc; | 59 | pub mod rtc; |
| 60 | 60 | ||
| 61 | pub use cortex_m_rt::interrupt; | 61 | pub use cortex_m_rt::interrupt; |
diff --git a/embassy-nrf/src/qspi.rs b/embassy-nrf/src/qspi.rs index 8833afbdc..c9c907cd1 100644 --- a/embassy-nrf/src/qspi.rs +++ b/embassy-nrf/src/qspi.rs | |||
| @@ -23,8 +23,6 @@ pub use crate::pac::qspi::ifconfig0::WRITEOC_A as WriteOpcode; | |||
| 23 | use embassy::flash::{Error, Flash}; | 23 | use embassy::flash::{Error, Flash}; |
| 24 | use embassy::util::{DropBomb, Signal}; | 24 | use embassy::util::{DropBomb, Signal}; |
| 25 | 25 | ||
| 26 | use crate::interrupt; | ||
| 27 | |||
| 28 | pub struct Pins { | 26 | pub struct Pins { |
| 29 | pub sck: GpioPin<Output<PushPull>>, | 27 | pub sck: GpioPin<Output<PushPull>>, |
| 30 | pub csn: GpioPin<Output<PushPull>>, | 28 | pub csn: GpioPin<Output<PushPull>>, |
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); |
diff --git a/examples/src/bin/multiprio.rs b/examples/src/bin/multiprio.rs index be7e91a9d..c821e3dba 100644 --- a/examples/src/bin/multiprio.rs +++ b/examples/src/bin/multiprio.rs | |||
| @@ -61,7 +61,9 @@ | |||
| 61 | mod example_common; | 61 | mod example_common; |
| 62 | use example_common::*; | 62 | use example_common::*; |
| 63 | 63 | ||
| 64 | use cortex_m::peripheral::NVIC; | ||
| 64 | use cortex_m_rt::entry; | 65 | use cortex_m_rt::entry; |
| 66 | use defmt::panic; | ||
| 65 | use nrf52840_hal::clocks; | 67 | use nrf52840_hal::clocks; |
| 66 | 68 | ||
| 67 | use embassy::executor::{task, Executor}; | 69 | use embassy::executor::{task, Executor}; |
| @@ -130,7 +132,7 @@ fn main() -> ! { | |||
| 130 | .set_lfclk_src_external(clocks::LfOscConfiguration::NoExternalNoBypass) | 132 | .set_lfclk_src_external(clocks::LfOscConfiguration::NoExternalNoBypass) |
| 131 | .start_lfclk(); | 133 | .start_lfclk(); |
| 132 | 134 | ||
| 133 | let rtc = RTC.put(rtc::RTC::new(p.RTC1)); | 135 | let rtc = RTC.put(rtc::RTC::new(p.RTC1, interrupt::take!(RTC1))); |
| 134 | rtc.start(); | 136 | rtc.start(); |
| 135 | unsafe { embassy::time::set_clock(rtc) }; | 137 | unsafe { embassy::time::set_clock(rtc) }; |
| 136 | 138 | ||
| @@ -138,17 +140,20 @@ fn main() -> ! { | |||
| 138 | let executor_low = EXECUTOR_LOW.put(Executor::new_with_alarm(alarm_low, cortex_m::asm::sev)); | 140 | let executor_low = EXECUTOR_LOW.put(Executor::new_with_alarm(alarm_low, cortex_m::asm::sev)); |
| 139 | let alarm_med = ALARM_MED.put(rtc.alarm1()); | 141 | let alarm_med = ALARM_MED.put(rtc.alarm1()); |
| 140 | let executor_med = EXECUTOR_MED.put(Executor::new_with_alarm(alarm_med, || { | 142 | let executor_med = EXECUTOR_MED.put(Executor::new_with_alarm(alarm_med, || { |
| 141 | interrupt::pend(interrupt::SWI0_EGU0) | 143 | NVIC::pend(interrupt::SWI0_EGU0) |
| 142 | })); | 144 | })); |
| 143 | let alarm_high = ALARM_HIGH.put(rtc.alarm2()); | 145 | let alarm_high = ALARM_HIGH.put(rtc.alarm2()); |
| 144 | let executor_high = EXECUTOR_HIGH.put(Executor::new_with_alarm(alarm_high, || { | 146 | let executor_high = EXECUTOR_HIGH.put(Executor::new_with_alarm(alarm_high, || { |
| 145 | interrupt::pend(interrupt::SWI1_EGU1) | 147 | NVIC::pend(interrupt::SWI1_EGU1) |
| 146 | })); | 148 | })); |
| 147 | 149 | ||
| 148 | interrupt::set_priority(interrupt::SWI0_EGU0, interrupt::Priority::Level7); | 150 | unsafe { |
| 149 | interrupt::set_priority(interrupt::SWI1_EGU1, interrupt::Priority::Level6); | 151 | let mut nvic: NVIC = core::mem::transmute(()); |
| 150 | interrupt::enable(interrupt::SWI0_EGU0); | 152 | nvic.set_priority(interrupt::SWI0_EGU0, 7 << 5); |
| 151 | interrupt::enable(interrupt::SWI1_EGU1); | 153 | nvic.set_priority(interrupt::SWI1_EGU1, 6 << 5); |
| 154 | NVIC::unmask(interrupt::SWI0_EGU0); | ||
| 155 | NVIC::unmask(interrupt::SWI1_EGU1); | ||
| 156 | } | ||
| 152 | 157 | ||
| 153 | unwrap!(executor_low.spawn(run_low())); | 158 | unwrap!(executor_low.spawn(run_low())); |
| 154 | unwrap!(executor_med.spawn(run_med())); | 159 | unwrap!(executor_med.spawn(run_med())); |
diff --git a/examples/src/bin/rtc_async.rs b/examples/src/bin/rtc_async.rs index aec70a072..dcdeb7049 100644 --- a/examples/src/bin/rtc_async.rs +++ b/examples/src/bin/rtc_async.rs | |||
| @@ -8,13 +8,13 @@ use example_common::*; | |||
| 8 | 8 | ||
| 9 | use core::mem::MaybeUninit; | 9 | use core::mem::MaybeUninit; |
| 10 | use cortex_m_rt::entry; | 10 | use cortex_m_rt::entry; |
| 11 | use nrf52840_hal::clocks; | 11 | use defmt::panic; |
| 12 | |||
| 13 | use embassy::executor::{task, Executor}; | 12 | use embassy::executor::{task, Executor}; |
| 14 | use embassy::time::{Clock, Duration, Timer}; | 13 | use embassy::time::{Clock, Duration, Timer}; |
| 15 | use embassy::util::Forever; | 14 | use embassy::util::Forever; |
| 16 | use embassy_nrf::pac; | 15 | use embassy_nrf::pac; |
| 17 | use embassy_nrf::rtc; | 16 | use embassy_nrf::{interrupt, rtc}; |
| 17 | use nrf52840_hal::clocks; | ||
| 18 | 18 | ||
| 19 | #[task] | 19 | #[task] |
| 20 | async fn run1() { | 20 | async fn run1() { |
| @@ -47,7 +47,7 @@ fn main() -> ! { | |||
| 47 | .set_lfclk_src_external(clocks::LfOscConfiguration::NoExternalNoBypass) | 47 | .set_lfclk_src_external(clocks::LfOscConfiguration::NoExternalNoBypass) |
| 48 | .start_lfclk(); | 48 | .start_lfclk(); |
| 49 | 49 | ||
| 50 | let rtc = RTC.put(rtc::RTC::new(p.RTC1)); | 50 | let rtc = RTC.put(rtc::RTC::new(p.RTC1, interrupt::take!(RTC1))); |
| 51 | rtc.start(); | 51 | rtc.start(); |
| 52 | 52 | ||
| 53 | unsafe { embassy::time::set_clock(rtc) }; | 53 | unsafe { embassy::time::set_clock(rtc) }; |
diff --git a/examples/src/bin/rtc_raw.rs b/examples/src/bin/rtc_raw.rs index ad5fab246..438585460 100644 --- a/examples/src/bin/rtc_raw.rs +++ b/examples/src/bin/rtc_raw.rs | |||
| @@ -8,8 +8,9 @@ use example_common::*; | |||
| 8 | 8 | ||
| 9 | use core::mem::MaybeUninit; | 9 | use core::mem::MaybeUninit; |
| 10 | use cortex_m_rt::entry; | 10 | use cortex_m_rt::entry; |
| 11 | use defmt::panic; | ||
| 11 | use embassy::time::{Alarm, Clock}; | 12 | use embassy::time::{Alarm, Clock}; |
| 12 | use embassy_nrf::rtc; | 13 | use embassy_nrf::{interrupt, rtc}; |
| 13 | use nrf52840_hal::clocks; | 14 | use nrf52840_hal::clocks; |
| 14 | 15 | ||
| 15 | static mut RTC: MaybeUninit<rtc::RTC<embassy_nrf::pac::RTC1>> = MaybeUninit::uninit(); | 16 | static mut RTC: MaybeUninit<rtc::RTC<embassy_nrf::pac::RTC1>> = MaybeUninit::uninit(); |
| @@ -25,9 +26,11 @@ fn main() -> ! { | |||
| 25 | .set_lfclk_src_external(clocks::LfOscConfiguration::NoExternalNoBypass) | 26 | .set_lfclk_src_external(clocks::LfOscConfiguration::NoExternalNoBypass) |
| 26 | .start_lfclk(); | 27 | .start_lfclk(); |
| 27 | 28 | ||
| 29 | let irq = interrupt::take!(RTC1); | ||
| 30 | |||
| 28 | let rtc: &'static _ = unsafe { | 31 | let rtc: &'static _ = unsafe { |
| 29 | let ptr = RTC.as_mut_ptr(); | 32 | let ptr = RTC.as_mut_ptr(); |
| 30 | ptr.write(rtc::RTC::new(p.RTC1)); | 33 | ptr.write(rtc::RTC::new(p.RTC1, irq)); |
| 31 | &*ptr | 34 | &*ptr |
| 32 | }; | 35 | }; |
| 33 | 36 | ||
