aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2022-02-12 01:34:41 +0100
committerDario Nieuwenhuis <[email protected]>2022-02-12 01:34:41 +0100
commit0c9b1abb67f34474c83e7b41f65615ebc1b9f54f (patch)
treedafa710d42a4c2102be037ff3cf7f90ad82f1003
parent4a75475cfc9325a807b7202f563902946eca3b9e (diff)
rp: remove OptionalPin
-rw-r--r--embassy-rp/src/gpio.rs50
-rw-r--r--embassy-rp/src/spi.rs88
-rw-r--r--examples/rp/src/bin/spi.rs3
-rw-r--r--examples/rp/src/bin/spi_display.rs3
4 files changed, 65 insertions, 79 deletions
diff --git a/embassy-rp/src/gpio.rs b/embassy-rp/src/gpio.rs
index 585e9861e..f9a5eff50 100644
--- a/embassy-rp/src/gpio.rs
+++ b/embassy-rp/src/gpio.rs
@@ -224,8 +224,6 @@ pub(crate) mod sealed {
224 SIO.gpio_in(self.bank() as _) 224 SIO.gpio_in(self.bank() as _)
225 } 225 }
226 } 226 }
227
228 pub trait OptionalPin {}
229} 227}
230 228
231pub trait Pin: Unborrow<Target = Self> + sealed::Pin { 229pub trait Pin: Unborrow<Target = Self> + sealed::Pin {
@@ -250,54 +248,6 @@ impl sealed::Pin for AnyPin {
250 248
251// ========================== 249// ==========================
252 250
253pub trait OptionalPin: Unborrow<Target = Self> + sealed::OptionalPin + Sized {
254 type Pin: Pin;
255 fn pin(&self) -> Option<&Self::Pin>;
256 fn pin_mut(&mut self) -> Option<&mut Self::Pin>;
257
258 /// Convert from concrete pin type PIN_XX to type erased `Option<AnyPin>`.
259 #[inline]
260 fn degrade_optional(mut self) -> Option<AnyPin> {
261 self.pin_mut()
262 .map(|pin| unsafe { core::ptr::read(pin) }.degrade())
263 }
264}
265
266impl<T: Pin> sealed::OptionalPin for T {}
267impl<T: Pin> OptionalPin for T {
268 type Pin = T;
269
270 #[inline]
271 fn pin(&self) -> Option<&T> {
272 Some(self)
273 }
274
275 #[inline]
276 fn pin_mut(&mut self) -> Option<&mut T> {
277 Some(self)
278 }
279}
280
281#[derive(Clone, Copy, Debug)]
282pub struct NoPin;
283unsafe_impl_unborrow!(NoPin);
284impl sealed::OptionalPin for NoPin {}
285impl OptionalPin for NoPin {
286 type Pin = AnyPin;
287
288 #[inline]
289 fn pin(&self) -> Option<&AnyPin> {
290 None
291 }
292
293 #[inline]
294 fn pin_mut(&mut self) -> Option<&mut AnyPin> {
295 None
296 }
297}
298
299// ==========================
300
301macro_rules! impl_pin { 251macro_rules! impl_pin {
302 ($name:ident, $bank:expr, $pin_num:expr) => { 252 ($name:ident, $bank:expr, $pin_num:expr) => {
303 impl Pin for peripherals::$name {} 253 impl Pin for peripherals::$name {}
diff --git a/embassy-rp/src/spi.rs b/embassy-rp/src/spi.rs
index 491082188..3d498aff9 100644
--- a/embassy-rp/src/spi.rs
+++ b/embassy-rp/src/spi.rs
@@ -6,7 +6,7 @@ use embedded_hal::blocking::spi as eh;
6use embedded_hal::spi as ehnb; 6use embedded_hal::spi as ehnb;
7 7
8use crate::gpio::sealed::Pin as _; 8use crate::gpio::sealed::Pin as _;
9use crate::gpio::{NoPin, OptionalPin}; 9use crate::gpio::{AnyPin, Pin as GpioPin};
10use crate::{pac, peripherals}; 10use crate::{pac, peripherals};
11 11
12pub use ehnb::{Phase, Polarity}; 12pub use ehnb::{Phase, Polarity};
@@ -66,10 +66,62 @@ impl<'d, T: Instance> Spi<'d, T> {
66 clk: impl Unborrow<Target = impl ClkPin<T>> + 'd, 66 clk: impl Unborrow<Target = impl ClkPin<T>> + 'd,
67 mosi: impl Unborrow<Target = impl MosiPin<T>> + 'd, 67 mosi: impl Unborrow<Target = impl MosiPin<T>> + 'd,
68 miso: impl Unborrow<Target = impl MisoPin<T>> + 'd, 68 miso: impl Unborrow<Target = impl MisoPin<T>> + 'd,
69 cs: impl Unborrow<Target = impl CsPin<T>> + 'd,
70 config: Config, 69 config: Config,
71 ) -> Self { 70 ) -> Self {
72 unborrow!(inner, clk, mosi, miso, cs); 71 unborrow!(clk, mosi, miso);
72 Self::new_inner(
73 inner,
74 Some(clk.degrade()),
75 Some(mosi.degrade()),
76 Some(miso.degrade()),
77 None,
78 config,
79 )
80 }
81
82 pub fn new_txonly(
83 inner: impl Unborrow<Target = T> + 'd,
84 clk: impl Unborrow<Target = impl ClkPin<T>> + 'd,
85 mosi: impl Unborrow<Target = impl MosiPin<T>> + 'd,
86 config: Config,
87 ) -> Self {
88 unborrow!(clk, mosi);
89 Self::new_inner(
90 inner,
91 Some(clk.degrade()),
92 Some(mosi.degrade()),
93 None,
94 None,
95 config,
96 )
97 }
98
99 pub fn new_rxonly(
100 inner: impl Unborrow<Target = T> + 'd,
101 clk: impl Unborrow<Target = impl ClkPin<T>> + 'd,
102 miso: impl Unborrow<Target = impl MisoPin<T>> + 'd,
103 config: Config,
104 ) -> Self {
105 unborrow!(clk, miso);
106 Self::new_inner(
107 inner,
108 Some(clk.degrade()),
109 None,
110 Some(miso.degrade()),
111 None,
112 config,
113 )
114 }
115
116 fn new_inner(
117 inner: impl Unborrow<Target = T> + 'd,
118 clk: Option<AnyPin>,
119 mosi: Option<AnyPin>,
120 miso: Option<AnyPin>,
121 cs: Option<AnyPin>,
122 config: Config,
123 ) -> Self {
124 unborrow!(inner);
73 125
74 unsafe { 126 unsafe {
75 let p = inner.regs(); 127 let p = inner.regs();
@@ -86,16 +138,16 @@ impl<'d, T: Instance> Spi<'d, T> {
86 w.set_sse(true); // enable 138 w.set_sse(true); // enable
87 }); 139 });
88 140
89 if let Some(pin) = clk.pin_mut() { 141 if let Some(pin) = &clk {
90 pin.io().ctrl().write(|w| w.set_funcsel(1)); 142 pin.io().ctrl().write(|w| w.set_funcsel(1));
91 } 143 }
92 if let Some(pin) = mosi.pin_mut() { 144 if let Some(pin) = &mosi {
93 pin.io().ctrl().write(|w| w.set_funcsel(1)); 145 pin.io().ctrl().write(|w| w.set_funcsel(1));
94 } 146 }
95 if let Some(pin) = miso.pin_mut() { 147 if let Some(pin) = &miso {
96 pin.io().ctrl().write(|w| w.set_funcsel(1)); 148 pin.io().ctrl().write(|w| w.set_funcsel(1));
97 } 149 }
98 if let Some(pin) = cs.pin_mut() { 150 if let Some(pin) = &cs {
99 pin.io().ctrl().write(|w| w.set_funcsel(1)); 151 pin.io().ctrl().write(|w| w.set_funcsel(1));
100 } 152 }
101 } 153 }
@@ -180,10 +232,6 @@ mod sealed {
180 pub trait Instance { 232 pub trait Instance {
181 fn regs(&self) -> pac::spi::Spi; 233 fn regs(&self) -> pac::spi::Spi;
182 } 234 }
183 pub trait ClkPin<T: Instance> {}
184 pub trait CsPin<T: Instance> {}
185 pub trait MosiPin<T: Instance> {}
186 pub trait MisoPin<T: Instance> {}
187} 235}
188 236
189pub trait Instance: sealed::Instance {} 237pub trait Instance: sealed::Instance {}
@@ -202,23 +250,13 @@ macro_rules! impl_instance {
202impl_instance!(SPI0, Spi0); 250impl_instance!(SPI0, Spi0);
203impl_instance!(SPI1, Spi1); 251impl_instance!(SPI1, Spi1);
204 252
205pub trait ClkPin<T: Instance>: sealed::ClkPin<T> + OptionalPin {} 253pub trait ClkPin<T: Instance>: GpioPin {}
206pub trait CsPin<T: Instance>: sealed::CsPin<T> + OptionalPin {} 254pub trait CsPin<T: Instance>: GpioPin {}
207pub trait MosiPin<T: Instance>: sealed::MosiPin<T> + OptionalPin {} 255pub trait MosiPin<T: Instance>: GpioPin {}
208pub trait MisoPin<T: Instance>: sealed::MisoPin<T> + OptionalPin {} 256pub trait MisoPin<T: Instance>: GpioPin {}
209
210impl<T: Instance> sealed::ClkPin<T> for NoPin {}
211impl<T: Instance> ClkPin<T> for NoPin {}
212impl<T: Instance> sealed::CsPin<T> for NoPin {}
213impl<T: Instance> CsPin<T> for NoPin {}
214impl<T: Instance> sealed::MosiPin<T> for NoPin {}
215impl<T: Instance> MosiPin<T> for NoPin {}
216impl<T: Instance> sealed::MisoPin<T> for NoPin {}
217impl<T: Instance> MisoPin<T> for NoPin {}
218 257
219macro_rules! impl_pin { 258macro_rules! impl_pin {
220 ($pin:ident, $instance:ident, $function:ident) => { 259 ($pin:ident, $instance:ident, $function:ident) => {
221 impl sealed::$function<peripherals::$instance> for peripherals::$pin {}
222 impl $function<peripherals::$instance> for peripherals::$pin {} 260 impl $function<peripherals::$instance> for peripherals::$pin {}
223 }; 261 };
224} 262}
diff --git a/examples/rp/src/bin/spi.rs b/examples/rp/src/bin/spi.rs
index c4748f2ec..71dec94f3 100644
--- a/examples/rp/src/bin/spi.rs
+++ b/examples/rp/src/bin/spi.rs
@@ -7,7 +7,6 @@ mod example_common;
7 7
8use defmt::*; 8use defmt::*;
9use embassy::executor::Spawner; 9use embassy::executor::Spawner;
10use embassy_rp::gpio::NoPin;
11use embassy_rp::spi; 10use embassy_rp::spi;
12use embassy_rp::spi::Spi; 11use embassy_rp::spi::Spi;
13use embassy_rp::{gpio, Peripherals}; 12use embassy_rp::{gpio, Peripherals};
@@ -27,7 +26,7 @@ async fn main(_spawner: Spawner, p: Peripherals) {
27 // create SPI 26 // create SPI
28 let mut config = spi::Config::default(); 27 let mut config = spi::Config::default();
29 config.frequency = 2_000_000; 28 config.frequency = 2_000_000;
30 let mut spi = Spi::new(p.SPI1, clk, mosi, miso, NoPin, config); 29 let mut spi = Spi::new(p.SPI1, clk, mosi, miso, config);
31 30
32 // Configure CS 31 // Configure CS
33 let mut cs = Output::new(touch_cs, Level::Low); 32 let mut cs = Output::new(touch_cs, Level::Low);
diff --git a/examples/rp/src/bin/spi_display.rs b/examples/rp/src/bin/spi_display.rs
index 1ae3ae66e..96f0cf378 100644
--- a/examples/rp/src/bin/spi_display.rs
+++ b/examples/rp/src/bin/spi_display.rs
@@ -12,7 +12,6 @@ use defmt::*;
12use display_interface_spi::SPIInterfaceNoCS; 12use display_interface_spi::SPIInterfaceNoCS;
13use embassy::executor::Spawner; 13use embassy::executor::Spawner;
14use embassy::time::Delay; 14use embassy::time::Delay;
15use embassy_rp::gpio::NoPin;
16use embassy_rp::peripherals; 15use embassy_rp::peripherals;
17use embassy_rp::spi; 16use embassy_rp::spi;
18use embassy_rp::spi::Spi; 17use embassy_rp::spi::Spi;
@@ -49,7 +48,7 @@ async fn main(_spawner: Spawner, p: Peripherals) {
49 48
50 let spi = RefCell::new(SpiState { 49 let spi = RefCell::new(SpiState {
51 last_mode: SpiMode::Display, 50 last_mode: SpiMode::Display,
52 spi: Spi::new(p.SPI1, clk, mosi, miso, NoPin, config), 51 spi: Spi::new(p.SPI1, clk, mosi, miso, config),
53 display_cs: Output::new(display_cs, Level::Low), 52 display_cs: Output::new(display_cs, Level::Low),
54 }); 53 });
55 54