rp: remove OptionalPin

This commit is contained in:
Dario Nieuwenhuis 2022-02-12 01:34:41 +01:00
parent 4a75475cfc
commit 0c9b1abb67
4 changed files with 65 additions and 79 deletions

View file

@ -224,8 +224,6 @@ pub(crate) mod sealed {
SIO.gpio_in(self.bank() as _) SIO.gpio_in(self.bank() as _)
} }
} }
pub trait OptionalPin {}
} }
pub trait Pin: Unborrow<Target = Self> + sealed::Pin { pub trait Pin: Unborrow<Target = Self> + sealed::Pin {
@ -250,54 +248,6 @@ impl sealed::Pin for AnyPin {
// ========================== // ==========================
pub trait OptionalPin: Unborrow<Target = Self> + sealed::OptionalPin + Sized {
type Pin: Pin;
fn pin(&self) -> Option<&Self::Pin>;
fn pin_mut(&mut self) -> Option<&mut Self::Pin>;
/// Convert from concrete pin type PIN_XX to type erased `Option<AnyPin>`.
#[inline]
fn degrade_optional(mut self) -> Option<AnyPin> {
self.pin_mut()
.map(|pin| unsafe { core::ptr::read(pin) }.degrade())
}
}
impl<T: Pin> sealed::OptionalPin for T {}
impl<T: Pin> OptionalPin for T {
type Pin = T;
#[inline]
fn pin(&self) -> Option<&T> {
Some(self)
}
#[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
}
}
// ==========================
macro_rules! impl_pin { macro_rules! impl_pin {
($name:ident, $bank:expr, $pin_num:expr) => { ($name:ident, $bank:expr, $pin_num:expr) => {
impl Pin for peripherals::$name {} impl Pin for peripherals::$name {}

View file

@ -6,7 +6,7 @@ use embedded_hal::blocking::spi as eh;
use embedded_hal::spi as ehnb; use embedded_hal::spi as ehnb;
use crate::gpio::sealed::Pin as _; use crate::gpio::sealed::Pin as _;
use crate::gpio::{NoPin, OptionalPin}; use crate::gpio::{AnyPin, Pin as GpioPin};
use crate::{pac, peripherals}; use crate::{pac, peripherals};
pub use ehnb::{Phase, Polarity}; pub use ehnb::{Phase, Polarity};
@ -66,10 +66,62 @@ impl<'d, T: Instance> Spi<'d, T> {
clk: impl Unborrow<Target = impl ClkPin<T>> + 'd, clk: impl Unborrow<Target = impl ClkPin<T>> + 'd,
mosi: impl Unborrow<Target = impl MosiPin<T>> + 'd, mosi: impl Unborrow<Target = impl MosiPin<T>> + 'd,
miso: impl Unborrow<Target = impl MisoPin<T>> + 'd, miso: impl Unborrow<Target = impl MisoPin<T>> + 'd,
cs: impl Unborrow<Target = impl CsPin<T>> + 'd,
config: Config, config: Config,
) -> Self { ) -> Self {
unborrow!(inner, clk, mosi, miso, cs); unborrow!(clk, mosi, miso);
Self::new_inner(
inner,
Some(clk.degrade()),
Some(mosi.degrade()),
Some(miso.degrade()),
None,
config,
)
}
pub fn new_txonly(
inner: impl Unborrow<Target = T> + 'd,
clk: impl Unborrow<Target = impl ClkPin<T>> + 'd,
mosi: impl Unborrow<Target = impl MosiPin<T>> + 'd,
config: Config,
) -> Self {
unborrow!(clk, mosi);
Self::new_inner(
inner,
Some(clk.degrade()),
Some(mosi.degrade()),
None,
None,
config,
)
}
pub fn new_rxonly(
inner: impl Unborrow<Target = T> + 'd,
clk: impl Unborrow<Target = impl ClkPin<T>> + 'd,
miso: impl Unborrow<Target = impl MisoPin<T>> + 'd,
config: Config,
) -> Self {
unborrow!(clk, miso);
Self::new_inner(
inner,
Some(clk.degrade()),
None,
Some(miso.degrade()),
None,
config,
)
}
fn new_inner(
inner: impl Unborrow<Target = T> + 'd,
clk: Option<AnyPin>,
mosi: Option<AnyPin>,
miso: Option<AnyPin>,
cs: Option<AnyPin>,
config: Config,
) -> Self {
unborrow!(inner);
unsafe { unsafe {
let p = inner.regs(); let p = inner.regs();
@ -86,16 +138,16 @@ impl<'d, T: Instance> Spi<'d, T> {
w.set_sse(true); // enable w.set_sse(true); // enable
}); });
if let Some(pin) = clk.pin_mut() { if let Some(pin) = &clk {
pin.io().ctrl().write(|w| w.set_funcsel(1)); pin.io().ctrl().write(|w| w.set_funcsel(1));
} }
if let Some(pin) = mosi.pin_mut() { if let Some(pin) = &mosi {
pin.io().ctrl().write(|w| w.set_funcsel(1)); pin.io().ctrl().write(|w| w.set_funcsel(1));
} }
if let Some(pin) = miso.pin_mut() { if let Some(pin) = &miso {
pin.io().ctrl().write(|w| w.set_funcsel(1)); pin.io().ctrl().write(|w| w.set_funcsel(1));
} }
if let Some(pin) = cs.pin_mut() { if let Some(pin) = &cs {
pin.io().ctrl().write(|w| w.set_funcsel(1)); pin.io().ctrl().write(|w| w.set_funcsel(1));
} }
} }
@ -180,10 +232,6 @@ mod sealed {
pub trait Instance { pub trait Instance {
fn regs(&self) -> pac::spi::Spi; fn regs(&self) -> pac::spi::Spi;
} }
pub trait ClkPin<T: Instance> {}
pub trait CsPin<T: Instance> {}
pub trait MosiPin<T: Instance> {}
pub trait MisoPin<T: Instance> {}
} }
pub trait Instance: sealed::Instance {} pub trait Instance: sealed::Instance {}
@ -202,23 +250,13 @@ macro_rules! impl_instance {
impl_instance!(SPI0, Spi0); impl_instance!(SPI0, Spi0);
impl_instance!(SPI1, Spi1); impl_instance!(SPI1, Spi1);
pub trait ClkPin<T: Instance>: sealed::ClkPin<T> + OptionalPin {} pub trait ClkPin<T: Instance>: GpioPin {}
pub trait CsPin<T: Instance>: sealed::CsPin<T> + OptionalPin {} pub trait CsPin<T: Instance>: GpioPin {}
pub trait MosiPin<T: Instance>: sealed::MosiPin<T> + OptionalPin {} pub trait MosiPin<T: Instance>: GpioPin {}
pub trait MisoPin<T: Instance>: sealed::MisoPin<T> + OptionalPin {} pub trait MisoPin<T: Instance>: GpioPin {}
impl<T: Instance> sealed::ClkPin<T> for NoPin {}
impl<T: Instance> ClkPin<T> for NoPin {}
impl<T: Instance> sealed::CsPin<T> for NoPin {}
impl<T: Instance> CsPin<T> for NoPin {}
impl<T: Instance> sealed::MosiPin<T> for NoPin {}
impl<T: Instance> MosiPin<T> for NoPin {}
impl<T: Instance> sealed::MisoPin<T> for NoPin {}
impl<T: Instance> MisoPin<T> for NoPin {}
macro_rules! impl_pin { macro_rules! impl_pin {
($pin:ident, $instance:ident, $function:ident) => { ($pin:ident, $instance:ident, $function:ident) => {
impl sealed::$function<peripherals::$instance> for peripherals::$pin {}
impl $function<peripherals::$instance> for peripherals::$pin {} impl $function<peripherals::$instance> for peripherals::$pin {}
}; };
} }

View file

@ -7,7 +7,6 @@ mod example_common;
use defmt::*; use defmt::*;
use embassy::executor::Spawner; use embassy::executor::Spawner;
use embassy_rp::gpio::NoPin;
use embassy_rp::spi; use embassy_rp::spi;
use embassy_rp::spi::Spi; use embassy_rp::spi::Spi;
use embassy_rp::{gpio, Peripherals}; use embassy_rp::{gpio, Peripherals};
@ -27,7 +26,7 @@ async fn main(_spawner: Spawner, p: Peripherals) {
// create SPI // create SPI
let mut config = spi::Config::default(); let mut config = spi::Config::default();
config.frequency = 2_000_000; config.frequency = 2_000_000;
let mut spi = Spi::new(p.SPI1, clk, mosi, miso, NoPin, config); let mut spi = Spi::new(p.SPI1, clk, mosi, miso, config);
// Configure CS // Configure CS
let mut cs = Output::new(touch_cs, Level::Low); let mut cs = Output::new(touch_cs, Level::Low);

View file

@ -12,7 +12,6 @@ use defmt::*;
use display_interface_spi::SPIInterfaceNoCS; use display_interface_spi::SPIInterfaceNoCS;
use embassy::executor::Spawner; use embassy::executor::Spawner;
use embassy::time::Delay; use embassy::time::Delay;
use embassy_rp::gpio::NoPin;
use embassy_rp::peripherals; use embassy_rp::peripherals;
use embassy_rp::spi; use embassy_rp::spi;
use embassy_rp::spi::Spi; use embassy_rp::spi::Spi;
@ -49,7 +48,7 @@ async fn main(_spawner: Spawner, p: Peripherals) {
let spi = RefCell::new(SpiState { let spi = RefCell::new(SpiState {
last_mode: SpiMode::Display, last_mode: SpiMode::Display,
spi: Spi::new(p.SPI1, clk, mosi, miso, NoPin, config), spi: Spi::new(p.SPI1, clk, mosi, miso, config),
display_cs: Output::new(display_cs, Level::Low), display_cs: Output::new(display_cs, Level::Low),
}); });