aboutsummaryrefslogtreecommitdiff
path: root/embassy-nrf
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2022-07-23 15:13:47 +0200
committerDario Nieuwenhuis <[email protected]>2022-07-23 15:13:47 +0200
commit709df0dc1dfff577fb79bbc2f67ea84670072456 (patch)
tree4a54aee47c0d3881b9e0bc809e075728cee8eeae /embassy-nrf
parent19d1ef0e29fdd0bf0407cbe37c388e8a87e7ddfe (diff)
nrf: replace PhantomData usages with PeripheralRef.
Diffstat (limited to 'embassy-nrf')
-rw-r--r--embassy-nrf/src/buffered_uarte.rs11
-rw-r--r--embassy-nrf/src/gpiote.rs42
-rw-r--r--embassy-nrf/src/nvmc.rs8
-rw-r--r--embassy-nrf/src/pwm.rs13
-rw-r--r--embassy-nrf/src/qdec.rs9
-rw-r--r--embassy-nrf/src/saadc.rs134
-rw-r--r--embassy-nrf/src/spim.rs9
-rw-r--r--embassy-nrf/src/timer.rs28
-rw-r--r--embassy-nrf/src/twim.rs11
-rw-r--r--embassy-nrf/src/uarte.rs29
-rw-r--r--embassy-nrf/src/usb.rs20
11 files changed, 150 insertions, 164 deletions
diff --git a/embassy-nrf/src/buffered_uarte.rs b/embassy-nrf/src/buffered_uarte.rs
index 036af3804..89c1ba908 100644
--- a/embassy-nrf/src/buffered_uarte.rs
+++ b/embassy-nrf/src/buffered_uarte.rs
@@ -15,14 +15,13 @@
15 15
16use core::cmp::min; 16use core::cmp::min;
17use core::future::Future; 17use core::future::Future;
18use core::marker::PhantomData;
19use core::sync::atomic::{compiler_fence, Ordering}; 18use core::sync::atomic::{compiler_fence, Ordering};
20use core::task::Poll; 19use core::task::Poll;
21 20
22use embassy::waitqueue::WakerRegistration; 21use embassy::waitqueue::WakerRegistration;
23use embassy_cortex_m::peripheral::{PeripheralMutex, PeripheralState, StateStorage}; 22use embassy_cortex_m::peripheral::{PeripheralMutex, PeripheralState, StateStorage};
24use embassy_hal_common::ring_buffer::RingBuffer; 23use embassy_hal_common::ring_buffer::RingBuffer;
25use embassy_hal_common::{into_ref, low_power_wait_until}; 24use embassy_hal_common::{into_ref, low_power_wait_until, PeripheralRef};
26use futures::future::poll_fn; 25use futures::future::poll_fn;
27// Re-export SVD variants to allow user to directly set values 26// Re-export SVD variants to allow user to directly set values
28pub use pac::uarte0::{baudrate::BAUDRATE_A as Baudrate, config::PARITY_A as Parity}; 27pub use pac::uarte0::{baudrate::BAUDRATE_A as Baudrate, config::PARITY_A as Parity};
@@ -54,7 +53,7 @@ impl<'d, U: UarteInstance, T: TimerInstance> State<'d, U, T> {
54} 53}
55 54
56struct StateInner<'d, U: UarteInstance, T: TimerInstance> { 55struct StateInner<'d, U: UarteInstance, T: TimerInstance> {
57 phantom: PhantomData<&'d mut U>, 56 _peri: PeripheralRef<'d, U>,
58 timer: Timer<'d, T>, 57 timer: Timer<'d, T>,
59 _ppi_ch1: Ppi<'d, AnyConfigurableChannel, 1, 2>, 58 _ppi_ch1: Ppi<'d, AnyConfigurableChannel, 1, 2>,
60 _ppi_ch2: Ppi<'d, AnyConfigurableChannel, 1, 1>, 59 _ppi_ch2: Ppi<'d, AnyConfigurableChannel, 1, 1>,
@@ -78,7 +77,7 @@ impl<'d, U: UarteInstance, T: TimerInstance> Unpin for BufferedUarte<'d, U, T> {
78impl<'d, U: UarteInstance, T: TimerInstance> BufferedUarte<'d, U, T> { 77impl<'d, U: UarteInstance, T: TimerInstance> BufferedUarte<'d, U, T> {
79 pub fn new( 78 pub fn new(
80 state: &'d mut State<'d, U, T>, 79 state: &'d mut State<'d, U, T>,
81 _uarte: impl Peripheral<P = U> + 'd, 80 peri: impl Peripheral<P = U> + 'd,
82 timer: impl Peripheral<P = T> + 'd, 81 timer: impl Peripheral<P = T> + 'd,
83 ppi_ch1: impl Peripheral<P = impl ConfigurableChannel + 'd> + 'd, 82 ppi_ch1: impl Peripheral<P = impl ConfigurableChannel + 'd> + 'd,
84 ppi_ch2: impl Peripheral<P = impl ConfigurableChannel + 'd> + 'd, 83 ppi_ch2: impl Peripheral<P = impl ConfigurableChannel + 'd> + 'd,
@@ -91,7 +90,7 @@ impl<'d, U: UarteInstance, T: TimerInstance> BufferedUarte<'d, U, T> {
91 rx_buffer: &'d mut [u8], 90 rx_buffer: &'d mut [u8],
92 tx_buffer: &'d mut [u8], 91 tx_buffer: &'d mut [u8],
93 ) -> Self { 92 ) -> Self {
94 into_ref!(ppi_ch1, ppi_ch2, irq, rxd, txd, cts, rts); 93 into_ref!(peri, ppi_ch1, ppi_ch2, irq, rxd, txd, cts, rts);
95 94
96 let r = U::regs(); 95 let r = U::regs();
97 96
@@ -163,7 +162,7 @@ impl<'d, U: UarteInstance, T: TimerInstance> BufferedUarte<'d, U, T> {
163 162
164 Self { 163 Self {
165 inner: PeripheralMutex::new(irq, &mut state.0, move || StateInner { 164 inner: PeripheralMutex::new(irq, &mut state.0, move || StateInner {
166 phantom: PhantomData, 165 _peri: peri,
167 timer, 166 timer,
168 _ppi_ch1: ppi_ch1, 167 _ppi_ch1: ppi_ch1,
169 _ppi_ch2: ppi_ch2, 168 _ppi_ch2: ppi_ch2,
diff --git a/embassy-nrf/src/gpiote.rs b/embassy-nrf/src/gpiote.rs
index e89d01685..cef80ae0a 100644
--- a/embassy-nrf/src/gpiote.rs
+++ b/embassy-nrf/src/gpiote.rs
@@ -1,10 +1,9 @@
1use core::convert::Infallible; 1use core::convert::Infallible;
2use core::future::Future; 2use core::future::Future;
3use core::marker::PhantomData;
4use core::task::{Context, Poll}; 3use core::task::{Context, Poll};
5 4
6use embassy::waitqueue::AtomicWaker; 5use embassy::waitqueue::AtomicWaker;
7use embassy_hal_common::impl_peripheral; 6use embassy_hal_common::{impl_peripheral, Peripheral, PeripheralRef};
8use futures::future::poll_fn; 7use futures::future::poll_fn;
9 8
10use crate::gpio::sealed::Pin as _; 9use crate::gpio::sealed::Pin as _;
@@ -301,16 +300,22 @@ impl<'d, C: Channel, T: GpioPin> OutputChannel<'d, C, T> {
301// ======================= 300// =======================
302 301
303pub(crate) struct PortInputFuture<'a> { 302pub(crate) struct PortInputFuture<'a> {
304 pin_port: u8, 303 pin: PeripheralRef<'a, AnyPin>,
305 phantom: PhantomData<&'a mut AnyPin>, 304}
305
306impl<'a> PortInputFuture<'a> {
307 fn new(pin: impl Peripheral<P = impl GpioPin> + 'a) -> Self {
308 Self {
309 pin: pin.into_ref().map_into(),
310 }
311 }
306} 312}
307 313
308impl<'a> Unpin for PortInputFuture<'a> {} 314impl<'a> Unpin for PortInputFuture<'a> {}
309 315
310impl<'a> Drop for PortInputFuture<'a> { 316impl<'a> Drop for PortInputFuture<'a> {
311 fn drop(&mut self) { 317 fn drop(&mut self) {
312 let pin = unsafe { AnyPin::steal(self.pin_port) }; 318 self.pin.conf().modify(|_, w| w.sense().disabled());
313 pin.conf().modify(|_, w| w.sense().disabled());
314 } 319 }
315} 320}
316 321
@@ -318,10 +323,9 @@ impl<'a> Future for PortInputFuture<'a> {
318 type Output = (); 323 type Output = ();
319 324
320 fn poll(self: core::pin::Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { 325 fn poll(self: core::pin::Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
321 PORT_WAKERS[self.pin_port as usize].register(cx.waker()); 326 PORT_WAKERS[self.pin.pin_port() as usize].register(cx.waker());
322 327
323 let pin = unsafe { AnyPin::steal(self.pin_port) }; 328 if self.pin.conf().read().sense().is_disabled() {
324 if pin.conf().read().sense().is_disabled() {
325 Poll::Ready(()) 329 Poll::Ready(())
326 } else { 330 } else {
327 Poll::Pending 331 Poll::Pending
@@ -354,22 +358,12 @@ impl<'d, T: GpioPin> Input<'d, T> {
354impl<'d, T: GpioPin> Flex<'d, T> { 358impl<'d, T: GpioPin> Flex<'d, T> {
355 pub async fn wait_for_high(&mut self) { 359 pub async fn wait_for_high(&mut self) {
356 self.pin.conf().modify(|_, w| w.sense().high()); 360 self.pin.conf().modify(|_, w| w.sense().high());
357 361 PortInputFuture::new(&mut self.pin).await
358 PortInputFuture {
359 pin_port: self.pin.pin_port(),
360 phantom: PhantomData,
361 }
362 .await
363 } 362 }
364 363
365 pub async fn wait_for_low(&mut self) { 364 pub async fn wait_for_low(&mut self) {
366 self.pin.conf().modify(|_, w| w.sense().low()); 365 self.pin.conf().modify(|_, w| w.sense().low());
367 366 PortInputFuture::new(&mut self.pin).await
368 PortInputFuture {
369 pin_port: self.pin.pin_port(),
370 phantom: PhantomData,
371 }
372 .await
373 } 367 }
374 368
375 pub async fn wait_for_rising_edge(&mut self) { 369 pub async fn wait_for_rising_edge(&mut self) {
@@ -388,11 +382,7 @@ impl<'d, T: GpioPin> Flex<'d, T> {
388 } else { 382 } else {
389 self.pin.conf().modify(|_, w| w.sense().high()); 383 self.pin.conf().modify(|_, w| w.sense().high());
390 } 384 }
391 PortInputFuture { 385 PortInputFuture::new(&mut self.pin).await
392 pin_port: self.pin.pin_port(),
393 phantom: PhantomData,
394 }
395 .await
396 } 386 }
397} 387}
398 388
diff --git a/embassy-nrf/src/nvmc.rs b/embassy-nrf/src/nvmc.rs
index 731def46a..cd6100339 100644
--- a/embassy-nrf/src/nvmc.rs
+++ b/embassy-nrf/src/nvmc.rs
@@ -1,9 +1,8 @@
1//! Nvmcerature sensor interface. 1//! Nvmcerature sensor interface.
2 2
3use core::marker::PhantomData;
4use core::{ptr, slice}; 3use core::{ptr, slice};
5 4
6use embassy_hal_common::into_ref; 5use embassy_hal_common::{into_ref, PeripheralRef};
7use embedded_storage::nor_flash::{ 6use embedded_storage::nor_flash::{
8 ErrorType, MultiwriteNorFlash, NorFlash, NorFlashError, NorFlashErrorKind, ReadNorFlash, 7 ErrorType, MultiwriteNorFlash, NorFlash, NorFlashError, NorFlashErrorKind, ReadNorFlash,
9}; 8};
@@ -31,14 +30,13 @@ impl NorFlashError for Error {
31} 30}
32 31
33pub struct Nvmc<'d> { 32pub struct Nvmc<'d> {
34 _p: PhantomData<&'d NVMC>, 33 _p: PeripheralRef<'d, NVMC>,
35} 34}
36 35
37impl<'d> Nvmc<'d> { 36impl<'d> Nvmc<'d> {
38 pub fn new(_p: impl Peripheral<P = NVMC> + 'd) -> Self { 37 pub fn new(_p: impl Peripheral<P = NVMC> + 'd) -> Self {
39 into_ref!(_p); 38 into_ref!(_p);
40 39 Self { _p }
41 Self { _p: PhantomData }
42 } 40 }
43 41
44 fn regs() -> &'static pac::nvmc::RegisterBlock { 42 fn regs() -> &'static pac::nvmc::RegisterBlock {
diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs
index ecc674ce0..5f750a91e 100644
--- a/embassy-nrf/src/pwm.rs
+++ b/embassy-nrf/src/pwm.rs
@@ -1,6 +1,5 @@
1#![macro_use] 1#![macro_use]
2 2
3use core::marker::PhantomData;
4use core::sync::atomic::{compiler_fence, Ordering}; 3use core::sync::atomic::{compiler_fence, Ordering};
5 4
6use embassy_hal_common::{into_ref, PeripheralRef}; 5use embassy_hal_common::{into_ref, PeripheralRef};
@@ -15,7 +14,7 @@ use crate::{pac, Peripheral};
15/// SimplePwm is the traditional pwm interface you're probably used to, allowing 14/// SimplePwm is the traditional pwm interface you're probably used to, allowing
16/// to simply set a duty cycle across up to four channels. 15/// to simply set a duty cycle across up to four channels.
17pub struct SimplePwm<'d, T: Instance> { 16pub struct SimplePwm<'d, T: Instance> {
18 phantom: PhantomData<&'d mut T>, 17 _peri: PeripheralRef<'d, T>,
19 duty: [u16; 4], 18 duty: [u16; 4],
20 ch0: Option<PeripheralRef<'d, AnyPin>>, 19 ch0: Option<PeripheralRef<'d, AnyPin>>,
21 ch1: Option<PeripheralRef<'d, AnyPin>>, 20 ch1: Option<PeripheralRef<'d, AnyPin>>,
@@ -26,7 +25,7 @@ pub struct SimplePwm<'d, T: Instance> {
26/// SequencePwm allows you to offload the updating of a sequence of duty cycles 25/// SequencePwm allows you to offload the updating of a sequence of duty cycles
27/// to up to four channels, as well as repeat that sequence n times. 26/// to up to four channels, as well as repeat that sequence n times.
28pub struct SequencePwm<'d, T: Instance> { 27pub struct SequencePwm<'d, T: Instance> {
29 phantom: PhantomData<&'d mut T>, 28 _peri: PeripheralRef<'d, T>,
30 ch0: Option<PeripheralRef<'d, AnyPin>>, 29 ch0: Option<PeripheralRef<'d, AnyPin>>,
31 ch1: Option<PeripheralRef<'d, AnyPin>>, 30 ch1: Option<PeripheralRef<'d, AnyPin>>,
32 ch2: Option<PeripheralRef<'d, AnyPin>>, 31 ch2: Option<PeripheralRef<'d, AnyPin>>,
@@ -120,6 +119,8 @@ impl<'d, T: Instance> SequencePwm<'d, T> {
120 ch3: Option<PeripheralRef<'d, AnyPin>>, 119 ch3: Option<PeripheralRef<'d, AnyPin>>,
121 config: Config, 120 config: Config,
122 ) -> Result<Self, Error> { 121 ) -> Result<Self, Error> {
122 into_ref!(_pwm);
123
123 let r = T::regs(); 124 let r = T::regs();
124 125
125 if let Some(pin) = &ch0 { 126 if let Some(pin) = &ch0 {
@@ -168,7 +169,7 @@ impl<'d, T: Instance> SequencePwm<'d, T> {
168 r.countertop.write(|w| unsafe { w.countertop().bits(config.max_duty) }); 169 r.countertop.write(|w| unsafe { w.countertop().bits(config.max_duty) });
169 170
170 Ok(Self { 171 Ok(Self {
171 phantom: PhantomData, 172 _peri: _pwm,
172 ch0, 173 ch0,
173 ch1, 174 ch1,
174 ch2, 175 ch2,
@@ -639,6 +640,8 @@ impl<'d, T: Instance> SimplePwm<'d, T> {
639 ch2: Option<PeripheralRef<'d, AnyPin>>, 640 ch2: Option<PeripheralRef<'d, AnyPin>>,
640 ch3: Option<PeripheralRef<'d, AnyPin>>, 641 ch3: Option<PeripheralRef<'d, AnyPin>>,
641 ) -> Self { 642 ) -> Self {
643 into_ref!(_pwm);
644
642 let r = T::regs(); 645 let r = T::regs();
643 646
644 if let Some(pin) = &ch0 { 647 if let Some(pin) = &ch0 {
@@ -666,7 +669,7 @@ impl<'d, T: Instance> SimplePwm<'d, T> {
666 r.psel.out[3].write(|w| unsafe { w.bits(ch3.psel_bits()) }); 669 r.psel.out[3].write(|w| unsafe { w.bits(ch3.psel_bits()) });
667 670
668 let pwm = Self { 671 let pwm = Self {
669 phantom: PhantomData, 672 _peri: _pwm,
670 ch0, 673 ch0,
671 ch1, 674 ch1,
672 ch2, 675 ch2,
diff --git a/embassy-nrf/src/qdec.rs b/embassy-nrf/src/qdec.rs
index d5dc14466..f6daec252 100644
--- a/embassy-nrf/src/qdec.rs
+++ b/embassy-nrf/src/qdec.rs
@@ -1,6 +1,5 @@
1//! Quadrature decoder interface 1//! Quadrature decoder interface
2 2
3use core::marker::PhantomData;
4use core::task::Poll; 3use core::task::Poll;
5 4
6use embassy::waitqueue::AtomicWaker; 5use embassy::waitqueue::AtomicWaker;
@@ -15,7 +14,7 @@ use crate::{interrupt, pac, Peripheral};
15 14
16/// Quadrature decoder 15/// Quadrature decoder
17pub struct Qdec<'d> { 16pub struct Qdec<'d> {
18 phantom: PhantomData<&'d QDEC>, 17 _p: PeripheralRef<'d, QDEC>,
19} 18}
20 19
21#[non_exhaustive] 20#[non_exhaustive]
@@ -66,14 +65,14 @@ impl<'d> Qdec<'d> {
66 } 65 }
67 66
68 fn new_inner( 67 fn new_inner(
69 _t: impl Peripheral<P = QDEC> + 'd, 68 p: impl Peripheral<P = QDEC> + 'd,
70 irq: impl Peripheral<P = interrupt::QDEC> + 'd, 69 irq: impl Peripheral<P = interrupt::QDEC> + 'd,
71 a: PeripheralRef<'d, AnyPin>, 70 a: PeripheralRef<'d, AnyPin>,
72 b: PeripheralRef<'d, AnyPin>, 71 b: PeripheralRef<'d, AnyPin>,
73 led: Option<PeripheralRef<'d, AnyPin>>, 72 led: Option<PeripheralRef<'d, AnyPin>>,
74 config: Config, 73 config: Config,
75 ) -> Self { 74 ) -> Self {
76 into_ref!(irq); 75 into_ref!(p, irq);
77 let r = Self::regs(); 76 let r = Self::regs();
78 77
79 // Select pins. 78 // Select pins.
@@ -131,7 +130,7 @@ impl<'d> Qdec<'d> {
131 }); 130 });
132 irq.enable(); 131 irq.enable();
133 132
134 Self { phantom: PhantomData } 133 Self { _p: p }
135 } 134 }
136 135
137 /// Perform an asynchronous read of the decoder. 136 /// Perform an asynchronous read of the decoder.
diff --git a/embassy-nrf/src/saadc.rs b/embassy-nrf/src/saadc.rs
index 644ffa1f0..6ddc70e52 100644
--- a/embassy-nrf/src/saadc.rs
+++ b/embassy-nrf/src/saadc.rs
@@ -1,11 +1,10 @@
1#![macro_use] 1#![macro_use]
2 2
3use core::marker::PhantomData;
4use core::sync::atomic::{compiler_fence, Ordering}; 3use core::sync::atomic::{compiler_fence, Ordering};
5use core::task::Poll; 4use core::task::Poll;
6 5
7use embassy::waitqueue::AtomicWaker; 6use embassy::waitqueue::AtomicWaker;
8use embassy_hal_common::{impl_peripheral, into_ref}; 7use embassy_hal_common::{impl_peripheral, into_ref, PeripheralRef};
9use futures::future::poll_fn; 8use futures::future::poll_fn;
10use pac::{saadc, SAADC}; 9use pac::{saadc, SAADC};
11use saadc::ch::config::{GAIN_A, REFSEL_A, RESP_A, TACQ_A}; 10use saadc::ch::config::{GAIN_A, REFSEL_A, RESP_A, TACQ_A};
@@ -14,6 +13,7 @@ pub(crate) use saadc::ch::pselp::PSELP_A as InputChannel;
14use saadc::oversample::OVERSAMPLE_A; 13use saadc::oversample::OVERSAMPLE_A;
15use saadc::resolution::VAL_A; 14use saadc::resolution::VAL_A;
16 15
16use self::sealed::Input as _;
17use crate::interrupt::InterruptExt; 17use crate::interrupt::InterruptExt;
18use crate::ppi::{ConfigurableChannel, Event, Ppi, Task}; 18use crate::ppi::{ConfigurableChannel, Event, Ppi, Task};
19use crate::timer::{Frequency, Instance as TimerInstance, Timer}; 19use crate::timer::{Frequency, Instance as TimerInstance, Timer};
@@ -26,7 +26,7 @@ pub enum Error {}
26 26
27/// One-shot and continuous SAADC. 27/// One-shot and continuous SAADC.
28pub struct Saadc<'d, const N: usize> { 28pub struct Saadc<'d, const N: usize> {
29 phantom: PhantomData<&'d mut peripherals::SAADC>, 29 _p: PeripheralRef<'d, peripherals::SAADC>,
30} 30}
31 31
32static WAKER: AtomicWaker = AtomicWaker::new(); 32static WAKER: AtomicWaker = AtomicWaker::new();
@@ -66,63 +66,11 @@ pub struct ChannelConfig<'d> {
66 /// Acquisition time in microseconds. 66 /// Acquisition time in microseconds.
67 pub time: Time, 67 pub time: Time,
68 /// Positive channel to sample 68 /// Positive channel to sample
69 p_channel: InputChannel, 69 p_channel: PeripheralRef<'d, AnyInput>,
70 /// An optional negative channel to sample 70 /// An optional negative channel to sample
71 n_channel: Option<InputChannel>, 71 n_channel: Option<PeripheralRef<'d, AnyInput>>,
72
73 phantom: PhantomData<&'d ()>,
74}
75
76/// A dummy `Input` pin implementation for SAADC peripheral sampling from the
77/// internal voltage.
78pub struct VddInput;
79
80impl_peripheral!(VddInput);
81
82impl sealed::Input for VddInput {
83 #[cfg(not(feature = "_nrf9160"))]
84 fn channel(&self) -> InputChannel {
85 InputChannel::VDD
86 }
87 #[cfg(feature = "_nrf9160")]
88 fn channel(&self) -> InputChannel {
89 InputChannel::VDDGPIO
90 }
91}
92impl Input for VddInput {}
93
94/// A dummy `Input` pin implementation for SAADC peripheral sampling from the
95/// VDDH / 5 voltage.
96#[cfg(any(feature = "_nrf5340-app", feature = "nrf52833", feature = "nrf52840"))]
97pub struct VddhDiv5Input;
98
99#[cfg(any(feature = "_nrf5340-app", feature = "nrf52833", feature = "nrf52840"))]
100impl_peripheral!(VddhDiv5Input);
101
102#[cfg(any(feature = "_nrf5340-app", feature = "nrf52833", feature = "nrf52840"))]
103impl sealed::Input for VddhDiv5Input {
104 fn channel(&self) -> InputChannel {
105 InputChannel::VDDHDIV5
106 }
107}
108
109#[cfg(any(feature = "_nrf5340-app", feature = "nrf52833", feature = "nrf52840"))]
110impl Input for VddhDiv5Input {}
111
112pub struct AnyInput {
113 channel: InputChannel,
114}
115
116impl_peripheral!(AnyInput);
117
118impl sealed::Input for AnyInput {
119 fn channel(&self) -> InputChannel {
120 self.channel
121 }
122} 72}
123 73
124impl Input for AnyInput {}
125
126impl<'d> ChannelConfig<'d> { 74impl<'d> ChannelConfig<'d> {
127 /// Default configuration for single ended channel sampling. 75 /// Default configuration for single ended channel sampling.
128 pub fn single_ended(input: impl Peripheral<P = impl Input> + 'd) -> Self { 76 pub fn single_ended(input: impl Peripheral<P = impl Input> + 'd) -> Self {
@@ -132,9 +80,8 @@ impl<'d> ChannelConfig<'d> {
132 gain: Gain::GAIN1_6, 80 gain: Gain::GAIN1_6,
133 resistor: Resistor::BYPASS, 81 resistor: Resistor::BYPASS,
134 time: Time::_10US, 82 time: Time::_10US,
135 p_channel: input.channel(), 83 p_channel: input.map_into(),
136 n_channel: None, 84 n_channel: None,
137 phantom: PhantomData,
138 } 85 }
139 } 86 }
140 /// Default configuration for differential channel sampling. 87 /// Default configuration for differential channel sampling.
@@ -148,9 +95,8 @@ impl<'d> ChannelConfig<'d> {
148 gain: Gain::GAIN1_6, 95 gain: Gain::GAIN1_6,
149 resistor: Resistor::BYPASS, 96 resistor: Resistor::BYPASS,
150 time: Time::_10US, 97 time: Time::_10US,
151 p_channel: p_input.channel(), 98 p_channel: p_input.map_into(),
152 n_channel: Some(n_input.channel()), 99 n_channel: Some(n_input.map_into()),
153 phantom: PhantomData,
154 } 100 }
155 } 101 }
156} 102}
@@ -167,12 +113,12 @@ pub enum SamplerState {
167 113
168impl<'d, const N: usize> Saadc<'d, N> { 114impl<'d, const N: usize> Saadc<'d, N> {
169 pub fn new( 115 pub fn new(
170 _saadc: impl Peripheral<P = peripherals::SAADC> + 'd, 116 saadc: impl Peripheral<P = peripherals::SAADC> + 'd,
171 irq: impl Peripheral<P = interrupt::SAADC> + 'd, 117 irq: impl Peripheral<P = interrupt::SAADC> + 'd,
172 config: Config, 118 config: Config,
173 channel_configs: [ChannelConfig; N], 119 channel_configs: [ChannelConfig; N],
174 ) -> Self { 120 ) -> Self {
175 into_ref!(irq); 121 into_ref!(saadc, irq);
176 122
177 let r = unsafe { &*SAADC::ptr() }; 123 let r = unsafe { &*SAADC::ptr() };
178 124
@@ -184,9 +130,11 @@ impl<'d, const N: usize> Saadc<'d, N> {
184 r.oversample.write(|w| w.oversample().variant(oversample.into())); 130 r.oversample.write(|w| w.oversample().variant(oversample.into()));
185 131
186 for (i, cc) in channel_configs.iter().enumerate() { 132 for (i, cc) in channel_configs.iter().enumerate() {
187 r.ch[i].pselp.write(|w| w.pselp().variant(cc.p_channel)); 133 r.ch[i].pselp.write(|w| w.pselp().variant(cc.p_channel.channel()));
188 if let Some(n_channel) = cc.n_channel { 134 if let Some(n_channel) = &cc.n_channel {
189 r.ch[i].pseln.write(|w| unsafe { w.pseln().bits(n_channel as u8) }); 135 r.ch[i]
136 .pseln
137 .write(|w| unsafe { w.pseln().bits(n_channel.channel() as u8) });
190 } 138 }
191 r.ch[i].config.write(|w| { 139 r.ch[i].config.write(|w| {
192 w.refsel().variant(cc.reference.into()); 140 w.refsel().variant(cc.reference.into());
@@ -215,7 +163,7 @@ impl<'d, const N: usize> Saadc<'d, N> {
215 irq.unpend(); 163 irq.unpend();
216 irq.enable(); 164 irq.enable();
217 165
218 Self { phantom: PhantomData } 166 Self { _p: saadc }
219 } 167 }
220 168
221 fn on_interrupt(_ctx: *mut ()) { 169 fn on_interrupt(_ctx: *mut ()) {
@@ -674,7 +622,7 @@ pub(crate) mod sealed {
674} 622}
675 623
676/// An input that can be used as either or negative end of a ADC differential in the SAADC periperhal. 624/// An input that can be used as either or negative end of a ADC differential in the SAADC periperhal.
677pub trait Input: sealed::Input + Peripheral<P = Self> + Sized { 625pub trait Input: sealed::Input + Into<AnyInput> + Peripheral<P = Self> + Sized + 'static {
678 fn degrade_saadc(self) -> AnyInput { 626 fn degrade_saadc(self) -> AnyInput {
679 AnyInput { 627 AnyInput {
680 channel: self.channel(), 628 channel: self.channel(),
@@ -682,13 +630,57 @@ pub trait Input: sealed::Input + Peripheral<P = Self> + Sized {
682 } 630 }
683} 631}
684 632
633pub struct AnyInput {
634 channel: InputChannel,
635}
636
637impl_peripheral!(AnyInput);
638
639impl sealed::Input for AnyInput {
640 fn channel(&self) -> InputChannel {
641 self.channel
642 }
643}
644
645impl Input for AnyInput {}
646
685macro_rules! impl_saadc_input { 647macro_rules! impl_saadc_input {
686 ($pin:ident, $ch:ident) => { 648 ($pin:ident, $ch:ident) => {
687 impl crate::saadc::sealed::Input for crate::peripherals::$pin { 649 impl_saadc_input!(@local, crate::peripherals::$pin, $ch);
650 };
651 (@local, $pin:ty, $ch:ident) => {
652 impl crate::saadc::sealed::Input for $pin {
688 fn channel(&self) -> crate::saadc::InputChannel { 653 fn channel(&self) -> crate::saadc::InputChannel {
689 crate::saadc::InputChannel::$ch 654 crate::saadc::InputChannel::$ch
690 } 655 }
691 } 656 }
692 impl crate::saadc::Input for crate::peripherals::$pin {} 657 impl crate::saadc::Input for $pin {}
658
659 impl From<$pin> for crate::saadc::AnyInput {
660 fn from(val: $pin) -> Self {
661 crate::saadc::Input::degrade_saadc(val)
662 }
663 }
693 }; 664 };
694} 665}
666
667/// A dummy `Input` pin implementation for SAADC peripheral sampling from the
668/// internal voltage.
669pub struct VddInput;
670
671impl_peripheral!(VddInput);
672#[cfg(not(feature = "_nrf9160"))]
673impl_saadc_input!(@local, VddInput, VDD);
674#[cfg(feature = "_nrf9160")]
675impl_saadc_input!(@local, VddInput, VDDGPIO);
676
677/// A dummy `Input` pin implementation for SAADC peripheral sampling from the
678/// VDDH / 5 voltage.
679#[cfg(any(feature = "_nrf5340-app", feature = "nrf52833", feature = "nrf52840"))]
680pub struct VddhDiv5Input;
681
682#[cfg(any(feature = "_nrf5340-app", feature = "nrf52833", feature = "nrf52840"))]
683impl_peripheral!(VddhDiv5Input);
684
685#[cfg(any(feature = "_nrf5340-app", feature = "nrf52833", feature = "nrf52840"))]
686impl_saadc_input!(@local, VddhDiv5Input, VDDHDIV5);
diff --git a/embassy-nrf/src/spim.rs b/embassy-nrf/src/spim.rs
index a6b0be076..a512b4813 100644
--- a/embassy-nrf/src/spim.rs
+++ b/embassy-nrf/src/spim.rs
@@ -1,6 +1,5 @@
1#![macro_use] 1#![macro_use]
2 2
3use core::marker::PhantomData;
4use core::sync::atomic::{compiler_fence, Ordering}; 3use core::sync::atomic::{compiler_fence, Ordering};
5use core::task::Poll; 4use core::task::Poll;
6 5
@@ -31,7 +30,7 @@ pub enum Error {
31/// 30///
32/// For more details about EasyDMA, consult the module documentation. 31/// For more details about EasyDMA, consult the module documentation.
33pub struct Spim<'d, T: Instance> { 32pub struct Spim<'d, T: Instance> {
34 phantom: PhantomData<&'d mut T>, 33 _p: PeripheralRef<'d, T>,
35} 34}
36 35
37#[non_exhaustive] 36#[non_exhaustive]
@@ -94,14 +93,14 @@ impl<'d, T: Instance> Spim<'d, T> {
94 } 93 }
95 94
96 fn new_inner( 95 fn new_inner(
97 _spim: impl Peripheral<P = T> + 'd, 96 spim: impl Peripheral<P = T> + 'd,
98 irq: impl Peripheral<P = T::Interrupt> + 'd, 97 irq: impl Peripheral<P = T::Interrupt> + 'd,
99 sck: PeripheralRef<'d, AnyPin>, 98 sck: PeripheralRef<'d, AnyPin>,
100 miso: Option<PeripheralRef<'d, AnyPin>>, 99 miso: Option<PeripheralRef<'d, AnyPin>>,
101 mosi: Option<PeripheralRef<'d, AnyPin>>, 100 mosi: Option<PeripheralRef<'d, AnyPin>>,
102 config: Config, 101 config: Config,
103 ) -> Self { 102 ) -> Self {
104 into_ref!(irq); 103 into_ref!(spim, irq);
105 104
106 let r = T::regs(); 105 let r = T::regs();
107 106
@@ -181,7 +180,7 @@ impl<'d, T: Instance> Spim<'d, T> {
181 irq.unpend(); 180 irq.unpend();
182 irq.enable(); 181 irq.enable();
183 182
184 Self { phantom: PhantomData } 183 Self { _p: spim }
185 } 184 }
186 185
187 fn on_interrupt(_: *mut ()) { 186 fn on_interrupt(_: *mut ()) {
diff --git a/embassy-nrf/src/timer.rs b/embassy-nrf/src/timer.rs
index d7a7c4d70..8deecdc03 100644
--- a/embassy-nrf/src/timer.rs
+++ b/embassy-nrf/src/timer.rs
@@ -5,7 +5,7 @@ use core::task::Poll;
5 5
6use embassy::waitqueue::AtomicWaker; 6use embassy::waitqueue::AtomicWaker;
7use embassy_hal_common::drop::OnDrop; 7use embassy_hal_common::drop::OnDrop;
8use embassy_hal_common::into_ref; 8use embassy_hal_common::{into_ref, PeripheralRef};
9use futures::future::poll_fn; 9use futures::future::poll_fn;
10 10
11use crate::interrupt::{Interrupt, InterruptExt}; 11use crate::interrupt::{Interrupt, InterruptExt};
@@ -95,7 +95,8 @@ impl TimerType for Awaitable {}
95impl TimerType for NotAwaitable {} 95impl TimerType for NotAwaitable {}
96 96
97pub struct Timer<'d, T: Instance, I: TimerType = NotAwaitable> { 97pub struct Timer<'d, T: Instance, I: TimerType = NotAwaitable> {
98 phantom: PhantomData<(&'d mut T, I)>, 98 _p: PeripheralRef<'d, T>,
99 _i: PhantomData<I>,
99} 100}
100 101
101impl<'d, T: Instance> Timer<'d, T, Awaitable> { 102impl<'d, T: Instance> Timer<'d, T, Awaitable> {
@@ -123,10 +124,15 @@ impl<'d, T: Instance, I: TimerType> Timer<'d, T, I> {
123 /// Create a `Timer` without an interrupt, meaning `Cc::wait` won't work. 124 /// Create a `Timer` without an interrupt, meaning `Cc::wait` won't work.
124 /// 125 ///
125 /// This is used by the public constructors. 126 /// This is used by the public constructors.
126 fn new_irqless(_timer: impl Peripheral<P = T> + 'd) -> Self { 127 fn new_irqless(timer: impl Peripheral<P = T> + 'd) -> Self {
128 into_ref!(timer);
129
127 let regs = T::regs(); 130 let regs = T::regs();
128 131
129 let mut this = Self { phantom: PhantomData }; 132 let mut this = Self {
133 _p: timer,
134 _i: PhantomData,
135 };
130 136
131 // Stop the timer before doing anything else, 137 // Stop the timer before doing anything else,
132 // since changing BITMODE while running can cause 'unpredictable behaviour' according to the specification. 138 // since changing BITMODE while running can cause 'unpredictable behaviour' according to the specification.
@@ -230,7 +236,8 @@ impl<'d, T: Instance, I: TimerType> Timer<'d, T, I> {
230 } 236 }
231 Cc { 237 Cc {
232 n, 238 n,
233 phantom: PhantomData, 239 _p: self._p.reborrow(),
240 _i: PhantomData,
234 } 241 }
235 } 242 }
236} 243}
@@ -242,12 +249,13 @@ impl<'d, T: Instance, I: TimerType> Timer<'d, T, I> {
242/// 249///
243/// The timer will fire the register's COMPARE event when its counter reaches the value stored in the register. 250/// The timer will fire the register's COMPARE event when its counter reaches the value stored in the register.
244/// When the register's CAPTURE task is triggered, the timer will store the current value of its counter in the register 251/// When the register's CAPTURE task is triggered, the timer will store the current value of its counter in the register
245pub struct Cc<'a, T: Instance, I: TimerType = NotAwaitable> { 252pub struct Cc<'d, T: Instance, I: TimerType = NotAwaitable> {
246 n: usize, 253 n: usize,
247 phantom: PhantomData<(&'a mut T, I)>, 254 _p: PeripheralRef<'d, T>,
255 _i: PhantomData<I>,
248} 256}
249 257
250impl<'a, T: Instance> Cc<'a, T, Awaitable> { 258impl<'d, T: Instance> Cc<'d, T, Awaitable> {
251 /// Wait until the timer's counter reaches the value stored in this register. 259 /// Wait until the timer's counter reaches the value stored in this register.
252 /// 260 ///
253 /// This requires a mutable reference so that this task's waker cannot be overwritten by a second call to `wait`. 261 /// This requires a mutable reference so that this task's waker cannot be overwritten by a second call to `wait`.
@@ -281,9 +289,9 @@ impl<'a, T: Instance> Cc<'a, T, Awaitable> {
281 on_drop.defuse(); 289 on_drop.defuse();
282 } 290 }
283} 291}
284impl<'a, T: Instance> Cc<'a, T, NotAwaitable> {} 292impl<'d, T: Instance> Cc<'d, T, NotAwaitable> {}
285 293
286impl<'a, T: Instance, I: TimerType> Cc<'a, T, I> { 294impl<'d, T: Instance, I: TimerType> Cc<'d, T, I> {
287 /// Get the current value stored in the register. 295 /// Get the current value stored in the register.
288 pub fn read(&self) -> u32 { 296 pub fn read(&self) -> u32 {
289 T::regs().cc[self.n].read().cc().bits() 297 T::regs().cc[self.n].read().cc().bits()
diff --git a/embassy-nrf/src/twim.rs b/embassy-nrf/src/twim.rs
index 7699d2a71..6d6eb84e7 100644
--- a/embassy-nrf/src/twim.rs
+++ b/embassy-nrf/src/twim.rs
@@ -7,7 +7,6 @@
7//! - nRF52832: Section 33 7//! - nRF52832: Section 33
8//! - nRF52840: Section 6.31 8//! - nRF52840: Section 6.31
9use core::future::Future; 9use core::future::Future;
10use core::marker::PhantomData;
11use core::sync::atomic::compiler_fence; 10use core::sync::atomic::compiler_fence;
12use core::sync::atomic::Ordering::SeqCst; 11use core::sync::atomic::Ordering::SeqCst;
13use core::task::Poll; 12use core::task::Poll;
@@ -16,7 +15,7 @@ use core::task::Poll;
16use embassy::time::{Duration, Instant}; 15use embassy::time::{Duration, Instant};
17use embassy::waitqueue::AtomicWaker; 16use embassy::waitqueue::AtomicWaker;
18use embassy_embedded_hal::SetConfig; 17use embassy_embedded_hal::SetConfig;
19use embassy_hal_common::into_ref; 18use embassy_hal_common::{into_ref, PeripheralRef};
20use futures::future::poll_fn; 19use futures::future::poll_fn;
21 20
22use crate::chip::{EASY_DMA_SIZE, FORCE_COPY_BUFFER_SIZE}; 21use crate::chip::{EASY_DMA_SIZE, FORCE_COPY_BUFFER_SIZE};
@@ -75,18 +74,18 @@ pub enum Error {
75/// 74///
76/// For more details about EasyDMA, consult the module documentation. 75/// For more details about EasyDMA, consult the module documentation.
77pub struct Twim<'d, T: Instance> { 76pub struct Twim<'d, T: Instance> {
78 phantom: PhantomData<&'d mut T>, 77 _p: PeripheralRef<'d, T>,
79} 78}
80 79
81impl<'d, T: Instance> Twim<'d, T> { 80impl<'d, T: Instance> Twim<'d, T> {
82 pub fn new( 81 pub fn new(
83 _twim: impl Peripheral<P = T> + 'd, 82 twim: impl Peripheral<P = T> + 'd,
84 irq: impl Peripheral<P = T::Interrupt> + 'd, 83 irq: impl Peripheral<P = T::Interrupt> + 'd,
85 sda: impl Peripheral<P = impl GpioPin> + 'd, 84 sda: impl Peripheral<P = impl GpioPin> + 'd,
86 scl: impl Peripheral<P = impl GpioPin> + 'd, 85 scl: impl Peripheral<P = impl GpioPin> + 'd,
87 config: Config, 86 config: Config,
88 ) -> Self { 87 ) -> Self {
89 into_ref!(irq, sda, scl); 88 into_ref!(twim, irq, sda, scl);
90 89
91 let r = T::regs(); 90 let r = T::regs();
92 91
@@ -136,7 +135,7 @@ impl<'d, T: Instance> Twim<'d, T> {
136 irq.unpend(); 135 irq.unpend();
137 irq.enable(); 136 irq.enable();
138 137
139 Self { phantom: PhantomData } 138 Self { _p: twim }
140 } 139 }
141 140
142 fn on_interrupt(_: *mut ()) { 141 fn on_interrupt(_: *mut ()) {
diff --git a/embassy-nrf/src/uarte.rs b/embassy-nrf/src/uarte.rs
index e23525563..792b8ecca 100644
--- a/embassy-nrf/src/uarte.rs
+++ b/embassy-nrf/src/uarte.rs
@@ -13,7 +13,6 @@
13//! memory may be used given that buffers are passed in directly to its read and write 13//! memory may be used given that buffers are passed in directly to its read and write
14//! methods. 14//! methods.
15 15
16use core::marker::PhantomData;
17use core::sync::atomic::{compiler_fence, Ordering}; 16use core::sync::atomic::{compiler_fence, Ordering};
18use core::task::Poll; 17use core::task::Poll;
19 18
@@ -63,7 +62,6 @@ pub enum Error {
63/// 62///
64/// For more details about EasyDMA, consult the module documentation. 63/// For more details about EasyDMA, consult the module documentation.
65pub struct Uarte<'d, T: Instance> { 64pub struct Uarte<'d, T: Instance> {
66 phantom: PhantomData<&'d mut T>,
67 tx: UarteTx<'d, T>, 65 tx: UarteTx<'d, T>,
68 rx: UarteRx<'d, T>, 66 rx: UarteRx<'d, T>,
69} 67}
@@ -71,13 +69,13 @@ pub struct Uarte<'d, T: Instance> {
71/// Transmitter interface to the UARTE peripheral obtained 69/// Transmitter interface to the UARTE peripheral obtained
72/// via [Uarte]::split. 70/// via [Uarte]::split.
73pub struct UarteTx<'d, T: Instance> { 71pub struct UarteTx<'d, T: Instance> {
74 phantom: PhantomData<&'d mut T>, 72 _p: PeripheralRef<'d, T>,
75} 73}
76 74
77/// Receiver interface to the UARTE peripheral obtained 75/// Receiver interface to the UARTE peripheral obtained
78/// via [Uarte]::split. 76/// via [Uarte]::split.
79pub struct UarteRx<'d, T: Instance> { 77pub struct UarteRx<'d, T: Instance> {
80 phantom: PhantomData<&'d mut T>, 78 _p: PeripheralRef<'d, T>,
81} 79}
82 80
83impl<'d, T: Instance> Uarte<'d, T> { 81impl<'d, T: Instance> Uarte<'d, T> {
@@ -116,7 +114,7 @@ impl<'d, T: Instance> Uarte<'d, T> {
116 } 114 }
117 115
118 fn new_inner( 116 fn new_inner(
119 _uarte: impl Peripheral<P = T> + 'd, 117 uarte: impl Peripheral<P = T> + 'd,
120 irq: impl Peripheral<P = T::Interrupt> + 'd, 118 irq: impl Peripheral<P = T::Interrupt> + 'd,
121 rxd: PeripheralRef<'d, AnyPin>, 119 rxd: PeripheralRef<'d, AnyPin>,
122 txd: PeripheralRef<'d, AnyPin>, 120 txd: PeripheralRef<'d, AnyPin>,
@@ -124,7 +122,7 @@ impl<'d, T: Instance> Uarte<'d, T> {
124 rts: Option<PeripheralRef<'d, AnyPin>>, 122 rts: Option<PeripheralRef<'d, AnyPin>>,
125 config: Config, 123 config: Config,
126 ) -> Self { 124 ) -> Self {
127 into_ref!(irq); 125 into_ref!(uarte, irq);
128 126
129 let r = T::regs(); 127 let r = T::regs();
130 128
@@ -161,9 +159,10 @@ impl<'d, T: Instance> Uarte<'d, T> {
161 s.tx_rx_refcount.store(2, Ordering::Relaxed); 159 s.tx_rx_refcount.store(2, Ordering::Relaxed);
162 160
163 Self { 161 Self {
164 phantom: PhantomData, 162 tx: UarteTx {
165 tx: UarteTx { phantom: PhantomData }, 163 _p: unsafe { uarte.clone_unchecked() },
166 rx: UarteRx { phantom: PhantomData }, 164 },
165 rx: UarteRx { _p: uarte },
167 } 166 }
168 } 167 }
169 168
@@ -267,13 +266,13 @@ impl<'d, T: Instance> UarteTx<'d, T> {
267 } 266 }
268 267
269 fn new_inner( 268 fn new_inner(
270 _uarte: impl Peripheral<P = T> + 'd, 269 uarte: impl Peripheral<P = T> + 'd,
271 irq: impl Peripheral<P = T::Interrupt> + 'd, 270 irq: impl Peripheral<P = T::Interrupt> + 'd,
272 txd: PeripheralRef<'d, AnyPin>, 271 txd: PeripheralRef<'d, AnyPin>,
273 cts: Option<PeripheralRef<'d, AnyPin>>, 272 cts: Option<PeripheralRef<'d, AnyPin>>,
274 config: Config, 273 config: Config,
275 ) -> Self { 274 ) -> Self {
276 into_ref!(irq); 275 into_ref!(uarte, irq);
277 276
278 let r = T::regs(); 277 let r = T::regs();
279 278
@@ -299,7 +298,7 @@ impl<'d, T: Instance> UarteTx<'d, T> {
299 let s = T::state(); 298 let s = T::state();
300 s.tx_rx_refcount.store(1, Ordering::Relaxed); 299 s.tx_rx_refcount.store(1, Ordering::Relaxed);
301 300
302 Self { phantom: PhantomData } 301 Self { _p: uarte }
303 } 302 }
304 303
305 pub async fn write(&mut self, buffer: &[u8]) -> Result<(), Error> { 304 pub async fn write(&mut self, buffer: &[u8]) -> Result<(), Error> {
@@ -459,13 +458,13 @@ impl<'d, T: Instance> UarteRx<'d, T> {
459 } 458 }
460 459
461 fn new_inner( 460 fn new_inner(
462 _uarte: impl Peripheral<P = T> + 'd, 461 uarte: impl Peripheral<P = T> + 'd,
463 irq: impl Peripheral<P = T::Interrupt> + 'd, 462 irq: impl Peripheral<P = T::Interrupt> + 'd,
464 rxd: PeripheralRef<'d, AnyPin>, 463 rxd: PeripheralRef<'d, AnyPin>,
465 rts: Option<PeripheralRef<'d, AnyPin>>, 464 rts: Option<PeripheralRef<'d, AnyPin>>,
466 config: Config, 465 config: Config,
467 ) -> Self { 466 ) -> Self {
468 into_ref!(irq); 467 into_ref!(uarte, irq);
469 468
470 let r = T::regs(); 469 let r = T::regs();
471 470
@@ -491,7 +490,7 @@ impl<'d, T: Instance> UarteRx<'d, T> {
491 let s = T::state(); 490 let s = T::state();
492 s.tx_rx_refcount.store(1, Ordering::Relaxed); 491 s.tx_rx_refcount.store(1, Ordering::Relaxed);
493 492
494 Self { phantom: PhantomData } 493 Self { _p: uarte }
495 } 494 }
496 495
497 pub async fn read(&mut self, buffer: &mut [u8]) -> Result<(), Error> { 496 pub async fn read(&mut self, buffer: &mut [u8]) -> Result<(), Error> {
diff --git a/embassy-nrf/src/usb.rs b/embassy-nrf/src/usb.rs
index eaecda570..378492859 100644
--- a/embassy-nrf/src/usb.rs
+++ b/embassy-nrf/src/usb.rs
@@ -7,7 +7,7 @@ use core::task::Poll;
7 7
8use cortex_m::peripheral::NVIC; 8use cortex_m::peripheral::NVIC;
9use embassy::waitqueue::AtomicWaker; 9use embassy::waitqueue::AtomicWaker;
10use embassy_hal_common::into_ref; 10use embassy_hal_common::{into_ref, PeripheralRef};
11pub use embassy_usb; 11pub use embassy_usb;
12use embassy_usb::driver::{self, EndpointError, Event, Unsupported}; 12use embassy_usb::driver::{self, EndpointError, Event, Unsupported};
13use embassy_usb::types::{EndpointAddress, EndpointInfo, EndpointType, UsbDirection}; 13use embassy_usb::types::{EndpointAddress, EndpointInfo, EndpointType, UsbDirection};
@@ -38,7 +38,7 @@ pub trait UsbSupply {
38} 38}
39 39
40pub struct Driver<'d, T: Instance, P: UsbSupply> { 40pub struct Driver<'d, T: Instance, P: UsbSupply> {
41 phantom: PhantomData<&'d mut T>, 41 _p: PeripheralRef<'d, T>,
42 alloc_in: Allocator, 42 alloc_in: Allocator,
43 alloc_out: Allocator, 43 alloc_out: Allocator,
44 usb_supply: P, 44 usb_supply: P,
@@ -166,14 +166,14 @@ impl UsbSupply for SignalledSupply {
166} 166}
167 167
168impl<'d, T: Instance, P: UsbSupply> Driver<'d, T, P> { 168impl<'d, T: Instance, P: UsbSupply> Driver<'d, T, P> {
169 pub fn new(_usb: impl Peripheral<P = T> + 'd, irq: impl Peripheral<P = T::Interrupt> + 'd, usb_supply: P) -> Self { 169 pub fn new(usb: impl Peripheral<P = T> + 'd, irq: impl Peripheral<P = T::Interrupt> + 'd, usb_supply: P) -> Self {
170 into_ref!(irq); 170 into_ref!(usb, irq);
171 irq.set_handler(Self::on_interrupt); 171 irq.set_handler(Self::on_interrupt);
172 irq.unpend(); 172 irq.unpend();
173 irq.enable(); 173 irq.enable();
174 174
175 Self { 175 Self {
176 phantom: PhantomData, 176 _p: usb,
177 alloc_in: Allocator::new(), 177 alloc_in: Allocator::new(),
178 alloc_out: Allocator::new(), 178 alloc_out: Allocator::new(),
179 usb_supply, 179 usb_supply,
@@ -269,15 +269,15 @@ impl<'d, T: Instance, P: UsbSupply + 'd> driver::Driver<'d> for Driver<'d, T, P>
269 })) 269 }))
270 } 270 }
271 271
272 fn start(self, control_max_packet_size: u16) -> (Self::Bus, Self::ControlPipe) { 272 fn start(mut self, control_max_packet_size: u16) -> (Self::Bus, Self::ControlPipe) {
273 ( 273 (
274 Bus { 274 Bus {
275 phantom: PhantomData, 275 _p: unsafe { self._p.clone_unchecked() },
276 power_available: false, 276 power_available: false,
277 usb_supply: self.usb_supply, 277 usb_supply: self.usb_supply,
278 }, 278 },
279 ControlPipe { 279 ControlPipe {
280 _phantom: PhantomData, 280 _p: self._p,
281 max_packet_size: control_max_packet_size, 281 max_packet_size: control_max_packet_size,
282 }, 282 },
283 ) 283 )
@@ -285,7 +285,7 @@ impl<'d, T: Instance, P: UsbSupply + 'd> driver::Driver<'d> for Driver<'d, T, P>
285} 285}
286 286
287pub struct Bus<'d, T: Instance, P: UsbSupply> { 287pub struct Bus<'d, T: Instance, P: UsbSupply> {
288 phantom: PhantomData<&'d mut T>, 288 _p: PeripheralRef<'d, T>,
289 power_available: bool, 289 power_available: bool,
290 usb_supply: P, 290 usb_supply: P,
291} 291}
@@ -746,7 +746,7 @@ impl<'d, T: Instance> driver::EndpointIn for Endpoint<'d, T, In> {
746} 746}
747 747
748pub struct ControlPipe<'d, T: Instance> { 748pub struct ControlPipe<'d, T: Instance> {
749 _phantom: PhantomData<&'d mut T>, 749 _p: PeripheralRef<'d, T>,
750 max_packet_size: u16, 750 max_packet_size: u16,
751} 751}
752 752