Merge #615
615: rp: remove OptionalPin r=Dirbaio a=Dirbaio Mirror of https://github.com/embassy-rs/embassy/pull/605 for rp2040 Co-authored-by: Dario Nieuwenhuis <dirbaio@dirbaio.net>
This commit is contained in:
commit
f2eb438905
@ -128,7 +128,6 @@ impl<'d, T: Instance> Spim<'d, T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Select pins.
|
// Select pins.
|
||||||
// Note: OptionalPin reports 'disabled' for psel_bits when no pin was selected.
|
|
||||||
r.psel.sck.write(|w| unsafe { w.bits(sck.psel_bits()) });
|
r.psel.sck.write(|w| unsafe { w.bits(sck.psel_bits()) });
|
||||||
r.psel.mosi.write(|w| unsafe { w.bits(mosi.psel_bits()) });
|
r.psel.mosi.write(|w| unsafe { w.bits(mosi.psel_bits()) });
|
||||||
r.psel.miso.write(|w| unsafe { w.bits(miso.psel_bits()) });
|
r.psel.miso.write(|w| unsafe { w.bits(miso.psel_bits()) });
|
||||||
|
@ -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 {}
|
||||||
|
@ -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 {}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -479,8 +479,6 @@ pub(crate) mod sealed {
|
|||||||
.modify(|w| w.set_ospeedr(pin, speed.into()));
|
.modify(|w| w.set_ospeedr(pin, speed.into()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait OptionalPin {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait Pin: sealed::Pin + Sized + 'static {
|
pub trait Pin: sealed::Pin + Sized + 'static {
|
||||||
|
@ -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);
|
||||||
|
@ -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),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user