diff options
| -rw-r--r-- | embassy-nrf-examples/src/bin/gpiote_port.rs | 4 | ||||
| -rw-r--r-- | embassy-nrf/src/gpiote.rs | 5 | ||||
| -rw-r--r-- | embassy-stm32-examples/src/bin/exti.rs | 5 | ||||
| -rw-r--r-- | embassy-stm32/src/exti.rs | 41 | ||||
| -rw-r--r-- | embassy-traits/src/gpio.rs | 10 |
5 files changed, 27 insertions, 38 deletions
diff --git a/embassy-nrf-examples/src/bin/gpiote_port.rs b/embassy-nrf-examples/src/bin/gpiote_port.rs index bbf5bc49c..386806dfc 100644 --- a/embassy-nrf-examples/src/bin/gpiote_port.rs +++ b/embassy-nrf-examples/src/bin/gpiote_port.rs | |||
| @@ -21,9 +21,9 @@ use example_common::*; | |||
| 21 | #[embassy::task(pool_size = 4)] | 21 | #[embassy::task(pool_size = 4)] |
| 22 | async fn button_task(n: usize, mut pin: PortInput<'static, AnyPin>) { | 22 | async fn button_task(n: usize, mut pin: PortInput<'static, AnyPin>) { |
| 23 | loop { | 23 | loop { |
| 24 | Pin::new(&mut pin).wait_for_low().await; | 24 | pin.wait_for_low().await; |
| 25 | info!("Button {:?} pressed!", n); | 25 | info!("Button {:?} pressed!", n); |
| 26 | Pin::new(&mut pin).wait_for_high().await; | 26 | pin.wait_for_high().await; |
| 27 | info!("Button {:?} released!", n); | 27 | info!("Button {:?} released!", n); |
| 28 | } | 28 | } |
| 29 | } | 29 | } |
diff --git a/embassy-nrf/src/gpiote.rs b/embassy-nrf/src/gpiote.rs index 412eef1b5..ead3c47d3 100644 --- a/embassy-nrf/src/gpiote.rs +++ b/embassy-nrf/src/gpiote.rs | |||
| @@ -1,7 +1,6 @@ | |||
| 1 | use core::convert::Infallible; | 1 | use core::convert::Infallible; |
| 2 | use core::future::Future; | 2 | use core::future::Future; |
| 3 | use core::marker::PhantomData; | 3 | use core::marker::PhantomData; |
| 4 | use core::pin::Pin; | ||
| 5 | use core::task::{Context, Poll}; | 4 | use core::task::{Context, Poll}; |
| 6 | use embassy::interrupt::InterruptExt; | 5 | use embassy::interrupt::InterruptExt; |
| 7 | use embassy::traits::gpio::{WaitForHigh, WaitForLow}; | 6 | use embassy::traits::gpio::{WaitForHigh, WaitForLow}; |
| @@ -318,7 +317,7 @@ impl<'d, T: GpioPin> InputPin for PortInput<'d, T> { | |||
| 318 | impl<'d, T: GpioPin> WaitForHigh for PortInput<'d, T> { | 317 | impl<'d, T: GpioPin> WaitForHigh for PortInput<'d, T> { |
| 319 | type Future<'a> = PortInputFuture<'a>; | 318 | type Future<'a> = PortInputFuture<'a>; |
| 320 | 319 | ||
| 321 | fn wait_for_high<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a> { | 320 | fn wait_for_high<'a>(&'a mut self) -> Self::Future<'a> { |
| 322 | self.pin.pin.conf().modify(|_, w| w.sense().high()); | 321 | self.pin.pin.conf().modify(|_, w| w.sense().high()); |
| 323 | 322 | ||
| 324 | PortInputFuture { | 323 | PortInputFuture { |
| @@ -331,7 +330,7 @@ impl<'d, T: GpioPin> WaitForHigh for PortInput<'d, T> { | |||
| 331 | impl<'d, T: GpioPin> WaitForLow for PortInput<'d, T> { | 330 | impl<'d, T: GpioPin> WaitForLow for PortInput<'d, T> { |
| 332 | type Future<'a> = PortInputFuture<'a>; | 331 | type Future<'a> = PortInputFuture<'a>; |
| 333 | 332 | ||
| 334 | fn wait_for_low<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a> { | 333 | fn wait_for_low<'a>(&'a mut self) -> Self::Future<'a> { |
| 335 | self.pin.pin.conf().modify(|_, w| w.sense().low()); | 334 | self.pin.pin.conf().modify(|_, w| w.sense().low()); |
| 336 | 335 | ||
| 337 | PortInputFuture { | 336 | PortInputFuture { |
diff --git a/embassy-stm32-examples/src/bin/exti.rs b/embassy-stm32-examples/src/bin/exti.rs index 27744c4c7..e13b23bac 100644 --- a/embassy-stm32-examples/src/bin/exti.rs +++ b/embassy-stm32-examples/src/bin/exti.rs | |||
| @@ -26,13 +26,12 @@ async fn run(dp: stm32::Peripherals, _cp: cortex_m::Peripherals) { | |||
| 26 | let button = gpioa.pa0.into_pull_up_input(); | 26 | let button = gpioa.pa0.into_pull_up_input(); |
| 27 | let mut syscfg = dp.SYSCFG.constrain(); | 27 | let mut syscfg = dp.SYSCFG.constrain(); |
| 28 | 28 | ||
| 29 | let pin = ExtiPin::new(button, interrupt::take!(EXTI0), &mut syscfg); | 29 | let mut pin = ExtiPin::new(button, interrupt::take!(EXTI0), &mut syscfg); |
| 30 | pin_mut!(pin); | ||
| 31 | 30 | ||
| 32 | info!("Starting loop"); | 31 | info!("Starting loop"); |
| 33 | 32 | ||
| 34 | loop { | 33 | loop { |
| 35 | pin.as_mut().wait_for_rising_edge().await; | 34 | pin.wait_for_rising_edge().await; |
| 36 | info!("edge detected!"); | 35 | info!("edge detected!"); |
| 37 | } | 36 | } |
| 38 | } | 37 | } |
diff --git a/embassy-stm32/src/exti.rs b/embassy-stm32/src/exti.rs index 8d70defe6..9a12f8115 100644 --- a/embassy-stm32/src/exti.rs +++ b/embassy-stm32/src/exti.rs | |||
| @@ -1,6 +1,5 @@ | |||
| 1 | use core::future::Future; | 1 | use core::future::Future; |
| 2 | use core::mem; | 2 | use core::mem; |
| 3 | use core::pin::Pin; | ||
| 4 | use cortex_m; | 3 | use cortex_m; |
| 5 | 4 | ||
| 6 | use crate::hal::gpio; | 5 | use crate::hal::gpio; |
| @@ -96,13 +95,10 @@ impl<T: Instance + digital::InputPin> digital::InputPin for ExtiPin<T> { | |||
| 96 | } | 95 | } |
| 97 | 96 | ||
| 98 | impl<T: Instance + digital::InputPin + 'static> ExtiPin<T> { | 97 | impl<T: Instance + digital::InputPin + 'static> ExtiPin<T> { |
| 99 | fn wait_for_state<'a>(self: Pin<&'a mut Self>, state: bool) -> impl Future<Output = ()> + 'a { | 98 | fn wait_for_state<'a>(&'a mut self, state: bool) -> impl Future<Output = ()> + 'a { |
| 100 | let s = unsafe { self.get_unchecked_mut() }; | ||
| 101 | |||
| 102 | s.pin.clear_pending_bit(); | ||
| 103 | async move { | 99 | async move { |
| 104 | let fut = InterruptFuture::new(&mut s.interrupt); | 100 | let fut = InterruptFuture::new(&mut self.interrupt); |
| 105 | let pin = &mut s.pin; | 101 | let pin = &mut self.pin; |
| 106 | cortex_m::interrupt::free(|_| { | 102 | cortex_m::interrupt::free(|_| { |
| 107 | pin.trigger_edge(if state { | 103 | pin.trigger_edge(if state { |
| 108 | EdgeOption::Rising | 104 | EdgeOption::Rising |
| @@ -111,37 +107,32 @@ impl<T: Instance + digital::InputPin + 'static> ExtiPin<T> { | |||
| 111 | }); | 107 | }); |
| 112 | }); | 108 | }); |
| 113 | 109 | ||
| 114 | if (state && s.pin.is_high().unwrap_or(false)) | 110 | if (state && self.pin.is_high().unwrap_or(false)) |
| 115 | || (!state && s.pin.is_low().unwrap_or(false)) | 111 | || (!state && self.pin.is_low().unwrap_or(false)) |
| 116 | { | 112 | { |
| 117 | return; | 113 | return; |
| 118 | } | 114 | } |
| 119 | 115 | ||
| 120 | fut.await; | 116 | fut.await; |
| 121 | 117 | ||
| 122 | s.pin.clear_pending_bit(); | 118 | self.pin.clear_pending_bit(); |
| 123 | } | 119 | } |
| 124 | } | 120 | } |
| 125 | } | 121 | } |
| 126 | 122 | ||
| 127 | impl<T: Instance + 'static> ExtiPin<T> { | 123 | impl<T: Instance + 'static> ExtiPin<T> { |
| 128 | fn wait_for_edge<'a>( | 124 | fn wait_for_edge<'a>(&'a mut self, state: EdgeOption) -> impl Future<Output = ()> + 'a { |
| 129 | self: Pin<&'a mut Self>, | 125 | self.pin.clear_pending_bit(); |
| 130 | state: EdgeOption, | ||
| 131 | ) -> impl Future<Output = ()> + 'a { | ||
| 132 | let s = unsafe { self.get_unchecked_mut() }; | ||
| 133 | |||
| 134 | s.pin.clear_pending_bit(); | ||
| 135 | async move { | 126 | async move { |
| 136 | let fut = InterruptFuture::new(&mut s.interrupt); | 127 | let fut = InterruptFuture::new(&mut self.interrupt); |
| 137 | let pin = &mut s.pin; | 128 | let pin = &mut self.pin; |
| 138 | cortex_m::interrupt::free(|_| { | 129 | cortex_m::interrupt::free(|_| { |
| 139 | pin.trigger_edge(state); | 130 | pin.trigger_edge(state); |
| 140 | }); | 131 | }); |
| 141 | 132 | ||
| 142 | fut.await; | 133 | fut.await; |
| 143 | 134 | ||
| 144 | s.pin.clear_pending_bit(); | 135 | self.pin.clear_pending_bit(); |
| 145 | } | 136 | } |
| 146 | } | 137 | } |
| 147 | } | 138 | } |
| @@ -149,7 +140,7 @@ impl<T: Instance + 'static> ExtiPin<T> { | |||
| 149 | impl<T: Instance + digital::InputPin + 'static> WaitForHigh for ExtiPin<T> { | 140 | impl<T: Instance + digital::InputPin + 'static> WaitForHigh for ExtiPin<T> { |
| 150 | type Future<'a> = impl Future<Output = ()> + 'a; | 141 | type Future<'a> = impl Future<Output = ()> + 'a; |
| 151 | 142 | ||
| 152 | fn wait_for_high<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a> { | 143 | fn wait_for_high<'a>(&'a mut self) -> Self::Future<'a> { |
| 153 | self.wait_for_state(true) | 144 | self.wait_for_state(true) |
| 154 | } | 145 | } |
| 155 | } | 146 | } |
| @@ -157,7 +148,7 @@ impl<T: Instance + digital::InputPin + 'static> WaitForHigh for ExtiPin<T> { | |||
| 157 | impl<T: Instance + digital::InputPin + 'static> WaitForLow for ExtiPin<T> { | 148 | impl<T: Instance + digital::InputPin + 'static> WaitForLow for ExtiPin<T> { |
| 158 | type Future<'a> = impl Future<Output = ()> + 'a; | 149 | type Future<'a> = impl Future<Output = ()> + 'a; |
| 159 | 150 | ||
| 160 | fn wait_for_low<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a> { | 151 | fn wait_for_low<'a>(&'a mut self) -> Self::Future<'a> { |
| 161 | self.wait_for_state(false) | 152 | self.wait_for_state(false) |
| 162 | } | 153 | } |
| 163 | } | 154 | } |
| @@ -176,7 +167,7 @@ impl<T: Instance + digital::InputPin + 'static> WaitForLow for ExtiPin<T> { | |||
| 176 | impl<T: Instance + 'static> WaitForRisingEdge for ExtiPin<T> { | 167 | impl<T: Instance + 'static> WaitForRisingEdge for ExtiPin<T> { |
| 177 | type Future<'a> = impl Future<Output = ()> + 'a; | 168 | type Future<'a> = impl Future<Output = ()> + 'a; |
| 178 | 169 | ||
| 179 | fn wait_for_rising_edge<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a> { | 170 | fn wait_for_rising_edge<'a>(&'a mut self) -> Self::Future<'a> { |
| 180 | self.wait_for_edge(EdgeOption::Rising) | 171 | self.wait_for_edge(EdgeOption::Rising) |
| 181 | } | 172 | } |
| 182 | } | 173 | } |
| @@ -184,7 +175,7 @@ impl<T: Instance + 'static> WaitForRisingEdge for ExtiPin<T> { | |||
| 184 | impl<T: Instance + 'static> WaitForFallingEdge for ExtiPin<T> { | 175 | impl<T: Instance + 'static> WaitForFallingEdge for ExtiPin<T> { |
| 185 | type Future<'a> = impl Future<Output = ()> + 'a; | 176 | type Future<'a> = impl Future<Output = ()> + 'a; |
| 186 | 177 | ||
| 187 | fn wait_for_falling_edge<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a> { | 178 | fn wait_for_falling_edge<'a>(&'a mut self) -> Self::Future<'a> { |
| 188 | self.wait_for_edge(EdgeOption::Falling) | 179 | self.wait_for_edge(EdgeOption::Falling) |
| 189 | } | 180 | } |
| 190 | } | 181 | } |
| @@ -192,7 +183,7 @@ impl<T: Instance + 'static> WaitForFallingEdge for ExtiPin<T> { | |||
| 192 | impl<T: Instance + 'static> WaitForAnyEdge for ExtiPin<T> { | 183 | impl<T: Instance + 'static> WaitForAnyEdge for ExtiPin<T> { |
| 193 | type Future<'a> = impl Future<Output = ()> + 'a; | 184 | type Future<'a> = impl Future<Output = ()> + 'a; |
| 194 | 185 | ||
| 195 | fn wait_for_any_edge<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a> { | 186 | fn wait_for_any_edge<'a>(&'a mut self) -> Self::Future<'a> { |
| 196 | self.wait_for_edge(EdgeOption::RisingFalling) | 187 | self.wait_for_edge(EdgeOption::RisingFalling) |
| 197 | } | 188 | } |
| 198 | } | 189 | } |
diff --git a/embassy-traits/src/gpio.rs b/embassy-traits/src/gpio.rs index 4c3feac21..c4ae206cd 100644 --- a/embassy-traits/src/gpio.rs +++ b/embassy-traits/src/gpio.rs | |||
| @@ -9,7 +9,7 @@ pub trait WaitForHigh { | |||
| 9 | /// | 9 | /// |
| 10 | /// If the pin is already high, the future completes immediately. | 10 | /// If the pin is already high, the future completes immediately. |
| 11 | /// Otherwise, it completes when it becomes high. | 11 | /// Otherwise, it completes when it becomes high. |
| 12 | fn wait_for_high<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a>; | 12 | fn wait_for_high<'a>(&'a mut self) -> Self::Future<'a>; |
| 13 | } | 13 | } |
| 14 | 14 | ||
| 15 | /// Wait for a pin to become low. | 15 | /// Wait for a pin to become low. |
| @@ -20,7 +20,7 @@ pub trait WaitForLow { | |||
| 20 | /// | 20 | /// |
| 21 | /// If the pin is already low, the future completes immediately. | 21 | /// If the pin is already low, the future completes immediately. |
| 22 | /// Otherwise, it completes when it becomes low. | 22 | /// Otherwise, it completes when it becomes low. |
| 23 | fn wait_for_low<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a>; | 23 | fn wait_for_low<'a>(&'a mut self) -> Self::Future<'a>; |
| 24 | } | 24 | } |
| 25 | 25 | ||
| 26 | /// Wait for a rising edge (transition from low to high) | 26 | /// Wait for a rising edge (transition from low to high) |
| @@ -28,7 +28,7 @@ pub trait WaitForRisingEdge { | |||
| 28 | type Future<'a>: Future<Output = ()> + 'a; | 28 | type Future<'a>: Future<Output = ()> + 'a; |
| 29 | 29 | ||
| 30 | /// Wait for a rising edge (transition from low to high) | 30 | /// Wait for a rising edge (transition from low to high) |
| 31 | fn wait_for_rising_edge<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a>; | 31 | fn wait_for_rising_edge<'a>(&'a mut self) -> Self::Future<'a>; |
| 32 | } | 32 | } |
| 33 | 33 | ||
| 34 | /// Wait for a falling edge (transition from high to low) | 34 | /// Wait for a falling edge (transition from high to low) |
| @@ -36,7 +36,7 @@ pub trait WaitForFallingEdge { | |||
| 36 | type Future<'a>: Future<Output = ()> + 'a; | 36 | type Future<'a>: Future<Output = ()> + 'a; |
| 37 | 37 | ||
| 38 | /// Wait for a falling edge (transition from high to low) | 38 | /// Wait for a falling edge (transition from high to low) |
| 39 | fn wait_for_falling_edge<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a>; | 39 | fn wait_for_falling_edge<'a>(&'a mut self) -> Self::Future<'a>; |
| 40 | } | 40 | } |
| 41 | 41 | ||
| 42 | /// Wait for any edge (any transition, high to low or low to high) | 42 | /// Wait for any edge (any transition, high to low or low to high) |
| @@ -44,5 +44,5 @@ pub trait WaitForAnyEdge { | |||
| 44 | type Future<'a>: Future<Output = ()> + 'a; | 44 | type Future<'a>: Future<Output = ()> + 'a; |
| 45 | 45 | ||
| 46 | /// Wait for any edge (any transition, high to low or low to high) | 46 | /// Wait for any edge (any transition, high to low or low to high) |
| 47 | fn wait_for_any_edge<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a>; | 47 | fn wait_for_any_edge<'a>(&'a mut self) -> Self::Future<'a>; |
| 48 | } | 48 | } |
