Update RTC for owned irqs

This commit is contained in:
Dario Nieuwenhuis 2020-12-29 01:05:28 +01:00
parent 0750234fbe
commit 4b8d8ba87e
6 changed files with 40 additions and 34 deletions

View File

@ -56,6 +56,6 @@ pub(crate) mod fmt;
pub mod interrupt;
#[cfg(feature = "52840")]
pub mod qspi;
//pub mod rtc;
pub mod rtc;
pub use cortex_m_rt::interrupt;

View File

@ -23,8 +23,6 @@ pub use crate::pac::qspi::ifconfig0::WRITEOC_A as WriteOpcode;
use embassy::flash::{Error, Flash};
use embassy::util::{DropBomb, Signal};
use crate::interrupt;
pub struct Pins {
pub sck: GpioPin<Output<PushPull>>,
pub csn: GpioPin<Output<PushPull>>,

View File

@ -2,10 +2,10 @@ use core::cell::Cell;
use core::ops::Deref;
use core::sync::atomic::{AtomicU32, Ordering};
use embassy::time::Clock;
use embassy::time::{Clock, Instant};
use crate::interrupt;
use crate::interrupt::{CriticalSection, Mutex};
use crate::interrupt::{CriticalSection, Mutex, OwnedInterrupt};
use crate::pac::{rtc0, Interrupt, RTC0, RTC1};
#[cfg(any(feature = "52832", feature = "52833", feature = "52840"))]
@ -56,8 +56,9 @@ impl AlarmState {
const ALARM_COUNT: usize = 3;
pub struct RTC<T> {
pub struct RTC<T: Instance> {
rtc: T,
irq: T::Interrupt,
/// Number of 2^23 periods elapsed since boot.
///
@ -75,13 +76,14 @@ pub struct RTC<T> {
alarms: Mutex<[AlarmState; ALARM_COUNT]>,
}
unsafe impl<T> Send for RTC<T> {}
unsafe impl<T> Sync for RTC<T> {}
unsafe impl<T: Instance> Send for RTC<T> {}
unsafe impl<T: Instance> Sync for RTC<T> {}
impl<T: Instance> RTC<T> {
pub fn new(rtc: T) -> Self {
pub fn new(rtc: T, irq: T::Interrupt) -> Self {
Self {
rtc,
irq,
period: AtomicU32::new(0),
alarms: Mutex::new([AlarmState::new(), AlarmState::new(), AlarmState::new()]),
}
@ -103,7 +105,10 @@ impl<T: Instance> RTC<T> {
while self.rtc.counter.read().bits() != 0 {}
T::set_rtc_instance(self);
interrupt::enable(T::INTERRUPT);
self.irq
.set_handler(|| T::get_rtc_instance().on_interrupt());
self.irq.unpend();
self.irq.enable();
}
fn on_interrupt(&self) {
@ -234,18 +239,18 @@ impl<T: Instance> embassy::time::Alarm for Alarm<T> {
/// Implemented by all RTC instances.
pub trait Instance: Deref<Target = rtc0::RegisterBlock> + Sized + 'static {
/// The interrupt associated with this RTC instance.
const INTERRUPT: Interrupt;
type Interrupt: OwnedInterrupt;
fn set_rtc_instance(rtc: &'static RTC<Self>);
fn get_rtc_instance() -> &'static RTC<Self>;
}
macro_rules! impl_instance {
($name:ident, $static_name:ident) => {
($name:ident, $irq_name:path, $static_name:ident) => {
static mut $static_name: Option<&'static RTC<$name>> = None;
impl Instance for $name {
const INTERRUPT: Interrupt = Interrupt::$name;
type Interrupt = $irq_name;
fn set_rtc_instance(rtc: &'static RTC<Self>) {
unsafe { $static_name = Some(rtc) }
}
@ -253,16 +258,11 @@ macro_rules! impl_instance {
unsafe { $static_name.unwrap() }
}
}
#[interrupt]
fn $name() {
$name::get_rtc_instance().on_interrupt();
}
};
}
impl_instance!(RTC0, RTC0_INSTANCE);
impl_instance!(RTC1, RTC1_INSTANCE);
impl_instance!(RTC0, interrupt::RTC0Interrupt, RTC0_INSTANCE);
impl_instance!(RTC1, interrupt::RTC1Interrupt, RTC1_INSTANCE);
#[cfg(any(feature = "52832", feature = "52833", feature = "52840"))]
impl_instance!(RTC2, RTC2_INSTANCE);
impl_instance!(RTC2, interrupt::RTC2Interrupt, RTC2_INSTANCE);

View File

@ -61,7 +61,9 @@
mod example_common;
use example_common::*;
use cortex_m::peripheral::NVIC;
use cortex_m_rt::entry;
use defmt::panic;
use nrf52840_hal::clocks;
use embassy::executor::{task, Executor};
@ -130,7 +132,7 @@ fn main() -> ! {
.set_lfclk_src_external(clocks::LfOscConfiguration::NoExternalNoBypass)
.start_lfclk();
let rtc = RTC.put(rtc::RTC::new(p.RTC1));
let rtc = RTC.put(rtc::RTC::new(p.RTC1, interrupt::take!(RTC1)));
rtc.start();
unsafe { embassy::time::set_clock(rtc) };
@ -138,17 +140,20 @@ fn main() -> ! {
let executor_low = EXECUTOR_LOW.put(Executor::new_with_alarm(alarm_low, cortex_m::asm::sev));
let alarm_med = ALARM_MED.put(rtc.alarm1());
let executor_med = EXECUTOR_MED.put(Executor::new_with_alarm(alarm_med, || {
interrupt::pend(interrupt::SWI0_EGU0)
NVIC::pend(interrupt::SWI0_EGU0)
}));
let alarm_high = ALARM_HIGH.put(rtc.alarm2());
let executor_high = EXECUTOR_HIGH.put(Executor::new_with_alarm(alarm_high, || {
interrupt::pend(interrupt::SWI1_EGU1)
NVIC::pend(interrupt::SWI1_EGU1)
}));
interrupt::set_priority(interrupt::SWI0_EGU0, interrupt::Priority::Level7);
interrupt::set_priority(interrupt::SWI1_EGU1, interrupt::Priority::Level6);
interrupt::enable(interrupt::SWI0_EGU0);
interrupt::enable(interrupt::SWI1_EGU1);
unsafe {
let mut nvic: NVIC = core::mem::transmute(());
nvic.set_priority(interrupt::SWI0_EGU0, 7 << 5);
nvic.set_priority(interrupt::SWI1_EGU1, 6 << 5);
NVIC::unmask(interrupt::SWI0_EGU0);
NVIC::unmask(interrupt::SWI1_EGU1);
}
unwrap!(executor_low.spawn(run_low()));
unwrap!(executor_med.spawn(run_med()));

View File

@ -8,13 +8,13 @@ use example_common::*;
use core::mem::MaybeUninit;
use cortex_m_rt::entry;
use nrf52840_hal::clocks;
use defmt::panic;
use embassy::executor::{task, Executor};
use embassy::time::{Clock, Duration, Timer};
use embassy::util::Forever;
use embassy_nrf::pac;
use embassy_nrf::rtc;
use embassy_nrf::{interrupt, rtc};
use nrf52840_hal::clocks;
#[task]
async fn run1() {
@ -47,7 +47,7 @@ fn main() -> ! {
.set_lfclk_src_external(clocks::LfOscConfiguration::NoExternalNoBypass)
.start_lfclk();
let rtc = RTC.put(rtc::RTC::new(p.RTC1));
let rtc = RTC.put(rtc::RTC::new(p.RTC1, interrupt::take!(RTC1)));
rtc.start();
unsafe { embassy::time::set_clock(rtc) };

View File

@ -8,8 +8,9 @@ use example_common::*;
use core::mem::MaybeUninit;
use cortex_m_rt::entry;
use defmt::panic;
use embassy::time::{Alarm, Clock};
use embassy_nrf::rtc;
use embassy_nrf::{interrupt, rtc};
use nrf52840_hal::clocks;
static mut RTC: MaybeUninit<rtc::RTC<embassy_nrf::pac::RTC1>> = MaybeUninit::uninit();
@ -25,9 +26,11 @@ fn main() -> ! {
.set_lfclk_src_external(clocks::LfOscConfiguration::NoExternalNoBypass)
.start_lfclk();
let irq = interrupt::take!(RTC1);
let rtc: &'static _ = unsafe {
let ptr = RTC.as_mut_ptr();
ptr.write(rtc::RTC::new(p.RTC1));
ptr.write(rtc::RTC::new(p.RTC1, irq));
&*ptr
};