From 6de02bb23e270141885e24719dc8fbca0bb97feb Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Sat, 12 Feb 2022 01:04:01 +0100 Subject: nrf: remove OptionalPin --- embassy-nrf/src/buffered_uarte.rs | 24 ++--- embassy-nrf/src/gpio.rs | 54 +--------- embassy-nrf/src/pwm.rs | 202 ++++++++++++++++++++++++++++---------- embassy-nrf/src/spim.rs | 61 ++++++++++-- embassy-nrf/src/uarte.rs | 120 ++++++++++++++++++---- 5 files changed, 317 insertions(+), 144 deletions(-) (limited to 'embassy-nrf/src') diff --git a/embassy-nrf/src/buffered_uarte.rs b/embassy-nrf/src/buffered_uarte.rs index 2880c84f6..b49c12788 100644 --- a/embassy-nrf/src/buffered_uarte.rs +++ b/embassy-nrf/src/buffered_uarte.rs @@ -27,8 +27,7 @@ use embassy_hal_common::peripheral::{PeripheralMutex, PeripheralState, StateStor use embassy_hal_common::ring_buffer::RingBuffer; use embassy_hal_common::{low_power_wait_until, unborrow}; -use crate::gpio::sealed::Pin as _; -use crate::gpio::{OptionalPin as GpioOptionalPin, Pin as GpioPin}; +use crate::gpio::Pin as GpioPin; use crate::pac; use crate::ppi::{AnyConfigurableChannel, ConfigurableChannel, Event, Ppi, Task}; use crate::timer::Instance as TimerInstance; @@ -89,8 +88,8 @@ impl<'d, U: UarteInstance, T: TimerInstance> BufferedUarte<'d, U, T> { irq: impl Unborrow + 'd, rxd: impl Unborrow + 'd, txd: impl Unborrow + 'd, - cts: impl Unborrow + 'd, - rts: impl Unborrow + 'd, + cts: impl Unborrow + 'd, + rts: impl Unborrow + 'd, config: Config, rx_buffer: &'d mut [u8], tx_buffer: &'d mut [u8], @@ -108,28 +107,19 @@ impl<'d, U: UarteInstance, T: TimerInstance> BufferedUarte<'d, U, T> { txd.conf().write(|w| w.dir().output().drive().h0h1()); r.psel.txd.write(|w| unsafe { w.bits(txd.psel_bits()) }); - if let Some(pin) = rts.pin_mut() { - pin.set_high(); - pin.conf().write(|w| w.dir().output().drive().h0h1()); - } + cts.conf().write(|w| w.input().connect().drive().h0h1()); r.psel.cts.write(|w| unsafe { w.bits(cts.psel_bits()) }); - if let Some(pin) = cts.pin_mut() { - pin.conf().write(|w| w.input().connect().drive().h0h1()); - } + rts.set_high(); + rts.conf().write(|w| w.dir().output().drive().h0h1()); r.psel.rts.write(|w| unsafe { w.bits(rts.psel_bits()) }); r.baudrate.write(|w| w.baudrate().variant(config.baudrate)); r.config.write(|w| w.parity().variant(config.parity)); // Configure - let hardware_flow_control = match (rts.pin().is_some(), cts.pin().is_some()) { - (false, false) => false, - (true, true) => true, - _ => panic!("RTS and CTS pins must be either both set or none set."), - }; r.config.write(|w| { - w.hwfc().bit(hardware_flow_control); + w.hwfc().bit(true); w.parity().variant(config.parity); w }); diff --git a/embassy-nrf/src/gpio.rs b/embassy-nrf/src/gpio.rs index 3f204d564..09202e2f1 100644 --- a/embassy-nrf/src/gpio.rs +++ b/embassy-nrf/src/gpio.rs @@ -287,8 +287,6 @@ pub(crate) mod sealed { unsafe { self.block().outclr.write(|w| w.bits(1u32 << self._pin())) } } } - - pub trait OptionalPin {} } pub trait Pin: Unborrow + sealed::Pin + Sized + 'static { @@ -346,59 +344,17 @@ impl sealed::Pin for AnyPin { // ==================== -pub trait OptionalPin: Unborrow + sealed::OptionalPin + Sized { - type Pin: Pin; - fn pin(&self) -> Option<&Self::Pin>; - fn pin_mut(&mut self) -> Option<&mut Self::Pin>; - - #[inline] - fn psel_bits(&self) -> u32 { - self.pin().map_or(1u32 << 31, Pin::psel_bits) - } - - /// Convert from concrete pin type PX_XX to type erased `Option`. - #[inline] - fn degrade_optional(mut self) -> Option { - self.pin_mut() - .map(|pin| unsafe { core::ptr::read(pin) }.degrade()) - } +pub(crate) trait PselBits { + fn psel_bits(&self) -> u32; } -impl sealed::OptionalPin for T {} -impl OptionalPin for T { - type Pin = T; - - #[inline] - fn pin(&self) -> Option<&T> { - Some(self) - } - +impl PselBits for Option { #[inline] - fn pin_mut(&mut self) -> Option<&mut T> { - Some(self) - } -} - -#[derive(Clone, Copy, Debug)] -pub struct NoPin; -unsafe_impl_unborrow!(NoPin); -impl sealed::OptionalPin for NoPin {} -impl OptionalPin for NoPin { - type Pin = AnyPin; - - #[inline] - fn pin(&self) -> Option<&AnyPin> { - None - } - - #[inline] - fn pin_mut(&mut self) -> Option<&mut AnyPin> { - None + fn psel_bits(&self) -> u32 { + self.as_ref().map_or(1u32 << 31, Pin::psel_bits) } } -// ==================== - pub(crate) fn deconfigure_pin(psel_bits: u32) { if psel_bits & 0x8000_0000 != 0 { return; diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index 01b1f48d9..5ac52f172 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -6,7 +6,7 @@ use embassy::util::Unborrow; use embassy_hal_common::unborrow; use crate::gpio::sealed::Pin as _; -use crate::gpio::{AnyPin, OptionalPin as GpioOptionalPin}; +use crate::gpio::{AnyPin, Pin as GpioPin, PselBits}; use crate::interrupt::Interrupt; use crate::pac; use crate::ppi::{Event, Task}; @@ -48,47 +48,104 @@ pub enum Error { const MAX_SEQUENCE_LEN: usize = 32767; impl<'d, T: Instance> SequencePwm<'d, T> { - /// Creates the interface to a `SequencePwm`. - /// - /// Must be started by calling `start` - /// - /// # Safety - /// - /// The returned API is safe unless you use `mem::forget` (or similar safe - /// mechanisms) on stack allocated buffers which which have been passed to - /// [`new()`](SequencePwm::new). + /// Create a new 1-channel PWM #[allow(unused_unsafe)] - pub fn new( - _pwm: impl Unborrow + 'd, - ch0: impl Unborrow + 'd, - ch1: impl Unborrow + 'd, - ch2: impl Unborrow + 'd, - ch3: impl Unborrow + 'd, + pub fn new_1ch( + pwm: impl Unborrow + 'd, + ch0: impl Unborrow + 'd, + config: Config, + ) -> Result { + unborrow!(ch0); + Self::new_inner(pwm, Some(ch0.degrade()), 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, + config: Config, + ) -> Result { + unborrow!(ch0, ch1); + Self::new_inner( + pwm, + Some(ch0.degrade()), + Some(ch1.degrade()), + 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, + config: Config, + ) -> Result { + unborrow!(ch0, ch1, ch2); + Self::new_inner( + pwm, + Some(ch0.degrade()), + Some(ch1.degrade()), + Some(ch2.degrade()), + 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, config: Config, ) -> Result { unborrow!(ch0, ch1, ch2, ch3); + Self::new_inner( + pwm, + Some(ch0.degrade()), + Some(ch1.degrade()), + Some(ch2.degrade()), + Some(ch3.degrade()), + config, + ) + } + fn new_inner( + _pwm: impl Unborrow + 'd, + ch0: Option, + ch1: Option, + ch2: Option, + ch3: Option, + config: Config, + ) -> Result { let r = T::regs(); - if let Some(pin) = ch0.pin_mut() { + if let Some(pin) = &ch0 { pin.set_low(); pin.conf().write(|w| w.dir().output()); } - if let Some(pin) = ch1.pin_mut() { + if let Some(pin) = &ch1 { pin.set_low(); pin.conf().write(|w| w.dir().output()); } - if let Some(pin) = ch2.pin_mut() { + if let Some(pin) = &ch2 { pin.set_low(); pin.conf().write(|w| w.dir().output()); } - if let Some(pin) = ch3.pin_mut() { + if let Some(pin) = &ch3 { pin.set_low(); pin.conf().write(|w| w.dir().output()); } - // if NoPin provided writes disconnected (top bit 1) 0x80000000 else - // writes pin number ex 13 (0x0D) which is connected (top bit 0) r.psel.out[0].write(|w| unsafe { w.bits(ch0.psel_bits()) }); r.psel.out[1].write(|w| unsafe { w.bits(ch1.psel_bits()) }); r.psel.out[2].write(|w| unsafe { w.bits(ch2.psel_bits()) }); @@ -121,10 +178,10 @@ impl<'d, T: Instance> SequencePwm<'d, T> { Ok(Self { phantom: PhantomData, - ch0: ch0.degrade_optional(), - ch1: ch1.degrade_optional(), - ch2: ch2.degrade_optional(), - ch3: ch3.degrade_optional(), + ch0, + ch1, + ch2, + ch3, }) } @@ -545,41 +602,86 @@ pub enum CounterMode { } impl<'d, T: Instance> SimplePwm<'d, T> { - /// Creates the interface to a `SimplePwm` - /// - /// Enables the peripheral, defaults the freq to 1Mhz, max_duty 1000, duty - /// 0, up mode, and pins low. Must be started by calling `set_duty` - /// - /// # Safety - /// - /// The returned API is safe unless you use `mem::forget` (or similar safe - /// mechanisms) on stack allocated buffers which which have been passed to - /// [`new()`](SimplePwm::new). + /// Create a new 1-channel PWM #[allow(unused_unsafe)] - pub fn new( - _pwm: impl Unborrow + 'd, - ch0: impl Unborrow + 'd, - ch1: impl Unborrow + 'd, - ch2: impl Unborrow + 'd, - ch3: impl Unborrow + 'd, + pub fn new_1ch( + pwm: impl Unborrow + 'd, + ch0: impl Unborrow + 'd, + ) -> Self { + unborrow!(ch0); + Self::new_inner(pwm, Some(ch0.degrade()), None, None, None) + } + + /// 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, + ) -> Self { + unborrow!(ch0, ch1); + Self::new_inner(pwm, Some(ch0.degrade()), Some(ch1.degrade()), 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, + ) -> Self { + unborrow!(ch0, ch1, ch2); + Self::new_inner( + pwm, + Some(ch0.degrade()), + Some(ch1.degrade()), + Some(ch2.degrade()), + None, + ) + } + + /// 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, ) -> Self { unborrow!(ch0, ch1, ch2, ch3); + Self::new_inner( + pwm, + Some(ch0.degrade()), + Some(ch1.degrade()), + Some(ch2.degrade()), + Some(ch3.degrade()), + ) + } + fn new_inner( + _pwm: impl Unborrow + 'd, + ch0: Option, + ch1: Option, + ch2: Option, + ch3: Option, + ) -> Self { let r = T::regs(); - if let Some(pin) = ch0.pin_mut() { + if let Some(pin) = &ch0 { pin.set_low(); pin.conf().write(|w| w.dir().output()); } - if let Some(pin) = ch1.pin_mut() { + if let Some(pin) = &ch1 { pin.set_low(); pin.conf().write(|w| w.dir().output()); } - if let Some(pin) = ch2.pin_mut() { + if let Some(pin) = &ch2 { pin.set_low(); pin.conf().write(|w| w.dir().output()); } - if let Some(pin) = ch3.pin_mut() { + if let Some(pin) = &ch3 { pin.set_low(); pin.conf().write(|w| w.dir().output()); } @@ -593,10 +695,10 @@ impl<'d, T: Instance> SimplePwm<'d, T> { let pwm = Self { phantom: PhantomData, - ch0: ch0.degrade_optional(), - ch1: ch1.degrade_optional(), - ch2: ch2.degrade_optional(), - ch3: ch3.degrade_optional(), + ch0, + ch1, + ch2, + ch3, duty: [0; 4], }; diff --git a/embassy-nrf/src/spim.rs b/embassy-nrf/src/spim.rs index cd43b26e6..976a546ce 100644 --- a/embassy-nrf/src/spim.rs +++ b/embassy-nrf/src/spim.rs @@ -8,9 +8,9 @@ use embassy::util::Unborrow; use embassy_hal_common::unborrow; use futures::future::poll_fn; -use crate::gpio; use crate::gpio::sealed::Pin as _; -use crate::gpio::{OptionalPin, Pin as GpioPin}; +use crate::gpio::{self, AnyPin}; +use crate::gpio::{Pin as GpioPin, PselBits}; use crate::interrupt::Interrupt; use crate::util::{slice_ptr_parts, slice_ptr_parts_mut}; use crate::{pac, util::slice_in_ram_or}; @@ -51,36 +51,77 @@ impl Default for Config { impl<'d, T: Instance> Spim<'d, T> { pub fn new( - _spim: impl Unborrow + 'd, + spim: impl Unborrow + 'd, + irq: impl Unborrow + 'd, + sck: impl Unborrow + 'd, + miso: impl Unborrow + 'd, + mosi: impl Unborrow + 'd, + config: Config, + ) -> Self { + unborrow!(sck, miso, mosi); + Self::new_inner( + spim, + irq, + sck.degrade(), + Some(miso.degrade()), + Some(mosi.degrade()), + config, + ) + } + + pub fn new_txonly( + spim: impl Unborrow + 'd, irq: impl Unborrow + 'd, sck: impl Unborrow + 'd, - miso: impl Unborrow + 'd, - mosi: impl Unborrow + 'd, + mosi: impl Unborrow + 'd, + config: Config, + ) -> Self { + unborrow!(sck, mosi); + Self::new_inner(spim, irq, sck.degrade(), None, Some(mosi.degrade()), config) + } + + pub fn new_rxonly( + spim: impl Unborrow + 'd, + irq: impl Unborrow + 'd, + sck: impl Unborrow + 'd, + miso: impl Unborrow + 'd, + config: Config, + ) -> Self { + unborrow!(sck, miso); + Self::new_inner(spim, irq, sck.degrade(), Some(miso.degrade()), None, config) + } + + fn new_inner( + _spim: impl Unborrow + 'd, + irq: impl Unborrow + 'd, + sck: AnyPin, + miso: Option, + mosi: Option, config: Config, ) -> Self { - unborrow!(irq, sck, miso, mosi); + unborrow!(irq); let r = T::regs(); // Configure pins sck.conf().write(|w| w.dir().output().drive().h0h1()); - if let Some(mosi) = mosi.pin_mut() { + if let Some(mosi) = &mosi { mosi.conf().write(|w| w.dir().output().drive().h0h1()); } - if let Some(miso) = miso.pin_mut() { + if let Some(miso) = &miso { miso.conf().write(|w| w.input().connect().drive().h0h1()); } match config.mode.polarity { Polarity::IdleHigh => { sck.set_high(); - if let Some(mosi) = mosi.pin_mut() { + if let Some(mosi) = &mosi { mosi.set_high(); } } Polarity::IdleLow => { sck.set_low(); - if let Some(mosi) = mosi.pin_mut() { + if let Some(mosi) = &mosi { mosi.set_low(); } } diff --git a/embassy-nrf/src/uarte.rs b/embassy-nrf/src/uarte.rs index b10e55a05..a1c47cff5 100644 --- a/embassy-nrf/src/uarte.rs +++ b/embassy-nrf/src/uarte.rs @@ -24,7 +24,7 @@ use futures::future::poll_fn; use crate::chip::EASY_DMA_SIZE; use crate::gpio::sealed::Pin as _; -use crate::gpio::{self, OptionalPin as GpioOptionalPin, Pin as GpioPin}; +use crate::gpio::{self, AnyPin, Pin as GpioPin, PselBits}; use crate::interrupt::Interrupt; use crate::pac; use crate::ppi::{AnyConfigurableChannel, ConfigurableChannel, Event, Ppi, Task}; @@ -80,18 +80,50 @@ pub struct UarteRx<'d, T: Instance> { } impl<'d, T: Instance> Uarte<'d, T> { - /// Creates the interface to a UARTE instance. - /// Sets the baud rate, parity and assigns the pins to the UARTE peripheral. + /// Create a new UARTE without hardware flow control pub fn new( - _uarte: impl Unborrow + 'd, + uarte: impl Unborrow + 'd, irq: impl Unborrow + 'd, rxd: impl Unborrow + 'd, txd: impl Unborrow + 'd, - cts: impl Unborrow + 'd, - rts: impl Unborrow + 'd, config: Config, ) -> Self { - unborrow!(irq, rxd, txd, cts, rts); + unborrow!(rxd, txd); + Self::new_inner(uarte, irq, rxd.degrade(), txd.degrade(), 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, + config: Config, + ) -> Self { + unborrow!(rxd, txd, cts, rts); + Self::new_inner( + uarte, + irq, + rxd.degrade(), + txd.degrade(), + Some(cts.degrade()), + Some(rts.degrade()), + config, + ) + } + + fn new_inner( + _uarte: impl Unborrow + 'd, + irq: impl Unborrow + 'd, + rxd: AnyPin, + txd: AnyPin, + cts: Option, + rts: Option, + config: Config, + ) -> Self { + unborrow!(irq); let r = T::regs(); @@ -102,19 +134,19 @@ impl<'d, T: Instance> Uarte<'d, T> { txd.conf().write(|w| w.dir().output().drive().h0h1()); r.psel.txd.write(|w| unsafe { w.bits(txd.psel_bits()) }); - if let Some(pin) = rts.pin_mut() { - pin.set_high(); - pin.conf().write(|w| w.dir().output().drive().h0h1()); + if let Some(pin) = &cts { + pin.conf().write(|w| w.input().connect().drive().h0h1()); } r.psel.cts.write(|w| unsafe { w.bits(cts.psel_bits()) }); - if let Some(pin) = cts.pin_mut() { - pin.conf().write(|w| w.input().connect().drive().h0h1()); + if let Some(pin) = &rts { + pin.set_high(); + pin.conf().write(|w| w.dir().output().drive().h0h1()); } r.psel.rts.write(|w| unsafe { w.bits(rts.psel_bits()) }); // Configure - let hardware_flow_control = match (rts.pin().is_some(), cts.pin().is_some()) { + let hardware_flow_control = match (rts.is_some(), cts.is_some()) { (false, false) => false, (true, true) => true, _ => panic!("RTS and CTS pins must be either both set or none set."), @@ -491,8 +523,7 @@ pub struct UarteWithIdle<'d, U: Instance, T: TimerInstance> { } impl<'d, U: Instance, T: TimerInstance> UarteWithIdle<'d, U, T> { - /// Creates the interface to a UARTE instance. - /// Sets the baud rate, parity and assigns the pins to the UARTE peripheral. + /// Create a new UARTE without hardware flow control pub fn new( uarte: impl Unborrow + 'd, timer: impl Unborrow + 'd, @@ -501,12 +532,65 @@ impl<'d, U: Instance, T: TimerInstance> UarteWithIdle<'d, U, T> { irq: impl Unborrow + 'd, rxd: impl Unborrow + 'd, txd: impl Unborrow + 'd, - cts: impl Unborrow + 'd, - rts: impl Unborrow + 'd, + config: Config, + ) -> Self { + unborrow!(rxd, txd); + Self::new_inner( + uarte, + timer, + ppi_ch1, + ppi_ch2, + irq, + rxd.degrade(), + txd.degrade(), + 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, + config: Config, + ) -> Self { + unborrow!(rxd, txd, cts, rts); + Self::new_inner( + uarte, + timer, + ppi_ch1, + ppi_ch2, + irq, + rxd.degrade(), + txd.degrade(), + Some(cts.degrade()), + Some(rts.degrade()), + config, + ) + } + + 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: AnyPin, + txd: AnyPin, + cts: Option, + rts: Option, config: Config, ) -> Self { let baudrate = config.baudrate; - let uarte = Uarte::new(uarte, irq, rxd, txd, cts, rts, config); + let uarte = Uarte::new_inner(uarte, irq, rxd, txd, cts, rts, config); let mut timer = Timer::new(timer); unborrow!(ppi_ch1, ppi_ch2); -- cgit