From 5646926cca8fcb1d3286199bfe54a3666eae30c9 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Sat, 27 Mar 2021 03:12:58 +0100 Subject: [PATCH] nrf/rtc: update to new api --- embassy-extras/src/macros.rs | 23 +-- embassy-nrf-examples/src/bin/buffered_uart.rs | 1 + .../src/bin/executor_fairness_test.rs | 12 +- .../src/bin/gpiote_channel.rs | 1 + embassy-nrf-examples/src/bin/gpiote_port.rs | 11 +- embassy-nrf-examples/src/bin/multiprio.rs | 17 ++- embassy-nrf-examples/src/bin/qspi.rs | 15 +- embassy-nrf-examples/src/bin/raw_spawn.rs | 10 +- embassy-nrf-examples/src/bin/rtc_raw.rs | 62 -------- embassy-nrf-examples/src/bin/spim.rs | 22 +-- .../src/bin/{rtc_async.rs => timer.rs} | 15 +- embassy-nrf-examples/src/bin/uart.rs | 22 +-- embassy-nrf/src/lib.rs | 136 +++++++++--------- embassy-nrf/src/rtc.rs | 86 +++++------ 14 files changed, 197 insertions(+), 236 deletions(-) delete mode 100644 embassy-nrf-examples/src/bin/rtc_raw.rs rename embassy-nrf-examples/src/bin/{rtc_async.rs => timer.rs} (80%) diff --git a/embassy-extras/src/macros.rs b/embassy-extras/src/macros.rs index a0464378..0659a0b7 100644 --- a/embassy-extras/src/macros.rs +++ b/embassy-extras/src/macros.rs @@ -1,14 +1,14 @@ #[macro_export] macro_rules! peripherals { - ($($(#[$cfg:meta])? $name:ident: $type:ident),*$(,)?) => { + ($($(#[$cfg:meta])? $name:ident),*$(,)?) => { pub mod peripherals { $( $(#[$cfg])? #[allow(non_camel_case_types)] - pub struct $type { _private: () } + pub struct $name { _private: () } $(#[$cfg])? - impl embassy::util::Steal for $type { + impl embassy::util::Steal for $name { #[inline] unsafe fn steal() -> Self { Self{ _private: ()} @@ -16,29 +16,30 @@ macro_rules! peripherals { } $(#[$cfg])? - impl embassy::util::PeripheralBorrow for $type { - type Target = $type; + impl embassy::util::PeripheralBorrow for $name { + type Target = $name; #[inline] - unsafe fn unborrow(self) -> $type { + unsafe fn unborrow(self) -> $name { self } } $(#[$cfg])? - impl embassy::util::PeripheralBorrow for &mut $type { - type Target = $type; + impl embassy::util::PeripheralBorrow for &mut $name { + type Target = $name; #[inline] - unsafe fn unborrow(self) -> $type { + unsafe fn unborrow(self) -> $name { ::core::ptr::read(self) } } )* } + #[allow(non_snake_case)] pub struct Peripherals { $( $(#[$cfg])? - pub $name: peripherals::$type, + pub $name: peripherals::$name, )* } @@ -66,7 +67,7 @@ macro_rules! peripherals { Self { $( $(#[$cfg])? - $name: ::steal(), + $name: ::steal(), )* } } diff --git a/embassy-nrf-examples/src/bin/buffered_uart.rs b/embassy-nrf-examples/src/bin/buffered_uart.rs index 71e9b4a7..c7017128 100644 --- a/embassy-nrf-examples/src/bin/buffered_uart.rs +++ b/embassy-nrf-examples/src/bin/buffered_uart.rs @@ -3,6 +3,7 @@ #![feature(min_type_alias_impl_trait)] #![feature(impl_trait_in_bindings)] #![feature(type_alias_impl_trait)] +#![allow(incomplete_features)] #[path = "../example_common.rs"] mod example_common; diff --git a/embassy-nrf-examples/src/bin/executor_fairness_test.rs b/embassy-nrf-examples/src/bin/executor_fairness_test.rs index 6d0a311d..cfcfd949 100644 --- a/embassy-nrf-examples/src/bin/executor_fairness_test.rs +++ b/embassy-nrf-examples/src/bin/executor_fairness_test.rs @@ -3,18 +3,20 @@ #![feature(min_type_alias_impl_trait)] #![feature(impl_trait_in_bindings)] #![feature(type_alias_impl_trait)] +#![allow(incomplete_features)] #[path = "../example_common.rs"] mod example_common; use example_common::*; +use core::mem; use core::task::Poll; use cortex_m_rt::entry; use defmt::panic; use embassy::executor::{task, Executor}; use embassy::time::{Duration, Instant, Timer}; use embassy::util::Forever; -use embassy_nrf::pac; +use embassy_nrf::peripherals; use embassy_nrf::{interrupt, rtc}; use nrf52840_hal::clocks; @@ -42,17 +44,17 @@ async fn run3() { .await; } -static RTC: Forever> = Forever::new(); -static ALARM: Forever> = Forever::new(); +static RTC: Forever> = Forever::new(); +static ALARM: Forever> = Forever::new(); static EXECUTOR: Forever = Forever::new(); #[entry] fn main() -> ! { info!("Hello World!"); - let p = unwrap!(embassy_nrf::pac::Peripherals::take()); + let p = unwrap!(embassy_nrf::Peripherals::take()); - clocks::Clocks::new(p.CLOCK) + clocks::Clocks::new(unsafe { mem::transmute(()) }) .enable_ext_hfosc() .set_lfclk_src_external(clocks::LfOscConfiguration::NoExternalNoBypass) .start_lfclk(); diff --git a/embassy-nrf-examples/src/bin/gpiote_channel.rs b/embassy-nrf-examples/src/bin/gpiote_channel.rs index 3764ba1c..599882a9 100644 --- a/embassy-nrf-examples/src/bin/gpiote_channel.rs +++ b/embassy-nrf-examples/src/bin/gpiote_channel.rs @@ -3,6 +3,7 @@ #![feature(min_type_alias_impl_trait)] #![feature(impl_trait_in_bindings)] #![feature(type_alias_impl_trait)] +#![allow(incomplete_features)] #[path = "../example_common.rs"] mod example_common; diff --git a/embassy-nrf-examples/src/bin/gpiote_port.rs b/embassy-nrf-examples/src/bin/gpiote_port.rs index 556a199f..199a33da 100644 --- a/embassy-nrf-examples/src/bin/gpiote_port.rs +++ b/embassy-nrf-examples/src/bin/gpiote_port.rs @@ -3,6 +3,7 @@ #![feature(min_type_alias_impl_trait)] #![feature(impl_trait_in_bindings)] #![feature(type_alias_impl_trait)] +#![allow(incomplete_features)] #[path = "../example_common.rs"] mod example_common; @@ -32,23 +33,23 @@ async fn button(n: usize, mut pin: PortInput<'static, AnyPin>) { async fn run() { let p = Peripherals::take().unwrap(); - let g = gpiote::initialize(p.gpiote, interrupt::take!(GPIOTE)); + let g = gpiote::initialize(p.GPIOTE, interrupt::take!(GPIOTE)); let button1 = button( 1, - PortInput::new(g, Input::new(p.p0_11.degrade(), Pull::Up)), + PortInput::new(g, Input::new(p.P0_11.degrade(), Pull::Up)), ); let button2 = button( 2, - PortInput::new(g, Input::new(p.p0_12.degrade(), Pull::Up)), + PortInput::new(g, Input::new(p.P0_12.degrade(), Pull::Up)), ); let button3 = button( 3, - PortInput::new(g, Input::new(p.p0_24.degrade(), Pull::Up)), + PortInput::new(g, Input::new(p.P0_24.degrade(), Pull::Up)), ); let button4 = button( 4, - PortInput::new(g, Input::new(p.p0_25.degrade(), Pull::Up)), + PortInput::new(g, Input::new(p.P0_25.degrade(), Pull::Up)), ); futures::join!(button1, button2, button3, button4); } diff --git a/embassy-nrf-examples/src/bin/multiprio.rs b/embassy-nrf-examples/src/bin/multiprio.rs index 65c64621..017de3c0 100644 --- a/embassy-nrf-examples/src/bin/multiprio.rs +++ b/embassy-nrf-examples/src/bin/multiprio.rs @@ -58,9 +58,12 @@ #![feature(min_type_alias_impl_trait)] #![feature(impl_trait_in_bindings)] #![feature(type_alias_impl_trait)] +#![allow(incomplete_features)] #[path = "../example_common.rs"] mod example_common; +use core::mem; + use example_common::*; use cortex_m_rt::entry; @@ -71,7 +74,7 @@ use embassy::executor::{task, Executor, InterruptExecutor}; use embassy::interrupt::InterruptExt; use embassy::time::{Duration, Instant, Timer}; use embassy::util::Forever; -use embassy_nrf::{interrupt, pac, rtc}; +use embassy_nrf::{interrupt, peripherals, rtc}; #[task] async fn run_high() { @@ -115,21 +118,21 @@ async fn run_low() { } } -static RTC: Forever> = Forever::new(); -static ALARM_HIGH: Forever> = Forever::new(); +static RTC: Forever> = Forever::new(); +static ALARM_HIGH: Forever> = Forever::new(); static EXECUTOR_HIGH: Forever> = Forever::new(); -static ALARM_MED: Forever> = Forever::new(); +static ALARM_MED: Forever> = Forever::new(); static EXECUTOR_MED: Forever> = Forever::new(); -static ALARM_LOW: Forever> = Forever::new(); +static ALARM_LOW: Forever> = Forever::new(); static EXECUTOR_LOW: Forever = Forever::new(); #[entry] fn main() -> ! { info!("Hello World!"); - let p = unwrap!(embassy_nrf::pac::Peripherals::take()); + let p = unwrap!(embassy_nrf::Peripherals::take()); - clocks::Clocks::new(p.CLOCK) + clocks::Clocks::new(unsafe { mem::transmute(()) }) .enable_ext_hfosc() .set_lfclk_src_external(clocks::LfOscConfiguration::NoExternalNoBypass) .start_lfclk(); diff --git a/embassy-nrf-examples/src/bin/qspi.rs b/embassy-nrf-examples/src/bin/qspi.rs index 1637b397..4e24a13d 100644 --- a/embassy-nrf-examples/src/bin/qspi.rs +++ b/embassy-nrf-examples/src/bin/qspi.rs @@ -3,6 +3,7 @@ #![feature(min_type_alias_impl_trait)] #![feature(impl_trait_in_bindings)] #![feature(type_alias_impl_trait)] +#![allow(incomplete_features)] #[path = "../example_common.rs"] mod example_common; @@ -28,12 +29,12 @@ struct AlignedBuf([u8; 4096]); async fn run() { let p = Peripherals::take().unwrap(); - let csn = p.p0_17; - let sck = p.p0_19; - let io0 = p.p0_20; - let io1 = p.p0_21; - let io2 = p.p0_22; - let io3 = p.p0_23; + let csn = p.P0_17; + let sck = p.P0_19; + let io0 = p.P0_20; + let io1 = p.P0_21; + let io2 = p.P0_22; + let io3 = p.P0_23; let config = qspi::Config { read_opcode: qspi::ReadOpcode::READ4IO, @@ -44,7 +45,7 @@ async fn run() { }; let irq = interrupt::take!(QSPI); - let q = qspi::Qspi::new(p.qspi, irq, sck, csn, io0, io1, io2, io3, config); + let q = qspi::Qspi::new(p.QSPI, irq, sck, csn, io0, io1, io2, io3, config); pin_mut!(q); let mut id = [1; 3]; diff --git a/embassy-nrf-examples/src/bin/raw_spawn.rs b/embassy-nrf-examples/src/bin/raw_spawn.rs index 3747b49f..1dd60403 100644 --- a/embassy-nrf-examples/src/bin/raw_spawn.rs +++ b/embassy-nrf-examples/src/bin/raw_spawn.rs @@ -13,7 +13,7 @@ use defmt::panic; use embassy::executor::Executor; use embassy::time::{Duration, Timer}; use embassy::util::Forever; -use embassy_nrf::pac; +use embassy_nrf::peripherals; use embassy_nrf::{interrupt, rtc}; use nrf52840_hal::clocks; @@ -31,17 +31,17 @@ async fn run2() { } } -static RTC: Forever> = Forever::new(); -static ALARM: Forever> = Forever::new(); +static RTC: Forever> = Forever::new(); +static ALARM: Forever> = Forever::new(); static EXECUTOR: Forever = Forever::new(); #[entry] fn main() -> ! { info!("Hello World!"); - let p = unwrap!(embassy_nrf::pac::Peripherals::take()); + let p = unwrap!(embassy_nrf::Peripherals::take()); - clocks::Clocks::new(p.CLOCK) + clocks::Clocks::new(unsafe { mem::transmute(()) }) .enable_ext_hfosc() .set_lfclk_src_external(clocks::LfOscConfiguration::NoExternalNoBypass) .start_lfclk(); diff --git a/embassy-nrf-examples/src/bin/rtc_raw.rs b/embassy-nrf-examples/src/bin/rtc_raw.rs deleted file mode 100644 index 884ca92b..00000000 --- a/embassy-nrf-examples/src/bin/rtc_raw.rs +++ /dev/null @@ -1,62 +0,0 @@ -#![no_std] -#![no_main] -#![feature(min_type_alias_impl_trait)] -#![feature(impl_trait_in_bindings)] -#![feature(type_alias_impl_trait)] - -#[path = "../example_common.rs"] -mod example_common; -use example_common::*; - -use core::mem::MaybeUninit; -use cortex_m_rt::entry; -use defmt::panic; -use embassy::time::{Alarm, Clock}; -use embassy_nrf::{interrupt, rtc}; -use nrf52840_hal::clocks; - -static mut RTC: MaybeUninit> = MaybeUninit::uninit(); - -#[entry] -fn main() -> ! { - info!("Hello World!"); - - let p = unwrap!(embassy_nrf::pac::Peripherals::take()); - - clocks::Clocks::new(p.CLOCK) - .enable_ext_hfosc() - .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, irq)); - &*ptr - }; - - let alarm = rtc.alarm0(); - - rtc.start(); - - alarm.set_callback(|_| info!("ALARM TRIGGERED"), core::ptr::null_mut()); - alarm.set(53719); - - info!("initialized!"); - - let mut val = 0; - let mut printval = 0; - loop { - let val2 = rtc.now(); - if val2 < val { - info!("timer ran backwards! {} -> {}", val as u32, val2 as u32); - } - val = val2; - - if val > printval + 32768 { - info!("tick {}", val as u32); - printval = val; - } - } -} diff --git a/embassy-nrf-examples/src/bin/spim.rs b/embassy-nrf-examples/src/bin/spim.rs index f4fb22ba..77058bf2 100644 --- a/embassy-nrf-examples/src/bin/spim.rs +++ b/embassy-nrf-examples/src/bin/spim.rs @@ -3,17 +3,21 @@ #![feature(min_type_alias_impl_trait)] #![feature(impl_trait_in_bindings)] #![feature(type_alias_impl_trait)] +#![allow(incomplete_features)] #[path = "../example_common.rs"] mod example_common; +use core::mem; + use cortex_m_rt::entry; use defmt::panic; use embassy::executor::{task, Executor}; use embassy::util::Forever; +use embassy::util::Steal; use embassy_nrf::gpio::{Level, Output, OutputDrive}; -use embassy_nrf::Peripherals; -use embassy_nrf::{interrupt, pac, rtc, spim}; +use embassy_nrf::{interrupt, rtc, spim}; +use embassy_nrf::{peripherals, Peripherals}; use embassy_traits::spi::FullDuplex; use embedded_hal::digital::v2::*; use example_common::*; @@ -24,7 +28,7 @@ use nrf52840_hal::clocks; async fn run() { info!("running!"); - let p = Peripherals::take().unwrap(); + let p = unsafe { Peripherals::steal() }; let config = spim::Config { frequency: spim::Frequency::M16, @@ -33,10 +37,10 @@ async fn run() { }; let irq = interrupt::take!(SPIM3); - let spim = spim::Spim::new(p.spim3, irq, p.p0_29, p.p0_28, p.p0_30, config); + let spim = spim::Spim::new(p.SPIM3, irq, p.P0_29, p.P0_28, p.P0_30, config); pin_mut!(spim); - let mut ncs = Output::new(p.p0_31, Level::High, OutputDrive::Standard); + let mut ncs = Output::new(p.P0_31, Level::High, OutputDrive::Standard); // Example on how to talk to an ENC28J60 chip @@ -84,17 +88,17 @@ async fn run() { info!("erevid: {=[?]}", rx); } -static RTC: Forever> = Forever::new(); -static ALARM: Forever> = Forever::new(); +static RTC: Forever> = Forever::new(); +static ALARM: Forever> = Forever::new(); static EXECUTOR: Forever = Forever::new(); #[entry] fn main() -> ! { info!("Hello World!"); - let p = unwrap!(embassy_nrf::pac::Peripherals::take()); + let p = unwrap!(embassy_nrf::Peripherals::take()); - clocks::Clocks::new(p.CLOCK) + clocks::Clocks::new(unsafe { mem::transmute(()) }) .enable_ext_hfosc() .set_lfclk_src_external(clocks::LfOscConfiguration::NoExternalNoBypass) .start_lfclk(); diff --git a/embassy-nrf-examples/src/bin/rtc_async.rs b/embassy-nrf-examples/src/bin/timer.rs similarity index 80% rename from embassy-nrf-examples/src/bin/rtc_async.rs rename to embassy-nrf-examples/src/bin/timer.rs index ec437425..41e5d77a 100644 --- a/embassy-nrf-examples/src/bin/rtc_async.rs +++ b/embassy-nrf-examples/src/bin/timer.rs @@ -3,9 +3,12 @@ #![feature(min_type_alias_impl_trait)] #![feature(impl_trait_in_bindings)] #![feature(type_alias_impl_trait)] +#![allow(incomplete_features)] #[path = "../example_common.rs"] mod example_common; +use core::mem; + use example_common::*; use cortex_m_rt::entry; @@ -13,7 +16,7 @@ use defmt::panic; use embassy::executor::{task, Executor}; use embassy::time::{Duration, Timer}; use embassy::util::Forever; -use embassy_nrf::pac; +use embassy_nrf::peripherals; use embassy_nrf::{interrupt, rtc}; use nrf52840_hal::clocks; @@ -33,21 +36,21 @@ async fn run2() { } } -static RTC: Forever> = Forever::new(); -static ALARM: Forever> = Forever::new(); +static RTC: Forever> = Forever::new(); +static ALARM: Forever> = Forever::new(); static EXECUTOR: Forever = Forever::new(); #[entry] fn main() -> ! { info!("Hello World!"); - let p = unwrap!(embassy_nrf::pac::Peripherals::take()); - - clocks::Clocks::new(p.CLOCK) + clocks::Clocks::new(unsafe { mem::transmute(()) }) .enable_ext_hfosc() .set_lfclk_src_external(clocks::LfOscConfiguration::NoExternalNoBypass) .start_lfclk(); + let p = unwrap!(embassy_nrf::Peripherals::take()); + let rtc = RTC.put(rtc::RTC::new(p.RTC1, interrupt::take!(RTC1))); rtc.start(); diff --git a/embassy-nrf-examples/src/bin/uart.rs b/embassy-nrf-examples/src/bin/uart.rs index 7efb3ebe..d8644167 100644 --- a/embassy-nrf-examples/src/bin/uart.rs +++ b/embassy-nrf-examples/src/bin/uart.rs @@ -3,34 +3,34 @@ #![feature(min_type_alias_impl_trait)] #![feature(impl_trait_in_bindings)] #![feature(type_alias_impl_trait)] +#![allow(incomplete_features)] #[path = "../example_common.rs"] mod example_common; +use core::mem; + use embassy_nrf::gpio::NoPin; use example_common::*; use cortex_m_rt::entry; use defmt::panic; use embassy::executor::{task, Executor}; -use embassy::time::{Duration, Timer}; use embassy::traits::uart::{Read, Write}; -use embassy::util::Forever; -use embassy_nrf::{interrupt, pac, rtc, uarte, Peripherals}; -use futures::future::{select, Either}; +use embassy::util::{Forever, Steal}; +use embassy_nrf::{interrupt, peripherals, rtc, uarte, Peripherals}; use futures::pin_mut; use nrf52840_hal::clocks; -use nrf52840_hal::gpio; #[task] async fn run() { - let p = Peripherals::take().unwrap(); + let p = unsafe { Peripherals::steal() }; let mut config = uarte::Config::default(); config.parity = uarte::Parity::EXCLUDED; config.baudrate = uarte::Baudrate::BAUD115200; let irq = interrupt::take!(UARTE0_UART0); - let uart = unsafe { uarte::Uarte::new(p.uarte0, irq, p.p0_08, p.p0_06, NoPin, NoPin, config) }; + let uart = unsafe { uarte::Uarte::new(p.UARTE0, irq, p.P0_08, p.P0_06, NoPin, NoPin, config) }; pin_mut!(uart); info!("uarte initialized!"); @@ -78,17 +78,17 @@ async fn run() { } } -static RTC: Forever> = Forever::new(); -static ALARM: Forever> = Forever::new(); +static RTC: Forever> = Forever::new(); +static ALARM: Forever> = Forever::new(); static EXECUTOR: Forever = Forever::new(); #[entry] fn main() -> ! { info!("Hello World!"); - let p = unwrap!(embassy_nrf::pac::Peripherals::take()); + let p = unwrap!(embassy_nrf::Peripherals::take()); - clocks::Clocks::new(p.CLOCK) + clocks::Clocks::new(unsafe { mem::transmute(()) }) .enable_ext_hfosc() .set_lfclk_src_external(clocks::LfOscConfiguration::NoExternalNoBypass) .start_lfclk(); diff --git a/embassy-nrf/src/lib.rs b/embassy-nrf/src/lib.rs index 009cb99b..9e5132c5 100644 --- a/embassy-nrf/src/lib.rs +++ b/embassy-nrf/src/lib.rs @@ -106,110 +106,110 @@ pub mod uarte; embassy_extras::peripherals! { // RTC - rtc0: RTC0, - rtc1: RTC1, + RTC0, + RTC1, #[cfg(any(feature = "52832", feature = "52833", feature = "52840"))] - rtc2: RTC2, + RTC2, // QSPI #[cfg(feature = "52840")] - qspi: QSPI, + QSPI, // UARTE - uarte0: UARTE0, + UARTE0, #[cfg(any(feature = "52833", feature = "52840", feature = "9160"))] - uarte1: UARTE1, + UARTE1, // SPIM // TODO this is actually shared with SPI, SPIM, SPIS, TWI, TWIS, TWIS. // When they're all implemented, they should be only one peripheral here. - spim0: SPIM0, + SPIM0, #[cfg(any(feature = "52832", feature = "52833", feature = "52840"))] - spim1: SPIM1, + SPIM1, #[cfg(any(feature = "52832", feature = "52833", feature = "52840"))] - spim2: SPIM2, + SPIM2, #[cfg(any(feature = "52833", feature = "52840"))] - spim3: SPIM3, + SPIM3, // SAADC - saadc: SAADC, + SAADC, // GPIOTE - gpiote: GPIOTE, - gpiote_ch_0: GPIOTE_CH0, - gpiote_ch_1: GPIOTE_CH1, - gpiote_ch_2: GPIOTE_CH2, - gpiote_ch_3: GPIOTE_CH3, - gpiote_ch_4: GPIOTE_CH4, - gpiote_ch_5: GPIOTE_CH5, - gpiote_ch_6: GPIOTE_CH6, - gpiote_ch_7: GPIOTE_CH7, + GPIOTE, + GPIOTE_CH0, + GPIOTE_CH1, + GPIOTE_CH2, + GPIOTE_CH3, + GPIOTE_CH4, + GPIOTE_CH5, + GPIOTE_CH6, + GPIOTE_CH7, // GPIO port 0 - p0_00: P0_00, - p0_01: P0_01, - p0_02: P0_02, - p0_03: P0_03, - p0_04: P0_04, - p0_05: P0_05, - p0_06: P0_06, - p0_07: P0_07, - p0_08: P0_08, - p0_09: P0_09, - p0_10: P0_10, - p0_11: P0_11, - p0_12: P0_12, - p0_13: P0_13, - p0_14: P0_14, - p0_15: P0_15, - p0_16: P0_16, - p0_17: P0_17, - p0_18: P0_18, - p0_19: P0_19, - p0_20: P0_20, - p0_21: P0_21, - p0_22: P0_22, - p0_23: P0_23, - p0_24: P0_24, - p0_25: P0_25, - p0_26: P0_26, - p0_27: P0_27, - p0_28: P0_28, - p0_29: P0_29, - p0_30: P0_30, - p0_31: P0_31, + P0_00, + P0_01, + P0_02, + P0_03, + P0_04, + P0_05, + P0_06, + P0_07, + P0_08, + P0_09, + P0_10, + P0_11, + P0_12, + P0_13, + P0_14, + P0_15, + P0_16, + P0_17, + P0_18, + P0_19, + P0_20, + P0_21, + P0_22, + P0_23, + P0_24, + P0_25, + P0_26, + P0_27, + P0_28, + P0_29, + P0_30, + P0_31, // GPIO port 1 #[cfg(any(feature = "52833", feature = "52840"))] - p1_00: P1_00, + P1_00, #[cfg(any(feature = "52833", feature = "52840"))] - p1_01: P1_01, + P1_01, #[cfg(any(feature = "52833", feature = "52840"))] - p1_02: P1_02, + P1_02, #[cfg(any(feature = "52833", feature = "52840"))] - p1_03: P1_03, + P1_03, #[cfg(any(feature = "52833", feature = "52840"))] - p1_04: P1_04, + P1_04, #[cfg(any(feature = "52833", feature = "52840"))] - p1_05: P1_05, + P1_05, #[cfg(any(feature = "52833", feature = "52840"))] - p1_06: P1_06, + P1_06, #[cfg(any(feature = "52833", feature = "52840"))] - p1_07: P1_07, + P1_07, #[cfg(any(feature = "52833", feature = "52840"))] - p1_08: P1_08, + P1_08, #[cfg(any(feature = "52833", feature = "52840"))] - p1_09: P1_09, + P1_09, #[cfg(any(feature = "52833", feature = "52840"))] - p1_10: P1_10, + P1_10, #[cfg(any(feature = "52833", feature = "52840"))] - p1_11: P1_11, + P1_11, #[cfg(any(feature = "52833", feature = "52840"))] - p1_12: P1_12, + P1_12, #[cfg(any(feature = "52833", feature = "52840"))] - p1_13: P1_13, + P1_13, #[cfg(any(feature = "52833", feature = "52840"))] - p1_14: P1_14, + P1_14, #[cfg(any(feature = "52833", feature = "52840"))] - p1_15: P1_15, + P1_15, } diff --git a/embassy-nrf/src/rtc.rs b/embassy-nrf/src/rtc.rs index e0e645fe..7d29d72b 100644 --- a/embassy-nrf/src/rtc.rs +++ b/embassy-nrf/src/rtc.rs @@ -1,13 +1,12 @@ use core::cell::Cell; -use core::ops::Deref; use core::sync::atomic::{compiler_fence, AtomicU32, Ordering}; use embassy::interrupt::InterruptExt; use embassy::time::Clock; -use crate::interrupt; use crate::interrupt::{CriticalSection, Interrupt, Mutex}; -use crate::pac::rtc0; +use crate::pac; +use crate::{interrupt, peripherals}; // RTC timekeeping works with something we call "periods", which are time intervals // of 2^23 ticks. The RTC counter value is 24 bits, so one "overflow cycle" is 2 periods. @@ -96,19 +95,20 @@ impl RTC { } pub fn start(&'static self) { - self.rtc.cc[3].write(|w| unsafe { w.bits(0x800000) }); + let r = self.rtc.regs(); + r.cc[3].write(|w| unsafe { w.bits(0x800000) }); - self.rtc.intenset.write(|w| { + r.intenset.write(|w| { let w = w.ovrflw().set(); let w = w.compare3().set(); w }); - self.rtc.tasks_clear.write(|w| unsafe { w.bits(1) }); - self.rtc.tasks_start.write(|w| unsafe { w.bits(1) }); + r.tasks_clear.write(|w| unsafe { w.bits(1) }); + r.tasks_start.write(|w| unsafe { w.bits(1) }); // Wait for clear - while self.rtc.counter.read().bits() != 0 {} + while r.counter.read().bits() != 0 {} self.irq.set_handler(|ptr| unsafe { let this = &*(ptr as *const () as *const Self); @@ -120,19 +120,20 @@ impl RTC { } fn on_interrupt(&self) { - if self.rtc.events_ovrflw.read().bits() == 1 { - self.rtc.events_ovrflw.write(|w| w); + let r = self.rtc.regs(); + if r.events_ovrflw.read().bits() == 1 { + r.events_ovrflw.write(|w| w); self.next_period(); } - if self.rtc.events_compare[3].read().bits() == 1 { - self.rtc.events_compare[3].write(|w| w); + if r.events_compare[3].read().bits() == 1 { + r.events_compare[3].write(|w| w); self.next_period(); } for n in 0..ALARM_COUNT { - if self.rtc.events_compare[n].read().bits() == 1 { - self.rtc.events_compare[n].write(|w| w); + if r.events_compare[n].read().bits() == 1 { + r.events_compare[n].write(|w| w); interrupt::free(|cs| { self.trigger_alarm(n, cs); }) @@ -142,6 +143,7 @@ impl RTC { fn next_period(&self) { interrupt::free(|cs| { + let r = self.rtc.regs(); let period = self.period.fetch_add(1, Ordering::Relaxed) + 1; let t = (period as u64) << 23; @@ -151,15 +153,16 @@ impl RTC { let diff = at - t; if diff < 0xc00000 { - self.rtc.cc[n].write(|w| unsafe { w.bits(at as u32 & 0xFFFFFF) }); - self.rtc.intenset.write(|w| unsafe { w.bits(compare_n(n)) }); + r.cc[n].write(|w| unsafe { w.bits(at as u32 & 0xFFFFFF) }); + r.intenset.write(|w| unsafe { w.bits(compare_n(n)) }); } } }) } fn trigger_alarm(&self, n: usize, cs: &CriticalSection) { - self.rtc.intenclr.write(|w| unsafe { w.bits(compare_n(n)) }); + let r = self.rtc.regs(); + r.intenclr.write(|w| unsafe { w.bits(compare_n(n)) }); let alarm = &self.alarms.borrow(cs)[n]; alarm.timestamp.set(u64::MAX); @@ -190,6 +193,8 @@ impl RTC { return; } + let r = self.rtc.regs(); + // If it hasn't triggered yet, setup it in the compare channel. let diff = timestamp - t; if diff < 0xc00000 { @@ -206,12 +211,12 @@ impl RTC { // by the Alarm trait contract. What's not allowed is triggering alarms *before* their scheduled time, // and we don't do that here. let safe_timestamp = timestamp.max(t + 3); - self.rtc.cc[n].write(|w| unsafe { w.bits(safe_timestamp as u32 & 0xFFFFFF) }); - self.rtc.intenset.write(|w| unsafe { w.bits(compare_n(n)) }); + r.cc[n].write(|w| unsafe { w.bits(safe_timestamp as u32 & 0xFFFFFF) }); + r.intenset.write(|w| unsafe { w.bits(compare_n(n)) }); } else { // If it's too far in the future, don't setup the compare channel yet. // It will be setup later by `next_period`. - self.rtc.intenclr.write(|w| unsafe { w.bits(compare_n(n)) }); + r.intenclr.write(|w| unsafe { w.bits(compare_n(n)) }); } }) } @@ -232,7 +237,7 @@ impl embassy::time::Clock for RTC { // `period` MUST be read before `counter`, see comment at the top for details. let period = self.period.load(Ordering::Relaxed); compiler_fence(Ordering::Acquire); - let counter = self.rtc.counter.read().bits(); + let counter = self.rtc.regs().counter.read().bits(); calc_now(period, counter) } } @@ -257,31 +262,32 @@ impl embassy::time::Alarm for Alarm { } mod sealed { - pub trait Instance {} + use super::*; + pub trait Instance { + fn regs(&self) -> &pac::rtc0::RegisterBlock; + } +} - impl Instance for crate::pac::RTC0 {} - impl Instance for crate::pac::RTC1 {} - #[cfg(any(feature = "52832", feature = "52833", feature = "52840"))] - impl Instance for crate::pac::RTC2 {} +macro_rules! make_impl { + ($type:ident, $irq:ident) => { + impl sealed::Instance for peripherals::$type { + fn regs(&self) -> &pac::rtc0::RegisterBlock { + unsafe { &*pac::$type::ptr() } + } + } + impl Instance for peripherals::$type { + type Interrupt = interrupt::$irq; + } + }; } /// Implemented by all RTC instances. -pub trait Instance: - sealed::Instance + Deref + Sized + 'static -{ +pub trait Instance: sealed::Instance + 'static { /// The interrupt associated with this RTC instance. type Interrupt: Interrupt; } -impl Instance for crate::pac::RTC0 { - type Interrupt = interrupt::RTC0; -} - -impl Instance for crate::pac::RTC1 { - type Interrupt = interrupt::RTC1; -} - +make_impl!(RTC0, RTC0); +make_impl!(RTC1, RTC1); #[cfg(any(feature = "52832", feature = "52833", feature = "52840"))] -impl Instance for crate::pac::RTC2 { - type Interrupt = interrupt::RTC2; -} +make_impl!(RTC2, RTC2);