diff --git a/embassy-cortex-m/src/interrupt.rs b/embassy-cortex-m/src/interrupt.rs index 715f0038..7358caa4 100644 --- a/embassy-cortex-m/src/interrupt.rs +++ b/embassy-cortex-m/src/interrupt.rs @@ -3,7 +3,7 @@ use core::{mem, ptr}; use atomic_polyfill::{compiler_fence, AtomicPtr, Ordering}; use cortex_m::peripheral::NVIC; -use embassy_hal_common::Unborrow; +use embassy_hal_common::Peripheral; pub use embassy_macros::cortex_m_interrupt_take as take; /// Implementation detail, do not use outside embassy crates. @@ -32,7 +32,7 @@ unsafe impl cortex_m::interrupt::InterruptNumber for NrWrap { /// Represents an interrupt type that can be configured by embassy to handle /// interrupts. -pub unsafe trait Interrupt: Unborrow { +pub unsafe trait Interrupt: Peripheral

{ /// Return the NVIC interrupt number for this interrupt. fn number(&self) -> u16; /// Steal an instance of this interrupt diff --git a/embassy-cortex-m/src/peripheral.rs b/embassy-cortex-m/src/peripheral.rs index c5fa20e7..e2f29557 100644 --- a/embassy-cortex-m/src/peripheral.rs +++ b/embassy-cortex-m/src/peripheral.rs @@ -3,7 +3,7 @@ use core::mem::MaybeUninit; use cortex_m::peripheral::scb::VectActive; use cortex_m::peripheral::{NVIC, SCB}; -use embassy_hal_common::{unborrow, Unborrow, Unborrowed}; +use embassy_hal_common::{into_ref, Peripheral, PeripheralRef}; use crate::interrupt::{Interrupt, InterruptExt, Priority}; @@ -33,7 +33,7 @@ impl StateStorage { /// a safe way. pub struct PeripheralMutex<'a, S: PeripheralState> { state: *mut S, - irq: Unborrowed<'a, S::Interrupt>, + irq: PeripheralRef<'a, S::Interrupt>, } /// Whether `irq` can be preempted by the current interrupt. @@ -62,11 +62,11 @@ impl<'a, S: PeripheralState> PeripheralMutex<'a, S> { /// /// Registers `on_interrupt` as the `irq`'s handler, and enables it. pub fn new( - irq: impl Unborrow + 'a, + irq: impl Peripheral

+ 'a, storage: &'a mut StateStorage, init: impl FnOnce() -> S, ) -> Self { - unborrow!(irq); + into_ref!(irq); if can_be_preempted(&*irq) { panic!( diff --git a/embassy-hal-common/src/lib.rs b/embassy-hal-common/src/lib.rs index da7ae991..d3d9e0a8 100644 --- a/embassy-hal-common/src/lib.rs +++ b/embassy-hal-common/src/lib.rs @@ -6,10 +6,10 @@ pub(crate) mod fmt; pub mod drop; mod macros; +mod peripheral; pub mod ratio; pub mod ring_buffer; -mod unborrow; -pub use unborrow::{Unborrow, Unborrowed}; +pub use peripheral::{Peripheral, PeripheralRef}; /// Low power blocking wait loop using WFE/SEV. pub fn low_power_wait_until(mut condition: impl FnMut() -> bool) { diff --git a/embassy-hal-common/src/macros.rs b/embassy-hal-common/src/macros.rs index d8e2a06e..7af85f78 100644 --- a/embassy-hal-common/src/macros.rs +++ b/embassy-hal-common/src/macros.rs @@ -21,7 +21,7 @@ macro_rules! peripherals { } $(#[$cfg])? - $crate::impl_unborrow!($name); + $crate::impl_peripheral!($name); )* } @@ -71,22 +71,22 @@ macro_rules! peripherals { } #[macro_export] -macro_rules! unborrow { +macro_rules! into_ref { ($($name:ident),*) => { $( - let mut $name = $name.unborrow(); + let mut $name = $name.into_ref(); )* } } #[macro_export] -macro_rules! impl_unborrow { +macro_rules! impl_peripheral { ($type:ident) => { - impl $crate::Unborrow for $type { - type Target = $type; + impl $crate::Peripheral for $type { + type P = $type; #[inline] - unsafe fn unborrow_unchecked(&mut self) -> Self::Target { + unsafe fn clone_unchecked(&mut self) -> Self::P { $type { ..*self } } } diff --git a/embassy-hal-common/src/peripheral.rs b/embassy-hal-common/src/peripheral.rs new file mode 100644 index 00000000..62004738 --- /dev/null +++ b/embassy-hal-common/src/peripheral.rs @@ -0,0 +1,141 @@ +use core::marker::PhantomData; +use core::ops::{Deref, DerefMut}; + +/// An exclusive reference to a peripheral. +/// +/// This is functionally the same as a `&'a mut T`. The reason for having a +/// dedicated struct is memory efficiency: +/// +/// Peripheral singletons are typically either zero-sized (for concrete peripehrals +/// like `PA9` or `Spi4`) or very small (for example `AnyPin` which is 1 byte). +/// However `&mut T` is always 4 bytes for 32-bit targets, even if T is zero-sized. +/// PeripheralRef stores a copy of `T` instead, so it's the same size. +/// +/// but it is the size of `T` not the size +/// of a pointer. This is useful if T is a zero sized type. +pub struct PeripheralRef<'a, T> { + inner: T, + _lifetime: PhantomData<&'a mut T>, +} + +impl<'a, T> PeripheralRef<'a, T> { + #[inline] + pub fn new(inner: T) -> Self { + Self { + inner, + _lifetime: PhantomData, + } + } + + #[inline] + pub fn map_into(self) -> PeripheralRef<'a, U> + where + T: Into, + { + PeripheralRef { + inner: self.inner.into(), + _lifetime: PhantomData, + } + } + + pub unsafe fn into_inner(self) -> T { + self.inner + } +} + +impl<'a, T> Deref for PeripheralRef<'a, T> { + type Target = T; + + #[inline] + fn deref(&self) -> &Self::Target { + &self.inner + } +} + +impl<'a, T> DerefMut for PeripheralRef<'a, T> { + #[inline] + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.inner + } +} + +/// Trait for any type that can be used as a peripheral of type `P`. +/// +/// This is used in driver constructors, to allow passing either owned peripherals (e.g. `TWISPI0`), +/// or borrowed peripherals (e.g. `&mut TWISPI0`). +/// +/// For example, if you have a driver with a constructor like this: +/// +/// ```ignore +/// impl<'d, T: Instance> Twim<'d, T> { +/// pub fn new( +/// twim: impl Peripheral

+ 'd, +/// irq: impl Peripheral

+ 'd, +/// sda: impl Peripheral

+ 'd, +/// scl: impl Peripheral

+ 'd, +/// config: Config, +/// ) -> Self { .. } +/// } +/// ``` +/// +/// You may call it with owned peripherals, which yields an instance that can live forever (`'static`): +/// +/// ```ignore +/// let mut twi: Twim<'static, ...> = Twim::new(p.TWISPI0, irq, p.P0_03, p.P0_04, config); +/// ``` +/// +/// Or you may call it with borrowed peripherals, which yields an instance that can only live for as long +/// as the borrows last: +/// +/// ```ignore +/// let mut twi: Twim<'_, ...> = Twim::new(&mut p.TWISPI0, &mut irq, &mut p.P0_03, &mut p.P0_04, config); +/// ``` +/// +/// # Implementation details, for HAL authors +/// +/// When writing a HAL, the intended way to use this trait is to take `impl Peripheral

` in +/// the HAL's public API (such as driver constructors), calling `.into_ref()` to obtain a `PeripheralRef`, +/// and storing that in the driver struct. +/// +/// `.into_ref()` on an owned `T` yields a `PeripheralRef<'static, T>`. +/// `.into_ref()` on an `&'a mut T` yields a `PeripheralRef<'a, T>`. +pub trait Peripheral: Sized { + /// Peripheral singleton type + type P; + + /// Unsafely clone (duplicate) a peripheral singleton. + /// + /// # Safety + /// + /// This returns an owned clone of the peripheral. You must manually ensure + /// only one copy of the peripheral is in use at a time. For example, don't + /// create two SPI drivers on `SPI1`, because they will "fight" each other. + /// + /// You should strongly prefer using `into_ref()` instead. It returns a + /// `PeripheralRef`, which allows the borrow checker to enforce this at compile time. + unsafe fn clone_unchecked(&mut self) -> Self::P; + + /// Convert a value into a `PeripheralRef`. + /// + /// When called on an owned `T`, yields a `PeripheralRef<'static, T>`. + /// When called on an `&'a mut T`, yields a `PeripheralRef<'a, T>`. + #[inline] + fn into_ref<'a>(mut self) -> PeripheralRef<'a, Self::P> + where + Self: 'a, + { + PeripheralRef::new(unsafe { self.clone_unchecked() }) + } +} + +impl<'b, T: DerefMut> Peripheral for T +where + T::Target: Peripheral, +{ + type P = ::P; + + #[inline] + unsafe fn clone_unchecked(&mut self) -> Self::P { + self.deref_mut().clone_unchecked() + } +} diff --git a/embassy-hal-common/src/unborrow.rs b/embassy-hal-common/src/unborrow.rs deleted file mode 100644 index 06e8d0c8..00000000 --- a/embassy-hal-common/src/unborrow.rs +++ /dev/null @@ -1,82 +0,0 @@ -use core::marker::PhantomData; -use core::ops::{Deref, DerefMut}; - -/// This is essentially a `&mut T`, but it is the size of `T` not the size -/// of a pointer. This is useful if T is a zero sized type. -pub struct Unborrowed<'a, T> { - inner: T, - _lifetime: PhantomData<&'a mut T>, -} - -impl<'a, T> Unborrowed<'a, T> { - pub fn new(inner: T) -> Self { - Self { - inner, - _lifetime: PhantomData, - } - } - - pub fn map_into(self) -> Unborrowed<'a, U> - where - T: Into, - { - Unborrowed { - inner: self.inner.into(), - _lifetime: PhantomData, - } - } - - pub unsafe fn into_inner(self) -> T { - self.inner - } -} - -impl<'a, T> Deref for Unborrowed<'a, T> { - type Target = T; - - fn deref(&self) -> &Self::Target { - &self.inner - } -} - -impl<'a, T> DerefMut for Unborrowed<'a, T> { - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.inner - } -} - -/// Unsafely unborrow an owned singleton out of a `&mut`. -/// -/// It is intended to be implemented for owned peripheral singletons, such as `USART3` or `AnyPin`. -/// Unborrowing an owned `T` yields an `Unborrowed<'static, T>`. -/// Unborrowing a `&'a mut T` yields an `Unborrowed<'a, T>`. -/// -/// This allows writing HAL drivers that either own or borrow their peripherals, but that don't have -/// to store pointers in the borrowed case. -pub trait Unborrow: Sized { - /// Unborrow result type - type Target; - - unsafe fn unborrow_unchecked(&mut self) -> Self::Target; - - /// Unborrow a value. - #[inline] - fn unborrow<'a>(mut self) -> Unborrowed<'a, Self::Target> - where - Self: 'a, - { - Unborrowed::new(unsafe { self.unborrow_unchecked() }) - } -} - -impl<'b, T: DerefMut> Unborrow for T -where - T::Target: Unborrow, -{ - type Target = ::Target; - - #[inline] - unsafe fn unborrow_unchecked(&mut self) -> Self::Target { - self.deref_mut().unborrow_unchecked() - } -} diff --git a/embassy-lora/src/stm32wl/mod.rs b/embassy-lora/src/stm32wl/mod.rs index 49991db1..b0d101b7 100644 --- a/embassy-lora/src/stm32wl/mod.rs +++ b/embassy-lora/src/stm32wl/mod.rs @@ -3,7 +3,7 @@ use core::future::Future; use core::mem::MaybeUninit; use embassy::channel::signal::Signal; -use embassy_hal_common::{unborrow, Unborrowed}; +use embassy_hal_common::{into_ref, PeripheralRef}; use embassy_stm32::dma::NoDma; use embassy_stm32::gpio::{AnyPin, Output}; use embassy_stm32::interrupt::{InterruptExt, SUBGHZ_RADIO}; @@ -12,7 +12,7 @@ use embassy_stm32::subghz::{ LoRaSyncWord, Ocp, PaConfig, PaSel, PacketType, RampTime, RegMode, RfFreq, SpreadingFactor as SF, StandbyClk, Status, SubGhz, TcxoMode, TcxoTrim, Timeout, TxParams, }; -use embassy_stm32::Unborrow; +use embassy_stm32::Peripheral; use lorawan_device::async_device::radio::{Bandwidth, PhyRxTx, RfConfig, RxQuality, SpreadingFactor, TxConfig}; use lorawan_device::async_device::Timings; @@ -46,7 +46,7 @@ impl<'d> SubGhzState<'d> { /// The radio peripheral keeping the radio state and owning the radio IRQ. pub struct SubGhzRadio<'d> { state: *mut StateInner<'d>, - _irq: Unborrowed<'d, SUBGHZ_RADIO>, + _irq: PeripheralRef<'d, SUBGHZ_RADIO>, } impl<'d> SubGhzRadio<'d> { @@ -58,9 +58,9 @@ impl<'d> SubGhzRadio<'d> { state: &'d mut SubGhzState<'d>, radio: SubGhz<'d, NoDma, NoDma>, switch: RadioSwitch<'d>, - irq: impl Unborrow + 'd, + irq: impl Peripheral

+ 'd, ) -> Self { - unborrow!(irq); + into_ref!(irq); let mut inner = StateInner { radio, switch }; inner.radio.reset(); diff --git a/embassy-macros/src/macros/cortex_m_interrupt_declare.rs b/embassy-macros/src/macros/cortex_m_interrupt_declare.rs index 13150634..ab61ad5d 100644 --- a/embassy-macros/src/macros/cortex_m_interrupt_declare.rs +++ b/embassy-macros/src/macros/cortex_m_interrupt_declare.rs @@ -25,7 +25,7 @@ pub fn run(name: syn::Ident) -> Result { } } - ::embassy_hal_common::impl_unborrow!(#name_interrupt); + ::embassy_hal_common::impl_peripheral!(#name_interrupt); }; Ok(result) } diff --git a/embassy-nrf/src/buffered_uarte.rs b/embassy-nrf/src/buffered_uarte.rs index d251ce34..48ffe5c2 100644 --- a/embassy-nrf/src/buffered_uarte.rs +++ b/embassy-nrf/src/buffered_uarte.rs @@ -22,7 +22,7 @@ use core::task::Poll; use embassy::waitqueue::WakerRegistration; use embassy_cortex_m::peripheral::{PeripheralMutex, PeripheralState, StateStorage}; use embassy_hal_common::ring_buffer::RingBuffer; -use embassy_hal_common::{low_power_wait_until, unborrow}; +use embassy_hal_common::{into_ref, low_power_wait_until}; use futures::future::poll_fn; // Re-export SVD variants to allow user to directly set values pub use pac::uarte0::{baudrate::BAUDRATE_A as Baudrate, config::PARITY_A as Parity}; @@ -32,7 +32,7 @@ use crate::interrupt::InterruptExt; use crate::ppi::{AnyConfigurableChannel, ConfigurableChannel, Event, Ppi, Task}; use crate::timer::{Frequency, Instance as TimerInstance, Timer}; use crate::uarte::{apply_workaround_for_enable_anomaly, Config, Instance as UarteInstance}; -use crate::{pac, Unborrow}; +use crate::{pac, Peripheral}; #[derive(Copy, Clone, Debug, PartialEq)] enum RxState { @@ -78,20 +78,20 @@ impl<'d, U: UarteInstance, T: TimerInstance> Unpin for BufferedUarte<'d, U, T> { impl<'d, U: UarteInstance, T: TimerInstance> BufferedUarte<'d, U, T> { pub fn new( state: &'d mut State<'d, U, T>, - _uarte: impl Unborrow + 'd, - timer: impl Unborrow + 'd, - ppi_ch1: impl Unborrow + 'd, - ppi_ch2: impl Unborrow + 'd, - irq: impl Unborrow + 'd, - rxd: impl Unborrow + 'd, - txd: impl Unborrow + 'd, - cts: impl Unborrow + 'd, - rts: impl Unborrow + 'd, + _uarte: impl Peripheral

+ 'd, + timer: impl Peripheral

+ 'd, + ppi_ch1: impl Peripheral

+ 'd, + ppi_ch2: impl Peripheral

+ 'd, + irq: impl Peripheral

+ 'd, + rxd: impl Peripheral

+ 'd, + txd: impl Peripheral

+ 'd, + cts: impl Peripheral

+ 'd, + rts: impl Peripheral

+ 'd, config: Config, rx_buffer: &'d mut [u8], tx_buffer: &'d mut [u8], ) -> Self { - unborrow!(ppi_ch1, ppi_ch2, irq, rxd, txd, cts, rts); + into_ref!(ppi_ch1, ppi_ch2, irq, rxd, txd, cts, rts); let r = U::regs(); diff --git a/embassy-nrf/src/gpio.rs b/embassy-nrf/src/gpio.rs index f6320b8e..ae08d859 100644 --- a/embassy-nrf/src/gpio.rs +++ b/embassy-nrf/src/gpio.rs @@ -4,12 +4,12 @@ use core::convert::Infallible; use core::hint::unreachable_unchecked; use cfg_if::cfg_if; -use embassy_hal_common::{impl_unborrow, unborrow, Unborrowed}; +use embassy_hal_common::{impl_peripheral, into_ref, PeripheralRef}; use self::sealed::Pin as _; use crate::pac::p0 as gpio; use crate::pac::p0::pin_cnf::{DRIVE_A, PULL_A}; -use crate::{pac, Unborrow}; +use crate::{pac, Peripheral}; /// A GPIO port with up to 32 pins. #[derive(Debug, Eq, PartialEq)] @@ -38,7 +38,7 @@ pub struct Input<'d, T: Pin> { impl<'d, T: Pin> Input<'d, T> { #[inline] - pub fn new(pin: impl Unborrow + 'd, pull: Pull) -> Self { + pub fn new(pin: impl Peripheral

+ 'd, pull: Pull) -> Self { let mut pin = Flex::new(pin); pin.set_as_input(pull); @@ -118,7 +118,7 @@ pub struct Output<'d, T: Pin> { impl<'d, T: Pin> Output<'d, T> { #[inline] - pub fn new(pin: impl Unborrow + 'd, initial_output: Level, drive: OutputDrive) -> Self { + pub fn new(pin: impl Peripheral

+ 'd, initial_output: Level, drive: OutputDrive) -> Self { let mut pin = Flex::new(pin); match initial_output { Level::High => pin.set_high(), @@ -193,7 +193,7 @@ fn convert_pull(pull: Pull) -> PULL_A { /// set while not in output mode, so the pin's level will be 'remembered' when it is not in output /// mode. pub struct Flex<'d, T: Pin> { - pub(crate) pin: Unborrowed<'d, T>, + pub(crate) pin: PeripheralRef<'d, T>, } impl<'d, T: Pin> Flex<'d, T> { @@ -202,8 +202,8 @@ impl<'d, T: Pin> Flex<'d, T> { /// The pin remains disconnected. The initial output level is unspecified, but can be changed /// before the pin is put into output mode. #[inline] - pub fn new(pin: impl Unborrow + 'd) -> Self { - unborrow!(pin); + pub fn new(pin: impl Peripheral

+ 'd) -> Self { + into_ref!(pin); // Pin will be in disconnected state. Self { pin } } @@ -374,7 +374,7 @@ pub(crate) mod sealed { } } -pub trait Pin: Unborrow + sealed::Pin + Sized + 'static { +pub trait Pin: Peripheral

+ sealed::Pin + Sized + 'static { /// Number of the pin within the port (0..31) #[inline] fn pin(&self) -> u8 { @@ -417,22 +417,22 @@ impl AnyPin { Self { pin_port } } - pub(crate) fn unborrow_and_degrade<'a>(pin: impl Unborrow + 'a) -> Unborrowed<'a, Self> { - Unborrowed::new(AnyPin { - pin_port: pin.unborrow().pin_port(), + pub(crate) fn into_degraded_ref<'a>(pin: impl Peripheral

+ 'a) -> PeripheralRef<'a, Self> { + PeripheralRef::new(AnyPin { + pin_port: pin.into_ref().pin_port(), }) } } -macro_rules! unborrow_and_degrade { +macro_rules! into_degraded_ref { ($($name:ident),*) => { $( - let $name = $crate::gpio::AnyPin::unborrow_and_degrade($name); + let $name = $crate::gpio::AnyPin::into_degraded_ref($name); )* }; } -impl_unborrow!(AnyPin); +impl_peripheral!(AnyPin); impl Pin for AnyPin {} impl sealed::Pin for AnyPin { #[inline] @@ -447,7 +447,7 @@ pub(crate) trait PselBits { fn psel_bits(&self) -> u32; } -impl<'a, P: Pin> PselBits for Option> { +impl<'a, P: Pin> PselBits for Option> { #[inline] fn psel_bits(&self) -> u32 { match self { diff --git a/embassy-nrf/src/gpiote.rs b/embassy-nrf/src/gpiote.rs index d59c2e34..e89d0168 100644 --- a/embassy-nrf/src/gpiote.rs +++ b/embassy-nrf/src/gpiote.rs @@ -4,7 +4,7 @@ use core::marker::PhantomData; use core::task::{Context, Poll}; use embassy::waitqueue::AtomicWaker; -use embassy_hal_common::impl_unborrow; +use embassy_hal_common::impl_peripheral; use futures::future::poll_fn; use crate::gpio::sealed::Pin as _; @@ -414,7 +414,7 @@ pub trait Channel: sealed::Channel + Sized { pub struct AnyChannel { number: u8, } -impl_unborrow!(AnyChannel); +impl_peripheral!(AnyChannel); impl sealed::Channel for AnyChannel {} impl Channel for AnyChannel { fn number(&self) -> usize { diff --git a/embassy-nrf/src/lib.rs b/embassy-nrf/src/lib.rs index b6a21caa..ad6c16c1 100644 --- a/embassy-nrf/src/lib.rs +++ b/embassy-nrf/src/lib.rs @@ -135,7 +135,7 @@ pub use chip::pac; pub(crate) use chip::pac; pub use chip::{peripherals, Peripherals}; pub use embassy_cortex_m::executor; -pub use embassy_hal_common::{unborrow, Unborrow, Unborrowed}; +pub use embassy_hal_common::{into_ref, Peripheral, PeripheralRef}; pub use embassy_macros::cortex_m_interrupt as interrupt; pub mod config { diff --git a/embassy-nrf/src/nvmc.rs b/embassy-nrf/src/nvmc.rs index e350f8c9..731def46 100644 --- a/embassy-nrf/src/nvmc.rs +++ b/embassy-nrf/src/nvmc.rs @@ -3,13 +3,13 @@ use core::marker::PhantomData; use core::{ptr, slice}; -use embassy_hal_common::unborrow; +use embassy_hal_common::into_ref; use embedded_storage::nor_flash::{ ErrorType, MultiwriteNorFlash, NorFlash, NorFlashError, NorFlashErrorKind, ReadNorFlash, }; use crate::peripherals::NVMC; -use crate::{pac, Unborrow}; +use crate::{pac, Peripheral}; pub const PAGE_SIZE: usize = 4096; pub const FLASH_SIZE: usize = crate::chip::FLASH_SIZE; @@ -35,8 +35,8 @@ pub struct Nvmc<'d> { } impl<'d> Nvmc<'d> { - pub fn new(_p: impl Unborrow + 'd) -> Self { - unborrow!(_p); + pub fn new(_p: impl Peripheral

+ 'd) -> Self { + into_ref!(_p); Self { _p: PhantomData } } diff --git a/embassy-nrf/src/ppi/dppi.rs b/embassy-nrf/src/ppi/dppi.rs index 87ebb708..de856c0c 100644 --- a/embassy-nrf/src/ppi/dppi.rs +++ b/embassy-nrf/src/ppi/dppi.rs @@ -1,7 +1,7 @@ -use embassy_hal_common::unborrow; +use embassy_hal_common::into_ref; use super::{Channel, ConfigurableChannel, Event, Ppi, Task}; -use crate::{pac, Unborrow}; +use crate::{pac, Peripheral}; const DPPI_ENABLE_BIT: u32 = 0x8000_0000; const DPPI_CHANNEL_MASK: u32 = 0x0000_00FF; @@ -11,13 +11,13 @@ fn regs() -> &'static pac::dppic::RegisterBlock { } impl<'d, C: ConfigurableChannel> Ppi<'d, C, 1, 1> { - pub fn new_one_to_one(ch: impl Unborrow + 'd, event: Event, task: Task) -> Self { + pub fn new_one_to_one(ch: impl Peripheral

+ 'd, event: Event, task: Task) -> Self { Ppi::new_many_to_many(ch, [event], [task]) } } impl<'d, C: ConfigurableChannel> Ppi<'d, C, 1, 2> { - pub fn new_one_to_two(ch: impl Unborrow + 'd, event: Event, task1: Task, task2: Task) -> Self { + pub fn new_one_to_two(ch: impl Peripheral

+ 'd, event: Event, task1: Task, task2: Task) -> Self { Ppi::new_many_to_many(ch, [event], [task1, task2]) } } @@ -26,11 +26,11 @@ impl<'d, C: ConfigurableChannel, const EVENT_COUNT: usize, const TASK_COUNT: usi Ppi<'d, C, EVENT_COUNT, TASK_COUNT> { pub fn new_many_to_many( - ch: impl Unborrow + 'd, + ch: impl Peripheral

+ 'd, events: [Event; EVENT_COUNT], tasks: [Task; TASK_COUNT], ) -> Self { - unborrow!(ch); + into_ref!(ch); let val = DPPI_ENABLE_BIT | (ch.number() as u32 & DPPI_CHANNEL_MASK); for task in tasks { diff --git a/embassy-nrf/src/ppi/mod.rs b/embassy-nrf/src/ppi/mod.rs index fd1d0cf8..796de217 100644 --- a/embassy-nrf/src/ppi/mod.rs +++ b/embassy-nrf/src/ppi/mod.rs @@ -17,9 +17,9 @@ use core::ptr::NonNull; -use embassy_hal_common::{impl_unborrow, Unborrowed}; +use embassy_hal_common::{impl_peripheral, PeripheralRef}; -use crate::{peripherals, Unborrow}; +use crate::{peripherals, Peripheral}; #[cfg(feature = "_dppi")] mod dppi; @@ -27,7 +27,7 @@ mod dppi; mod ppi; pub struct Ppi<'d, C: Channel, const EVENT_COUNT: usize, const TASK_COUNT: usize> { - ch: Unborrowed<'d, C>, + ch: PeripheralRef<'d, C>, #[cfg(feature = "_dppi")] events: [Event; EVENT_COUNT], #[cfg(feature = "_dppi")] @@ -87,7 +87,7 @@ pub(crate) mod sealed { pub trait Group {} } -pub trait Channel: sealed::Channel + Unborrow + Sized { +pub trait Channel: sealed::Channel + Peripheral

+ Sized { /// Returns the number of the channel fn number(&self) -> usize; } @@ -117,7 +117,7 @@ pub trait Group: sealed::Group + Sized { pub struct AnyStaticChannel { pub(crate) number: u8, } -impl_unborrow!(AnyStaticChannel); +impl_peripheral!(AnyStaticChannel); impl sealed::Channel for AnyStaticChannel {} impl Channel for AnyStaticChannel { fn number(&self) -> usize { @@ -135,7 +135,7 @@ impl StaticChannel for AnyStaticChannel { pub struct AnyConfigurableChannel { pub(crate) number: u8, } -impl_unborrow!(AnyConfigurableChannel); +impl_peripheral!(AnyConfigurableChannel); impl sealed::Channel for AnyConfigurableChannel {} impl Channel for AnyConfigurableChannel { fn number(&self) -> usize { @@ -187,7 +187,7 @@ macro_rules! impl_ppi_channel { pub struct AnyGroup { number: u8, } -impl_unborrow!(AnyGroup); +impl_peripheral!(AnyGroup); impl sealed::Group for AnyGroup {} impl Group for AnyGroup { fn number(&self) -> usize { diff --git a/embassy-nrf/src/ppi/ppi.rs b/embassy-nrf/src/ppi/ppi.rs index 3b8f44da..450a290a 100644 --- a/embassy-nrf/src/ppi/ppi.rs +++ b/embassy-nrf/src/ppi/ppi.rs @@ -1,7 +1,7 @@ -use embassy_hal_common::unborrow; +use embassy_hal_common::into_ref; use super::{Channel, ConfigurableChannel, Event, Ppi, StaticChannel, Task}; -use crate::{pac, Unborrow}; +use crate::{pac, Peripheral}; impl Task { fn reg_val(&self) -> u32 { @@ -20,8 +20,8 @@ fn regs() -> &'static pac::ppi::RegisterBlock { #[cfg(not(feature = "nrf51"))] // Not for nrf51 because of the fork task impl<'d, C: StaticChannel> Ppi<'d, C, 0, 1> { - pub fn new_zero_to_one(ch: impl Unborrow + 'd, task: Task) -> Self { - unborrow!(ch); + pub fn new_zero_to_one(ch: impl Peripheral

+ 'd, task: Task) -> Self { + into_ref!(ch); let r = regs(); let n = ch.number(); @@ -32,8 +32,8 @@ impl<'d, C: StaticChannel> Ppi<'d, C, 0, 1> { } impl<'d, C: ConfigurableChannel> Ppi<'d, C, 1, 1> { - pub fn new_one_to_one(ch: impl Unborrow + 'd, event: Event, task: Task) -> Self { - unborrow!(ch); + pub fn new_one_to_one(ch: impl Peripheral

+ 'd, event: Event, task: Task) -> Self { + into_ref!(ch); let r = regs(); let n = ch.number(); @@ -46,8 +46,8 @@ impl<'d, C: ConfigurableChannel> Ppi<'d, C, 1, 1> { #[cfg(not(feature = "nrf51"))] // Not for nrf51 because of the fork task impl<'d, C: ConfigurableChannel> Ppi<'d, C, 1, 2> { - pub fn new_one_to_two(ch: impl Unborrow + 'd, event: Event, task1: Task, task2: Task) -> Self { - unborrow!(ch); + pub fn new_one_to_two(ch: impl Peripheral

+ 'd, event: Event, task1: Task, task2: Task) -> Self { + into_ref!(ch); let r = regs(); let n = ch.number(); diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index 050461ea..603fd8ef 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -3,34 +3,34 @@ use core::marker::PhantomData; use core::sync::atomic::{compiler_fence, Ordering}; -use embassy_hal_common::Unborrowed; +use embassy_hal_common::PeripheralRef; use crate::gpio::sealed::Pin as _; use crate::gpio::{AnyPin, Pin as GpioPin, PselBits}; use crate::interrupt::Interrupt; use crate::ppi::{Event, Task}; use crate::util::slice_in_ram_or; -use crate::{pac, Unborrow}; +use crate::{pac, Peripheral}; /// SimplePwm is the traditional pwm interface you're probably used to, allowing /// to simply set a duty cycle across up to four channels. pub struct SimplePwm<'d, T: Instance> { phantom: PhantomData<&'d mut T>, duty: [u16; 4], - ch0: Option>, - ch1: Option>, - ch2: Option>, - ch3: Option>, + ch0: Option>, + ch1: Option>, + ch2: Option>, + ch3: Option>, } /// SequencePwm allows you to offload the updating of a sequence of duty cycles /// to up to four channels, as well as repeat that sequence n times. pub struct SequencePwm<'d, T: Instance> { phantom: PhantomData<&'d mut T>, - ch0: Option>, - ch1: Option>, - ch2: Option>, - ch3: Option>, + ch0: Option>, + ch1: Option>, + ch2: Option>, + ch3: Option>, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -51,59 +51,59 @@ impl<'d, T: Instance> SequencePwm<'d, T> { /// Create a new 1-channel PWM #[allow(unused_unsafe)] pub fn new_1ch( - pwm: impl Unborrow + 'd, - ch0: impl Unborrow + 'd, + pwm: impl Peripheral

+ 'd, + ch0: impl Peripheral

+ 'd, config: Config, ) -> Result { - unborrow_and_degrade!(ch0); + into_degraded_ref!(ch0); Self::new_inner(pwm, Some(ch0), None, None, None, config) } /// Create a new 2-channel PWM #[allow(unused_unsafe)] pub fn new_2ch( - pwm: impl Unborrow + 'd, - ch0: impl Unborrow + 'd, - ch1: impl Unborrow + 'd, + pwm: impl Peripheral

+ 'd, + ch0: impl Peripheral

+ 'd, + ch1: impl Peripheral

+ 'd, config: Config, ) -> Result { - unborrow_and_degrade!(ch0, ch1); + into_degraded_ref!(ch0, ch1); Self::new_inner(pwm, Some(ch0), Some(ch1), None, None, config) } /// Create a new 3-channel PWM #[allow(unused_unsafe)] pub fn new_3ch( - pwm: impl Unborrow + 'd, - ch0: impl Unborrow + 'd, - ch1: impl Unborrow + 'd, - ch2: impl Unborrow + 'd, + pwm: impl Peripheral

+ 'd, + ch0: impl Peripheral

+ 'd, + ch1: impl Peripheral

+ 'd, + ch2: impl Peripheral

+ 'd, config: Config, ) -> Result { - unborrow_and_degrade!(ch0, ch1, ch2); + into_degraded_ref!(ch0, ch1, ch2); Self::new_inner(pwm, Some(ch0), Some(ch1), Some(ch2), None, config) } /// Create a new 4-channel PWM #[allow(unused_unsafe)] pub fn new_4ch( - pwm: impl Unborrow + 'd, - ch0: impl Unborrow + 'd, - ch1: impl Unborrow + 'd, - ch2: impl Unborrow + 'd, - ch3: impl Unborrow + 'd, + pwm: impl Peripheral

+ 'd, + ch0: impl Peripheral

+ 'd, + ch1: impl Peripheral

+ 'd, + ch2: impl Peripheral

+ 'd, + ch3: impl Peripheral

+ 'd, config: Config, ) -> Result { - unborrow_and_degrade!(ch0, ch1, ch2, ch3); + into_degraded_ref!(ch0, ch1, ch2, ch3); Self::new_inner(pwm, Some(ch0), Some(ch1), Some(ch2), Some(ch3), config) } fn new_inner( - _pwm: impl Unborrow + 'd, - ch0: Option>, - ch1: Option>, - ch2: Option>, - ch3: Option>, + _pwm: impl Peripheral

+ 'd, + ch0: Option>, + ch1: Option>, + ch2: Option>, + ch3: Option>, config: Config, ) -> Result { let r = T::regs(); @@ -559,9 +559,9 @@ pub enum CounterMode { impl<'d, T: Instance> SimplePwm<'d, T> { /// Create a new 1-channel PWM #[allow(unused_unsafe)] - pub fn new_1ch(pwm: impl Unborrow + 'd, ch0: impl Unborrow + 'd) -> Self { + pub fn new_1ch(pwm: impl Peripheral

+ 'd, ch0: impl Peripheral

+ 'd) -> Self { unsafe { - unborrow_and_degrade!(ch0); + into_degraded_ref!(ch0); Self::new_inner(pwm, Some(ch0), None, None, None) } } @@ -569,24 +569,24 @@ impl<'d, T: Instance> SimplePwm<'d, T> { /// Create a new 2-channel PWM #[allow(unused_unsafe)] pub fn new_2ch( - pwm: impl Unborrow + 'd, - ch0: impl Unborrow + 'd, - ch1: impl Unborrow + 'd, + pwm: impl Peripheral

+ 'd, + ch0: impl Peripheral

+ 'd, + ch1: impl Peripheral

+ 'd, ) -> Self { - unborrow_and_degrade!(ch0, ch1); + into_degraded_ref!(ch0, ch1); Self::new_inner(pwm, Some(ch0), Some(ch1), None, None) } /// Create a new 3-channel PWM #[allow(unused_unsafe)] pub fn new_3ch( - pwm: impl Unborrow + 'd, - ch0: impl Unborrow + 'd, - ch1: impl Unborrow + 'd, - ch2: impl Unborrow + 'd, + pwm: impl Peripheral

+ 'd, + ch0: impl Peripheral

+ 'd, + ch1: impl Peripheral

+ 'd, + ch2: impl Peripheral

+ 'd, ) -> Self { unsafe { - unborrow_and_degrade!(ch0, ch1, ch2); + into_degraded_ref!(ch0, ch1, ch2); Self::new_inner(pwm, Some(ch0), Some(ch1), Some(ch2), None) } } @@ -594,24 +594,24 @@ impl<'d, T: Instance> SimplePwm<'d, T> { /// Create a new 4-channel PWM #[allow(unused_unsafe)] pub fn new_4ch( - pwm: impl Unborrow + 'd, - ch0: impl Unborrow + 'd, - ch1: impl Unborrow + 'd, - ch2: impl Unborrow + 'd, - ch3: impl Unborrow + 'd, + pwm: impl Peripheral

+ 'd, + ch0: impl Peripheral

+ 'd, + ch1: impl Peripheral

+ 'd, + ch2: impl Peripheral

+ 'd, + ch3: impl Peripheral

+ 'd, ) -> Self { unsafe { - unborrow_and_degrade!(ch0, ch1, ch2, ch3); + into_degraded_ref!(ch0, ch1, ch2, ch3); Self::new_inner(pwm, Some(ch0), Some(ch1), Some(ch2), Some(ch3)) } } fn new_inner( - _pwm: impl Unborrow + 'd, - ch0: Option>, - ch1: Option>, - ch2: Option>, - ch3: Option>, + _pwm: impl Peripheral

+ 'd, + ch0: Option>, + ch1: Option>, + ch2: Option>, + ch3: Option>, ) -> Self { let r = T::regs(); @@ -799,7 +799,7 @@ pub(crate) mod sealed { } } -pub trait Instance: Unborrow + sealed::Instance + 'static { +pub trait Instance: Peripheral

+ sealed::Instance + 'static { type Interrupt: Interrupt; } diff --git a/embassy-nrf/src/qdec.rs b/embassy-nrf/src/qdec.rs index da4f1260..2fb31fc2 100644 --- a/embassy-nrf/src/qdec.rs +++ b/embassy-nrf/src/qdec.rs @@ -4,14 +4,14 @@ use core::marker::PhantomData; use core::task::Poll; use embassy::waitqueue::AtomicWaker; -use embassy_hal_common::{unborrow, Unborrowed}; +use embassy_hal_common::{into_ref, PeripheralRef}; use futures::future::poll_fn; use crate::gpio::sealed::Pin as _; use crate::gpio::{AnyPin, Pin as GpioPin}; use crate::interrupt::InterruptExt; use crate::peripherals::QDEC; -use crate::{interrupt, pac, Unborrow}; +use crate::{interrupt, pac, Peripheral}; /// Quadrature decoder pub struct Qdec<'d> { @@ -43,37 +43,37 @@ static WAKER: AtomicWaker = AtomicWaker::new(); impl<'d> Qdec<'d> { pub fn new( - qdec: impl Unborrow + 'd, - irq: impl Unborrow + 'd, - a: impl Unborrow + 'd, - b: impl Unborrow + 'd, + qdec: impl Peripheral

+ 'd, + irq: impl Peripheral

+ 'd, + a: impl Peripheral

+ 'd, + b: impl Peripheral

+ 'd, config: Config, ) -> Self { - unborrow_and_degrade!(a, b); + into_degraded_ref!(a, b); Self::new_inner(qdec, irq, a, b, None, config) } pub fn new_with_led( - qdec: impl Unborrow + 'd, - irq: impl Unborrow + 'd, - a: impl Unborrow + 'd, - b: impl Unborrow + 'd, - led: impl Unborrow + 'd, + qdec: impl Peripheral

+ 'd, + irq: impl Peripheral

+ 'd, + a: impl Peripheral

+ 'd, + b: impl Peripheral

+ 'd, + led: impl Peripheral

+ 'd, config: Config, ) -> Self { - unborrow_and_degrade!(a, b, led); + into_degraded_ref!(a, b, led); Self::new_inner(qdec, irq, a, b, Some(led), config) } fn new_inner( - _t: impl Unborrow + 'd, - irq: impl Unborrow + 'd, - a: Unborrowed<'d, AnyPin>, - b: Unborrowed<'d, AnyPin>, - led: Option>, + _t: impl Peripheral

+ 'd, + irq: impl Peripheral

+ 'd, + a: PeripheralRef<'d, AnyPin>, + b: PeripheralRef<'d, AnyPin>, + led: Option>, config: Config, ) -> Self { - unborrow!(irq); + into_ref!(irq); let r = Self::regs(); // Select pins. diff --git a/embassy-nrf/src/qspi.rs b/embassy-nrf/src/qspi.rs index 2b987b46..51d9446a 100644 --- a/embassy-nrf/src/qspi.rs +++ b/embassy-nrf/src/qspi.rs @@ -4,7 +4,7 @@ use core::ptr; use core::task::Poll; use embassy_hal_common::drop::DropBomb; -use embassy_hal_common::{unborrow, Unborrowed}; +use embassy_hal_common::{into_ref, PeripheralRef}; use futures::future::poll_fn; use crate::gpio::sealed::Pin as _; @@ -13,7 +13,7 @@ use crate::interrupt::{Interrupt, InterruptExt}; pub use crate::pac::qspi::ifconfig0::{ ADDRMODE_A as AddressMode, PPSIZE_A as WritePageSize, READOC_A as ReadOpcode, WRITEOC_A as WriteOpcode, }; -use crate::{pac, Unborrow}; +use crate::{pac, Peripheral}; // TODO // - config: @@ -62,27 +62,27 @@ pub enum Error { } pub struct Qspi<'d, T: Instance, const FLASH_SIZE: usize> { - irq: Unborrowed<'d, T::Interrupt>, + irq: PeripheralRef<'d, T::Interrupt>, dpm_enabled: bool, } impl<'d, T: Instance, const FLASH_SIZE: usize> Qspi<'d, T, FLASH_SIZE> { pub fn new( - _qspi: impl Unborrow + 'd, - irq: impl Unborrow + 'd, - sck: impl Unborrow + 'd, - csn: impl Unborrow + 'd, - io0: impl Unborrow + 'd, - io1: impl Unborrow + 'd, - io2: impl Unborrow + 'd, - io3: impl Unborrow + 'd, + _qspi: impl Peripheral

+ 'd, + irq: impl Peripheral

+ 'd, + sck: impl Peripheral

+ 'd, + csn: impl Peripheral

+ 'd, + io0: impl Peripheral

+ 'd, + io1: impl Peripheral

+ 'd, + io2: impl Peripheral

+ 'd, + io3: impl Peripheral

+ 'd, config: Config, ) -> Qspi<'d, T, FLASH_SIZE> { - unborrow!(irq, sck, csn, io0, io1, io2, io3); + into_ref!(irq, sck, csn, io0, io1, io2, io3); let r = T::regs(); - unborrow_and_degrade!(sck, csn, io0, io1, io2, io3); + into_degraded_ref!(sck, csn, io0, io1, io2, io3); for pin in [&sck, &csn, &io0, &io1, &io2, &io3] { pin.set_high(); @@ -528,7 +528,7 @@ pub(crate) mod sealed { } } -pub trait Instance: Unborrow + sealed::Instance + 'static { +pub trait Instance: Peripheral

+ sealed::Instance + 'static { type Interrupt: Interrupt; } diff --git a/embassy-nrf/src/rng.rs b/embassy-nrf/src/rng.rs index 111a5424..9bebd6fa 100644 --- a/embassy-nrf/src/rng.rs +++ b/embassy-nrf/src/rng.rs @@ -4,12 +4,12 @@ use core::task::Poll; use embassy::waitqueue::AtomicWaker; use embassy_hal_common::drop::OnDrop; -use embassy_hal_common::{unborrow, Unborrowed}; +use embassy_hal_common::{into_ref, PeripheralRef}; use futures::future::poll_fn; use crate::interrupt::InterruptExt; use crate::peripherals::RNG; -use crate::{interrupt, pac, Unborrow}; +use crate::{interrupt, pac, Peripheral}; impl RNG { fn regs() -> &'static pac::rng::RegisterBlock { @@ -33,7 +33,7 @@ struct State { /// /// It has a non-blocking API, and a blocking api through `rand`. pub struct Rng<'d> { - irq: Unborrowed<'d, interrupt::RNG>, + irq: PeripheralRef<'d, interrupt::RNG>, } impl<'d> Rng<'d> { @@ -43,8 +43,8 @@ impl<'d> Rng<'d> { /// e.g. using `mem::forget`. /// /// The synchronous API is safe. - pub fn new(_rng: impl Unborrow + 'd, irq: impl Unborrow + 'd) -> Self { - unborrow!(irq); + pub fn new(_rng: impl Peripheral

+ 'd, irq: impl Peripheral

+ 'd) -> Self { + into_ref!(irq); let this = Self { irq }; diff --git a/embassy-nrf/src/saadc.rs b/embassy-nrf/src/saadc.rs index 9c7207c8..644ffa1f 100644 --- a/embassy-nrf/src/saadc.rs +++ b/embassy-nrf/src/saadc.rs @@ -5,7 +5,7 @@ use core::sync::atomic::{compiler_fence, Ordering}; use core::task::Poll; use embassy::waitqueue::AtomicWaker; -use embassy_hal_common::{impl_unborrow, unborrow}; +use embassy_hal_common::{impl_peripheral, into_ref}; use futures::future::poll_fn; use pac::{saadc, SAADC}; use saadc::ch::config::{GAIN_A, REFSEL_A, RESP_A, TACQ_A}; @@ -17,7 +17,7 @@ use saadc::resolution::VAL_A; use crate::interrupt::InterruptExt; use crate::ppi::{ConfigurableChannel, Event, Ppi, Task}; use crate::timer::{Frequency, Instance as TimerInstance, Timer}; -use crate::{interrupt, pac, peripherals, Unborrow}; +use crate::{interrupt, pac, peripherals, Peripheral}; #[derive(Debug, Clone, Copy, PartialEq, Eq)] #[cfg_attr(feature = "defmt", derive(defmt::Format))] @@ -77,7 +77,7 @@ pub struct ChannelConfig<'d> { /// internal voltage. pub struct VddInput; -impl_unborrow!(VddInput); +impl_peripheral!(VddInput); impl sealed::Input for VddInput { #[cfg(not(feature = "_nrf9160"))] @@ -97,7 +97,7 @@ impl Input for VddInput {} pub struct VddhDiv5Input; #[cfg(any(feature = "_nrf5340-app", feature = "nrf52833", feature = "nrf52840"))] -impl_unborrow!(VddhDiv5Input); +impl_peripheral!(VddhDiv5Input); #[cfg(any(feature = "_nrf5340-app", feature = "nrf52833", feature = "nrf52840"))] impl sealed::Input for VddhDiv5Input { @@ -113,7 +113,7 @@ pub struct AnyInput { channel: InputChannel, } -impl_unborrow!(AnyInput); +impl_peripheral!(AnyInput); impl sealed::Input for AnyInput { fn channel(&self) -> InputChannel { @@ -125,8 +125,8 @@ impl Input for AnyInput {} impl<'d> ChannelConfig<'d> { /// Default configuration for single ended channel sampling. - pub fn single_ended(input: impl Unborrow + 'd) -> Self { - unborrow!(input); + pub fn single_ended(input: impl Peripheral

+ 'd) -> Self { + into_ref!(input); Self { reference: Reference::INTERNAL, gain: Gain::GAIN1_6, @@ -139,10 +139,10 @@ impl<'d> ChannelConfig<'d> { } /// Default configuration for differential channel sampling. pub fn differential( - p_input: impl Unborrow + 'd, - n_input: impl Unborrow + 'd, + p_input: impl Peripheral

+ 'd, + n_input: impl Peripheral

+ 'd, ) -> Self { - unborrow!(p_input, n_input); + into_ref!(p_input, n_input); Self { reference: Reference::INTERNAL, gain: Gain::GAIN1_6, @@ -167,12 +167,12 @@ pub enum SamplerState { impl<'d, const N: usize> Saadc<'d, N> { pub fn new( - _saadc: impl Unborrow + 'd, - irq: impl Unborrow + 'd, + _saadc: impl Peripheral

+ 'd, + irq: impl Peripheral

+ 'd, config: Config, channel_configs: [ChannelConfig; N], ) -> Self { - unborrow!(irq); + into_ref!(irq); let r = unsafe { &*SAADC::ptr() }; @@ -674,7 +674,7 @@ pub(crate) mod sealed { } /// An input that can be used as either or negative end of a ADC differential in the SAADC periperhal. -pub trait Input: sealed::Input + Unborrow + Sized { +pub trait Input: sealed::Input + Peripheral

+ Sized { fn degrade_saadc(self) -> AnyInput { AnyInput { channel: self.channel(), diff --git a/embassy-nrf/src/spim.rs b/embassy-nrf/src/spim.rs index 889d04dc..4bb0f445 100644 --- a/embassy-nrf/src/spim.rs +++ b/embassy-nrf/src/spim.rs @@ -5,7 +5,7 @@ use core::sync::atomic::{compiler_fence, Ordering}; use core::task::Poll; use embassy_embedded_hal::SetConfig; -use embassy_hal_common::{unborrow, Unborrowed}; +use embassy_hal_common::{into_ref, PeripheralRef}; pub use embedded_hal_02::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3}; use futures::future::poll_fn; pub use pac::spim0::frequency::FREQUENCY_A as Frequency; @@ -15,7 +15,7 @@ use crate::gpio::sealed::Pin as _; use crate::gpio::{self, AnyPin, Pin as GpioPin, PselBits}; use crate::interrupt::{Interrupt, InterruptExt}; use crate::util::{slice_in_ram_or, slice_ptr_parts, slice_ptr_parts_mut}; -use crate::{pac, Unborrow}; +use crate::{pac, Peripheral}; #[derive(Debug, Clone, Copy, PartialEq, Eq)] #[cfg_attr(feature = "defmt", derive(defmt::Format))] @@ -53,48 +53,48 @@ impl Default for Config { impl<'d, T: Instance> Spim<'d, T> { pub fn new( - spim: impl Unborrow + 'd, - irq: impl Unborrow + 'd, - sck: impl Unborrow + 'd, - miso: impl Unborrow + 'd, - mosi: impl Unborrow + 'd, + spim: impl Peripheral

+ 'd, + irq: impl Peripheral

+ 'd, + sck: impl Peripheral

+ 'd, + miso: impl Peripheral

+ 'd, + mosi: impl Peripheral

+ 'd, config: Config, ) -> Self { - unborrow_and_degrade!(sck, miso, mosi); + into_degraded_ref!(sck, miso, mosi); Self::new_inner(spim, irq, sck, Some(miso), Some(mosi), config) } pub fn new_txonly( - spim: impl Unborrow + 'd, - irq: impl Unborrow + 'd, - sck: impl Unborrow + 'd, - mosi: impl Unborrow + 'd, + spim: impl Peripheral

+ 'd, + irq: impl Peripheral

+ 'd, + sck: impl Peripheral

+ 'd, + mosi: impl Peripheral

+ 'd, config: Config, ) -> Self { - unborrow_and_degrade!(sck, mosi); + into_degraded_ref!(sck, mosi); Self::new_inner(spim, irq, sck, None, Some(mosi), config) } pub fn new_rxonly( - spim: impl Unborrow + 'd, - irq: impl Unborrow + 'd, - sck: impl Unborrow + 'd, - miso: impl Unborrow + 'd, + spim: impl Peripheral

+ 'd, + irq: impl Peripheral

+ 'd, + sck: impl Peripheral

+ 'd, + miso: impl Peripheral

+ 'd, config: Config, ) -> Self { - unborrow_and_degrade!(sck, miso); + into_degraded_ref!(sck, miso); Self::new_inner(spim, irq, sck, Some(miso), None, config) } fn new_inner( - _spim: impl Unborrow + 'd, - irq: impl Unborrow + 'd, - sck: Unborrowed<'d, AnyPin>, - miso: Option>, - mosi: Option>, + _spim: impl Peripheral

+ 'd, + irq: impl Peripheral

+ 'd, + sck: PeripheralRef<'d, AnyPin>, + miso: Option>, + mosi: Option>, config: Config, ) -> Self { - unborrow!(irq); + into_ref!(irq); let r = T::regs(); @@ -379,7 +379,7 @@ pub(crate) mod sealed { } } -pub trait Instance: Unborrow + sealed::Instance + 'static { +pub trait Instance: Peripheral

+ sealed::Instance + 'static { type Interrupt: Interrupt; } diff --git a/embassy-nrf/src/temp.rs b/embassy-nrf/src/temp.rs index 0bb82a49..a3b25ce0 100644 --- a/embassy-nrf/src/temp.rs +++ b/embassy-nrf/src/temp.rs @@ -4,24 +4,24 @@ use core::task::Poll; use embassy::waitqueue::AtomicWaker; use embassy_hal_common::drop::OnDrop; -use embassy_hal_common::{unborrow, Unborrowed}; +use embassy_hal_common::{into_ref, PeripheralRef}; use fixed::types::I30F2; use futures::future::poll_fn; use crate::interrupt::InterruptExt; use crate::peripherals::TEMP; -use crate::{interrupt, pac, Unborrow}; +use crate::{interrupt, pac, Peripheral}; /// Integrated temperature sensor. pub struct Temp<'d> { - _irq: Unborrowed<'d, interrupt::TEMP>, + _irq: PeripheralRef<'d, interrupt::TEMP>, } static WAKER: AtomicWaker = AtomicWaker::new(); impl<'d> Temp<'d> { - pub fn new(_t: impl Unborrow + 'd, irq: impl Unborrow + 'd) -> Self { - unborrow!(_t, irq); + pub fn new(_t: impl Peripheral

+ 'd, irq: impl Peripheral

+ 'd) -> Self { + into_ref!(_t, irq); // Enable interrupt that signals temperature values irq.disable(); diff --git a/embassy-nrf/src/timer.rs b/embassy-nrf/src/timer.rs index c8c36dfa..d7a7c4d7 100644 --- a/embassy-nrf/src/timer.rs +++ b/embassy-nrf/src/timer.rs @@ -5,12 +5,12 @@ use core::task::Poll; use embassy::waitqueue::AtomicWaker; use embassy_hal_common::drop::OnDrop; -use embassy_hal_common::unborrow; +use embassy_hal_common::into_ref; use futures::future::poll_fn; use crate::interrupt::{Interrupt, InterruptExt}; use crate::ppi::{Event, Task}; -use crate::{pac, Unborrow}; +use crate::{pac, Peripheral}; pub(crate) mod sealed { @@ -28,7 +28,7 @@ pub(crate) mod sealed { pub trait TimerType {} } -pub trait Instance: Unborrow + sealed::Instance + 'static + Send { +pub trait Instance: Peripheral

+ sealed::Instance + 'static + Send { type Interrupt: Interrupt; } pub trait ExtendedInstance: Instance + sealed::ExtendedInstance {} @@ -99,11 +99,8 @@ pub struct Timer<'d, T: Instance, I: TimerType = NotAwaitable> { } impl<'d, T: Instance> Timer<'d, T, Awaitable> { - pub fn new_awaitable( - timer: impl Unborrow + 'd, - irq: impl Unborrow + 'd, - ) -> Self { - unborrow!(irq); + pub fn new_awaitable(timer: impl Peripheral

+ 'd, irq: impl Peripheral

+ 'd) -> Self { + into_ref!(irq); irq.set_handler(Self::on_interrupt); irq.unpend(); @@ -117,7 +114,7 @@ impl<'d, T: Instance> Timer<'d, T, NotAwaitable> { /// /// This can be useful for triggering tasks via PPI /// `Uarte` uses this internally. - pub fn new(timer: impl Unborrow + 'd) -> Self { + pub fn new(timer: impl Peripheral

+ 'd) -> Self { Self::new_irqless(timer) } } @@ -126,7 +123,7 @@ impl<'d, T: Instance, I: TimerType> Timer<'d, T, I> { /// Create a `Timer` without an interrupt, meaning `Cc::wait` won't work. /// /// This is used by the public constructors. - fn new_irqless(_timer: impl Unborrow + 'd) -> Self { + fn new_irqless(_timer: impl Peripheral

+ 'd) -> Self { let regs = T::regs(); let mut this = Self { phantom: PhantomData }; diff --git a/embassy-nrf/src/twim.rs b/embassy-nrf/src/twim.rs index 2088691b..7699d2a7 100644 --- a/embassy-nrf/src/twim.rs +++ b/embassy-nrf/src/twim.rs @@ -16,14 +16,14 @@ use core::task::Poll; use embassy::time::{Duration, Instant}; use embassy::waitqueue::AtomicWaker; use embassy_embedded_hal::SetConfig; -use embassy_hal_common::unborrow; +use embassy_hal_common::into_ref; use futures::future::poll_fn; use crate::chip::{EASY_DMA_SIZE, FORCE_COPY_BUFFER_SIZE}; use crate::gpio::Pin as GpioPin; use crate::interrupt::{Interrupt, InterruptExt}; use crate::util::{slice_in_ram, slice_in_ram_or}; -use crate::{gpio, pac, Unborrow}; +use crate::{gpio, pac, Peripheral}; #[derive(Clone, Copy)] pub enum Frequency { @@ -80,13 +80,13 @@ pub struct Twim<'d, T: Instance> { impl<'d, T: Instance> Twim<'d, T> { pub fn new( - _twim: impl Unborrow + 'd, - irq: impl Unborrow + 'd, - sda: impl Unborrow + 'd, - scl: impl Unborrow + 'd, + _twim: impl Peripheral

+ 'd, + irq: impl Peripheral

+ 'd, + sda: impl Peripheral

+ 'd, + scl: impl Peripheral

+ 'd, config: Config, ) -> Self { - unborrow!(irq, sda, scl); + into_ref!(irq, sda, scl); let r = T::regs(); @@ -707,7 +707,7 @@ pub(crate) mod sealed { } } -pub trait Instance: Unborrow + sealed::Instance + 'static { +pub trait Instance: Peripheral

+ sealed::Instance + 'static { type Interrupt: Interrupt; } diff --git a/embassy-nrf/src/uarte.rs b/embassy-nrf/src/uarte.rs index d5ffb315..a556d6b9 100644 --- a/embassy-nrf/src/uarte.rs +++ b/embassy-nrf/src/uarte.rs @@ -18,7 +18,7 @@ use core::sync::atomic::{compiler_fence, Ordering}; use core::task::Poll; use embassy_hal_common::drop::OnDrop; -use embassy_hal_common::{unborrow, Unborrowed}; +use embassy_hal_common::{into_ref, PeripheralRef}; use futures::future::poll_fn; use pac::uarte0::RegisterBlock; // Re-export SVD variants to allow user to directly set values. @@ -31,7 +31,7 @@ use crate::interrupt::{Interrupt, InterruptExt}; use crate::ppi::{AnyConfigurableChannel, ConfigurableChannel, Event, Ppi, Task}; use crate::timer::{Frequency, Instance as TimerInstance, Timer}; use crate::util::slice_in_ram_or; -use crate::{pac, Unborrow}; +use crate::{pac, Peripheral}; #[derive(Clone)] #[non_exhaustive] @@ -83,40 +83,40 @@ pub struct UarteRx<'d, T: Instance> { impl<'d, T: Instance> Uarte<'d, T> { /// Create a new UARTE without hardware flow control pub fn new( - uarte: impl Unborrow + 'd, - irq: impl Unborrow + 'd, - rxd: impl Unborrow + 'd, - txd: impl Unborrow + 'd, + uarte: impl Peripheral

+ 'd, + irq: impl Peripheral

+ 'd, + rxd: impl Peripheral

+ 'd, + txd: impl Peripheral

+ 'd, config: Config, ) -> Self { - unborrow_and_degrade!(rxd, txd); + into_degraded_ref!(rxd, txd); Self::new_inner(uarte, irq, rxd, txd, None, None, config) } /// Create a new UARTE with hardware flow control (RTS/CTS) pub fn new_with_rtscts( - uarte: impl Unborrow + 'd, - irq: impl Unborrow + 'd, - rxd: impl Unborrow + 'd, - txd: impl Unborrow + 'd, - cts: impl Unborrow + 'd, - rts: impl Unborrow + 'd, + uarte: impl Peripheral

+ 'd, + irq: impl Peripheral

+ 'd, + rxd: impl Peripheral

+ 'd, + txd: impl Peripheral

+ 'd, + cts: impl Peripheral

+ 'd, + rts: impl Peripheral

+ 'd, config: Config, ) -> Self { - unborrow_and_degrade!(rxd, txd, cts, rts); + into_degraded_ref!(rxd, txd, cts, rts); Self::new_inner(uarte, irq, rxd, txd, Some(cts), Some(rts), config) } fn new_inner( - _uarte: impl Unborrow + 'd, - irq: impl Unborrow + 'd, - rxd: Unborrowed<'d, AnyPin>, - txd: Unborrowed<'d, AnyPin>, - cts: Option>, - rts: Option>, + _uarte: impl Peripheral

+ 'd, + irq: impl Peripheral

+ 'd, + rxd: PeripheralRef<'d, AnyPin>, + txd: PeripheralRef<'d, AnyPin>, + cts: Option>, + rts: Option>, config: Config, ) -> Self { - unborrow!(irq); + into_ref!(irq); let r = T::regs(); @@ -237,35 +237,35 @@ fn configure(r: &RegisterBlock, config: Config, hardware_flow_control: bool) { impl<'d, T: Instance> UarteTx<'d, T> { /// Create a new tx-only UARTE without hardware flow control pub fn new( - uarte: impl Unborrow + 'd, - irq: impl Unborrow + 'd, - txd: impl Unborrow + 'd, + uarte: impl Peripheral

+ 'd, + irq: impl Peripheral

+ 'd, + txd: impl Peripheral

+ 'd, config: Config, ) -> Self { - unborrow_and_degrade!(txd); + into_degraded_ref!(txd); Self::new_inner(uarte, irq, txd, None, config) } /// Create a new tx-only UARTE with hardware flow control (RTS/CTS) pub fn new_with_rtscts( - uarte: impl Unborrow + 'd, - irq: impl Unborrow + 'd, - txd: impl Unborrow + 'd, - cts: impl Unborrow + 'd, + uarte: impl Peripheral

+ 'd, + irq: impl Peripheral

+ 'd, + txd: impl Peripheral

+ 'd, + cts: impl Peripheral

+ 'd, config: Config, ) -> Self { - unborrow_and_degrade!(txd, cts); + into_degraded_ref!(txd, cts); Self::new_inner(uarte, irq, txd, Some(cts), config) } fn new_inner( - _uarte: impl Unborrow + 'd, - irq: impl Unborrow + 'd, - txd: Unborrowed<'d, AnyPin>, - cts: Option>, + _uarte: impl Peripheral

+ 'd, + irq: impl Peripheral

+ 'd, + txd: PeripheralRef<'d, AnyPin>, + cts: Option>, config: Config, ) -> Self { - unborrow!(irq); + into_ref!(irq); let r = T::regs(); @@ -429,35 +429,35 @@ impl<'a, T: Instance> Drop for UarteTx<'a, T> { impl<'d, T: Instance> UarteRx<'d, T> { /// Create a new rx-only UARTE without hardware flow control pub fn new( - uarte: impl Unborrow + 'd, - irq: impl Unborrow + 'd, - rxd: impl Unborrow + 'd, + uarte: impl Peripheral

+ 'd, + irq: impl Peripheral

+ 'd, + rxd: impl Peripheral

+ 'd, config: Config, ) -> Self { - unborrow_and_degrade!(rxd); + into_degraded_ref!(rxd); Self::new_inner(uarte, irq, rxd, None, config) } /// Create a new rx-only UARTE with hardware flow control (RTS/CTS) pub fn new_with_rtscts( - uarte: impl Unborrow + 'd, - irq: impl Unborrow + 'd, - rxd: impl Unborrow + 'd, - rts: impl Unborrow + 'd, + uarte: impl Peripheral

+ 'd, + irq: impl Peripheral

+ 'd, + rxd: impl Peripheral

+ 'd, + rts: impl Peripheral

+ 'd, config: Config, ) -> Self { - unborrow_and_degrade!(rxd, rts); + into_degraded_ref!(rxd, rts); Self::new_inner(uarte, irq, rxd, Some(rts), config) } fn new_inner( - _uarte: impl Unborrow + 'd, - irq: impl Unborrow + 'd, - rxd: Unborrowed<'d, AnyPin>, - rts: Option>, + _uarte: impl Peripheral

+ 'd, + irq: impl Peripheral

+ 'd, + rxd: PeripheralRef<'d, AnyPin>, + rts: Option>, config: Config, ) -> Self { - unborrow!(irq); + into_ref!(irq); let r = T::regs(); @@ -668,33 +668,33 @@ pub struct UarteWithIdle<'d, U: Instance, T: TimerInstance> { impl<'d, U: Instance, T: TimerInstance> UarteWithIdle<'d, U, T> { /// Create a new UARTE without hardware flow control pub fn new( - uarte: impl Unborrow + 'd, - timer: impl Unborrow + 'd, - ppi_ch1: impl Unborrow + 'd, - ppi_ch2: impl Unborrow + 'd, - irq: impl Unborrow + 'd, - rxd: impl Unborrow + 'd, - txd: impl Unborrow + 'd, + uarte: impl Peripheral

+ 'd, + timer: impl Peripheral

+ 'd, + ppi_ch1: impl Peripheral

+ 'd, + ppi_ch2: impl Peripheral

+ 'd, + irq: impl Peripheral

+ 'd, + rxd: impl Peripheral

+ 'd, + txd: impl Peripheral

+ 'd, config: Config, ) -> Self { - unborrow_and_degrade!(rxd, txd); + into_degraded_ref!(rxd, txd); Self::new_inner(uarte, timer, ppi_ch1, ppi_ch2, irq, rxd, txd, None, None, config) } /// Create a new UARTE with hardware flow control (RTS/CTS) pub fn new_with_rtscts( - uarte: impl Unborrow + 'd, - timer: impl Unborrow + 'd, - ppi_ch1: impl Unborrow + 'd, - ppi_ch2: impl Unborrow + 'd, - irq: impl Unborrow + 'd, - rxd: impl Unborrow + 'd, - txd: impl Unborrow + 'd, - cts: impl Unborrow + 'd, - rts: impl Unborrow + 'd, + uarte: impl Peripheral

+ 'd, + timer: impl Peripheral

+ 'd, + ppi_ch1: impl Peripheral

+ 'd, + ppi_ch2: impl Peripheral

+ 'd, + irq: impl Peripheral

+ 'd, + rxd: impl Peripheral

+ 'd, + txd: impl Peripheral

+ 'd, + cts: impl Peripheral

+ 'd, + rts: impl Peripheral

+ 'd, config: Config, ) -> Self { - unborrow_and_degrade!(rxd, txd, cts, rts); + into_degraded_ref!(rxd, txd, cts, rts); Self::new_inner( uarte, timer, @@ -710,15 +710,15 @@ impl<'d, U: Instance, T: TimerInstance> UarteWithIdle<'d, U, T> { } fn new_inner( - uarte: impl Unborrow + 'd, - timer: impl Unborrow + 'd, - ppi_ch1: impl Unborrow + 'd, - ppi_ch2: impl Unborrow + 'd, - irq: impl Unborrow + 'd, - rxd: Unborrowed<'d, AnyPin>, - txd: Unborrowed<'d, AnyPin>, - cts: Option>, - rts: Option>, + uarte: impl Peripheral

+ 'd, + timer: impl Peripheral

+ 'd, + ppi_ch1: impl Peripheral

+ 'd, + ppi_ch2: impl Peripheral

+ 'd, + irq: impl Peripheral

+ 'd, + rxd: PeripheralRef<'d, AnyPin>, + txd: PeripheralRef<'d, AnyPin>, + cts: Option>, + rts: Option>, config: Config, ) -> Self { let baudrate = config.baudrate; @@ -726,7 +726,7 @@ impl<'d, U: Instance, T: TimerInstance> UarteWithIdle<'d, U, T> { let mut timer = Timer::new(timer); - unborrow!(ppi_ch1, ppi_ch2); + into_ref!(ppi_ch1, ppi_ch2); let r = U::regs(); @@ -939,7 +939,7 @@ pub(crate) mod sealed { } } -pub trait Instance: Unborrow + sealed::Instance + 'static + Send { +pub trait Instance: Peripheral

+ sealed::Instance + 'static + Send { type Interrupt: Interrupt; } diff --git a/embassy-nrf/src/usb.rs b/embassy-nrf/src/usb.rs index a039427f..eaecda57 100644 --- a/embassy-nrf/src/usb.rs +++ b/embassy-nrf/src/usb.rs @@ -7,7 +7,7 @@ use core::task::Poll; use cortex_m::peripheral::NVIC; use embassy::waitqueue::AtomicWaker; -use embassy_hal_common::unborrow; +use embassy_hal_common::into_ref; pub use embassy_usb; use embassy_usb::driver::{self, EndpointError, Event, Unsupported}; use embassy_usb::types::{EndpointAddress, EndpointInfo, EndpointType, UsbDirection}; @@ -17,7 +17,7 @@ use pac::usbd::RegisterBlock; use crate::interrupt::{Interrupt, InterruptExt}; use crate::util::slice_in_ram; -use crate::{pac, Unborrow}; +use crate::{pac, Peripheral}; const NEW_AW: AtomicWaker = AtomicWaker::new(); static BUS_WAKER: AtomicWaker = NEW_AW; @@ -166,12 +166,8 @@ impl UsbSupply for SignalledSupply { } impl<'d, T: Instance, P: UsbSupply> Driver<'d, T, P> { - pub fn new( - _usb: impl Unborrow + 'd, - irq: impl Unborrow + 'd, - usb_supply: P, - ) -> Self { - unborrow!(irq); + pub fn new(_usb: impl Peripheral

+ 'd, irq: impl Peripheral

+ 'd, usb_supply: P) -> Self { + into_ref!(irq); irq.set_handler(Self::on_interrupt); irq.unpend(); irq.enable(); @@ -950,7 +946,7 @@ pub(crate) mod sealed { } } -pub trait Instance: Unborrow + sealed::Instance + 'static + Send { +pub trait Instance: Peripheral

+ sealed::Instance + 'static + Send { type Interrupt: Interrupt; } diff --git a/embassy-rp/src/gpio.rs b/embassy-rp/src/gpio.rs index 08a731f4..3588c2f7 100644 --- a/embassy-rp/src/gpio.rs +++ b/embassy-rp/src/gpio.rs @@ -5,11 +5,11 @@ use core::task::{Context, Poll}; use embassy::waitqueue::AtomicWaker; use embassy_cortex_m::interrupt::{Interrupt, InterruptExt}; -use embassy_hal_common::{impl_unborrow, unborrow, Unborrowed}; +use embassy_hal_common::{impl_peripheral, into_ref, PeripheralRef}; use crate::pac::common::{Reg, RW}; use crate::pac::SIO; -use crate::{interrupt, pac, peripherals, Unborrow}; +use crate::{interrupt, pac, peripherals, Peripheral}; const PIN_COUNT: usize = 30; const NEW_AW: AtomicWaker = AtomicWaker::new(); @@ -61,7 +61,7 @@ pub struct Input<'d, T: Pin> { impl<'d, T: Pin> Input<'d, T> { #[inline] - pub fn new(pin: impl Unborrow + 'd, pull: Pull) -> Self { + pub fn new(pin: impl Peripheral

+ 'd, pull: Pull) -> Self { let mut pin = Flex::new(pin); pin.set_as_input(); pin.set_pull(pull); @@ -177,13 +177,13 @@ unsafe fn IO_IRQ_BANK0() { } struct InputFuture<'a, T: Pin> { - pin: Unborrowed<'a, T>, + pin: PeripheralRef<'a, T>, level: InterruptTrigger, } impl<'d, T: Pin> InputFuture<'d, T> { - pub fn new(pin: impl Unborrow + 'd, level: InterruptTrigger) -> Self { - unborrow!(pin); + pub fn new(pin: impl Peripheral

+ 'd, level: InterruptTrigger) -> Self { + into_ref!(pin); unsafe { let irq = interrupt::IO_IRQ_BANK0::steal(); irq.disable(); @@ -290,7 +290,7 @@ pub struct Output<'d, T: Pin> { impl<'d, T: Pin> Output<'d, T> { #[inline] - pub fn new(pin: impl Unborrow + 'd, initial_output: Level) -> Self { + pub fn new(pin: impl Peripheral

+ 'd, initial_output: Level) -> Self { let mut pin = Flex::new(pin); match initial_output { Level::High => pin.set_high(), @@ -351,7 +351,7 @@ pub struct OutputOpenDrain<'d, T: Pin> { impl<'d, T: Pin> OutputOpenDrain<'d, T> { #[inline] - pub fn new(pin: impl Unborrow + 'd, initial_output: Level) -> Self { + pub fn new(pin: impl Peripheral

+ 'd, initial_output: Level) -> Self { let mut pin = Flex::new(pin); pin.set_low(); match initial_output { @@ -415,13 +415,13 @@ impl<'d, T: Pin> OutputOpenDrain<'d, T> { /// set while not in output mode, so the pin's level will be 'remembered' when it is not in output /// mode. pub struct Flex<'d, T: Pin> { - pin: Unborrowed<'d, T>, + pin: PeripheralRef<'d, T>, } impl<'d, T: Pin> Flex<'d, T> { #[inline] - pub fn new(pin: impl Unborrow + 'd) -> Self { - unborrow!(pin); + pub fn new(pin: impl Peripheral

+ 'd) -> Self { + into_ref!(pin); unsafe { pin.pad_ctrl().write(|w| { @@ -647,7 +647,7 @@ pub(crate) mod sealed { } } -pub trait Pin: Unborrow + sealed::Pin { +pub trait Pin: Peripheral

+ sealed::Pin { /// Degrade to a generic pin struct fn degrade(self) -> AnyPin { AnyPin { @@ -661,22 +661,22 @@ pub struct AnyPin { } impl AnyPin { - pub(crate) fn unborrow_and_degrade<'a>(pin: impl Unborrow + 'a) -> Unborrowed<'a, Self> { - Unborrowed::new(AnyPin { - pin_bank: pin.unborrow().pin_bank(), + pub(crate) fn into_degraded_ref<'a>(pin: impl Peripheral

+ 'a) -> PeripheralRef<'a, Self> { + PeripheralRef::new(AnyPin { + pin_bank: pin.into_ref().pin_bank(), }) } } -macro_rules! unborrow_and_degrade { +macro_rules! into_degraded_ref { ($($name:ident),*) => { $( - let $name = $crate::gpio::AnyPin::unborrow_and_degrade($name); + let $name = $crate::gpio::AnyPin::into_degraded_ref($name); )* }; } -impl_unborrow!(AnyPin); +impl_peripheral!(AnyPin); impl Pin for AnyPin {} impl sealed::Pin for AnyPin { diff --git a/embassy-rp/src/lib.rs b/embassy-rp/src/lib.rs index e36761ac..44150be0 100644 --- a/embassy-rp/src/lib.rs +++ b/embassy-rp/src/lib.rs @@ -17,7 +17,7 @@ mod reset; // Reexports pub use embassy_cortex_m::executor; -pub use embassy_hal_common::{unborrow, Unborrow, Unborrowed}; +pub use embassy_hal_common::{into_ref, Peripheral, PeripheralRef}; pub use embassy_macros::cortex_m_interrupt as interrupt; #[cfg(feature = "unstable-pac")] pub use rp2040_pac2 as pac; diff --git a/embassy-rp/src/spi.rs b/embassy-rp/src/spi.rs index df030942..a984d16a 100644 --- a/embassy-rp/src/spi.rs +++ b/embassy-rp/src/spi.rs @@ -1,10 +1,10 @@ use embassy_embedded_hal::SetConfig; -use embassy_hal_common::{unborrow, Unborrowed}; +use embassy_hal_common::{into_ref, PeripheralRef}; pub use embedded_hal_02::spi::{Phase, Polarity}; use crate::gpio::sealed::Pin as _; use crate::gpio::{AnyPin, Pin as GpioPin}; -use crate::{pac, peripherals, Unborrow}; +use crate::{pac, peripherals, Peripheral}; #[derive(Debug, Clone, Copy, PartialEq, Eq)] #[cfg_attr(feature = "defmt", derive(defmt::Format))] @@ -31,7 +31,7 @@ impl Default for Config { } pub struct Spi<'d, T: Instance> { - inner: Unborrowed<'d, T>, + inner: PeripheralRef<'d, T>, } fn div_roundup(a: u32, b: u32) -> u32 { @@ -59,45 +59,45 @@ fn calc_prescs(freq: u32) -> (u8, u8) { impl<'d, T: Instance> Spi<'d, T> { pub fn new( - inner: impl Unborrow + 'd, - clk: impl Unborrow + 'd> + 'd, - mosi: impl Unborrow + 'd> + 'd, - miso: impl Unborrow + 'd> + 'd, + inner: impl Peripheral

+ 'd, + clk: impl Peripheral

+ 'd> + 'd, + mosi: impl Peripheral

+ 'd> + 'd, + miso: impl Peripheral

+ 'd> + 'd, config: Config, ) -> Self { - unborrow_and_degrade!(clk, mosi, miso); + into_degraded_ref!(clk, mosi, miso); Self::new_inner(inner, Some(clk), Some(mosi), Some(miso), None, config) } pub fn new_txonly( - inner: impl Unborrow + 'd, - clk: impl Unborrow + 'd> + 'd, - mosi: impl Unborrow + 'd> + 'd, + inner: impl Peripheral

+ 'd, + clk: impl Peripheral

+ 'd> + 'd, + mosi: impl Peripheral

+ 'd> + 'd, config: Config, ) -> Self { - unborrow_and_degrade!(clk, mosi); + into_degraded_ref!(clk, mosi); Self::new_inner(inner, Some(clk), Some(mosi), None, None, config) } pub fn new_rxonly( - inner: impl Unborrow + 'd, - clk: impl Unborrow + 'd> + 'd, - miso: impl Unborrow + 'd> + 'd, + inner: impl Peripheral

+ 'd, + clk: impl Peripheral

+ 'd> + 'd, + miso: impl Peripheral

+ 'd> + 'd, config: Config, ) -> Self { - unborrow_and_degrade!(clk, miso); + into_degraded_ref!(clk, miso); Self::new_inner(inner, Some(clk), None, Some(miso), None, config) } fn new_inner( - inner: impl Unborrow + 'd, - clk: Option>, - mosi: Option>, - miso: Option>, - cs: Option>, + inner: impl Peripheral

+ 'd, + clk: Option>, + mosi: Option>, + miso: Option>, + cs: Option>, config: Config, ) -> Self { - unborrow!(inner); + into_ref!(inner); unsafe { let p = inner.regs(); diff --git a/embassy-rp/src/uart.rs b/embassy-rp/src/uart.rs index 33119af6..b19f043f 100644 --- a/embassy-rp/src/uart.rs +++ b/embassy-rp/src/uart.rs @@ -1,7 +1,7 @@ -use embassy_hal_common::{unborrow, Unborrowed}; +use embassy_hal_common::{into_ref, PeripheralRef}; use gpio::Pin; -use crate::{gpio, pac, peripherals, Unborrow}; +use crate::{gpio, pac, peripherals, Peripheral}; #[non_exhaustive] pub struct Config { @@ -21,19 +21,19 @@ impl Default for Config { } pub struct Uart<'d, T: Instance> { - inner: Unborrowed<'d, T>, + inner: PeripheralRef<'d, T>, } impl<'d, T: Instance> Uart<'d, T> { pub fn new( - inner: impl Unborrow + 'd, - tx: impl Unborrow> + 'd, - rx: impl Unborrow> + 'd, - cts: impl Unborrow> + 'd, - rts: impl Unborrow> + 'd, + inner: impl Peripheral

+ 'd, + tx: impl Peripheral

> + 'd, + rx: impl Peripheral

> + 'd, + cts: impl Peripheral

> + 'd, + rts: impl Peripheral

> + 'd, config: Config, ) -> Self { - unborrow!(inner, tx, rx, cts, rts); + into_ref!(inner, tx, rx, cts, rts); unsafe { let p = inner.regs(); diff --git a/embassy-stm32/src/adc/f1.rs b/embassy-stm32/src/adc/f1.rs index 74cfac13..50d4f9bf 100644 --- a/embassy-stm32/src/adc/f1.rs +++ b/embassy-stm32/src/adc/f1.rs @@ -1,12 +1,12 @@ use core::marker::PhantomData; -use embassy_hal_common::unborrow; +use embassy_hal_common::into_ref; use embedded_hal_02::blocking::delay::DelayUs; use crate::adc::{AdcPin, Instance}; use crate::rcc::get_freqs; use crate::time::Hertz; -use crate::Unborrow; +use crate::Peripheral; pub const VDDA_CALIB_MV: u32 = 3300; pub const ADC_MAX: u32 = (1 << 12) - 1; @@ -91,8 +91,8 @@ pub struct Adc<'d, T: Instance> { } impl<'d, T: Instance> Adc<'d, T> { - pub fn new(_peri: impl Unborrow + 'd, delay: &mut impl DelayUs) -> Self { - unborrow!(_peri); + pub fn new(_peri: impl Peripheral

+ 'd, delay: &mut impl DelayUs) -> Self { + into_ref!(_peri); T::enable(); T::reset(); unsafe { diff --git a/embassy-stm32/src/adc/v2.rs b/embassy-stm32/src/adc/v2.rs index 936a3556..5c608451 100644 --- a/embassy-stm32/src/adc/v2.rs +++ b/embassy-stm32/src/adc/v2.rs @@ -1,11 +1,11 @@ use core::marker::PhantomData; -use embassy_hal_common::unborrow; +use embassy_hal_common::into_ref; use embedded_hal_02::blocking::delay::DelayUs; use crate::adc::{AdcPin, Instance}; use crate::time::Hertz; -use crate::Unborrow; +use crate::Peripheral; pub const VDDA_CALIB_MV: u32 = 3000; @@ -159,8 +159,8 @@ impl<'d, T> Adc<'d, T> where T: Instance, { - pub fn new(_peri: impl Unborrow + 'd, delay: &mut impl DelayUs) -> Self { - unborrow!(_peri); + pub fn new(_peri: impl Peripheral

+ 'd, delay: &mut impl DelayUs) -> Self { + into_ref!(_peri); enable(); let presc = unsafe { Prescaler::from_pclk2(crate::rcc::get_freqs().apb2) }; diff --git a/embassy-stm32/src/adc/v3.rs b/embassy-stm32/src/adc/v3.rs index 49d149b1..dbfd1881 100644 --- a/embassy-stm32/src/adc/v3.rs +++ b/embassy-stm32/src/adc/v3.rs @@ -1,10 +1,10 @@ use core::marker::PhantomData; -use embassy_hal_common::unborrow; +use embassy_hal_common::into_ref; use embedded_hal_02::blocking::delay::DelayUs; use crate::adc::{AdcPin, Instance}; -use crate::Unborrow; +use crate::Peripheral; pub const VDDA_CALIB_MV: u32 = 3000; @@ -208,8 +208,8 @@ pub struct Adc<'d, T: Instance> { } impl<'d, T: Instance> Adc<'d, T> { - pub fn new(_peri: impl Unborrow + 'd, delay: &mut impl DelayUs) -> Self { - unborrow!(_peri); + pub fn new(_peri: impl Peripheral

+ 'd, delay: &mut impl DelayUs) -> Self { + into_ref!(_peri); enable(); unsafe { T::regs().cr().modify(|reg| { diff --git a/embassy-stm32/src/adc/v4.rs b/embassy-stm32/src/adc/v4.rs index 3f933f4f..92e8ac36 100644 --- a/embassy-stm32/src/adc/v4.rs +++ b/embassy-stm32/src/adc/v4.rs @@ -7,7 +7,7 @@ use pac::adccommon::vals::Presc; use super::{AdcPin, Instance}; use crate::time::Hertz; -use crate::{pac, Unborrow}; +use crate::{pac, Peripheral}; pub enum Resolution { SixteenBit, @@ -322,8 +322,8 @@ pub struct Adc<'d, T: Instance> { } impl<'d, T: Instance + crate::rcc::RccPeripheral> Adc<'d, T> { - pub fn new(_peri: impl Unborrow + 'd, delay: &mut impl DelayUs) -> Self { - embassy_hal_common::unborrow!(_peri); + pub fn new(_peri: impl Peripheral

+ 'd, delay: &mut impl DelayUs) -> Self { + embassy_hal_common::into_ref!(_peri); T::enable(); T::reset(); diff --git a/embassy-stm32/src/can/bxcan.rs b/embassy-stm32/src/can/bxcan.rs index 6c0de2de..c0bd44e0 100644 --- a/embassy-stm32/src/can/bxcan.rs +++ b/embassy-stm32/src/can/bxcan.rs @@ -1,11 +1,11 @@ use core::ops::{Deref, DerefMut}; pub use bxcan; -use embassy_hal_common::{unborrow, Unborrowed}; +use embassy_hal_common::{into_ref, PeripheralRef}; use crate::gpio::sealed::AFType; use crate::rcc::RccPeripheral; -use crate::{peripherals, Unborrow}; +use crate::{peripherals, Peripheral}; pub struct Can<'d, T: Instance> { can: bxcan::Can>, @@ -13,11 +13,11 @@ pub struct Can<'d, T: Instance> { impl<'d, T: Instance> Can<'d, T> { pub fn new( - peri: impl Unborrow + 'd, - rx: impl Unborrow> + 'd, - tx: impl Unborrow> + 'd, + peri: impl Peripheral

+ 'd, + rx: impl Peripheral

> + 'd, + tx: impl Peripheral

> + 'd, ) -> Self { - unborrow!(peri, rx, tx); + into_ref!(peri, rx, tx); unsafe { rx.set_as_af(rx.af_num(), AFType::Input); @@ -66,7 +66,7 @@ pub(crate) mod sealed { pub trait Instance: sealed::Instance + RccPeripheral {} -pub struct BxcanInstance<'a, T>(Unborrowed<'a, T>); +pub struct BxcanInstance<'a, T>(PeripheralRef<'a, T>); unsafe impl<'d, T: Instance> bxcan::Instance for BxcanInstance<'d, T> { const REGISTERS: *mut bxcan::RegisterBlock = T::REGISTERS; diff --git a/embassy-stm32/src/crc/v1.rs b/embassy-stm32/src/crc/v1.rs index 85abf22d..393089ee 100644 --- a/embassy-stm32/src/crc/v1.rs +++ b/embassy-stm32/src/crc/v1.rs @@ -1,25 +1,25 @@ -use embassy_hal_common::{unborrow, Unborrowed}; +use embassy_hal_common::{into_ref, PeripheralRef}; use crate::pac::CRC as PAC_CRC; use crate::peripherals::CRC; use crate::rcc::sealed::RccPeripheral; -use crate::Unborrow; +use crate::Peripheral; pub struct Crc<'d> { - _peri: Unborrowed<'d, CRC>, + _peri: PeripheralRef<'d, CRC>, } impl<'d> Crc<'d> { /// Instantiates the CRC32 peripheral and initializes it to default values. - pub fn new(peripheral: impl Unborrow + 'd) -> Self { - unborrow!(peripheral); + pub fn new(peripheral: impl Peripheral

+ 'd) -> Self { + into_ref!(peripheral); // Note: enable and reset come from RccPeripheral. // enable CRC clock in RCC. CRC::enable(); // Reset CRC to default values. CRC::reset(); - // Unborrow the peripheral + // Peripheral the peripheral let mut instance = Self { _peri: peripheral }; instance.reset(); instance diff --git a/embassy-stm32/src/crc/v2v3.rs b/embassy-stm32/src/crc/v2v3.rs index 06da29c1..8acb3a77 100644 --- a/embassy-stm32/src/crc/v2v3.rs +++ b/embassy-stm32/src/crc/v2v3.rs @@ -1,13 +1,13 @@ -use embassy_hal_common::{unborrow, Unborrowed}; +use embassy_hal_common::{into_ref, PeripheralRef}; use crate::pac::crc::vals; use crate::pac::CRC as PAC_CRC; use crate::peripherals::CRC; use crate::rcc::sealed::RccPeripheral; -use crate::Unborrow; +use crate::Peripheral; pub struct Crc<'d> { - _peripheral: Unborrowed<'d, CRC>, + _peripheral: PeripheralRef<'d, CRC>, _config: Config, } @@ -67,13 +67,13 @@ pub enum PolySize { impl<'d> Crc<'d> { /// Instantiates the CRC32 peripheral and initializes it to default values. - pub fn new(peripheral: impl Unborrow + 'd, config: Config) -> Self { + pub fn new(peripheral: impl Peripheral

+ 'd, config: Config) -> Self { // Note: enable and reset come from RccPeripheral. // enable CRC clock in RCC. CRC::enable(); // Reset CRC to default values. CRC::reset(); - unborrow!(peripheral); + into_ref!(peripheral); let mut instance = Self { _peripheral: peripheral, _config: config, diff --git a/embassy-stm32/src/dac/v2.rs b/embassy-stm32/src/dac/v2.rs index 798eefbf..6b7f41c6 100644 --- a/embassy-stm32/src/dac/v2.rs +++ b/embassy-stm32/src/dac/v2.rs @@ -1,8 +1,8 @@ -use embassy_hal_common::{unborrow, Unborrowed}; +use embassy_hal_common::{into_ref, PeripheralRef}; use crate::dac::{DacPin, Instance}; use crate::pac::dac; -use crate::Unborrow; +use crate::Peripheral; #[derive(Debug, Copy, Clone, Eq, PartialEq)] #[cfg_attr(feature = "defmt", derive(defmt::Format))] @@ -88,7 +88,7 @@ pub enum Value { pub struct Dac<'d, T: Instance> { channels: u8, - _peri: Unborrowed<'d, T>, + _peri: PeripheralRef<'d, T>, } macro_rules! enable { @@ -100,21 +100,21 @@ macro_rules! enable { } impl<'d, T: Instance> Dac<'d, T> { - pub fn new_1ch(peri: impl Unborrow + 'd, _ch1: impl Unborrow> + 'd) -> Self { - unborrow!(peri); + pub fn new_1ch(peri: impl Peripheral

+ 'd, _ch1: impl Peripheral

> + 'd) -> Self { + into_ref!(peri); Self::new_inner(peri, 1) } pub fn new_2ch( - peri: impl Unborrow + 'd, - _ch1: impl Unborrow> + 'd, - _ch2: impl Unborrow> + 'd, + peri: impl Peripheral

+ 'd, + _ch1: impl Peripheral

> + 'd, + _ch2: impl Peripheral

> + 'd, ) -> Self { - unborrow!(peri); + into_ref!(peri); Self::new_inner(peri, 2) } - fn new_inner(peri: Unborrowed<'d, T>, channels: u8) -> Self { + fn new_inner(peri: PeripheralRef<'d, T>, channels: u8) -> Self { unsafe { // Sadly we cannot use `RccPeripheral::enable` since devices are quite inconsistent DAC clock // configuration. diff --git a/embassy-stm32/src/dcmi.rs b/embassy-stm32/src/dcmi.rs index bcf72349..e2822542 100644 --- a/embassy-stm32/src/dcmi.rs +++ b/embassy-stm32/src/dcmi.rs @@ -1,13 +1,13 @@ use core::task::Poll; use embassy::waitqueue::AtomicWaker; -use embassy_hal_common::{unborrow, Unborrowed}; +use embassy_hal_common::{into_ref, PeripheralRef}; use futures::future::poll_fn; use crate::gpio::sealed::AFType; use crate::gpio::Speed; use crate::interrupt::{Interrupt, InterruptExt}; -use crate::Unborrow; +use crate::Peripheral; /// The level on the VSync pin when the data is not valid on the parallel interface. #[derive(Clone, Copy, PartialEq)] @@ -69,7 +69,7 @@ impl Default for Config { macro_rules! config_pins { ($($pin:ident),*) => { - unborrow!($($pin),*); + into_ref!($($pin),*); // NOTE(unsafe) Exclusive access to the registers critical_section::with(|_| unsafe { $( @@ -81,8 +81,8 @@ macro_rules! config_pins { } pub struct Dcmi<'d, T: Instance, Dma: FrameDma> { - inner: Unborrowed<'d, T>, - dma: Unborrowed<'d, Dma>, + inner: PeripheralRef<'d, T>, + dma: PeripheralRef<'d, Dma>, } impl<'d, T, Dma> Dcmi<'d, T, Dma> @@ -91,23 +91,23 @@ where Dma: FrameDma, { pub fn new_8bit( - peri: impl Unborrow + 'd, - dma: impl Unborrow + 'd, - irq: impl Unborrow + 'd, - d0: impl Unborrow> + 'd, - d1: impl Unborrow> + 'd, - d2: impl Unborrow> + 'd, - d3: impl Unborrow> + 'd, - d4: impl Unborrow> + 'd, - d5: impl Unborrow> + 'd, - d6: impl Unborrow> + 'd, - d7: impl Unborrow> + 'd, - v_sync: impl Unborrow> + 'd, - h_sync: impl Unborrow> + 'd, - pixclk: impl Unborrow> + 'd, + peri: impl Peripheral

+ 'd, + dma: impl Peripheral

+ 'd, + irq: impl Peripheral

+ 'd, + d0: impl Peripheral

> + 'd, + d1: impl Peripheral

> + 'd, + d2: impl Peripheral

> + 'd, + d3: impl Peripheral

> + 'd, + d4: impl Peripheral

> + 'd, + d5: impl Peripheral

> + 'd, + d6: impl Peripheral

> + 'd, + d7: impl Peripheral

> + 'd, + v_sync: impl Peripheral

> + 'd, + h_sync: impl Peripheral

> + 'd, + pixclk: impl Peripheral

> + 'd, config: Config, ) -> Self { - unborrow!(peri, dma, irq); + into_ref!(peri, dma, irq); config_pins!(d0, d1, d2, d3, d4, d5, d6, d7); config_pins!(v_sync, h_sync, pixclk); @@ -115,25 +115,25 @@ where } pub fn new_10bit( - peri: impl Unborrow + 'd, - dma: impl Unborrow + 'd, - irq: impl Unborrow + 'd, - d0: impl Unborrow> + 'd, - d1: impl Unborrow> + 'd, - d2: impl Unborrow> + 'd, - d3: impl Unborrow> + 'd, - d4: impl Unborrow> + 'd, - d5: impl Unborrow> + 'd, - d6: impl Unborrow> + 'd, - d7: impl Unborrow> + 'd, - d8: impl Unborrow> + 'd, - d9: impl Unborrow> + 'd, - v_sync: impl Unborrow> + 'd, - h_sync: impl Unborrow> + 'd, - pixclk: impl Unborrow> + 'd, + peri: impl Peripheral

+ 'd, + dma: impl Peripheral

+ 'd, + irq: impl Peripheral

+ 'd, + d0: impl Peripheral

> + 'd, + d1: impl Peripheral

> + 'd, + d2: impl Peripheral

> + 'd, + d3: impl Peripheral

> + 'd, + d4: impl Peripheral

> + 'd, + d5: impl Peripheral

> + 'd, + d6: impl Peripheral

> + 'd, + d7: impl Peripheral

> + 'd, + d8: impl Peripheral

> + 'd, + d9: impl Peripheral

> + 'd, + v_sync: impl Peripheral

> + 'd, + h_sync: impl Peripheral

> + 'd, + pixclk: impl Peripheral

> + 'd, config: Config, ) -> Self { - unborrow!(peri, dma, irq); + into_ref!(peri, dma, irq); config_pins!(d0, d1, d2, d3, d4, d5, d6, d7, d8, d9); config_pins!(v_sync, h_sync, pixclk); @@ -141,27 +141,27 @@ where } pub fn new_12bit( - peri: impl Unborrow + 'd, - dma: impl Unborrow + 'd, - irq: impl Unborrow + 'd, - d0: impl Unborrow> + 'd, - d1: impl Unborrow> + 'd, - d2: impl Unborrow> + 'd, - d3: impl Unborrow> + 'd, - d4: impl Unborrow> + 'd, - d5: impl Unborrow> + 'd, - d6: impl Unborrow> + 'd, - d7: impl Unborrow> + 'd, - d8: impl Unborrow> + 'd, - d9: impl Unborrow> + 'd, - d10: impl Unborrow> + 'd, - d11: impl Unborrow> + 'd, - v_sync: impl Unborrow> + 'd, - h_sync: impl Unborrow> + 'd, - pixclk: impl Unborrow> + 'd, + peri: impl Peripheral

+ 'd, + dma: impl Peripheral

+ 'd, + irq: impl Peripheral

+ 'd, + d0: impl Peripheral

> + 'd, + d1: impl Peripheral

> + 'd, + d2: impl Peripheral

> + 'd, + d3: impl Peripheral

> + 'd, + d4: impl Peripheral

> + 'd, + d5: impl Peripheral

> + 'd, + d6: impl Peripheral

> + 'd, + d7: impl Peripheral

> + 'd, + d8: impl Peripheral

> + 'd, + d9: impl Peripheral

> + 'd, + d10: impl Peripheral

> + 'd, + d11: impl Peripheral

> + 'd, + v_sync: impl Peripheral

> + 'd, + h_sync: impl Peripheral

> + 'd, + pixclk: impl Peripheral

> + 'd, config: Config, ) -> Self { - unborrow!(peri, dma, irq); + into_ref!(peri, dma, irq); config_pins!(d0, d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11); config_pins!(v_sync, h_sync, pixclk); @@ -169,29 +169,29 @@ where } pub fn new_14bit( - peri: impl Unborrow + 'd, - dma: impl Unborrow + 'd, - irq: impl Unborrow + 'd, - d0: impl Unborrow> + 'd, - d1: impl Unborrow> + 'd, - d2: impl Unborrow> + 'd, - d3: impl Unborrow> + 'd, - d4: impl Unborrow> + 'd, - d5: impl Unborrow> + 'd, - d6: impl Unborrow> + 'd, - d7: impl Unborrow> + 'd, - d8: impl Unborrow> + 'd, - d9: impl Unborrow> + 'd, - d10: impl Unborrow> + 'd, - d11: impl Unborrow> + 'd, - d12: impl Unborrow> + 'd, - d13: impl Unborrow> + 'd, - v_sync: impl Unborrow> + 'd, - h_sync: impl Unborrow> + 'd, - pixclk: impl Unborrow> + 'd, + peri: impl Peripheral

+ 'd, + dma: impl Peripheral

+ 'd, + irq: impl Peripheral

+ 'd, + d0: impl Peripheral

> + 'd, + d1: impl Peripheral

> + 'd, + d2: impl Peripheral

> + 'd, + d3: impl Peripheral

> + 'd, + d4: impl Peripheral

> + 'd, + d5: impl Peripheral

> + 'd, + d6: impl Peripheral

> + 'd, + d7: impl Peripheral

> + 'd, + d8: impl Peripheral

> + 'd, + d9: impl Peripheral

> + 'd, + d10: impl Peripheral

> + 'd, + d11: impl Peripheral

> + 'd, + d12: impl Peripheral

> + 'd, + d13: impl Peripheral

> + 'd, + v_sync: impl Peripheral

> + 'd, + h_sync: impl Peripheral

> + 'd, + pixclk: impl Peripheral

> + 'd, config: Config, ) -> Self { - unborrow!(peri, dma, irq); + into_ref!(peri, dma, irq); config_pins!(d0, d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12, d13); config_pins!(v_sync, h_sync, pixclk); @@ -199,21 +199,21 @@ where } pub fn new_es_8bit( - peri: impl Unborrow + 'd, - dma: impl Unborrow + 'd, - irq: impl Unborrow + 'd, - d0: impl Unborrow> + 'd, - d1: impl Unborrow> + 'd, - d2: impl Unborrow> + 'd, - d3: impl Unborrow> + 'd, - d4: impl Unborrow> + 'd, - d5: impl Unborrow> + 'd, - d6: impl Unborrow> + 'd, - d7: impl Unborrow> + 'd, - pixclk: impl Unborrow> + 'd, + peri: impl Peripheral

+ 'd, + dma: impl Peripheral

+ 'd, + irq: impl Peripheral

+ 'd, + d0: impl Peripheral

> + 'd, + d1: impl Peripheral

> + 'd, + d2: impl Peripheral

> + 'd, + d3: impl Peripheral

> + 'd, + d4: impl Peripheral

> + 'd, + d5: impl Peripheral

> + 'd, + d6: impl Peripheral

> + 'd, + d7: impl Peripheral

> + 'd, + pixclk: impl Peripheral

> + 'd, config: Config, ) -> Self { - unborrow!(peri, dma, irq); + into_ref!(peri, dma, irq); config_pins!(d0, d1, d2, d3, d4, d5, d6, d7); config_pins!(pixclk); @@ -221,23 +221,23 @@ where } pub fn new_es_10bit( - peri: impl Unborrow + 'd, - dma: impl Unborrow + 'd, - irq: impl Unborrow + 'd, - d0: impl Unborrow> + 'd, - d1: impl Unborrow> + 'd, - d2: impl Unborrow> + 'd, - d3: impl Unborrow> + 'd, - d4: impl Unborrow> + 'd, - d5: impl Unborrow> + 'd, - d6: impl Unborrow> + 'd, - d7: impl Unborrow> + 'd, - d8: impl Unborrow> + 'd, - d9: impl Unborrow> + 'd, - pixclk: impl Unborrow> + 'd, + peri: impl Peripheral

+ 'd, + dma: impl Peripheral

+ 'd, + irq: impl Peripheral

+ 'd, + d0: impl Peripheral

> + 'd, + d1: impl Peripheral

> + 'd, + d2: impl Peripheral

> + 'd, + d3: impl Peripheral

> + 'd, + d4: impl Peripheral

> + 'd, + d5: impl Peripheral

> + 'd, + d6: impl Peripheral

> + 'd, + d7: impl Peripheral

> + 'd, + d8: impl Peripheral

> + 'd, + d9: impl Peripheral

> + 'd, + pixclk: impl Peripheral

> + 'd, config: Config, ) -> Self { - unborrow!(peri, dma, irq); + into_ref!(peri, dma, irq); config_pins!(d0, d1, d2, d3, d4, d5, d6, d7, d8, d9); config_pins!(pixclk); @@ -245,25 +245,25 @@ where } pub fn new_es_12bit( - peri: impl Unborrow + 'd, - dma: impl Unborrow + 'd, - irq: impl Unborrow + 'd, - d0: impl Unborrow> + 'd, - d1: impl Unborrow> + 'd, - d2: impl Unborrow> + 'd, - d3: impl Unborrow> + 'd, - d4: impl Unborrow> + 'd, - d5: impl Unborrow> + 'd, - d6: impl Unborrow> + 'd, - d7: impl Unborrow> + 'd, - d8: impl Unborrow> + 'd, - d9: impl Unborrow> + 'd, - d10: impl Unborrow> + 'd, - d11: impl Unborrow> + 'd, - pixclk: impl Unborrow> + 'd, + peri: impl Peripheral

+ 'd, + dma: impl Peripheral

+ 'd, + irq: impl Peripheral

+ 'd, + d0: impl Peripheral

> + 'd, + d1: impl Peripheral

> + 'd, + d2: impl Peripheral

> + 'd, + d3: impl Peripheral

> + 'd, + d4: impl Peripheral

> + 'd, + d5: impl Peripheral

> + 'd, + d6: impl Peripheral

> + 'd, + d7: impl Peripheral

> + 'd, + d8: impl Peripheral

> + 'd, + d9: impl Peripheral

> + 'd, + d10: impl Peripheral

> + 'd, + d11: impl Peripheral

> + 'd, + pixclk: impl Peripheral

> + 'd, config: Config, ) -> Self { - unborrow!(peri, dma, irq); + into_ref!(peri, dma, irq); config_pins!(d0, d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11); config_pins!(pixclk); @@ -271,27 +271,27 @@ where } pub fn new_es_14bit( - peri: impl Unborrow + 'd, - dma: impl Unborrow + 'd, - irq: impl Unborrow + 'd, - d0: impl Unborrow> + 'd, - d1: impl Unborrow> + 'd, - d2: impl Unborrow> + 'd, - d3: impl Unborrow> + 'd, - d4: impl Unborrow> + 'd, - d5: impl Unborrow> + 'd, - d6: impl Unborrow> + 'd, - d7: impl Unborrow> + 'd, - d8: impl Unborrow> + 'd, - d9: impl Unborrow> + 'd, - d10: impl Unborrow> + 'd, - d11: impl Unborrow> + 'd, - d12: impl Unborrow> + 'd, - d13: impl Unborrow> + 'd, - pixclk: impl Unborrow> + 'd, + peri: impl Peripheral

+ 'd, + dma: impl Peripheral

+ 'd, + irq: impl Peripheral

+ 'd, + d0: impl Peripheral

> + 'd, + d1: impl Peripheral

> + 'd, + d2: impl Peripheral

> + 'd, + d3: impl Peripheral

> + 'd, + d4: impl Peripheral

> + 'd, + d5: impl Peripheral

> + 'd, + d6: impl Peripheral

> + 'd, + d7: impl Peripheral

> + 'd, + d8: impl Peripheral

> + 'd, + d9: impl Peripheral

> + 'd, + d10: impl Peripheral

> + 'd, + d11: impl Peripheral

> + 'd, + d12: impl Peripheral

> + 'd, + d13: impl Peripheral

> + 'd, + pixclk: impl Peripheral

> + 'd, config: Config, ) -> Self { - unborrow!(peri, dma, irq); + into_ref!(peri, dma, irq); config_pins!(d0, d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12, d13); config_pins!(pixclk); @@ -299,9 +299,9 @@ where } fn new_inner( - peri: Unborrowed<'d, T>, - dma: Unborrowed<'d, Dma>, - irq: Unborrowed<'d, T::Interrupt>, + peri: PeripheralRef<'d, T>, + dma: PeripheralRef<'d, Dma>, + irq: PeripheralRef<'d, T::Interrupt>, config: Config, use_embedded_synchronization: bool, edm: u8, diff --git a/embassy-stm32/src/dma/mod.rs b/embassy-stm32/src/dma/mod.rs index fe2bf900..cc030a93 100644 --- a/embassy-stm32/src/dma/mod.rs +++ b/embassy-stm32/src/dma/mod.rs @@ -12,11 +12,11 @@ use core::mem; use core::pin::Pin; use core::task::{Context, Poll, Waker}; -use embassy_hal_common::{impl_unborrow, unborrow}; +use embassy_hal_common::{impl_peripheral, into_ref}; #[cfg(dmamux)] pub use self::dmamux::*; -use crate::Unborrow; +use crate::Peripheral; #[cfg(feature = "unstable-pac")] pub mod low_level { @@ -206,19 +206,19 @@ impl Default for TransferOptions { } mod transfers { - use embassy_hal_common::Unborrowed; + use embassy_hal_common::PeripheralRef; use super::*; #[allow(unused)] pub fn read<'a, W: Word>( - channel: impl Unborrow + 'a, + channel: impl Peripheral

+ 'a, request: Request, reg_addr: *mut W, buf: &'a mut [W], ) -> impl Future + 'a { assert!(buf.len() > 0 && buf.len() <= 0xFFFF); - unborrow!(channel); + into_ref!(channel); unsafe { channel.start_read::(request, reg_addr, buf, Default::default()) }; @@ -227,13 +227,13 @@ mod transfers { #[allow(unused)] pub fn write<'a, W: Word>( - channel: impl Unborrow + 'a, + channel: impl Peripheral

+ 'a, request: Request, buf: &'a [W], reg_addr: *mut W, ) -> impl Future + 'a { assert!(buf.len() > 0 && buf.len() <= 0xFFFF); - unborrow!(channel); + into_ref!(channel); unsafe { channel.start_write::(request, buf, reg_addr, Default::default()) }; @@ -242,13 +242,13 @@ mod transfers { #[allow(unused)] pub fn write_repeated<'a, W: Word>( - channel: impl Unborrow + 'a, + channel: impl Peripheral

+ 'a, request: Request, repeated: W, count: usize, reg_addr: *mut W, ) -> impl Future + 'a { - unborrow!(channel); + into_ref!(channel); unsafe { channel.start_write_repeated::(request, repeated, count, reg_addr, Default::default()) }; @@ -256,12 +256,12 @@ mod transfers { } pub(crate) struct Transfer<'a, C: Channel> { - channel: Unborrowed<'a, C>, + channel: PeripheralRef<'a, C>, } impl<'a, C: Channel> Transfer<'a, C> { - pub(crate) fn new(channel: impl Unborrow + 'a) -> Self { - unborrow!(channel); + pub(crate) fn new(channel: impl Peripheral

+ 'a) -> Self { + into_ref!(channel); Self { channel } } } @@ -287,11 +287,11 @@ mod transfers { } } -pub trait Channel: sealed::Channel + Unborrow + 'static {} +pub trait Channel: sealed::Channel + Peripheral

+ 'static {} pub struct NoDma; -impl_unborrow!(NoDma); +impl_peripheral!(NoDma); // safety: must be called only once at startup pub(crate) unsafe fn init() { diff --git a/embassy-stm32/src/eth/v1/mod.rs b/embassy-stm32/src/eth/v1/mod.rs index f2cdd909..5e31c32b 100644 --- a/embassy-stm32/src/eth/v1/mod.rs +++ b/embassy-stm32/src/eth/v1/mod.rs @@ -6,7 +6,7 @@ use core::task::Waker; use embassy::waitqueue::AtomicWaker; use embassy_cortex_m::peripheral::{PeripheralMutex, PeripheralState, StateStorage}; -use embassy_hal_common::{unborrow, Unborrowed}; +use embassy_hal_common::{into_ref, PeripheralRef}; use embassy_net::{Device, DeviceCapabilities, LinkState, PacketBuf, MTU}; use crate::gpio::sealed::{AFType, Pin as __GpioPin}; @@ -16,7 +16,7 @@ use crate::pac::AFIO; #[cfg(any(eth_v1b, eth_v1c))] use crate::pac::SYSCFG; use crate::pac::{ETH, RCC}; -use crate::Unborrow; +use crate::Peripheral; mod descriptors; mod rx_desc; @@ -36,7 +36,7 @@ impl<'d, T: Instance, const TX: usize, const RX: usize> State<'d, T, TX, RX> { pub struct Ethernet<'d, T: Instance, P: PHY, const TX: usize, const RX: usize> { state: PeripheralMutex<'d, Inner<'d, T, TX, RX>>, - pins: [Unborrowed<'d, AnyPin>; 9], + pins: [PeripheralRef<'d, AnyPin>; 9], _phy: P, clock_range: Cr, phy_addr: u8, @@ -86,22 +86,22 @@ impl<'d, T: Instance, P: PHY, const TX: usize, const RX: usize> Ethernet<'d, T, /// safety: the returned instance is not leak-safe pub unsafe fn new( state: &'d mut State<'d, T, TX, RX>, - peri: impl Unborrow + 'd, - interrupt: impl Unborrow + 'd, - ref_clk: impl Unborrow> + 'd, - mdio: impl Unborrow> + 'd, - mdc: impl Unborrow> + 'd, - crs: impl Unborrow> + 'd, - rx_d0: impl Unborrow> + 'd, - rx_d1: impl Unborrow> + 'd, - tx_d0: impl Unborrow> + 'd, - tx_d1: impl Unborrow> + 'd, - tx_en: impl Unborrow> + 'd, + peri: impl Peripheral

+ 'd, + interrupt: impl Peripheral

+ 'd, + ref_clk: impl Peripheral

> + 'd, + mdio: impl Peripheral

> + 'd, + mdc: impl Peripheral

> + 'd, + crs: impl Peripheral

> + 'd, + rx_d0: impl Peripheral

> + 'd, + rx_d1: impl Peripheral

> + 'd, + tx_d0: impl Peripheral

> + 'd, + tx_d1: impl Peripheral

> + 'd, + tx_en: impl Peripheral

> + 'd, phy: P, mac_addr: [u8; 6], phy_addr: u8, ) -> Self { - unborrow!(interrupt, ref_clk, mdio, mdc, crs, rx_d0, rx_d1, tx_d0, tx_d1, tx_en); + into_ref!(interrupt, ref_clk, mdio, mdc, crs, rx_d0, rx_d1, tx_d0, tx_d1, tx_en); // Enable the necessary Clocks // NOTE(unsafe) We have exclusive access to the registers @@ -370,7 +370,7 @@ struct Inner<'d, T: Instance, const TX: usize, const RX: usize> { } impl<'d, T: Instance, const TX: usize, const RX: usize> Inner<'d, T, TX, RX> { - pub fn new(_peri: impl Unborrow + 'd) -> Self { + pub fn new(_peri: impl Peripheral

+ 'd) -> Self { Self { _peri: PhantomData, desc_ring: DescriptorRing::new(), diff --git a/embassy-stm32/src/eth/v2/mod.rs b/embassy-stm32/src/eth/v2/mod.rs index c67a0307..2b4a9367 100644 --- a/embassy-stm32/src/eth/v2/mod.rs +++ b/embassy-stm32/src/eth/v2/mod.rs @@ -4,13 +4,13 @@ use core::task::Waker; use embassy::waitqueue::AtomicWaker; use embassy_cortex_m::peripheral::{PeripheralMutex, PeripheralState, StateStorage}; -use embassy_hal_common::{unborrow, Unborrowed}; +use embassy_hal_common::{into_ref, PeripheralRef}; use embassy_net::{Device, DeviceCapabilities, LinkState, PacketBuf, MTU}; use crate::gpio::sealed::{AFType, Pin as _}; use crate::gpio::{AnyPin, Speed}; use crate::pac::{ETH, RCC, SYSCFG}; -use crate::Unborrow; +use crate::Peripheral; mod descriptors; use descriptors::DescriptorRing; @@ -25,7 +25,7 @@ impl<'d, T: Instance, const TX: usize, const RX: usize> State<'d, T, TX, RX> { } pub struct Ethernet<'d, T: Instance, P: PHY, const TX: usize, const RX: usize> { state: PeripheralMutex<'d, Inner<'d, T, TX, RX>>, - pins: [Unborrowed<'d, AnyPin>; 9], + pins: [PeripheralRef<'d, AnyPin>; 9], _phy: P, clock_range: u8, phy_addr: u8, @@ -48,22 +48,22 @@ impl<'d, T: Instance, P: PHY, const TX: usize, const RX: usize> Ethernet<'d, T, /// safety: the returned instance is not leak-safe pub unsafe fn new( state: &'d mut State<'d, T, TX, RX>, - peri: impl Unborrow + 'd, - interrupt: impl Unborrow + 'd, - ref_clk: impl Unborrow> + 'd, - mdio: impl Unborrow> + 'd, - mdc: impl Unborrow> + 'd, - crs: impl Unborrow> + 'd, - rx_d0: impl Unborrow> + 'd, - rx_d1: impl Unborrow> + 'd, - tx_d0: impl Unborrow> + 'd, - tx_d1: impl Unborrow> + 'd, - tx_en: impl Unborrow> + 'd, + peri: impl Peripheral

+ 'd, + interrupt: impl Peripheral

+ 'd, + ref_clk: impl Peripheral

> + 'd, + mdio: impl Peripheral

> + 'd, + mdc: impl Peripheral

> + 'd, + crs: impl Peripheral

> + 'd, + rx_d0: impl Peripheral

> + 'd, + rx_d1: impl Peripheral

> + 'd, + tx_d0: impl Peripheral

> + 'd, + tx_d1: impl Peripheral

> + 'd, + tx_en: impl Peripheral

> + 'd, phy: P, mac_addr: [u8; 6], phy_addr: u8, ) -> Self { - unborrow!(interrupt, ref_clk, mdio, mdc, crs, rx_d0, rx_d1, tx_d0, tx_d1, tx_en); + into_ref!(interrupt, ref_clk, mdio, mdc, crs, rx_d0, rx_d1, tx_d0, tx_d1, tx_en); // Enable the necessary Clocks // NOTE(unsafe) We have exclusive access to the registers @@ -316,7 +316,7 @@ struct Inner<'d, T: Instance, const TX: usize, const RX: usize> { } impl<'d, T: Instance, const TX: usize, const RX: usize> Inner<'d, T, TX, RX> { - pub fn new(_peri: impl Unborrow + 'd) -> Self { + pub fn new(_peri: impl Peripheral

+ 'd) -> Self { Self { _peri: PhantomData, desc_ring: DescriptorRing::new(), diff --git a/embassy-stm32/src/exti.rs b/embassy-stm32/src/exti.rs index 5076de2e..3b8d9390 100644 --- a/embassy-stm32/src/exti.rs +++ b/embassy-stm32/src/exti.rs @@ -4,12 +4,12 @@ use core::pin::Pin; use core::task::{Context, Poll}; use embassy::waitqueue::AtomicWaker; -use embassy_hal_common::impl_unborrow; +use embassy_hal_common::impl_peripheral; use crate::gpio::{AnyPin, Input, Pin as GpioPin}; use crate::pac::exti::regs::Lines; use crate::pac::EXTI; -use crate::{interrupt, pac, peripherals, Unborrow}; +use crate::{interrupt, pac, peripherals, Peripheral}; const EXTI_COUNT: usize = 16; const NEW_AW: AtomicWaker = AtomicWaker::new(); @@ -86,7 +86,7 @@ pub struct ExtiInput<'d, T: GpioPin> { impl<'d, T: GpioPin> Unpin for ExtiInput<'d, T> {} impl<'d, T: GpioPin> ExtiInput<'d, T> { - pub fn new(pin: Input<'d, T>, _ch: impl Unborrow + 'd) -> Self { + pub fn new(pin: Input<'d, T>, _ch: impl Peripheral

+ 'd) -> Self { Self { pin } } @@ -320,7 +320,7 @@ pub trait Channel: sealed::Channel + Sized { pub struct AnyChannel { number: u8, } -impl_unborrow!(AnyChannel); +impl_peripheral!(AnyChannel); impl sealed::Channel for AnyChannel {} impl Channel for AnyChannel { fn number(&self) -> usize { diff --git a/embassy-stm32/src/flash/mod.rs b/embassy-stm32/src/flash/mod.rs index bd64fdd7..5258c9b0 100644 --- a/embassy-stm32/src/flash/mod.rs +++ b/embassy-stm32/src/flash/mod.rs @@ -1,9 +1,9 @@ -use embassy_hal_common::{unborrow, Unborrowed}; +use embassy_hal_common::{into_ref, PeripheralRef}; use embedded_storage::nor_flash::{ErrorType, NorFlash, NorFlashError, NorFlashErrorKind, ReadNorFlash}; pub use crate::pac::{ERASE_SIZE, ERASE_VALUE, FLASH_BASE, FLASH_SIZE, WRITE_SIZE}; use crate::peripherals::FLASH; -use crate::Unborrow; +use crate::Peripheral; const FLASH_END: usize = FLASH_BASE + FLASH_SIZE; #[cfg_attr(any(flash_wl, flash_wb, flash_l0, flash_l1, flash_l4), path = "l.rs")] @@ -14,16 +14,16 @@ const FLASH_END: usize = FLASH_BASE + FLASH_SIZE; mod family; pub struct Flash<'d> { - _inner: Unborrowed<'d, FLASH>, + _inner: PeripheralRef<'d, FLASH>, } impl<'d> Flash<'d> { - pub fn new(p: impl Unborrow + 'd) -> Self { - unborrow!(p); + pub fn new(p: impl Peripheral

+ 'd) -> Self { + into_ref!(p); Self { _inner: p } } - pub fn unlock(p: impl Unborrow + 'd) -> Self { + pub fn unlock(p: impl Peripheral

+ 'd) -> Self { let flash = Self::new(p); unsafe { family::unlock() }; diff --git a/embassy-stm32/src/fmc/mod.rs b/embassy-stm32/src/fmc/mod.rs index 4f8e467d..856a4adc 100644 --- a/embassy-stm32/src/fmc/mod.rs +++ b/embassy-stm32/src/fmc/mod.rs @@ -1,10 +1,10 @@ use core::marker::PhantomData; -use embassy_hal_common::unborrow; +use embassy_hal_common::into_ref; use crate::gpio::sealed::AFType; use crate::gpio::{Pull, Speed}; -use crate::Unborrow; +use crate::Peripheral; mod pins; pub use pins::*; @@ -39,7 +39,7 @@ where macro_rules! config_pins { ($($pin:ident),*) => { - unborrow!($($pin),*); + into_ref!($($pin),*); $( $pin.set_as_af_pull($pin.af_num(), AFType::OutputPushPull, Pull::Up); $pin.set_speed(Speed::VeryHigh); @@ -57,12 +57,12 @@ macro_rules! fmc_sdram_constructor { ctrl: [$(($ctrl_pin_name:ident: $ctrl_signal:ident)),*] )) => { pub fn $name( - _instance: impl Unborrow + 'd, - $($addr_pin_name: impl Unborrow> + 'd),*, - $($ba_pin_name: impl Unborrow> + 'd),*, - $($d_pin_name: impl Unborrow> + 'd),*, - $($nbl_pin_name: impl Unborrow> + 'd),*, - $($ctrl_pin_name: impl Unborrow> + 'd),*, + _instance: impl Peripheral

+ 'd, + $($addr_pin_name: impl Peripheral

> + 'd),*, + $($ba_pin_name: impl Peripheral

> + 'd),*, + $($d_pin_name: impl Peripheral

> + 'd),*, + $($nbl_pin_name: impl Peripheral

> + 'd),*, + $($ctrl_pin_name: impl Peripheral

> + 'd),*, chip: CHIP ) -> stm32_fmc::Sdram, CHIP> { diff --git a/embassy-stm32/src/gpio.rs b/embassy-stm32/src/gpio.rs index 92cd2bb2..bdb18187 100644 --- a/embassy-stm32/src/gpio.rs +++ b/embassy-stm32/src/gpio.rs @@ -1,10 +1,10 @@ #![macro_use] use core::convert::Infallible; -use embassy_hal_common::{impl_unborrow, unborrow, Unborrowed}; +use embassy_hal_common::{impl_peripheral, into_ref, PeripheralRef}; use crate::pac::gpio::{self, vals}; -use crate::{pac, peripherals, Unborrow}; +use crate::{pac, peripherals, Peripheral}; /// GPIO flexible pin. /// @@ -12,7 +12,7 @@ use crate::{pac, peripherals, Unborrow}; /// set while not in output mode, so the pin's level will be 'remembered' when it is not in output /// mode. pub struct Flex<'d, T: Pin> { - pub(crate) pin: Unborrowed<'d, T>, + pub(crate) pin: PeripheralRef<'d, T>, } impl<'d, T: Pin> Flex<'d, T> { @@ -22,8 +22,8 @@ impl<'d, T: Pin> Flex<'d, T> { /// before the pin is put into output mode. /// #[inline] - pub fn new(pin: impl Unborrow + 'd) -> Self { - unborrow!(pin); + pub fn new(pin: impl Peripheral

+ 'd) -> Self { + into_ref!(pin); // Pin will be in disconnected state. Self { pin } } @@ -280,7 +280,7 @@ pub struct Input<'d, T: Pin> { impl<'d, T: Pin> Input<'d, T> { #[inline] - pub fn new(pin: impl Unborrow + 'd, pull: Pull) -> Self { + pub fn new(pin: impl Peripheral

+ 'd, pull: Pull) -> Self { let mut pin = Flex::new(pin); pin.set_as_input(pull); Self { pin } @@ -335,7 +335,7 @@ pub struct Output<'d, T: Pin> { impl<'d, T: Pin> Output<'d, T> { #[inline] - pub fn new(pin: impl Unborrow + 'd, initial_output: Level, speed: Speed) -> Self { + pub fn new(pin: impl Peripheral

+ 'd, initial_output: Level, speed: Speed) -> Self { let mut pin = Flex::new(pin); match initial_output { Level::High => pin.set_high(), @@ -395,7 +395,7 @@ pub struct OutputOpenDrain<'d, T: Pin> { impl<'d, T: Pin> OutputOpenDrain<'d, T> { #[inline] - pub fn new(pin: impl Unborrow + 'd, initial_output: Level, speed: Speed, pull: Pull) -> Self { + pub fn new(pin: impl Peripheral

+ 'd, initial_output: Level, speed: Speed, pull: Pull) -> Self { let mut pin = Flex::new(pin); match initial_output { @@ -668,7 +668,7 @@ impl AnyPin { } } -impl_unborrow!(AnyPin); +impl_peripheral!(AnyPin); impl Pin for AnyPin { #[cfg(feature = "exti")] type ExtiChannel = crate::exti::AnyChannel; diff --git a/embassy-stm32/src/i2c/v1.rs b/embassy-stm32/src/i2c/v1.rs index 65c91878..613815a9 100644 --- a/embassy-stm32/src/i2c/v1.rs +++ b/embassy-stm32/src/i2c/v1.rs @@ -1,13 +1,13 @@ use core::marker::PhantomData; use embassy_embedded_hal::SetConfig; -use embassy_hal_common::unborrow; +use embassy_hal_common::into_ref; use crate::gpio::sealed::AFType; use crate::i2c::{Error, Instance, SclPin, SdaPin}; use crate::pac::i2c; use crate::time::Hertz; -use crate::Unborrow; +use crate::Peripheral; pub struct State {} @@ -23,12 +23,12 @@ pub struct I2c<'d, T: Instance> { impl<'d, T: Instance> I2c<'d, T> { pub fn new( - _peri: impl Unborrow + 'd, - scl: impl Unborrow> + 'd, - sda: impl Unborrow> + 'd, + _peri: impl Peripheral

+ 'd, + scl: impl Peripheral

> + 'd, + sda: impl Peripheral

> + 'd, freq: Hertz, ) -> Self { - unborrow!(scl, sda); + into_ref!(scl, sda); T::enable(); T::reset(); diff --git a/embassy-stm32/src/i2c/v2.rs b/embassy-stm32/src/i2c/v2.rs index ec7fec15..dec92cc8 100644 --- a/embassy-stm32/src/i2c/v2.rs +++ b/embassy-stm32/src/i2c/v2.rs @@ -5,7 +5,7 @@ use atomic_polyfill::{AtomicUsize, Ordering}; use embassy::waitqueue::AtomicWaker; use embassy_embedded_hal::SetConfig; use embassy_hal_common::drop::OnDrop; -use embassy_hal_common::{unborrow, Unborrowed}; +use embassy_hal_common::{into_ref, PeripheralRef}; use futures::future::poll_fn; use crate::dma::NoDma; @@ -14,7 +14,7 @@ use crate::i2c::{Error, Instance, SclPin, SdaPin}; use crate::interrupt::InterruptExt; use crate::pac::i2c; use crate::time::Hertz; -use crate::Unborrow; +use crate::Peripheral; pub struct State { waker: AtomicWaker, @@ -31,23 +31,23 @@ impl State { } pub struct I2c<'d, T: Instance, TXDMA = NoDma, RXDMA = NoDma> { - _peri: Unborrowed<'d, T>, - tx_dma: Unborrowed<'d, TXDMA>, + _peri: PeripheralRef<'d, T>, + tx_dma: PeripheralRef<'d, TXDMA>, #[allow(dead_code)] - rx_dma: Unborrowed<'d, RXDMA>, + rx_dma: PeripheralRef<'d, RXDMA>, } impl<'d, T: Instance, TXDMA, RXDMA> I2c<'d, T, TXDMA, RXDMA> { pub fn new( - peri: impl Unborrow + 'd, - scl: impl Unborrow> + 'd, - sda: impl Unborrow> + 'd, - irq: impl Unborrow + 'd, - tx_dma: impl Unborrow + 'd, - rx_dma: impl Unborrow + 'd, + peri: impl Peripheral

+ 'd, + scl: impl Peripheral

> + 'd, + sda: impl Peripheral

> + 'd, + irq: impl Peripheral

+ 'd, + tx_dma: impl Peripheral

+ 'd, + rx_dma: impl Peripheral

+ 'd, freq: Hertz, ) -> Self { - unborrow!(peri, irq, scl, sda, tx_dma, rx_dma); + into_ref!(peri, irq, scl, sda, tx_dma, rx_dma); T::enable(); T::reset(); diff --git a/embassy-stm32/src/lib.rs b/embassy-stm32/src/lib.rs index f58ab2f9..78025f3d 100644 --- a/embassy-stm32/src/lib.rs +++ b/embassy-stm32/src/lib.rs @@ -75,7 +75,7 @@ pub(crate) mod _generated { // Reexports pub use _generated::{peripherals, Peripherals}; pub use embassy_cortex_m::executor; -pub use embassy_hal_common::{unborrow, Unborrow, Unborrowed}; +pub use embassy_hal_common::{into_ref, Peripheral, PeripheralRef}; pub use embassy_macros::cortex_m_interrupt as interrupt; #[cfg(feature = "unstable-pac")] pub use stm32_metapac as pac; diff --git a/embassy-stm32/src/pwm/simple_pwm.rs b/embassy-stm32/src/pwm/simple_pwm.rs index 137d3145..7b2cdbc0 100644 --- a/embassy-stm32/src/pwm/simple_pwm.rs +++ b/embassy-stm32/src/pwm/simple_pwm.rs @@ -1,18 +1,18 @@ -use embassy_hal_common::{unborrow, Unborrowed}; +use embassy_hal_common::{into_ref, PeripheralRef}; use super::*; #[allow(unused_imports)] use crate::gpio::sealed::{AFType, Pin}; use crate::time::Hertz; -use crate::Unborrow; +use crate::Peripheral; pub struct SimplePwm<'d, T> { - inner: Unborrowed<'d, T>, + inner: PeripheralRef<'d, T>, } macro_rules! config_pins { ($($pin:ident),*) => { - unborrow!($($pin),*); + into_ref!($($pin),*); // NOTE(unsafe) Exclusive access to the registers critical_section::with(|_| unsafe { $( @@ -27,8 +27,8 @@ macro_rules! config_pins { impl<'d, T: CaptureCompare16bitInstance> SimplePwm<'d, T> { pub fn new_1ch( - tim: impl Unborrow + 'd, - ch1: impl Unborrow> + 'd, + tim: impl Peripheral

+ 'd, + ch1: impl Peripheral

> + 'd, freq: Hertz, ) -> Self { Self::new_inner(tim, freq, move || { @@ -37,9 +37,9 @@ impl<'d, T: CaptureCompare16bitInstance> SimplePwm<'d, T> { } pub fn new_2ch( - tim: impl Unborrow + 'd, - ch1: impl Unborrow> + 'd, - ch2: impl Unborrow> + 'd, + tim: impl Peripheral

+ 'd, + ch1: impl Peripheral

> + 'd, + ch2: impl Peripheral

> + 'd, freq: Hertz, ) -> Self { Self::new_inner(tim, freq, move || { @@ -48,10 +48,10 @@ impl<'d, T: CaptureCompare16bitInstance> SimplePwm<'d, T> { } pub fn new_3ch( - tim: impl Unborrow + 'd, - ch1: impl Unborrow> + 'd, - ch2: impl Unborrow> + 'd, - ch3: impl Unborrow> + 'd, + tim: impl Peripheral

+ 'd, + ch1: impl Peripheral

> + 'd, + ch2: impl Peripheral

> + 'd, + ch3: impl Peripheral

> + 'd, freq: Hertz, ) -> Self { Self::new_inner(tim, freq, move || { @@ -60,11 +60,11 @@ impl<'d, T: CaptureCompare16bitInstance> SimplePwm<'d, T> { } pub fn new_4ch( - tim: impl Unborrow + 'd, - ch1: impl Unborrow> + 'd, - ch2: impl Unborrow> + 'd, - ch3: impl Unborrow> + 'd, - ch4: impl Unborrow> + 'd, + tim: impl Peripheral

+ 'd, + ch1: impl Peripheral

> + 'd, + ch2: impl Peripheral

> + 'd, + ch3: impl Peripheral

> + 'd, + ch4: impl Peripheral

> + 'd, freq: Hertz, ) -> Self { Self::new_inner(tim, freq, move || { @@ -72,8 +72,8 @@ impl<'d, T: CaptureCompare16bitInstance> SimplePwm<'d, T> { }) } - fn new_inner(tim: impl Unborrow + 'd, freq: Hertz, configure_pins: impl FnOnce()) -> Self { - unborrow!(tim); + fn new_inner(tim: impl Peripheral

+ 'd, freq: Hertz, configure_pins: impl FnOnce()) -> Self { + into_ref!(tim); T::enable(); ::reset(); diff --git a/embassy-stm32/src/rcc/h7.rs b/embassy-stm32/src/rcc/h7.rs index 6b101431..0185f7ae 100644 --- a/embassy-stm32/src/rcc/h7.rs +++ b/embassy-stm32/src/rcc/h7.rs @@ -1,6 +1,6 @@ use core::marker::PhantomData; -use embassy_hal_common::unborrow; +use embassy_hal_common::into_ref; pub use pll::PllConfig; use stm32_metapac::rcc::vals::{Mco1, Mco2}; @@ -10,7 +10,7 @@ use crate::pac::rcc::vals::{Adcsel, Ckpersel, Dppre, Hpre, Hsidiv, Pllsrc, Sw, T use crate::pac::{PWR, RCC, SYSCFG}; use crate::rcc::{set_freqs, Clocks}; use crate::time::Hertz; -use crate::{peripherals, Unborrow}; +use crate::{peripherals, Peripheral}; /// HSI speed pub const HSI_FREQ: Hertz = Hertz(64_000_000); @@ -385,12 +385,12 @@ pub struct Mco<'d, T: McoInstance> { impl<'d, T: McoInstance> Mco<'d, T> { pub fn new( - _peri: impl Unborrow + 'd, - pin: impl Unborrow> + 'd, + _peri: impl Peripheral

+ 'd, + pin: impl Peripheral

> + 'd, source: impl McoSource, prescaler: McoClock, ) -> Self { - unborrow!(pin); + into_ref!(pin); critical_section::with(|_| unsafe { T::apply_clock_settings(source.into_raw(), prescaler.into_raw()); diff --git a/embassy-stm32/src/rng.rs b/embassy-stm32/src/rng.rs index 967fa71f..2b0ee713 100644 --- a/embassy-stm32/src/rng.rs +++ b/embassy-stm32/src/rng.rs @@ -3,11 +3,11 @@ use core::task::Poll; use embassy::waitqueue::AtomicWaker; -use embassy_hal_common::{unborrow, Unborrowed}; +use embassy_hal_common::{into_ref, PeripheralRef}; use futures::future::poll_fn; use rand_core::{CryptoRng, RngCore}; -use crate::{pac, peripherals, Unborrow}; +use crate::{pac, peripherals, Peripheral}; pub(crate) static RNG_WAKER: AtomicWaker = AtomicWaker::new(); @@ -18,14 +18,14 @@ pub enum Error { } pub struct Rng<'d, T: Instance> { - _inner: Unborrowed<'d, T>, + _inner: PeripheralRef<'d, T>, } impl<'d, T: Instance> Rng<'d, T> { - pub fn new(inner: impl Unborrow + 'd) -> Self { + pub fn new(inner: impl Peripheral

+ 'd) -> Self { T::enable(); T::reset(); - unborrow!(inner); + into_ref!(inner); let mut random = Self { _inner: inner }; random.reset(); random diff --git a/embassy-stm32/src/sdmmc/mod.rs b/embassy-stm32/src/sdmmc/mod.rs index fbb48263..b9dff8fa 100644 --- a/embassy-stm32/src/sdmmc/mod.rs +++ b/embassy-stm32/src/sdmmc/mod.rs @@ -5,7 +5,7 @@ use core::task::Poll; use embassy::waitqueue::AtomicWaker; use embassy_hal_common::drop::OnDrop; -use embassy_hal_common::{unborrow, Unborrowed}; +use embassy_hal_common::{into_ref, PeripheralRef}; use futures::future::poll_fn; use sdio_host::{BusWidth, CardCapacity, CardStatus, CurrentState, SDStatus, CID, CSD, OCR, SCR}; @@ -16,7 +16,7 @@ use crate::interrupt::{Interrupt, InterruptExt}; use crate::pac::sdmmc::Sdmmc as RegBlock; use crate::rcc::RccPeripheral; use crate::time::Hertz; -use crate::{peripherals, Unborrow}; +use crate::{peripherals, Peripheral}; /// The signalling scheme used on the SDMMC bus #[non_exhaustive] @@ -176,16 +176,16 @@ impl Default for Config { /// Sdmmc device pub struct Sdmmc<'d, T: Instance, Dma = NoDma> { - _peri: Unborrowed<'d, T>, - irq: Unborrowed<'d, T::Interrupt>, - dma: Unborrowed<'d, Dma>, + _peri: PeripheralRef<'d, T>, + irq: PeripheralRef<'d, T::Interrupt>, + dma: PeripheralRef<'d, Dma>, - clk: Unborrowed<'d, AnyPin>, - cmd: Unborrowed<'d, AnyPin>, - d0: Unborrowed<'d, AnyPin>, - d1: Option>, - d2: Option>, - d3: Option>, + clk: PeripheralRef<'d, AnyPin>, + cmd: PeripheralRef<'d, AnyPin>, + d0: PeripheralRef<'d, AnyPin>, + d1: Option>, + d2: Option>, + d3: Option>, config: Config, /// Current clock to card @@ -199,15 +199,15 @@ pub struct Sdmmc<'d, T: Instance, Dma = NoDma> { #[cfg(sdmmc_v1)] impl<'d, T: Instance, Dma: SdmmcDma> Sdmmc<'d, T, Dma> { pub fn new_1bit( - sdmmc: impl Unborrow + 'd, - irq: impl Unborrow + 'd, - dma: impl Unborrow + 'd, - clk: impl Unborrow> + 'd, - cmd: impl Unborrow> + 'd, - d0: impl Unborrow> + 'd, + sdmmc: impl Peripheral

+ 'd, + irq: impl Peripheral

+ 'd, + dma: impl Peripheral

+ 'd, + clk: impl Peripheral

> + 'd, + cmd: impl Peripheral

> + 'd, + d0: impl Peripheral

> + 'd, config: Config, ) -> Self { - unborrow!(clk, cmd, d0); + into_ref!(clk, cmd, d0); critical_section::with(|_| unsafe { clk.set_as_af_pull(clk.af_num(), AFType::OutputPushPull, Pull::None); @@ -234,18 +234,18 @@ impl<'d, T: Instance, Dma: SdmmcDma> Sdmmc<'d, T, Dma> { } pub fn new_4bit( - sdmmc: impl Unborrow + 'd, - irq: impl Unborrow + 'd, - dma: impl Unborrow + 'd, - clk: impl Unborrow> + 'd, - cmd: impl Unborrow> + 'd, - d0: impl Unborrow> + 'd, - d1: impl Unborrow> + 'd, - d2: impl Unborrow> + 'd, - d3: impl Unborrow> + 'd, + sdmmc: impl Peripheral

+ 'd, + irq: impl Peripheral

+ 'd, + dma: impl Peripheral

+ 'd, + clk: impl Peripheral

> + 'd, + cmd: impl Peripheral

> + 'd, + d0: impl Peripheral

> + 'd, + d1: impl Peripheral

> + 'd, + d2: impl Peripheral

> + 'd, + d3: impl Peripheral

> + 'd, config: Config, ) -> Self { - unborrow!(clk, cmd, d0, d1, d2, d3); + into_ref!(clk, cmd, d0, d1, d2, d3); critical_section::with(|_| unsafe { clk.set_as_af_pull(clk.af_num(), AFType::OutputPushPull, Pull::None); @@ -278,18 +278,18 @@ impl<'d, T: Instance, Dma: SdmmcDma> Sdmmc<'d, T, Dma> { } fn new_inner( - sdmmc: impl Unborrow + 'd, - irq: impl Unborrow + 'd, - dma: impl Unborrow + 'd, - clk: Unborrowed<'d, AnyPin>, - cmd: Unborrowed<'d, AnyPin>, - d0: Unborrowed<'d, AnyPin>, - d1: Option>, - d2: Option>, - d3: Option>, + sdmmc: impl Peripheral

+ 'd, + irq: impl Peripheral

+ 'd, + dma: impl Peripheral

+ 'd, + clk: PeripheralRef<'d, AnyPin>, + cmd: PeripheralRef<'d, AnyPin>, + d0: PeripheralRef<'d, AnyPin>, + d1: Option>, + d2: Option>, + d3: Option>, config: Config, ) -> Self { - unborrow!(sdmmc, irq, dma); + into_ref!(sdmmc, irq, dma); T::enable(); T::reset(); @@ -324,14 +324,14 @@ impl<'d, T: Instance, Dma: SdmmcDma> Sdmmc<'d, T, Dma> { #[cfg(sdmmc_v2)] impl<'d, T: Instance> Sdmmc<'d, T, NoDma> { pub fn new_1bit( - sdmmc: impl Unborrow + 'd, - irq: impl Unborrow + 'd, - clk: impl Unborrow> + 'd, - cmd: impl Unborrow> + 'd, - d0: impl Unborrow> + 'd, + sdmmc: impl Peripheral

+ 'd, + irq: impl Peripheral

+ 'd, + clk: impl Peripheral

> + 'd, + cmd: impl Peripheral

> + 'd, + d0: impl Peripheral

> + 'd, config: Config, ) -> Self { - unborrow!(clk, cmd, d0); + into_ref!(clk, cmd, d0); critical_section::with(|_| unsafe { clk.set_as_af_pull(clk.af_num(), AFType::OutputPushPull, Pull::None); @@ -357,17 +357,17 @@ impl<'d, T: Instance> Sdmmc<'d, T, NoDma> { } pub fn new_4bit( - sdmmc: impl Unborrow + 'd, - irq: impl Unborrow + 'd, - clk: impl Unborrow> + 'd, - cmd: impl Unborrow> + 'd, - d0: impl Unborrow> + 'd, - d1: impl Unborrow> + 'd, - d2: impl Unborrow> + 'd, - d3: impl Unborrow> + 'd, + sdmmc: impl Peripheral

+ 'd, + irq: impl Peripheral

+ 'd, + clk: impl Peripheral

> + 'd, + cmd: impl Peripheral

> + 'd, + d0: impl Peripheral

> + 'd, + d1: impl Peripheral

> + 'd, + d2: impl Peripheral

> + 'd, + d3: impl Peripheral

> + 'd, config: Config, ) -> Self { - unborrow!(clk, cmd, d0, d1, d2, d3); + into_ref!(clk, cmd, d0, d1, d2, d3); critical_section::with(|_| unsafe { clk.set_as_af_pull(clk.af_num(), AFType::OutputPushPull, Pull::None); @@ -399,17 +399,17 @@ impl<'d, T: Instance> Sdmmc<'d, T, NoDma> { } fn new_inner( - sdmmc: impl Unborrow + 'd, - irq: impl Unborrow + 'd, - clk: Unborrowed<'d, AnyPin>, - cmd: Unborrowed<'d, AnyPin>, - d0: Unborrowed<'d, AnyPin>, - d1: Option>, - d2: Option>, - d3: Option>, + sdmmc: impl Peripheral

+ 'd, + irq: impl Peripheral

+ 'd, + clk: PeripheralRef<'d, AnyPin>, + cmd: PeripheralRef<'d, AnyPin>, + d0: PeripheralRef<'d, AnyPin>, + d1: Option>, + d2: Option>, + d3: Option>, config: Config, ) -> Self { - unborrow!(sdmmc, irq); + into_ref!(sdmmc, irq); T::enable(); T::reset(); @@ -424,7 +424,7 @@ impl<'d, T: Instance> Sdmmc<'d, T, NoDma> { Self { _peri: sdmmc, irq, - dma: NoDma.unborrow(), + dma: NoDma.into_ref(), clk, cmd, diff --git a/embassy-stm32/src/spi/mod.rs b/embassy-stm32/src/spi/mod.rs index 595957b2..26fb392e 100644 --- a/embassy-stm32/src/spi/mod.rs +++ b/embassy-stm32/src/spi/mod.rs @@ -3,7 +3,7 @@ use core::ptr; use embassy_embedded_hal::SetConfig; -use embassy_hal_common::{unborrow, Unborrowed}; +use embassy_hal_common::{into_ref, PeripheralRef}; pub use embedded_hal_02::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3}; use futures::future::join; @@ -14,7 +14,7 @@ use crate::gpio::AnyPin; use crate::pac::spi::{regs, vals, Spi as Regs}; use crate::rcc::RccPeripheral; use crate::time::Hertz; -use crate::{peripherals, Unborrow}; +use crate::{peripherals, Peripheral}; #[derive(Debug)] #[cfg_attr(feature = "defmt", derive(defmt::Format))] @@ -72,27 +72,27 @@ impl Config { } pub struct Spi<'d, T: Instance, Tx, Rx> { - _peri: Unborrowed<'d, T>, - sck: Option>, - mosi: Option>, - miso: Option>, - txdma: Unborrowed<'d, Tx>, - rxdma: Unborrowed<'d, Rx>, + _peri: PeripheralRef<'d, T>, + sck: Option>, + mosi: Option>, + miso: Option>, + txdma: PeripheralRef<'d, Tx>, + rxdma: PeripheralRef<'d, Rx>, current_word_size: WordSize, } impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { pub fn new( - peri: impl Unborrow + 'd, - sck: impl Unborrow> + 'd, - mosi: impl Unborrow> + 'd, - miso: impl Unborrow> + 'd, - txdma: impl Unborrow + 'd, - rxdma: impl Unborrow + 'd, + peri: impl Peripheral

+ 'd, + sck: impl Peripheral

> + 'd, + mosi: impl Peripheral

> + 'd, + miso: impl Peripheral

> + 'd, + txdma: impl Peripheral

+ 'd, + rxdma: impl Peripheral

+ 'd, freq: Hertz, config: Config, ) -> Self { - unborrow!(peri, sck, mosi, miso); + into_ref!(peri, sck, mosi, miso); unsafe { sck.set_as_af(sck.af_num(), AFType::OutputPushPull); #[cfg(any(spi_v2, spi_v3, spi_v4))] @@ -118,15 +118,15 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { } pub fn new_rxonly( - peri: impl Unborrow + 'd, - sck: impl Unborrow> + 'd, - miso: impl Unborrow> + 'd, - txdma: impl Unborrow + 'd, // TODO remove - rxdma: impl Unborrow + 'd, + peri: impl Peripheral

+ 'd, + sck: impl Peripheral

> + 'd, + miso: impl Peripheral

> + 'd, + txdma: impl Peripheral

+ 'd, // TODO remove + rxdma: impl Peripheral

+ 'd, freq: Hertz, config: Config, ) -> Self { - unborrow!(sck, miso); + into_ref!(sck, miso); unsafe { sck.set_as_af(sck.af_num(), AFType::OutputPushPull); #[cfg(any(spi_v2, spi_v3, spi_v4))] @@ -149,15 +149,15 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { } pub fn new_txonly( - peri: impl Unborrow + 'd, - sck: impl Unborrow> + 'd, - mosi: impl Unborrow> + 'd, - txdma: impl Unborrow + 'd, - rxdma: impl Unborrow + 'd, // TODO remove + peri: impl Peripheral

+ 'd, + sck: impl Peripheral

> + 'd, + mosi: impl Peripheral

> + 'd, + txdma: impl Peripheral

+ 'd, + rxdma: impl Peripheral

+ 'd, // TODO remove freq: Hertz, config: Config, ) -> Self { - unborrow!(sck, mosi); + into_ref!(sck, mosi); unsafe { sck.set_as_af(sck.af_num(), AFType::OutputPushPull); #[cfg(any(spi_v2, spi_v3, spi_v4))] @@ -180,16 +180,16 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { } fn new_inner( - peri: impl Unborrow + 'd, - sck: Option>, - mosi: Option>, - miso: Option>, - txdma: impl Unborrow + 'd, - rxdma: impl Unborrow + 'd, + peri: impl Peripheral

+ 'd, + sck: Option>, + mosi: Option>, + miso: Option>, + txdma: impl Peripheral

+ 'd, + rxdma: impl Peripheral

+ 'd, freq: Hertz, config: Config, ) -> Self { - unborrow!(peri, txdma, rxdma); + into_ref!(peri, txdma, rxdma); let pclk = T::frequency(); let br = compute_baud_rate(pclk, freq.into()); @@ -994,7 +994,7 @@ pub trait Word: Copy + 'static + sealed::Word + Default + crate::dma::Word {} impl Word for u8 {} impl Word for u16 {} -pub trait Instance: Unborrow + sealed::Instance + RccPeripheral {} +pub trait Instance: Peripheral

+ sealed::Instance + RccPeripheral {} pin_trait!(SckPin, Instance); pin_trait!(MosiPin, Instance); pin_trait!(MisoPin, Instance); diff --git a/embassy-stm32/src/subghz/mod.rs b/embassy-stm32/src/subghz/mod.rs index ce513ec6..f02fc140 100644 --- a/embassy-stm32/src/subghz/mod.rs +++ b/embassy-stm32/src/subghz/mod.rs @@ -83,7 +83,7 @@ use crate::peripherals::SUBGHZSPI; use crate::rcc::sealed::RccPeripheral; use crate::spi::{BitOrder, Config as SpiConfig, MisoPin, MosiPin, SckPin, Spi, MODE_0}; use crate::time::Hertz; -use crate::{pac, Unborrow}; +use crate::{pac, Peripheral}; /// Passthrough for SPI errors (for now) pub type Error = crate::spi::Error; @@ -211,12 +211,12 @@ impl<'d, Tx, Rx> SubGhz<'d, Tx, Rx> { /// This will reset the radio and the SPI bus, and enable the peripheral /// clock. pub fn new( - peri: impl Unborrow + 'd, - sck: impl Unborrow> + 'd, - mosi: impl Unborrow> + 'd, - miso: impl Unborrow> + 'd, - txdma: impl Unborrow + 'd, - rxdma: impl Unborrow + 'd, + peri: impl Peripheral

+ 'd, + sck: impl Peripheral

> + 'd, + mosi: impl Peripheral

> + 'd, + miso: impl Peripheral

> + 'd, + txdma: impl Peripheral

+ 'd, + rxdma: impl Peripheral

+ 'd, ) -> Self { Self::pulse_radio_reset(); diff --git a/embassy-stm32/src/usart/buffered.rs b/embassy-stm32/src/usart/buffered.rs index c4703cff..4e47ef73 100644 --- a/embassy-stm32/src/usart/buffered.rs +++ b/embassy-stm32/src/usart/buffered.rs @@ -39,11 +39,11 @@ impl<'d, T: Instance> BufferedUart<'d, T> { pub fn new( state: &'d mut State<'d, T>, _uart: Uart<'d, T, NoDma, NoDma>, - irq: impl Unborrow + 'd, + irq: impl Peripheral

+ 'd, tx_buffer: &'d mut [u8], rx_buffer: &'d mut [u8], ) -> BufferedUart<'d, T> { - unborrow!(irq); + into_ref!(irq); let r = T::regs(); unsafe { diff --git a/embassy-stm32/src/usart/mod.rs b/embassy-stm32/src/usart/mod.rs index 6b12378d..ca75bab4 100644 --- a/embassy-stm32/src/usart/mod.rs +++ b/embassy-stm32/src/usart/mod.rs @@ -2,14 +2,14 @@ use core::marker::PhantomData; -use embassy_hal_common::{unborrow, Unborrowed}; +use embassy_hal_common::{into_ref, PeripheralRef}; use crate::dma::NoDma; use crate::gpio::sealed::AFType; use crate::interrupt::Interrupt; use crate::pac::usart::{regs, vals}; use crate::rcc::RccPeripheral; -use crate::{peripherals, Unborrow}; +use crate::{peripherals, Peripheral}; #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub enum DataBits { @@ -78,16 +78,16 @@ pub struct Uart<'d, T: Instance, TxDma = NoDma, RxDma = NoDma> { pub struct UartTx<'d, T: Instance, TxDma = NoDma> { phantom: PhantomData<&'d mut T>, - tx_dma: Unborrowed<'d, TxDma>, + tx_dma: PeripheralRef<'d, TxDma>, } pub struct UartRx<'d, T: Instance, RxDma = NoDma> { phantom: PhantomData<&'d mut T>, - rx_dma: Unborrowed<'d, RxDma>, + rx_dma: PeripheralRef<'d, RxDma>, } impl<'d, T: Instance, TxDma> UartTx<'d, T, TxDma> { - fn new(tx_dma: Unborrowed<'d, TxDma>) -> Self { + fn new(tx_dma: PeripheralRef<'d, TxDma>) -> Self { Self { tx_dma, phantom: PhantomData, @@ -133,7 +133,7 @@ impl<'d, T: Instance, TxDma> UartTx<'d, T, TxDma> { } impl<'d, T: Instance, RxDma> UartRx<'d, T, RxDma> { - fn new(rx_dma: Unborrowed<'d, RxDma>) -> Self { + fn new(rx_dma: PeripheralRef<'d, RxDma>) -> Self { Self { rx_dma, phantom: PhantomData, @@ -189,14 +189,14 @@ impl<'d, T: Instance, RxDma> UartRx<'d, T, RxDma> { impl<'d, T: Instance, TxDma, RxDma> Uart<'d, T, TxDma, RxDma> { pub fn new( - _inner: impl Unborrow + 'd, - rx: impl Unborrow> + 'd, - tx: impl Unborrow> + 'd, - tx_dma: impl Unborrow + 'd, - rx_dma: impl Unborrow + 'd, + _inner: impl Peripheral

+ 'd, + rx: impl Peripheral

> + 'd, + tx: impl Peripheral

> + 'd, + tx_dma: impl Peripheral

+ 'd, + rx_dma: impl Peripheral

+ 'd, config: Config, ) -> Self { - unborrow!(_inner, rx, tx, tx_dma, rx_dma); + into_ref!(_inner, rx, tx, tx_dma, rx_dma); T::enable(); T::reset(); diff --git a/embassy-stm32/src/usb/usb.rs b/embassy-stm32/src/usb/usb.rs index 4633ff00..11ef7595 100644 --- a/embassy-stm32/src/usb/usb.rs +++ b/embassy-stm32/src/usb/usb.rs @@ -7,7 +7,7 @@ use core::task::Poll; use atomic_polyfill::{AtomicBool, AtomicU8}; use embassy::time::{block_for, Duration}; use embassy::waitqueue::AtomicWaker; -use embassy_hal_common::unborrow; +use embassy_hal_common::into_ref; use embassy_usb::driver::{self, EndpointAllocError, EndpointError, Event, Unsupported}; use embassy_usb::types::{EndpointAddress, EndpointInfo, EndpointType, UsbDirection}; use futures::future::poll_fn; @@ -20,7 +20,7 @@ use crate::gpio::sealed::AFType; use crate::interrupt::InterruptExt; use crate::pac::usb::regs; use crate::rcc::sealed::RccPeripheral; -use crate::{pac, Unborrow}; +use crate::{pac, Peripheral}; const EP_COUNT: usize = 8; @@ -125,12 +125,12 @@ pub struct Driver<'d, T: Instance> { impl<'d, T: Instance> Driver<'d, T> { pub fn new( - _usb: impl Unborrow + 'd, - irq: impl Unborrow + 'd, - dp: impl Unborrow> + 'd, - dm: impl Unborrow> + 'd, + _usb: impl Peripheral

+ 'd, + irq: impl Peripheral

+ 'd, + dp: impl Peripheral

> + 'd, + dm: impl Peripheral

> + 'd, ) -> Self { - unborrow!(irq, dp, dm); + into_ref!(irq, dp, dm); irq.set_handler(Self::on_interrupt); irq.unpend(); irq.enable(); diff --git a/embassy-stm32/src/usb_otg.rs b/embassy-stm32/src/usb_otg.rs index 58175413..f7faf12a 100644 --- a/embassy-stm32/src/usb_otg.rs +++ b/embassy-stm32/src/usb_otg.rs @@ -1,14 +1,14 @@ use core::marker::PhantomData; -use embassy_hal_common::unborrow; +use embassy_hal_common::into_ref; use crate::gpio::sealed::AFType; use crate::rcc::RccPeripheral; -use crate::{peripherals, Unborrow}; +use crate::{peripherals, Peripheral}; macro_rules! config_ulpi_pins { ($($pin:ident),*) => { - unborrow!($($pin),*); + into_ref!($($pin),*); // NOTE(unsafe) Exclusive access to the registers critical_section::with(|_| unsafe { $( @@ -43,11 +43,11 @@ pub struct UsbOtg<'d, T: Instance> { impl<'d, T: Instance> UsbOtg<'d, T> { /// Initializes USB OTG peripheral with internal Full-Speed PHY pub fn new_fs( - _peri: impl Unborrow + 'd, - dp: impl Unborrow> + 'd, - dm: impl Unborrow> + 'd, + _peri: impl Peripheral

+ 'd, + dp: impl Peripheral

> + 'd, + dm: impl Peripheral

> + 'd, ) -> Self { - unborrow!(dp, dm); + into_ref!(dp, dm); unsafe { dp.set_as_af(dp.af_num(), AFType::OutputPushPull); @@ -62,19 +62,19 @@ impl<'d, T: Instance> UsbOtg<'d, T> { /// Initializes USB OTG peripheral with external High-Speed PHY pub fn new_hs_ulpi( - _peri: impl Unborrow + 'd, - ulpi_clk: impl Unborrow> + 'd, - ulpi_dir: impl Unborrow> + 'd, - ulpi_nxt: impl Unborrow> + 'd, - ulpi_stp: impl Unborrow> + 'd, - ulpi_d0: impl Unborrow> + 'd, - ulpi_d1: impl Unborrow> + 'd, - ulpi_d2: impl Unborrow> + 'd, - ulpi_d3: impl Unborrow> + 'd, - ulpi_d4: impl Unborrow> + 'd, - ulpi_d5: impl Unborrow> + 'd, - ulpi_d6: impl Unborrow> + 'd, - ulpi_d7: impl Unborrow> + 'd, + _peri: impl Peripheral

+ 'd, + ulpi_clk: impl Peripheral

> + 'd, + ulpi_dir: impl Peripheral

> + 'd, + ulpi_nxt: impl Peripheral

> + 'd, + ulpi_stp: impl Peripheral

> + 'd, + ulpi_d0: impl Peripheral

> + 'd, + ulpi_d1: impl Peripheral

> + 'd, + ulpi_d2: impl Peripheral

> + 'd, + ulpi_d3: impl Peripheral

> + 'd, + ulpi_d4: impl Peripheral

> + 'd, + ulpi_d5: impl Peripheral

> + 'd, + ulpi_d6: impl Peripheral

> + 'd, + ulpi_d7: impl Peripheral

> + 'd, ) -> Self { config_ulpi_pins!( ulpi_clk, ulpi_dir, ulpi_nxt, ulpi_stp, ulpi_d0, ulpi_d1, ulpi_d2, ulpi_d3, ulpi_d4, ulpi_d5, ulpi_d6, diff --git a/embassy-stm32/src/wdg/mod.rs b/embassy-stm32/src/wdg/mod.rs index c4b2609b..85176eef 100644 --- a/embassy-stm32/src/wdg/mod.rs +++ b/embassy-stm32/src/wdg/mod.rs @@ -1,6 +1,6 @@ use core::marker::PhantomData; -use embassy_hal_common::{unborrow, Unborrow}; +use embassy_hal_common::{into_ref, Peripheral}; use stm32_metapac::iwdg::vals::{Key, Pr}; use crate::rcc::LSI_FREQ; @@ -27,8 +27,8 @@ impl<'d, T: Instance> IndependentWatchdog<'d, T> { /// /// [Self] has to be started with [Self::unleash()]. /// Once timer expires, MCU will be reset. To prevent this, timer must be reloaded by repeatedly calling [Self::pet()] within timeout interval. - pub fn new(_instance: impl Unborrow + 'd, timeout_us: u32) -> Self { - unborrow!(_instance); + pub fn new(_instance: impl Peripheral

+ 'd, timeout_us: u32) -> Self { + into_ref!(_instance); // Find lowest prescaler value, which makes watchdog period longer or equal to timeout. // This iterates from 4 (2^2) to 256 (2^8). diff --git a/examples/stm32h7/src/bin/low_level_timer_api.rs b/examples/stm32h7/src/bin/low_level_timer_api.rs index e54f1bb6..d7c6da5b 100644 --- a/examples/stm32h7/src/bin/low_level_timer_api.rs +++ b/examples/stm32h7/src/bin/low_level_timer_api.rs @@ -9,7 +9,7 @@ use embassy_stm32::gpio::low_level::AFType; use embassy_stm32::gpio::Speed; use embassy_stm32::pwm::*; use embassy_stm32::time::{khz, mhz, Hertz}; -use embassy_stm32::{unborrow, Config, Peripherals, Unborrow, Unborrowed}; +use embassy_stm32::{into_ref, Config, Peripheral, PeripheralRef, Peripherals}; use {defmt_rtt as _, panic_probe as _}; pub fn config() -> Config { @@ -47,19 +47,19 @@ async fn main(_spawner: Spawner, p: Peripherals) { } } pub struct SimplePwm32<'d, T: CaptureCompare32bitInstance> { - inner: Unborrowed<'d, T>, + inner: PeripheralRef<'d, T>, } impl<'d, T: CaptureCompare32bitInstance> SimplePwm32<'d, T> { pub fn new( - tim: impl Unborrow + 'd, - ch1: impl Unborrow> + 'd, - ch2: impl Unborrow> + 'd, - ch3: impl Unborrow> + 'd, - ch4: impl Unborrow> + 'd, + tim: impl Peripheral

+ 'd, + ch1: impl Peripheral

> + 'd, + ch2: impl Peripheral

> + 'd, + ch3: impl Peripheral

> + 'd, + ch4: impl Peripheral

> + 'd, freq: Hertz, ) -> Self { - unborrow!(tim, ch1, ch2, ch3, ch4); + into_ref!(tim, ch1, ch2, ch3, ch4); T::enable(); ::reset();