aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2021-03-27 04:27:57 +0100
committerDario Nieuwenhuis <[email protected]>2021-03-29 00:58:58 +0200
commit646be40ac582d88bfe58daa9c60502a6256f4da5 (patch)
tree7fd17b368b04aa58244f710533fabb5461c2fce9
parent90f599bc2fdc9ff94002970d9c147e02e6961acf (diff)
nrf/gpiote: make number() public, change to usize
-rw-r--r--embassy-nrf/src/gpiote.rs58
1 files changed, 26 insertions, 32 deletions
diff --git a/embassy-nrf/src/gpiote.rs b/embassy-nrf/src/gpiote.rs
index 32f085a60..c321c0fd9 100644
--- a/embassy-nrf/src/gpiote.rs
+++ b/embassy-nrf/src/gpiote.rs
@@ -1,15 +1,11 @@
1use core::convert::Infallible; 1use core::convert::Infallible;
2use core::future::Future; 2use core::future::Future;
3use core::intrinsics::transmute;
4use core::marker::PhantomData; 3use core::marker::PhantomData;
5use core::mem::{self, ManuallyDrop};
6use core::ops::Deref;
7use core::pin::Pin; 4use core::pin::Pin;
8use core::ptr;
9use core::task::{Context, Poll}; 5use core::task::{Context, Poll};
10use embassy::interrupt::InterruptExt; 6use embassy::interrupt::InterruptExt;
11use embassy::traits::gpio::{WaitForHigh, WaitForLow}; 7use embassy::traits::gpio::{WaitForHigh, WaitForLow};
12use embassy::util::{AtomicWaker, PeripheralBorrow, Signal}; 8use embassy::util::AtomicWaker;
13use embassy_extras::impl_unborrow; 9use embassy_extras::impl_unborrow;
14use embedded_hal::digital::v2::{InputPin, OutputPin, StatefulOutputPin}; 10use embedded_hal::digital::v2::{InputPin, OutputPin, StatefulOutputPin};
15use futures::future::poll_fn; 11use futures::future::poll_fn;
@@ -19,7 +15,6 @@ use crate::gpio::{AnyPin, Input, Output, Pin as GpioPin, Port, Pull};
19use crate::pac; 15use crate::pac;
20use crate::pac::generic::Reg; 16use crate::pac::generic::Reg;
21use crate::pac::gpiote::_TASKS_OUT; 17use crate::pac::gpiote::_TASKS_OUT;
22use crate::pac::p0 as pac_gpio;
23use crate::{interrupt, peripherals}; 18use crate::{interrupt, peripherals};
24 19
25#[cfg(not(feature = "51"))] 20#[cfg(not(feature = "51"))]
@@ -32,9 +27,9 @@ pub const PIN_COUNT: usize = 48;
32#[cfg(not(any(feature = "52833", feature = "52840")))] 27#[cfg(not(any(feature = "52833", feature = "52840")))]
33pub const PIN_COUNT: usize = 32; 28pub const PIN_COUNT: usize = 32;
34 29
35const NEW_AWR: AtomicWaker = AtomicWaker::new(); 30const NEW_AW: AtomicWaker = AtomicWaker::new();
36static CHANNEL_WAKERS: [AtomicWaker; CHANNEL_COUNT] = [NEW_AWR; CHANNEL_COUNT]; 31static CHANNEL_WAKERS: [AtomicWaker; CHANNEL_COUNT] = [NEW_AW; CHANNEL_COUNT];
37static PORT_WAKERS: [AtomicWaker; PIN_COUNT] = [NEW_AWR; PIN_COUNT]; 32static PORT_WAKERS: [AtomicWaker; PIN_COUNT] = [NEW_AW; PIN_COUNT];
38 33
39pub enum InputChannelPolarity { 34pub enum InputChannelPolarity {
40 None, 35 None,
@@ -135,7 +130,7 @@ pub struct InputChannel<'d, C: Channel, T: GpioPin> {
135impl<'d, C: Channel, T: GpioPin> Drop for InputChannel<'d, C, T> { 130impl<'d, C: Channel, T: GpioPin> Drop for InputChannel<'d, C, T> {
136 fn drop(&mut self) { 131 fn drop(&mut self) {
137 let g = unsafe { &*pac::GPIOTE::ptr() }; 132 let g = unsafe { &*pac::GPIOTE::ptr() };
138 let num = self.ch.number() as usize; 133 let num = self.ch.number();
139 g.config[num].write(|w| w.mode().disabled()); 134 g.config[num].write(|w| w.mode().disabled());
140 g.intenclr.write(|w| unsafe { w.bits(1 << num) }); 135 g.intenclr.write(|w| unsafe { w.bits(1 << num) });
141 } 136 }
@@ -149,7 +144,7 @@ impl<'d, C: Channel, T: GpioPin> InputChannel<'d, C, T> {
149 polarity: InputChannelPolarity, 144 polarity: InputChannelPolarity,
150 ) -> Self { 145 ) -> Self {
151 let g = unsafe { &*pac::GPIOTE::ptr() }; 146 let g = unsafe { &*pac::GPIOTE::ptr() };
152 let num = ch.number() as usize; 147 let num = ch.number();
153 148
154 g.config[num].write(|w| { 149 g.config[num].write(|w| {
155 match polarity { 150 match polarity {
@@ -173,7 +168,7 @@ impl<'d, C: Channel, T: GpioPin> InputChannel<'d, C, T> {
173 168
174 pub async fn wait(&self) { 169 pub async fn wait(&self) {
175 let g = unsafe { &*pac::GPIOTE::ptr() }; 170 let g = unsafe { &*pac::GPIOTE::ptr() };
176 let num = self.ch.number() as usize; 171 let num = self.ch.number();
177 172
178 // Enable interrupt 173 // Enable interrupt
179 g.events_in[num].reset(); 174 g.events_in[num].reset();
@@ -212,7 +207,7 @@ pub struct OutputChannel<'d, C: Channel, T: GpioPin> {
212impl<'d, C: Channel, T: GpioPin> Drop for OutputChannel<'d, C, T> { 207impl<'d, C: Channel, T: GpioPin> Drop for OutputChannel<'d, C, T> {
213 fn drop(&mut self) { 208 fn drop(&mut self) {
214 let g = unsafe { &*pac::GPIOTE::ptr() }; 209 let g = unsafe { &*pac::GPIOTE::ptr() };
215 let num = self.ch.number() as usize; 210 let num = self.ch.number();
216 g.config[num].write(|w| w.mode().disabled()); 211 g.config[num].write(|w| w.mode().disabled());
217 g.intenclr.write(|w| unsafe { w.bits(1 << num) }); 212 g.intenclr.write(|w| unsafe { w.bits(1 << num) });
218 } 213 }
@@ -226,7 +221,7 @@ impl<'d, C: Channel, T: GpioPin> OutputChannel<'d, C, T> {
226 polarity: OutputChannelPolarity, 221 polarity: OutputChannelPolarity,
227 ) -> Self { 222 ) -> Self {
228 let g = unsafe { &*pac::GPIOTE::ptr() }; 223 let g = unsafe { &*pac::GPIOTE::ptr() };
229 let num = ch.number() as usize; 224 let num = ch.number();
230 225
231 g.config[num].write(|w| { 226 g.config[num].write(|w| {
232 w.mode().task(); 227 w.mode().task();
@@ -253,41 +248,41 @@ impl<'d, C: Channel, T: GpioPin> OutputChannel<'d, C, T> {
253 /// Triggers `task out` (as configured with task_out_polarity, defaults to Toggle). 248 /// Triggers `task out` (as configured with task_out_polarity, defaults to Toggle).
254 pub fn out(&self) { 249 pub fn out(&self) {
255 let g = unsafe { &*pac::GPIOTE::ptr() }; 250 let g = unsafe { &*pac::GPIOTE::ptr() };
256 g.tasks_out[self.ch.number() as usize].write(|w| unsafe { w.bits(1) }); 251 g.tasks_out[self.ch.number()].write(|w| unsafe { w.bits(1) });
257 } 252 }
258 253
259 /// Triggers `task set` (set associated pin high). 254 /// Triggers `task set` (set associated pin high).
260 #[cfg(not(feature = "51"))] 255 #[cfg(not(feature = "51"))]
261 pub fn set(&self) { 256 pub fn set(&self) {
262 let g = unsafe { &*pac::GPIOTE::ptr() }; 257 let g = unsafe { &*pac::GPIOTE::ptr() };
263 g.tasks_set[self.ch.number() as usize].write(|w| unsafe { w.bits(1) }); 258 g.tasks_set[self.ch.number()].write(|w| unsafe { w.bits(1) });
264 } 259 }
265 260
266 /// Triggers `task clear` (set associated pin low). 261 /// Triggers `task clear` (set associated pin low).
267 #[cfg(not(feature = "51"))] 262 #[cfg(not(feature = "51"))]
268 pub fn clear(&self) { 263 pub fn clear(&self) {
269 let g = unsafe { &*pac::GPIOTE::ptr() }; 264 let g = unsafe { &*pac::GPIOTE::ptr() };
270 g.tasks_clr[self.ch.number() as usize].write(|w| unsafe { w.bits(1) }); 265 g.tasks_clr[self.ch.number()].write(|w| unsafe { w.bits(1) });
271 } 266 }
272 267
273 /// Returns reference to task_out endpoint for PPI. 268 /// Returns reference to task_out endpoint for PPI.
274 pub fn task_out(&self) -> &Reg<u32, _TASKS_OUT> { 269 pub fn task_out(&self) -> &Reg<u32, _TASKS_OUT> {
275 let g = unsafe { &*pac::GPIOTE::ptr() }; 270 let g = unsafe { &*pac::GPIOTE::ptr() };
276 &g.tasks_out[self.ch.number() as usize] 271 &g.tasks_out[self.ch.number()]
277 } 272 }
278 273
279 /// Returns reference to task_clr endpoint for PPI. 274 /// Returns reference to task_clr endpoint for PPI.
280 #[cfg(not(feature = "51"))] 275 #[cfg(not(feature = "51"))]
281 pub fn task_clr(&self) -> &Reg<u32, _TASKS_CLR> { 276 pub fn task_clr(&self) -> &Reg<u32, _TASKS_CLR> {
282 let g = unsafe { &*pac::GPIOTE::ptr() }; 277 let g = unsafe { &*pac::GPIOTE::ptr() };
283 &g.tasks_clr[self.ch.number() as usize] 278 &g.tasks_clr[self.ch.number()]
284 } 279 }
285 280
286 /// Returns reference to task_set endpoint for PPI. 281 /// Returns reference to task_set endpoint for PPI.
287 #[cfg(not(feature = "51"))] 282 #[cfg(not(feature = "51"))]
288 pub fn task_set(&self) -> &Reg<u32, _TASKS_SET> { 283 pub fn task_set(&self) -> &Reg<u32, _TASKS_SET> {
289 let g = unsafe { &*pac::GPIOTE::ptr() }; 284 let g = unsafe { &*pac::GPIOTE::ptr() };
290 &g.tasks_set[self.ch.number() as usize] 285 &g.tasks_set[self.ch.number()]
291 } 286 }
292} 287}
293 288
@@ -370,15 +365,14 @@ impl<'a> Future for PortInputFuture<'a> {
370} 365}
371 366
372mod sealed { 367mod sealed {
373 pub trait Channel { 368 pub trait Channel {}
374 fn number(&self) -> u8;
375 }
376} 369}
377 370
378pub trait Channel: sealed::Channel + Sized { 371pub trait Channel: sealed::Channel + Sized {
372 fn number(&self) -> usize;
379 fn degrade(self) -> AnyChannel { 373 fn degrade(self) -> AnyChannel {
380 AnyChannel { 374 AnyChannel {
381 number: self.number(), 375 number: self.number() as u8,
382 } 376 }
383 } 377 }
384} 378}
@@ -387,21 +381,21 @@ pub struct AnyChannel {
387 number: u8, 381 number: u8,
388} 382}
389impl_unborrow!(AnyChannel); 383impl_unborrow!(AnyChannel);
390impl Channel for AnyChannel {} 384impl sealed::Channel for AnyChannel {}
391impl sealed::Channel for AnyChannel { 385impl Channel for AnyChannel {
392 fn number(&self) -> u8 { 386 fn number(&self) -> usize {
393 self.number 387 self.number as usize
394 } 388 }
395} 389}
396 390
397macro_rules! impl_channel { 391macro_rules! impl_channel {
398 ($type:ident, $number:expr) => { 392 ($type:ident, $number:expr) => {
399 impl sealed::Channel for peripherals::$type { 393 impl sealed::Channel for peripherals::$type {}
400 fn number(&self) -> u8 { 394 impl Channel for peripherals::$type {
401 $number 395 fn number(&self) -> usize {
396 $number as usize
402 } 397 }
403 } 398 }
404 impl Channel for peripherals::$type {}
405 }; 399 };
406} 400}
407 401