aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2021-03-27 03:12:58 +0100
committerDario Nieuwenhuis <[email protected]>2021-03-29 00:58:58 +0200
commit5646926cca8fcb1d3286199bfe54a3666eae30c9 (patch)
treee168f865db4a732ed854c94dda4aecde875349af
parent3eccddc44db8d6c91d874a7be353aef3c90b8d43 (diff)
nrf/rtc: update to new api
-rw-r--r--embassy-extras/src/macros.rs23
-rw-r--r--embassy-nrf-examples/src/bin/buffered_uart.rs1
-rw-r--r--embassy-nrf-examples/src/bin/executor_fairness_test.rs12
-rw-r--r--embassy-nrf-examples/src/bin/gpiote_channel.rs1
-rw-r--r--embassy-nrf-examples/src/bin/gpiote_port.rs11
-rw-r--r--embassy-nrf-examples/src/bin/multiprio.rs17
-rw-r--r--embassy-nrf-examples/src/bin/qspi.rs15
-rw-r--r--embassy-nrf-examples/src/bin/raw_spawn.rs10
-rw-r--r--embassy-nrf-examples/src/bin/rtc_raw.rs62
-rw-r--r--embassy-nrf-examples/src/bin/spim.rs22
-rw-r--r--embassy-nrf-examples/src/bin/timer.rs (renamed from embassy-nrf-examples/src/bin/rtc_async.rs)15
-rw-r--r--embassy-nrf-examples/src/bin/uart.rs22
-rw-r--r--embassy-nrf/src/lib.rs136
-rw-r--r--embassy-nrf/src/rtc.rs86
14 files changed, 197 insertions, 236 deletions
diff --git a/embassy-extras/src/macros.rs b/embassy-extras/src/macros.rs
index a0464378e..0659a0b72 100644
--- a/embassy-extras/src/macros.rs
+++ b/embassy-extras/src/macros.rs
@@ -1,14 +1,14 @@
1#[macro_export] 1#[macro_export]
2macro_rules! peripherals { 2macro_rules! peripherals {
3 ($($(#[$cfg:meta])? $name:ident: $type:ident),*$(,)?) => { 3 ($($(#[$cfg:meta])? $name:ident),*$(,)?) => {
4 pub mod peripherals { 4 pub mod peripherals {
5 $( 5 $(
6 $(#[$cfg])? 6 $(#[$cfg])?
7 #[allow(non_camel_case_types)] 7 #[allow(non_camel_case_types)]
8 pub struct $type { _private: () } 8 pub struct $name { _private: () }
9 9
10 $(#[$cfg])? 10 $(#[$cfg])?
11 impl embassy::util::Steal for $type { 11 impl embassy::util::Steal for $name {
12 #[inline] 12 #[inline]
13 unsafe fn steal() -> Self { 13 unsafe fn steal() -> Self {
14 Self{ _private: ()} 14 Self{ _private: ()}
@@ -16,29 +16,30 @@ macro_rules! peripherals {
16 } 16 }
17 17
18 $(#[$cfg])? 18 $(#[$cfg])?
19 impl embassy::util::PeripheralBorrow for $type { 19 impl embassy::util::PeripheralBorrow for $name {
20 type Target = $type; 20 type Target = $name;
21 #[inline] 21 #[inline]
22 unsafe fn unborrow(self) -> $type { 22 unsafe fn unborrow(self) -> $name {
23 self 23 self
24 } 24 }
25 } 25 }
26 26
27 $(#[$cfg])? 27 $(#[$cfg])?
28 impl embassy::util::PeripheralBorrow for &mut $type { 28 impl embassy::util::PeripheralBorrow for &mut $name {
29 type Target = $type; 29 type Target = $name;
30 #[inline] 30 #[inline]
31 unsafe fn unborrow(self) -> $type { 31 unsafe fn unborrow(self) -> $name {
32 ::core::ptr::read(self) 32 ::core::ptr::read(self)
33 } 33 }
34 } 34 }
35 )* 35 )*
36 } 36 }
37 37
38 #[allow(non_snake_case)]
38 pub struct Peripherals { 39 pub struct Peripherals {
39 $( 40 $(
40 $(#[$cfg])? 41 $(#[$cfg])?
41 pub $name: peripherals::$type, 42 pub $name: peripherals::$name,
42 )* 43 )*
43 } 44 }
44 45
@@ -66,7 +67,7 @@ macro_rules! peripherals {
66 Self { 67 Self {
67 $( 68 $(
68 $(#[$cfg])? 69 $(#[$cfg])?
69 $name: <peripherals::$type as embassy::util::Steal>::steal(), 70 $name: <peripherals::$name as embassy::util::Steal>::steal(),
70 )* 71 )*
71 } 72 }
72 } 73 }
diff --git a/embassy-nrf-examples/src/bin/buffered_uart.rs b/embassy-nrf-examples/src/bin/buffered_uart.rs
index 71e9b4a77..c70171280 100644
--- a/embassy-nrf-examples/src/bin/buffered_uart.rs
+++ b/embassy-nrf-examples/src/bin/buffered_uart.rs
@@ -3,6 +3,7 @@
3#![feature(min_type_alias_impl_trait)] 3#![feature(min_type_alias_impl_trait)]
4#![feature(impl_trait_in_bindings)] 4#![feature(impl_trait_in_bindings)]
5#![feature(type_alias_impl_trait)] 5#![feature(type_alias_impl_trait)]
6#![allow(incomplete_features)]
6 7
7#[path = "../example_common.rs"] 8#[path = "../example_common.rs"]
8mod example_common; 9mod 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 6d0a311d8..cfcfd9496 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 @@
3#![feature(min_type_alias_impl_trait)] 3#![feature(min_type_alias_impl_trait)]
4#![feature(impl_trait_in_bindings)] 4#![feature(impl_trait_in_bindings)]
5#![feature(type_alias_impl_trait)] 5#![feature(type_alias_impl_trait)]
6#![allow(incomplete_features)]
6 7
7#[path = "../example_common.rs"] 8#[path = "../example_common.rs"]
8mod example_common; 9mod example_common;
9use example_common::*; 10use example_common::*;
10 11
12use core::mem;
11use core::task::Poll; 13use core::task::Poll;
12use cortex_m_rt::entry; 14use cortex_m_rt::entry;
13use defmt::panic; 15use defmt::panic;
14use embassy::executor::{task, Executor}; 16use embassy::executor::{task, Executor};
15use embassy::time::{Duration, Instant, Timer}; 17use embassy::time::{Duration, Instant, Timer};
16use embassy::util::Forever; 18use embassy::util::Forever;
17use embassy_nrf::pac; 19use embassy_nrf::peripherals;
18use embassy_nrf::{interrupt, rtc}; 20use embassy_nrf::{interrupt, rtc};
19use nrf52840_hal::clocks; 21use nrf52840_hal::clocks;
20 22
@@ -42,17 +44,17 @@ async fn run3() {
42 .await; 44 .await;
43} 45}
44 46
45static RTC: Forever<rtc::RTC<pac::RTC1>> = Forever::new(); 47static RTC: Forever<rtc::RTC<peripherals::RTC1>> = Forever::new();
46static ALARM: Forever<rtc::Alarm<pac::RTC1>> = Forever::new(); 48static ALARM: Forever<rtc::Alarm<peripherals::RTC1>> = Forever::new();
47static EXECUTOR: Forever<Executor> = Forever::new(); 49static EXECUTOR: Forever<Executor> = Forever::new();
48 50
49#[entry] 51#[entry]
50fn main() -> ! { 52fn main() -> ! {
51 info!("Hello World!"); 53 info!("Hello World!");
52 54
53 let p = unwrap!(embassy_nrf::pac::Peripherals::take()); 55 let p = unwrap!(embassy_nrf::Peripherals::take());
54 56
55 clocks::Clocks::new(p.CLOCK) 57 clocks::Clocks::new(unsafe { mem::transmute(()) })
56 .enable_ext_hfosc() 58 .enable_ext_hfosc()
57 .set_lfclk_src_external(clocks::LfOscConfiguration::NoExternalNoBypass) 59 .set_lfclk_src_external(clocks::LfOscConfiguration::NoExternalNoBypass)
58 .start_lfclk(); 60 .start_lfclk();
diff --git a/embassy-nrf-examples/src/bin/gpiote_channel.rs b/embassy-nrf-examples/src/bin/gpiote_channel.rs
index 3764ba1c2..599882a90 100644
--- a/embassy-nrf-examples/src/bin/gpiote_channel.rs
+++ b/embassy-nrf-examples/src/bin/gpiote_channel.rs
@@ -3,6 +3,7 @@
3#![feature(min_type_alias_impl_trait)] 3#![feature(min_type_alias_impl_trait)]
4#![feature(impl_trait_in_bindings)] 4#![feature(impl_trait_in_bindings)]
5#![feature(type_alias_impl_trait)] 5#![feature(type_alias_impl_trait)]
6#![allow(incomplete_features)]
6 7
7#[path = "../example_common.rs"] 8#[path = "../example_common.rs"]
8mod example_common; 9mod example_common;
diff --git a/embassy-nrf-examples/src/bin/gpiote_port.rs b/embassy-nrf-examples/src/bin/gpiote_port.rs
index 556a199f2..199a33dab 100644
--- a/embassy-nrf-examples/src/bin/gpiote_port.rs
+++ b/embassy-nrf-examples/src/bin/gpiote_port.rs
@@ -3,6 +3,7 @@
3#![feature(min_type_alias_impl_trait)] 3#![feature(min_type_alias_impl_trait)]
4#![feature(impl_trait_in_bindings)] 4#![feature(impl_trait_in_bindings)]
5#![feature(type_alias_impl_trait)] 5#![feature(type_alias_impl_trait)]
6#![allow(incomplete_features)]
6 7
7#[path = "../example_common.rs"] 8#[path = "../example_common.rs"]
8mod example_common; 9mod example_common;
@@ -32,23 +33,23 @@ async fn button(n: usize, mut pin: PortInput<'static, AnyPin>) {
32async fn run() { 33async fn run() {
33 let p = Peripherals::take().unwrap(); 34 let p = Peripherals::take().unwrap();
34 35
35 let g = gpiote::initialize(p.gpiote, interrupt::take!(GPIOTE)); 36 let g = gpiote::initialize(p.GPIOTE, interrupt::take!(GPIOTE));
36 37
37 let button1 = button( 38 let button1 = button(
38 1, 39 1,
39 PortInput::new(g, Input::new(p.p0_11.degrade(), Pull::Up)), 40 PortInput::new(g, Input::new(p.P0_11.degrade(), Pull::Up)),
40 ); 41 );
41 let button2 = button( 42 let button2 = button(
42 2, 43 2,
43 PortInput::new(g, Input::new(p.p0_12.degrade(), Pull::Up)), 44 PortInput::new(g, Input::new(p.P0_12.degrade(), Pull::Up)),
44 ); 45 );
45 let button3 = button( 46 let button3 = button(
46 3, 47 3,
47 PortInput::new(g, Input::new(p.p0_24.degrade(), Pull::Up)), 48 PortInput::new(g, Input::new(p.P0_24.degrade(), Pull::Up)),
48 ); 49 );
49 let button4 = button( 50 let button4 = button(
50 4, 51 4,
51 PortInput::new(g, Input::new(p.p0_25.degrade(), Pull::Up)), 52 PortInput::new(g, Input::new(p.P0_25.degrade(), Pull::Up)),
52 ); 53 );
53 futures::join!(button1, button2, button3, button4); 54 futures::join!(button1, button2, button3, button4);
54} 55}
diff --git a/embassy-nrf-examples/src/bin/multiprio.rs b/embassy-nrf-examples/src/bin/multiprio.rs
index 65c646217..017de3c0d 100644
--- a/embassy-nrf-examples/src/bin/multiprio.rs
+++ b/embassy-nrf-examples/src/bin/multiprio.rs
@@ -58,9 +58,12 @@
58#![feature(min_type_alias_impl_trait)] 58#![feature(min_type_alias_impl_trait)]
59#![feature(impl_trait_in_bindings)] 59#![feature(impl_trait_in_bindings)]
60#![feature(type_alias_impl_trait)] 60#![feature(type_alias_impl_trait)]
61#![allow(incomplete_features)]
61 62
62#[path = "../example_common.rs"] 63#[path = "../example_common.rs"]
63mod example_common; 64mod example_common;
65use core::mem;
66
64use example_common::*; 67use example_common::*;
65 68
66use cortex_m_rt::entry; 69use cortex_m_rt::entry;
@@ -71,7 +74,7 @@ use embassy::executor::{task, Executor, InterruptExecutor};
71use embassy::interrupt::InterruptExt; 74use embassy::interrupt::InterruptExt;
72use embassy::time::{Duration, Instant, Timer}; 75use embassy::time::{Duration, Instant, Timer};
73use embassy::util::Forever; 76use embassy::util::Forever;
74use embassy_nrf::{interrupt, pac, rtc}; 77use embassy_nrf::{interrupt, peripherals, rtc};
75 78
76#[task] 79#[task]
77async fn run_high() { 80async fn run_high() {
@@ -115,21 +118,21 @@ async fn run_low() {
115 } 118 }
116} 119}
117 120
118static RTC: Forever<rtc::RTC<pac::RTC1>> = Forever::new(); 121static RTC: Forever<rtc::RTC<peripherals::RTC1>> = Forever::new();
119static ALARM_HIGH: Forever<rtc::Alarm<pac::RTC1>> = Forever::new(); 122static ALARM_HIGH: Forever<rtc::Alarm<peripherals::RTC1>> = Forever::new();
120static EXECUTOR_HIGH: Forever<InterruptExecutor<interrupt::SWI1_EGU1>> = Forever::new(); 123static EXECUTOR_HIGH: Forever<InterruptExecutor<interrupt::SWI1_EGU1>> = Forever::new();
121static ALARM_MED: Forever<rtc::Alarm<pac::RTC1>> = Forever::new(); 124static ALARM_MED: Forever<rtc::Alarm<peripherals::RTC1>> = Forever::new();
122static EXECUTOR_MED: Forever<InterruptExecutor<interrupt::SWI0_EGU0>> = Forever::new(); 125static EXECUTOR_MED: Forever<InterruptExecutor<interrupt::SWI0_EGU0>> = Forever::new();
123static ALARM_LOW: Forever<rtc::Alarm<pac::RTC1>> = Forever::new(); 126static ALARM_LOW: Forever<rtc::Alarm<peripherals::RTC1>> = Forever::new();
124static EXECUTOR_LOW: Forever<Executor> = Forever::new(); 127static EXECUTOR_LOW: Forever<Executor> = Forever::new();
125 128
126#[entry] 129#[entry]
127fn main() -> ! { 130fn main() -> ! {
128 info!("Hello World!"); 131 info!("Hello World!");
129 132
130 let p = unwrap!(embassy_nrf::pac::Peripherals::take()); 133 let p = unwrap!(embassy_nrf::Peripherals::take());
131 134
132 clocks::Clocks::new(p.CLOCK) 135 clocks::Clocks::new(unsafe { mem::transmute(()) })
133 .enable_ext_hfosc() 136 .enable_ext_hfosc()
134 .set_lfclk_src_external(clocks::LfOscConfiguration::NoExternalNoBypass) 137 .set_lfclk_src_external(clocks::LfOscConfiguration::NoExternalNoBypass)
135 .start_lfclk(); 138 .start_lfclk();
diff --git a/embassy-nrf-examples/src/bin/qspi.rs b/embassy-nrf-examples/src/bin/qspi.rs
index 1637b3977..4e24a13dd 100644
--- a/embassy-nrf-examples/src/bin/qspi.rs
+++ b/embassy-nrf-examples/src/bin/qspi.rs
@@ -3,6 +3,7 @@
3#![feature(min_type_alias_impl_trait)] 3#![feature(min_type_alias_impl_trait)]
4#![feature(impl_trait_in_bindings)] 4#![feature(impl_trait_in_bindings)]
5#![feature(type_alias_impl_trait)] 5#![feature(type_alias_impl_trait)]
6#![allow(incomplete_features)]
6 7
7#[path = "../example_common.rs"] 8#[path = "../example_common.rs"]
8mod example_common; 9mod example_common;
@@ -28,12 +29,12 @@ struct AlignedBuf([u8; 4096]);
28async fn run() { 29async fn run() {
29 let p = Peripherals::take().unwrap(); 30 let p = Peripherals::take().unwrap();
30 31
31 let csn = p.p0_17; 32 let csn = p.P0_17;
32 let sck = p.p0_19; 33 let sck = p.P0_19;
33 let io0 = p.p0_20; 34 let io0 = p.P0_20;
34 let io1 = p.p0_21; 35 let io1 = p.P0_21;
35 let io2 = p.p0_22; 36 let io2 = p.P0_22;
36 let io3 = p.p0_23; 37 let io3 = p.P0_23;
37 38
38 let config = qspi::Config { 39 let config = qspi::Config {
39 read_opcode: qspi::ReadOpcode::READ4IO, 40 read_opcode: qspi::ReadOpcode::READ4IO,
@@ -44,7 +45,7 @@ async fn run() {
44 }; 45 };
45 46
46 let irq = interrupt::take!(QSPI); 47 let irq = interrupt::take!(QSPI);
47 let q = qspi::Qspi::new(p.qspi, irq, sck, csn, io0, io1, io2, io3, config); 48 let q = qspi::Qspi::new(p.QSPI, irq, sck, csn, io0, io1, io2, io3, config);
48 pin_mut!(q); 49 pin_mut!(q);
49 50
50 let mut id = [1; 3]; 51 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 3747b49f6..1dd604031 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;
13use embassy::executor::Executor; 13use embassy::executor::Executor;
14use embassy::time::{Duration, Timer}; 14use embassy::time::{Duration, Timer};
15use embassy::util::Forever; 15use embassy::util::Forever;
16use embassy_nrf::pac; 16use embassy_nrf::peripherals;
17use embassy_nrf::{interrupt, rtc}; 17use embassy_nrf::{interrupt, rtc};
18use nrf52840_hal::clocks; 18use nrf52840_hal::clocks;
19 19
@@ -31,17 +31,17 @@ async fn run2() {
31 } 31 }
32} 32}
33 33
34static RTC: Forever<rtc::RTC<pac::RTC1>> = Forever::new(); 34static RTC: Forever<rtc::RTC<peripherals::RTC1>> = Forever::new();
35static ALARM: Forever<rtc::Alarm<pac::RTC1>> = Forever::new(); 35static ALARM: Forever<rtc::Alarm<peripherals::RTC1>> = Forever::new();
36static EXECUTOR: Forever<Executor> = Forever::new(); 36static EXECUTOR: Forever<Executor> = Forever::new();
37 37
38#[entry] 38#[entry]
39fn main() -> ! { 39fn main() -> ! {
40 info!("Hello World!"); 40 info!("Hello World!");
41 41
42 let p = unwrap!(embassy_nrf::pac::Peripherals::take()); 42 let p = unwrap!(embassy_nrf::Peripherals::take());
43 43
44 clocks::Clocks::new(p.CLOCK) 44 clocks::Clocks::new(unsafe { mem::transmute(()) })
45 .enable_ext_hfosc() 45 .enable_ext_hfosc()
46 .set_lfclk_src_external(clocks::LfOscConfiguration::NoExternalNoBypass) 46 .set_lfclk_src_external(clocks::LfOscConfiguration::NoExternalNoBypass)
47 .start_lfclk(); 47 .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 884ca92b9..000000000
--- a/embassy-nrf-examples/src/bin/rtc_raw.rs
+++ /dev/null
@@ -1,62 +0,0 @@
1#![no_std]
2#![no_main]
3#![feature(min_type_alias_impl_trait)]
4#![feature(impl_trait_in_bindings)]
5#![feature(type_alias_impl_trait)]
6
7#[path = "../example_common.rs"]
8mod example_common;
9use example_common::*;
10
11use core::mem::MaybeUninit;
12use cortex_m_rt::entry;
13use defmt::panic;
14use embassy::time::{Alarm, Clock};
15use embassy_nrf::{interrupt, rtc};
16use nrf52840_hal::clocks;
17
18static mut RTC: MaybeUninit<rtc::RTC<embassy_nrf::pac::RTC1>> = MaybeUninit::uninit();
19
20#[entry]
21fn main() -> ! {
22 info!("Hello World!");
23
24 let p = unwrap!(embassy_nrf::pac::Peripherals::take());
25
26 clocks::Clocks::new(p.CLOCK)
27 .enable_ext_hfosc()
28 .set_lfclk_src_external(clocks::LfOscConfiguration::NoExternalNoBypass)
29 .start_lfclk();
30
31 let irq = interrupt::take!(RTC1);
32
33 let rtc: &'static _ = unsafe {
34 let ptr = RTC.as_mut_ptr();
35 ptr.write(rtc::RTC::new(p.RTC1, irq));
36 &*ptr
37 };
38
39 let alarm = rtc.alarm0();
40
41 rtc.start();
42
43 alarm.set_callback(|_| info!("ALARM TRIGGERED"), core::ptr::null_mut());
44 alarm.set(53719);
45
46 info!("initialized!");
47
48 let mut val = 0;
49 let mut printval = 0;
50 loop {
51 let val2 = rtc.now();
52 if val2 < val {
53 info!("timer ran backwards! {} -> {}", val as u32, val2 as u32);
54 }
55 val = val2;
56
57 if val > printval + 32768 {
58 info!("tick {}", val as u32);
59 printval = val;
60 }
61 }
62}
diff --git a/embassy-nrf-examples/src/bin/spim.rs b/embassy-nrf-examples/src/bin/spim.rs
index f4fb22bab..77058bf26 100644
--- a/embassy-nrf-examples/src/bin/spim.rs
+++ b/embassy-nrf-examples/src/bin/spim.rs
@@ -3,17 +3,21 @@
3#![feature(min_type_alias_impl_trait)] 3#![feature(min_type_alias_impl_trait)]
4#![feature(impl_trait_in_bindings)] 4#![feature(impl_trait_in_bindings)]
5#![feature(type_alias_impl_trait)] 5#![feature(type_alias_impl_trait)]
6#![allow(incomplete_features)]
6 7
7#[path = "../example_common.rs"] 8#[path = "../example_common.rs"]
8mod example_common; 9mod example_common;
9 10
11use core::mem;
12
10use cortex_m_rt::entry; 13use cortex_m_rt::entry;
11use defmt::panic; 14use defmt::panic;
12use embassy::executor::{task, Executor}; 15use embassy::executor::{task, Executor};
13use embassy::util::Forever; 16use embassy::util::Forever;
17use embassy::util::Steal;
14use embassy_nrf::gpio::{Level, Output, OutputDrive}; 18use embassy_nrf::gpio::{Level, Output, OutputDrive};
15use embassy_nrf::Peripherals; 19use embassy_nrf::{interrupt, rtc, spim};
16use embassy_nrf::{interrupt, pac, rtc, spim}; 20use embassy_nrf::{peripherals, Peripherals};
17use embassy_traits::spi::FullDuplex; 21use embassy_traits::spi::FullDuplex;
18use embedded_hal::digital::v2::*; 22use embedded_hal::digital::v2::*;
19use example_common::*; 23use example_common::*;
@@ -24,7 +28,7 @@ use nrf52840_hal::clocks;
24async fn run() { 28async fn run() {
25 info!("running!"); 29 info!("running!");
26 30
27 let p = Peripherals::take().unwrap(); 31 let p = unsafe { Peripherals::steal() };
28 32
29 let config = spim::Config { 33 let config = spim::Config {
30 frequency: spim::Frequency::M16, 34 frequency: spim::Frequency::M16,
@@ -33,10 +37,10 @@ async fn run() {
33 }; 37 };
34 38
35 let irq = interrupt::take!(SPIM3); 39 let irq = interrupt::take!(SPIM3);
36 let spim = spim::Spim::new(p.spim3, irq, p.p0_29, p.p0_28, p.p0_30, config); 40 let spim = spim::Spim::new(p.SPIM3, irq, p.P0_29, p.P0_28, p.P0_30, config);
37 pin_mut!(spim); 41 pin_mut!(spim);
38 42
39 let mut ncs = Output::new(p.p0_31, Level::High, OutputDrive::Standard); 43 let mut ncs = Output::new(p.P0_31, Level::High, OutputDrive::Standard);
40 44
41 // Example on how to talk to an ENC28J60 chip 45 // Example on how to talk to an ENC28J60 chip
42 46
@@ -84,17 +88,17 @@ async fn run() {
84 info!("erevid: {=[?]}", rx); 88 info!("erevid: {=[?]}", rx);
85} 89}
86 90
87static RTC: Forever<rtc::RTC<pac::RTC1>> = Forever::new(); 91static RTC: Forever<rtc::RTC<peripherals::RTC1>> = Forever::new();
88static ALARM: Forever<rtc::Alarm<pac::RTC1>> = Forever::new(); 92static ALARM: Forever<rtc::Alarm<peripherals::RTC1>> = Forever::new();
89static EXECUTOR: Forever<Executor> = Forever::new(); 93static EXECUTOR: Forever<Executor> = Forever::new();
90 94
91#[entry] 95#[entry]
92fn main() -> ! { 96fn main() -> ! {
93 info!("Hello World!"); 97 info!("Hello World!");
94 98
95 let p = unwrap!(embassy_nrf::pac::Peripherals::take()); 99 let p = unwrap!(embassy_nrf::Peripherals::take());
96 100
97 clocks::Clocks::new(p.CLOCK) 101 clocks::Clocks::new(unsafe { mem::transmute(()) })
98 .enable_ext_hfosc() 102 .enable_ext_hfosc()
99 .set_lfclk_src_external(clocks::LfOscConfiguration::NoExternalNoBypass) 103 .set_lfclk_src_external(clocks::LfOscConfiguration::NoExternalNoBypass)
100 .start_lfclk(); 104 .start_lfclk();
diff --git a/embassy-nrf-examples/src/bin/rtc_async.rs b/embassy-nrf-examples/src/bin/timer.rs
index ec4374254..41e5d77a7 100644
--- a/embassy-nrf-examples/src/bin/rtc_async.rs
+++ b/embassy-nrf-examples/src/bin/timer.rs
@@ -3,9 +3,12 @@
3#![feature(min_type_alias_impl_trait)] 3#![feature(min_type_alias_impl_trait)]
4#![feature(impl_trait_in_bindings)] 4#![feature(impl_trait_in_bindings)]
5#![feature(type_alias_impl_trait)] 5#![feature(type_alias_impl_trait)]
6#![allow(incomplete_features)]
6 7
7#[path = "../example_common.rs"] 8#[path = "../example_common.rs"]
8mod example_common; 9mod example_common;
10use core::mem;
11
9use example_common::*; 12use example_common::*;
10 13
11use cortex_m_rt::entry; 14use cortex_m_rt::entry;
@@ -13,7 +16,7 @@ use defmt::panic;
13use embassy::executor::{task, Executor}; 16use embassy::executor::{task, Executor};
14use embassy::time::{Duration, Timer}; 17use embassy::time::{Duration, Timer};
15use embassy::util::Forever; 18use embassy::util::Forever;
16use embassy_nrf::pac; 19use embassy_nrf::peripherals;
17use embassy_nrf::{interrupt, rtc}; 20use embassy_nrf::{interrupt, rtc};
18use nrf52840_hal::clocks; 21use nrf52840_hal::clocks;
19 22
@@ -33,21 +36,21 @@ async fn run2() {
33 } 36 }
34} 37}
35 38
36static RTC: Forever<rtc::RTC<pac::RTC1>> = Forever::new(); 39static RTC: Forever<rtc::RTC<peripherals::RTC1>> = Forever::new();
37static ALARM: Forever<rtc::Alarm<pac::RTC1>> = Forever::new(); 40static ALARM: Forever<rtc::Alarm<peripherals::RTC1>> = Forever::new();
38static EXECUTOR: Forever<Executor> = Forever::new(); 41static EXECUTOR: Forever<Executor> = Forever::new();
39 42
40#[entry] 43#[entry]
41fn main() -> ! { 44fn main() -> ! {
42 info!("Hello World!"); 45 info!("Hello World!");
43 46
44 let p = unwrap!(embassy_nrf::pac::Peripherals::take()); 47 clocks::Clocks::new(unsafe { mem::transmute(()) })
45
46 clocks::Clocks::new(p.CLOCK)
47 .enable_ext_hfosc() 48 .enable_ext_hfosc()
48 .set_lfclk_src_external(clocks::LfOscConfiguration::NoExternalNoBypass) 49 .set_lfclk_src_external(clocks::LfOscConfiguration::NoExternalNoBypass)
49 .start_lfclk(); 50 .start_lfclk();
50 51
52 let p = unwrap!(embassy_nrf::Peripherals::take());
53
51 let rtc = RTC.put(rtc::RTC::new(p.RTC1, interrupt::take!(RTC1))); 54 let rtc = RTC.put(rtc::RTC::new(p.RTC1, interrupt::take!(RTC1)));
52 rtc.start(); 55 rtc.start();
53 56
diff --git a/embassy-nrf-examples/src/bin/uart.rs b/embassy-nrf-examples/src/bin/uart.rs
index 7efb3ebe1..d8644167b 100644
--- a/embassy-nrf-examples/src/bin/uart.rs
+++ b/embassy-nrf-examples/src/bin/uart.rs
@@ -3,34 +3,34 @@
3#![feature(min_type_alias_impl_trait)] 3#![feature(min_type_alias_impl_trait)]
4#![feature(impl_trait_in_bindings)] 4#![feature(impl_trait_in_bindings)]
5#![feature(type_alias_impl_trait)] 5#![feature(type_alias_impl_trait)]
6#![allow(incomplete_features)]
6 7
7#[path = "../example_common.rs"] 8#[path = "../example_common.rs"]
8mod example_common; 9mod example_common;
10use core::mem;
11
9use embassy_nrf::gpio::NoPin; 12use embassy_nrf::gpio::NoPin;
10use example_common::*; 13use example_common::*;
11 14
12use cortex_m_rt::entry; 15use cortex_m_rt::entry;
13use defmt::panic; 16use defmt::panic;
14use embassy::executor::{task, Executor}; 17use embassy::executor::{task, Executor};
15use embassy::time::{Duration, Timer};
16use embassy::traits::uart::{Read, Write}; 18use embassy::traits::uart::{Read, Write};
17use embassy::util::Forever; 19use embassy::util::{Forever, Steal};
18use embassy_nrf::{interrupt, pac, rtc, uarte, Peripherals}; 20use embassy_nrf::{interrupt, peripherals, rtc, uarte, Peripherals};
19use futures::future::{select, Either};
20use futures::pin_mut; 21use futures::pin_mut;
21use nrf52840_hal::clocks; 22use nrf52840_hal::clocks;
22use nrf52840_hal::gpio;
23 23
24#[task] 24#[task]
25async fn run() { 25async fn run() {
26 let p = Peripherals::take().unwrap(); 26 let p = unsafe { Peripherals::steal() };
27 27
28 let mut config = uarte::Config::default(); 28 let mut config = uarte::Config::default();
29 config.parity = uarte::Parity::EXCLUDED; 29 config.parity = uarte::Parity::EXCLUDED;
30 config.baudrate = uarte::Baudrate::BAUD115200; 30 config.baudrate = uarte::Baudrate::BAUD115200;
31 31
32 let irq = interrupt::take!(UARTE0_UART0); 32 let irq = interrupt::take!(UARTE0_UART0);
33 let uart = unsafe { uarte::Uarte::new(p.uarte0, irq, p.p0_08, p.p0_06, NoPin, NoPin, config) }; 33 let uart = unsafe { uarte::Uarte::new(p.UARTE0, irq, p.P0_08, p.P0_06, NoPin, NoPin, config) };
34 pin_mut!(uart); 34 pin_mut!(uart);
35 35
36 info!("uarte initialized!"); 36 info!("uarte initialized!");
@@ -78,17 +78,17 @@ async fn run() {
78 } 78 }
79} 79}
80 80
81static RTC: Forever<rtc::RTC<pac::RTC1>> = Forever::new(); 81static RTC: Forever<rtc::RTC<peripherals::RTC1>> = Forever::new();
82static ALARM: Forever<rtc::Alarm<pac::RTC1>> = Forever::new(); 82static ALARM: Forever<rtc::Alarm<peripherals::RTC1>> = Forever::new();
83static EXECUTOR: Forever<Executor> = Forever::new(); 83static EXECUTOR: Forever<Executor> = Forever::new();
84 84
85#[entry] 85#[entry]
86fn main() -> ! { 86fn main() -> ! {
87 info!("Hello World!"); 87 info!("Hello World!");
88 88
89 let p = unwrap!(embassy_nrf::pac::Peripherals::take()); 89 let p = unwrap!(embassy_nrf::Peripherals::take());
90 90
91 clocks::Clocks::new(p.CLOCK) 91 clocks::Clocks::new(unsafe { mem::transmute(()) })
92 .enable_ext_hfosc() 92 .enable_ext_hfosc()
93 .set_lfclk_src_external(clocks::LfOscConfiguration::NoExternalNoBypass) 93 .set_lfclk_src_external(clocks::LfOscConfiguration::NoExternalNoBypass)
94 .start_lfclk(); 94 .start_lfclk();
diff --git a/embassy-nrf/src/lib.rs b/embassy-nrf/src/lib.rs
index 009cb99be..9e5132c59 100644
--- a/embassy-nrf/src/lib.rs
+++ b/embassy-nrf/src/lib.rs
@@ -106,110 +106,110 @@ pub mod uarte;
106 106
107embassy_extras::peripherals! { 107embassy_extras::peripherals! {
108 // RTC 108 // RTC
109 rtc0: RTC0, 109 RTC0,
110 rtc1: RTC1, 110 RTC1,
111 #[cfg(any(feature = "52832", feature = "52833", feature = "52840"))] 111 #[cfg(any(feature = "52832", feature = "52833", feature = "52840"))]
112 rtc2: RTC2, 112 RTC2,
113 113
114 // QSPI 114 // QSPI
115 #[cfg(feature = "52840")] 115 #[cfg(feature = "52840")]
116 qspi: QSPI, 116 QSPI,
117 117
118 // UARTE 118 // UARTE
119 uarte0: UARTE0, 119 UARTE0,
120 #[cfg(any(feature = "52833", feature = "52840", feature = "9160"))] 120 #[cfg(any(feature = "52833", feature = "52840", feature = "9160"))]
121 uarte1: UARTE1, 121 UARTE1,
122 122
123 // SPIM 123 // SPIM
124 // TODO this is actually shared with SPI, SPIM, SPIS, TWI, TWIS, TWIS. 124 // TODO this is actually shared with SPI, SPIM, SPIS, TWI, TWIS, TWIS.
125 // When they're all implemented, they should be only one peripheral here. 125 // When they're all implemented, they should be only one peripheral here.
126 spim0: SPIM0, 126 SPIM0,
127 #[cfg(any(feature = "52832", feature = "52833", feature = "52840"))] 127 #[cfg(any(feature = "52832", feature = "52833", feature = "52840"))]
128 spim1: SPIM1, 128 SPIM1,
129 #[cfg(any(feature = "52832", feature = "52833", feature = "52840"))] 129 #[cfg(any(feature = "52832", feature = "52833", feature = "52840"))]
130 spim2: SPIM2, 130 SPIM2,
131 #[cfg(any(feature = "52833", feature = "52840"))] 131 #[cfg(any(feature = "52833", feature = "52840"))]
132 spim3: SPIM3, 132 SPIM3,
133 133
134 // SAADC 134 // SAADC
135 saadc: SAADC, 135 SAADC,
136 136
137 // GPIOTE 137 // GPIOTE
138 gpiote: GPIOTE, 138 GPIOTE,
139 gpiote_ch_0: GPIOTE_CH0, 139 GPIOTE_CH0,
140 gpiote_ch_1: GPIOTE_CH1, 140 GPIOTE_CH1,
141 gpiote_ch_2: GPIOTE_CH2, 141 GPIOTE_CH2,
142 gpiote_ch_3: GPIOTE_CH3, 142 GPIOTE_CH3,
143 gpiote_ch_4: GPIOTE_CH4, 143 GPIOTE_CH4,
144 gpiote_ch_5: GPIOTE_CH5, 144 GPIOTE_CH5,
145 gpiote_ch_6: GPIOTE_CH6, 145 GPIOTE_CH6,
146 gpiote_ch_7: GPIOTE_CH7, 146 GPIOTE_CH7,
147 147
148 // GPIO port 0 148 // GPIO port 0
149 p0_00: P0_00, 149 P0_00,
150 p0_01: P0_01, 150 P0_01,
151 p0_02: P0_02, 151 P0_02,
152 p0_03: P0_03, 152 P0_03,
153 p0_04: P0_04, 153 P0_04,
154 p0_05: P0_05, 154 P0_05,
155 p0_06: P0_06, 155 P0_06,
156 p0_07: P0_07, 156 P0_07,
157 p0_08: P0_08, 157 P0_08,
158 p0_09: P0_09, 158 P0_09,
159 p0_10: P0_10, 159 P0_10,
160 p0_11: P0_11, 160 P0_11,
161 p0_12: P0_12, 161 P0_12,
162 p0_13: P0_13, 162 P0_13,
163 p0_14: P0_14, 163 P0_14,
164 p0_15: P0_15, 164 P0_15,
165 p0_16: P0_16, 165 P0_16,
166 p0_17: P0_17, 166 P0_17,
167 p0_18: P0_18, 167 P0_18,
168 p0_19: P0_19, 168 P0_19,
169 p0_20: P0_20, 169 P0_20,
170 p0_21: P0_21, 170 P0_21,
171 p0_22: P0_22, 171 P0_22,
172 p0_23: P0_23, 172 P0_23,
173 p0_24: P0_24, 173 P0_24,
174 p0_25: P0_25, 174 P0_25,
175 p0_26: P0_26, 175 P0_26,
176 p0_27: P0_27, 176 P0_27,
177 p0_28: P0_28, 177 P0_28,
178 p0_29: P0_29, 178 P0_29,
179 p0_30: P0_30, 179 P0_30,
180 p0_31: P0_31, 180 P0_31,
181 181
182 // GPIO port 1 182 // GPIO port 1
183 #[cfg(any(feature = "52833", feature = "52840"))] 183 #[cfg(any(feature = "52833", feature = "52840"))]
184 p1_00: P1_00, 184 P1_00,
185 #[cfg(any(feature = "52833", feature = "52840"))] 185 #[cfg(any(feature = "52833", feature = "52840"))]
186 p1_01: P1_01, 186 P1_01,
187 #[cfg(any(feature = "52833", feature = "52840"))] 187 #[cfg(any(feature = "52833", feature = "52840"))]
188 p1_02: P1_02, 188 P1_02,
189 #[cfg(any(feature = "52833", feature = "52840"))] 189 #[cfg(any(feature = "52833", feature = "52840"))]
190 p1_03: P1_03, 190 P1_03,
191 #[cfg(any(feature = "52833", feature = "52840"))] 191 #[cfg(any(feature = "52833", feature = "52840"))]
192 p1_04: P1_04, 192 P1_04,
193 #[cfg(any(feature = "52833", feature = "52840"))] 193 #[cfg(any(feature = "52833", feature = "52840"))]
194 p1_05: P1_05, 194 P1_05,
195 #[cfg(any(feature = "52833", feature = "52840"))] 195 #[cfg(any(feature = "52833", feature = "52840"))]
196 p1_06: P1_06, 196 P1_06,
197 #[cfg(any(feature = "52833", feature = "52840"))] 197 #[cfg(any(feature = "52833", feature = "52840"))]
198 p1_07: P1_07, 198 P1_07,
199 #[cfg(any(feature = "52833", feature = "52840"))] 199 #[cfg(any(feature = "52833", feature = "52840"))]
200 p1_08: P1_08, 200 P1_08,
201 #[cfg(any(feature = "52833", feature = "52840"))] 201 #[cfg(any(feature = "52833", feature = "52840"))]
202 p1_09: P1_09, 202 P1_09,
203 #[cfg(any(feature = "52833", feature = "52840"))] 203 #[cfg(any(feature = "52833", feature = "52840"))]
204 p1_10: P1_10, 204 P1_10,
205 #[cfg(any(feature = "52833", feature = "52840"))] 205 #[cfg(any(feature = "52833", feature = "52840"))]
206 p1_11: P1_11, 206 P1_11,
207 #[cfg(any(feature = "52833", feature = "52840"))] 207 #[cfg(any(feature = "52833", feature = "52840"))]
208 p1_12: P1_12, 208 P1_12,
209 #[cfg(any(feature = "52833", feature = "52840"))] 209 #[cfg(any(feature = "52833", feature = "52840"))]
210 p1_13: P1_13, 210 P1_13,
211 #[cfg(any(feature = "52833", feature = "52840"))] 211 #[cfg(any(feature = "52833", feature = "52840"))]
212 p1_14: P1_14, 212 P1_14,
213 #[cfg(any(feature = "52833", feature = "52840"))] 213 #[cfg(any(feature = "52833", feature = "52840"))]
214 p1_15: P1_15, 214 P1_15,
215} 215}
diff --git a/embassy-nrf/src/rtc.rs b/embassy-nrf/src/rtc.rs
index e0e645fe8..7d29d72b9 100644
--- a/embassy-nrf/src/rtc.rs
+++ b/embassy-nrf/src/rtc.rs
@@ -1,13 +1,12 @@
1use core::cell::Cell; 1use core::cell::Cell;
2use core::ops::Deref;
3use core::sync::atomic::{compiler_fence, AtomicU32, Ordering}; 2use core::sync::atomic::{compiler_fence, AtomicU32, Ordering};
4 3
5use embassy::interrupt::InterruptExt; 4use embassy::interrupt::InterruptExt;
6use embassy::time::Clock; 5use embassy::time::Clock;
7 6
8use crate::interrupt;
9use crate::interrupt::{CriticalSection, Interrupt, Mutex}; 7use crate::interrupt::{CriticalSection, Interrupt, Mutex};
10use crate::pac::rtc0; 8use crate::pac;
9use crate::{interrupt, peripherals};
11 10
12// RTC timekeeping works with something we call "periods", which are time intervals 11// RTC timekeeping works with something we call "periods", which are time intervals
13// of 2^23 ticks. The RTC counter value is 24 bits, so one "overflow cycle" is 2 periods. 12// of 2^23 ticks. The RTC counter value is 24 bits, so one "overflow cycle" is 2 periods.
@@ -96,19 +95,20 @@ impl<T: Instance> RTC<T> {
96 } 95 }
97 96
98 pub fn start(&'static self) { 97 pub fn start(&'static self) {
99 self.rtc.cc[3].write(|w| unsafe { w.bits(0x800000) }); 98 let r = self.rtc.regs();
99 r.cc[3].write(|w| unsafe { w.bits(0x800000) });
100 100
101 self.rtc.intenset.write(|w| { 101 r.intenset.write(|w| {
102 let w = w.ovrflw().set(); 102 let w = w.ovrflw().set();
103 let w = w.compare3().set(); 103 let w = w.compare3().set();
104 w 104 w
105 }); 105 });
106 106
107 self.rtc.tasks_clear.write(|w| unsafe { w.bits(1) }); 107 r.tasks_clear.write(|w| unsafe { w.bits(1) });
108 self.rtc.tasks_start.write(|w| unsafe { w.bits(1) }); 108 r.tasks_start.write(|w| unsafe { w.bits(1) });
109 109
110 // Wait for clear 110 // Wait for clear
111 while self.rtc.counter.read().bits() != 0 {} 111 while r.counter.read().bits() != 0 {}
112 112
113 self.irq.set_handler(|ptr| unsafe { 113 self.irq.set_handler(|ptr| unsafe {
114 let this = &*(ptr as *const () as *const Self); 114 let this = &*(ptr as *const () as *const Self);
@@ -120,19 +120,20 @@ impl<T: Instance> RTC<T> {
120 } 120 }
121 121
122 fn on_interrupt(&self) { 122 fn on_interrupt(&self) {
123 if self.rtc.events_ovrflw.read().bits() == 1 { 123 let r = self.rtc.regs();
124 self.rtc.events_ovrflw.write(|w| w); 124 if r.events_ovrflw.read().bits() == 1 {
125 r.events_ovrflw.write(|w| w);
125 self.next_period(); 126 self.next_period();
126 } 127 }
127 128
128 if self.rtc.events_compare[3].read().bits() == 1 { 129 if r.events_compare[3].read().bits() == 1 {
129 self.rtc.events_compare[3].write(|w| w); 130 r.events_compare[3].write(|w| w);
130 self.next_period(); 131 self.next_period();
131 } 132 }
132 133
133 for n in 0..ALARM_COUNT { 134 for n in 0..ALARM_COUNT {
134 if self.rtc.events_compare[n].read().bits() == 1 { 135 if r.events_compare[n].read().bits() == 1 {
135 self.rtc.events_compare[n].write(|w| w); 136 r.events_compare[n].write(|w| w);
136 interrupt::free(|cs| { 137 interrupt::free(|cs| {
137 self.trigger_alarm(n, cs); 138 self.trigger_alarm(n, cs);
138 }) 139 })
@@ -142,6 +143,7 @@ impl<T: Instance> RTC<T> {
142 143
143 fn next_period(&self) { 144 fn next_period(&self) {
144 interrupt::free(|cs| { 145 interrupt::free(|cs| {
146 let r = self.rtc.regs();
145 let period = self.period.fetch_add(1, Ordering::Relaxed) + 1; 147 let period = self.period.fetch_add(1, Ordering::Relaxed) + 1;
146 let t = (period as u64) << 23; 148 let t = (period as u64) << 23;
147 149
@@ -151,15 +153,16 @@ impl<T: Instance> RTC<T> {
151 153
152 let diff = at - t; 154 let diff = at - t;
153 if diff < 0xc00000 { 155 if diff < 0xc00000 {
154 self.rtc.cc[n].write(|w| unsafe { w.bits(at as u32 & 0xFFFFFF) }); 156 r.cc[n].write(|w| unsafe { w.bits(at as u32 & 0xFFFFFF) });
155 self.rtc.intenset.write(|w| unsafe { w.bits(compare_n(n)) }); 157 r.intenset.write(|w| unsafe { w.bits(compare_n(n)) });
156 } 158 }
157 } 159 }
158 }) 160 })
159 } 161 }
160 162
161 fn trigger_alarm(&self, n: usize, cs: &CriticalSection) { 163 fn trigger_alarm(&self, n: usize, cs: &CriticalSection) {
162 self.rtc.intenclr.write(|w| unsafe { w.bits(compare_n(n)) }); 164 let r = self.rtc.regs();
165 r.intenclr.write(|w| unsafe { w.bits(compare_n(n)) });
163 166
164 let alarm = &self.alarms.borrow(cs)[n]; 167 let alarm = &self.alarms.borrow(cs)[n];
165 alarm.timestamp.set(u64::MAX); 168 alarm.timestamp.set(u64::MAX);
@@ -190,6 +193,8 @@ impl<T: Instance> RTC<T> {
190 return; 193 return;
191 } 194 }
192 195
196 let r = self.rtc.regs();
197
193 // If it hasn't triggered yet, setup it in the compare channel. 198 // If it hasn't triggered yet, setup it in the compare channel.
194 let diff = timestamp - t; 199 let diff = timestamp - t;
195 if diff < 0xc00000 { 200 if diff < 0xc00000 {
@@ -206,12 +211,12 @@ impl<T: Instance> RTC<T> {
206 // by the Alarm trait contract. What's not allowed is triggering alarms *before* their scheduled time, 211 // by the Alarm trait contract. What's not allowed is triggering alarms *before* their scheduled time,
207 // and we don't do that here. 212 // and we don't do that here.
208 let safe_timestamp = timestamp.max(t + 3); 213 let safe_timestamp = timestamp.max(t + 3);
209 self.rtc.cc[n].write(|w| unsafe { w.bits(safe_timestamp as u32 & 0xFFFFFF) }); 214 r.cc[n].write(|w| unsafe { w.bits(safe_timestamp as u32 & 0xFFFFFF) });
210 self.rtc.intenset.write(|w| unsafe { w.bits(compare_n(n)) }); 215 r.intenset.write(|w| unsafe { w.bits(compare_n(n)) });
211 } else { 216 } else {
212 // If it's too far in the future, don't setup the compare channel yet. 217 // If it's too far in the future, don't setup the compare channel yet.
213 // It will be setup later by `next_period`. 218 // It will be setup later by `next_period`.
214 self.rtc.intenclr.write(|w| unsafe { w.bits(compare_n(n)) }); 219 r.intenclr.write(|w| unsafe { w.bits(compare_n(n)) });
215 } 220 }
216 }) 221 })
217 } 222 }
@@ -232,7 +237,7 @@ impl<T: Instance> embassy::time::Clock for RTC<T> {
232 // `period` MUST be read before `counter`, see comment at the top for details. 237 // `period` MUST be read before `counter`, see comment at the top for details.
233 let period = self.period.load(Ordering::Relaxed); 238 let period = self.period.load(Ordering::Relaxed);
234 compiler_fence(Ordering::Acquire); 239 compiler_fence(Ordering::Acquire);
235 let counter = self.rtc.counter.read().bits(); 240 let counter = self.rtc.regs().counter.read().bits();
236 calc_now(period, counter) 241 calc_now(period, counter)
237 } 242 }
238} 243}
@@ -257,31 +262,32 @@ impl<T: Instance> embassy::time::Alarm for Alarm<T> {
257} 262}
258 263
259mod sealed { 264mod sealed {
260 pub trait Instance {} 265 use super::*;
266 pub trait Instance {
267 fn regs(&self) -> &pac::rtc0::RegisterBlock;
268 }
269}
261 270
262 impl Instance for crate::pac::RTC0 {} 271macro_rules! make_impl {
263 impl Instance for crate::pac::RTC1 {} 272 ($type:ident, $irq:ident) => {
264 #[cfg(any(feature = "52832", feature = "52833", feature = "52840"))] 273 impl sealed::Instance for peripherals::$type {
265 impl Instance for crate::pac::RTC2 {} 274 fn regs(&self) -> &pac::rtc0::RegisterBlock {
275 unsafe { &*pac::$type::ptr() }
276 }
277 }
278 impl Instance for peripherals::$type {
279 type Interrupt = interrupt::$irq;
280 }
281 };
266} 282}
267 283
268/// Implemented by all RTC instances. 284/// Implemented by all RTC instances.
269pub trait Instance: 285pub trait Instance: sealed::Instance + 'static {
270 sealed::Instance + Deref<Target = rtc0::RegisterBlock> + Sized + 'static
271{
272 /// The interrupt associated with this RTC instance. 286 /// The interrupt associated with this RTC instance.
273 type Interrupt: Interrupt; 287 type Interrupt: Interrupt;
274} 288}
275 289
276impl Instance for crate::pac::RTC0 { 290make_impl!(RTC0, RTC0);
277 type Interrupt = interrupt::RTC0; 291make_impl!(RTC1, RTC1);
278}
279
280impl Instance for crate::pac::RTC1 {
281 type Interrupt = interrupt::RTC1;
282}
283
284#[cfg(any(feature = "52832", feature = "52833", feature = "52840"))] 292#[cfg(any(feature = "52832", feature = "52833", feature = "52840"))]
285impl Instance for crate::pac::RTC2 { 293make_impl!(RTC2, RTC2);
286 type Interrupt = interrupt::RTC2;
287}