aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2021-03-21 21:58:59 +0100
committerDario Nieuwenhuis <[email protected]>2021-03-29 00:58:57 +0200
commita134fce122d570cfcd3837944554fff6c35e4039 (patch)
treea6d2e80e94c6f230dd4c713406605466c1c1537c
parentd5ff1a0ae30db8963eec1b7f23b90991e4f47c3d (diff)
nrf: nicer Peripherals api, add take()
-rw-r--r--embassy-extras/src/macros.rs72
-rw-r--r--embassy-nrf-examples/src/bin/gpiote_port.rs6
-rw-r--r--embassy-nrf-examples/src/bin/qspi.rs9
-rw-r--r--embassy-nrf-examples/src/bin/spim.rs15
-rw-r--r--embassy-nrf/src/lib.rs108
-rw-r--r--embassy-nrf/src/peripherals.rs106
-rw-r--r--embassy/src/util/mod.rs4
7 files changed, 176 insertions, 144 deletions
diff --git a/embassy-extras/src/macros.rs b/embassy-extras/src/macros.rs
index 151f410af..478549ac0 100644
--- a/embassy-extras/src/macros.rs
+++ b/embassy-extras/src/macros.rs
@@ -1,41 +1,71 @@
1#[macro_export] 1#[macro_export]
2macro_rules! peripherals { 2macro_rules! peripherals {
3 ($($(#[$cfg:meta])? $name:ident: $type:ident),*$(,)?) => { 3 ($($(#[$cfg:meta])? $name:ident: $type:ident),*$(,)?) => {
4 $( 4 pub mod peripherals {
5 $(#[$cfg])? 5 $(
6 #[allow(non_camel_case_types)] 6 $(#[$cfg])?
7 pub struct $type { _private: () } 7 #[allow(non_camel_case_types)]
8 8 pub struct $type { _private: () }
9 $(#[$cfg])? 9
10 impl embassy::util::PeripheralBorrow for $type { 10 impl embassy::util::Steal for $type {
11 type Target = $type; 11 #[inline]
12 unsafe fn unborrow(self) -> $type { 12 unsafe fn steal() -> Self {
13 self 13 Self{ _private: ()}
14 }
14 } 15 }
15 }
16 16
17 $(#[$cfg])? 17 $(#[$cfg])?
18 impl embassy::util::PeripheralBorrow for &mut $type { 18 impl embassy::util::PeripheralBorrow for $type {
19 type Target = $type; 19 type Target = $type;
20 unsafe fn unborrow(self) -> $type { 20 #[inline]
21 ::core::ptr::read(self) 21 unsafe fn unborrow(self) -> $type {
22 self
23 }
22 } 24 }
23 } 25
24 )* 26 $(#[$cfg])?
27 impl embassy::util::PeripheralBorrow for &mut $type {
28 type Target = $type;
29 #[inline]
30 unsafe fn unborrow(self) -> $type {
31 ::core::ptr::read(self)
32 }
33 }
34 )*
35 }
25 36
26 pub struct Peripherals { 37 pub struct Peripherals {
27 $( 38 $(
28 $(#[$cfg])? 39 $(#[$cfg])?
29 pub $name: $type, 40 pub $name: peripherals::$type,
30 )* 41 )*
31 } 42 }
32 43
33 impl Peripherals { 44 impl Peripherals {
34 pub unsafe fn steal() -> Self { 45 ///Returns all the peripherals *once*
46 #[inline]
47 pub fn take() -> Option<Self> {
48
49 #[no_mangle]
50 static mut _EMBASSY_DEVICE_PERIPHERALS: bool = false;
51
52 cortex_m::interrupt::free(|_| {
53 if unsafe { _EMBASSY_DEVICE_PERIPHERALS } {
54 None
55 } else {
56 Some(unsafe { <Self as embassy::util::Steal>::steal() })
57 }
58 })
59 }
60 }
61
62 impl embassy::util::Steal for Peripherals {
63 #[inline]
64 unsafe fn steal() -> Self {
35 Self { 65 Self {
36 $( 66 $(
37 $(#[$cfg])? 67 $(#[$cfg])?
38 $name: $type { _private: () }, 68 $name: <peripherals::$type as embassy::util::Steal>::steal(),
39 )* 69 )*
40 } 70 }
41 } 71 }
diff --git a/embassy-nrf-examples/src/bin/gpiote_port.rs b/embassy-nrf-examples/src/bin/gpiote_port.rs
index 0ec9e5d38..593261aed 100644
--- a/embassy-nrf-examples/src/bin/gpiote_port.rs
+++ b/embassy-nrf-examples/src/bin/gpiote_port.rs
@@ -6,18 +6,18 @@
6 6
7#[path = "../example_common.rs"] 7#[path = "../example_common.rs"]
8mod example_common; 8mod example_common;
9use example_common::*;
10 9
11use core::pin::Pin; 10use core::pin::Pin;
12use cortex_m_rt::entry; 11use cortex_m_rt::entry;
13use defmt::panic; 12use defmt::panic;
14
15use embassy::executor::{task, Executor}; 13use embassy::executor::{task, Executor};
16use embassy::traits::gpio::{WaitForHigh, WaitForLow}; 14use embassy::traits::gpio::{WaitForHigh, WaitForLow};
17use embassy::util::Forever; 15use embassy::util::Forever;
18use embassy_nrf::gpio::{AnyPin, Input, Pin as _, Pull}; 16use embassy_nrf::gpio::{AnyPin, Input, Pin as _, Pull};
19use embassy_nrf::gpiote::{self, PortInput}; 17use embassy_nrf::gpiote::{self, PortInput};
20use embassy_nrf::interrupt; 18use embassy_nrf::interrupt;
19use embassy_nrf::Peripherals;
20use example_common::*;
21 21
22async fn button(n: usize, mut pin: PortInput<AnyPin>) { 22async fn button(n: usize, mut pin: PortInput<AnyPin>) {
23 loop { 23 loop {
@@ -30,7 +30,7 @@ async fn button(n: usize, mut pin: PortInput<AnyPin>) {
30 30
31#[task] 31#[task]
32async fn run() { 32async fn run() {
33 let p = unsafe { embassy_nrf::peripherals::Peripherals::steal() }; 33 let p = Peripherals::take().unwrap();
34 34
35 let g = gpiote::initialize(p.gpiote, interrupt::take!(GPIOTE)); 35 let g = gpiote::initialize(p.gpiote, interrupt::take!(GPIOTE));
36 36
diff --git a/embassy-nrf-examples/src/bin/qspi.rs b/embassy-nrf-examples/src/bin/qspi.rs
index a08969881..1637b3977 100644
--- a/embassy-nrf-examples/src/bin/qspi.rs
+++ b/embassy-nrf-examples/src/bin/qspi.rs
@@ -6,17 +6,16 @@
6 6
7#[path = "../example_common.rs"] 7#[path = "../example_common.rs"]
8mod example_common; 8mod example_common;
9use embassy_nrf::peripherals::Peripherals;
10use example_common::*;
11 9
12use cortex_m_rt::entry; 10use cortex_m_rt::entry;
13use defmt::{assert_eq, panic}; 11use defmt::{assert_eq, panic};
14use futures::pin_mut;
15
16use embassy::executor::{task, Executor}; 12use embassy::executor::{task, Executor};
17use embassy::traits::flash::Flash; 13use embassy::traits::flash::Flash;
18use embassy::util::Forever; 14use embassy::util::Forever;
15use embassy_nrf::Peripherals;
19use embassy_nrf::{interrupt, qspi}; 16use embassy_nrf::{interrupt, qspi};
17use example_common::*;
18use futures::pin_mut;
20 19
21const PAGE_SIZE: usize = 4096; 20const PAGE_SIZE: usize = 4096;
22 21
@@ -27,7 +26,7 @@ struct AlignedBuf([u8; 4096]);
27 26
28#[task] 27#[task]
29async fn run() { 28async fn run() {
30 let p = unsafe { Peripherals::steal() }; 29 let p = Peripherals::take().unwrap();
31 30
32 let csn = p.p0_17; 31 let csn = p.p0_17;
33 let sck = p.p0_19; 32 let sck = p.p0_19;
diff --git a/embassy-nrf-examples/src/bin/spim.rs b/embassy-nrf-examples/src/bin/spim.rs
index b7436332e..f4fb22bab 100644
--- a/embassy-nrf-examples/src/bin/spim.rs
+++ b/embassy-nrf-examples/src/bin/spim.rs
@@ -6,26 +6,25 @@
6 6
7#[path = "../example_common.rs"] 7#[path = "../example_common.rs"]
8mod example_common; 8mod example_common;
9use embassy_nrf::gpio::{Level, Output, OutputDrive};
10use embassy_nrf::peripherals::Peripherals;
11use embassy_traits::spi::FullDuplex;
12use example_common::*;
13 9
14use cortex_m_rt::entry; 10use cortex_m_rt::entry;
15use defmt::panic; 11use defmt::panic;
16use embassy::executor::{task, Executor}; 12use embassy::executor::{task, Executor};
17use embassy::util::Forever; 13use embassy::util::Forever;
14use embassy_nrf::gpio::{Level, Output, OutputDrive};
15use embassy_nrf::Peripherals;
16use embassy_nrf::{interrupt, pac, rtc, spim};
17use embassy_traits::spi::FullDuplex;
18use embedded_hal::digital::v2::*; 18use embedded_hal::digital::v2::*;
19use example_common::*;
19use futures::pin_mut; 20use futures::pin_mut;
20use nrf52840_hal::clocks; 21use nrf52840_hal::clocks;
21 22
22use embassy_nrf::{interrupt, pac, rtc, spim};
23
24#[task] 23#[task]
25async fn run() { 24async fn run() {
26 info!("running!"); 25 info!("running!");
27 26
28 let mut p = unsafe { Peripherals::steal() }; 27 let p = Peripherals::take().unwrap();
29 28
30 let config = spim::Config { 29 let config = spim::Config {
31 frequency: spim::Frequency::M16, 30 frequency: spim::Frequency::M16,
@@ -33,7 +32,7 @@ async fn run() {
33 orc: 0x00, 32 orc: 0x00,
34 }; 33 };
35 34
36 let mut irq = interrupt::take!(SPIM3); 35 let irq = interrupt::take!(SPIM3);
37 let spim = spim::Spim::new(p.spim3, irq, p.p0_29, p.p0_28, p.p0_30, config); 36 let spim = spim::Spim::new(p.spim3, irq, p.p0_29, p.p0_28, p.p0_30, config);
38 pin_mut!(spim); 37 pin_mut!(spim);
39 38
diff --git a/embassy-nrf/src/lib.rs b/embassy-nrf/src/lib.rs
index 07759996c..22c71e7f5 100644
--- a/embassy-nrf/src/lib.rs
+++ b/embassy-nrf/src/lib.rs
@@ -97,9 +97,115 @@ pub mod buffered_uarte;
97pub mod gpio; 97pub mod gpio;
98pub mod gpiote; 98pub mod gpiote;
99pub mod interrupt; 99pub mod interrupt;
100pub mod peripherals;
101#[cfg(feature = "52840")] 100#[cfg(feature = "52840")]
102pub mod qspi; 101pub mod qspi;
103pub mod rtc; 102pub mod rtc;
104pub mod spim; 103pub mod spim;
105pub mod uarte; 104pub mod uarte;
105
106embassy_extras::peripherals! {
107 // RTC
108 rtc0: RTC0,
109 rtc1: RTC1,
110 #[cfg(any(feature = "52832", feature = "52833", feature = "52840"))]
111 rtc2: RTC2,
112
113 // QSPI
114 #[cfg(feature = "52840")]
115 qspi: QSPI,
116
117 // UARTE
118 uarte0: UARTE0,
119 #[cfg(any(feature = "52833", feature = "52840", feature = "9160"))]
120 uarte1: UARTE1,
121
122 // SPIM
123 // TODO this is actually shared with SPI, SPIM, SPIS, TWI, TWIS, TWIS.
124 // When they're all implemented, they should be only one peripheral here.
125 spim0: SPIM0,
126 #[cfg(any(feature = "52832", feature = "52833", feature = "52840"))]
127 spim1: SPIM1,
128 #[cfg(any(feature = "52832", feature = "52833", feature = "52840"))]
129 spim2: SPIM2,
130 #[cfg(any(feature = "52833", feature = "52840"))]
131 spim3: SPIM3,
132
133 // GPIOTE
134 gpiote: GPIOTE,
135 gpiote_ch_0: GPIOTE_CH0,
136 gpiote_ch_1: GPIOTE_CH1,
137 gpiote_ch_2: GPIOTE_CH2,
138 gpiote_ch_3: GPIOTE_CH3,
139 gpiote_ch_4: GPIOTE_CH4,
140 gpiote_ch_5: GPIOTE_CH5,
141 gpiote_ch_6: GPIOTE_CH6,
142 gpiote_ch_7: GPIOTE_CH7,
143
144 // GPIO port 0
145 p0_00: P0_00,
146 p0_01: P0_01,
147 p0_02: P0_02,
148 p0_03: P0_03,
149 p0_04: P0_04,
150 p0_05: P0_05,
151 p0_06: P0_06,
152 p0_07: P0_07,
153 p0_08: P0_08,
154 p0_09: P0_09,
155 p0_10: P0_10,
156 p0_11: P0_11,
157 p0_12: P0_12,
158 p0_13: P0_13,
159 p0_14: P0_14,
160 p0_15: P0_15,
161 p0_16: P0_16,
162 p0_17: P0_17,
163 p0_18: P0_18,
164 p0_19: P0_19,
165 p0_20: P0_20,
166 p0_21: P0_21,
167 p0_22: P0_22,
168 p0_23: P0_23,
169 p0_24: P0_24,
170 p0_25: P0_25,
171 p0_26: P0_26,
172 p0_27: P0_27,
173 p0_28: P0_28,
174 p0_29: P0_29,
175 p0_30: P0_30,
176 p0_31: P0_31,
177
178 // GPIO port 1
179 #[cfg(any(feature = "52833", feature = "52840"))]
180 p1_00: P1_00,
181 #[cfg(any(feature = "52833", feature = "52840"))]
182 p1_01: P1_01,
183 #[cfg(any(feature = "52833", feature = "52840"))]
184 p1_02: P1_02,
185 #[cfg(any(feature = "52833", feature = "52840"))]
186 p1_03: P1_03,
187 #[cfg(any(feature = "52833", feature = "52840"))]
188 p1_04: P1_04,
189 #[cfg(any(feature = "52833", feature = "52840"))]
190 p1_05: P1_05,
191 #[cfg(any(feature = "52833", feature = "52840"))]
192 p1_06: P1_06,
193 #[cfg(any(feature = "52833", feature = "52840"))]
194 p1_07: P1_07,
195 #[cfg(any(feature = "52833", feature = "52840"))]
196 p1_08: P1_08,
197 #[cfg(any(feature = "52833", feature = "52840"))]
198 p1_09: P1_09,
199 #[cfg(any(feature = "52833", feature = "52840"))]
200 p1_10: P1_10,
201 #[cfg(any(feature = "52833", feature = "52840"))]
202 p1_11: P1_11,
203 #[cfg(any(feature = "52833", feature = "52840"))]
204 p1_12: P1_12,
205 #[cfg(any(feature = "52833", feature = "52840"))]
206 p1_13: P1_13,
207 #[cfg(any(feature = "52833", feature = "52840"))]
208 p1_14: P1_14,
209 #[cfg(any(feature = "52833", feature = "52840"))]
210 p1_15: P1_15,
211}
diff --git a/embassy-nrf/src/peripherals.rs b/embassy-nrf/src/peripherals.rs
deleted file mode 100644
index ea76c8092..000000000
--- a/embassy-nrf/src/peripherals.rs
+++ /dev/null
@@ -1,106 +0,0 @@
1embassy_extras::peripherals! {
2 // RTC
3 rtc0: RTC0,
4 rtc1: RTC1,
5 #[cfg(any(feature = "52832", feature = "52833", feature = "52840"))]
6 rtc2: RTC2,
7
8 // QSPI
9 #[cfg(feature = "52840")]
10 qspi: QSPI,
11
12 // UARTE
13 uarte0: UARTE0,
14 #[cfg(any(feature = "52833", feature = "52840", feature = "9160"))]
15 uarte1: UARTE1,
16
17 // SPIM
18 // TODO this is actually shared with SPI, SPIM, SPIS, TWI, TWIS, TWIS.
19 // When they're all implemented, they should be only one peripheral here.
20 spim0: SPIM0,
21 #[cfg(any(feature = "52832", feature = "52833", feature = "52840"))]
22 spim1: SPIM1,
23 #[cfg(any(feature = "52832", feature = "52833", feature = "52840"))]
24 spim2: SPIM2,
25 #[cfg(any(feature = "52833", feature = "52840"))]
26 spim3: SPIM3,
27
28 // GPIOTE
29 gpiote: GPIOTE,
30 gpiote_ch_0: GPIOTE_CH0,
31 gpiote_ch_1: GPIOTE_CH1,
32 gpiote_ch_2: GPIOTE_CH2,
33 gpiote_ch_3: GPIOTE_CH3,
34 gpiote_ch_4: GPIOTE_CH4,
35 gpiote_ch_5: GPIOTE_CH5,
36 gpiote_ch_6: GPIOTE_CH6,
37 gpiote_ch_7: GPIOTE_CH7,
38
39 // GPIO port 0
40 p0_00: P0_00,
41 p0_01: P0_01,
42 p0_02: P0_02,
43 p0_03: P0_03,
44 p0_04: P0_04,
45 p0_05: P0_05,
46 p0_06: P0_06,
47 p0_07: P0_07,
48 p0_08: P0_08,
49 p0_09: P0_09,
50 p0_10: P0_10,
51 p0_11: P0_11,
52 p0_12: P0_12,
53 p0_13: P0_13,
54 p0_14: P0_14,
55 p0_15: P0_15,
56 p0_16: P0_16,
57 p0_17: P0_17,
58 p0_18: P0_18,
59 p0_19: P0_19,
60 p0_20: P0_20,
61 p0_21: P0_21,
62 p0_22: P0_22,
63 p0_23: P0_23,
64 p0_24: P0_24,
65 p0_25: P0_25,
66 p0_26: P0_26,
67 p0_27: P0_27,
68 p0_28: P0_28,
69 p0_29: P0_29,
70 p0_30: P0_30,
71 p0_31: P0_31,
72
73 // GPIO port 1
74 #[cfg(any(feature = "52833", feature = "52840"))]
75 p1_00: P1_00,
76 #[cfg(any(feature = "52833", feature = "52840"))]
77 p1_01: P1_01,
78 #[cfg(any(feature = "52833", feature = "52840"))]
79 p1_02: P1_02,
80 #[cfg(any(feature = "52833", feature = "52840"))]
81 p1_03: P1_03,
82 #[cfg(any(feature = "52833", feature = "52840"))]
83 p1_04: P1_04,
84 #[cfg(any(feature = "52833", feature = "52840"))]
85 p1_05: P1_05,
86 #[cfg(any(feature = "52833", feature = "52840"))]
87 p1_06: P1_06,
88 #[cfg(any(feature = "52833", feature = "52840"))]
89 p1_07: P1_07,
90 #[cfg(any(feature = "52833", feature = "52840"))]
91 p1_08: P1_08,
92 #[cfg(any(feature = "52833", feature = "52840"))]
93 p1_09: P1_09,
94 #[cfg(any(feature = "52833", feature = "52840"))]
95 p1_10: P1_10,
96 #[cfg(any(feature = "52833", feature = "52840"))]
97 p1_11: P1_11,
98 #[cfg(any(feature = "52833", feature = "52840"))]
99 p1_12: P1_12,
100 #[cfg(any(feature = "52833", feature = "52840"))]
101 p1_13: P1_13,
102 #[cfg(any(feature = "52833", feature = "52840"))]
103 p1_14: P1_14,
104 #[cfg(any(feature = "52833", feature = "52840"))]
105 p1_15: P1_15,
106}
diff --git a/embassy/src/util/mod.rs b/embassy/src/util/mod.rs
index 18b61246b..3166c65d9 100644
--- a/embassy/src/util/mod.rs
+++ b/embassy/src/util/mod.rs
@@ -19,3 +19,7 @@ pub trait PeripheralBorrow {
19 type Target; 19 type Target;
20 unsafe fn unborrow(self) -> Self::Target; 20 unsafe fn unborrow(self) -> Self::Target;
21} 21}
22
23pub trait Steal {
24 unsafe fn steal() -> Self;
25}