diff options
| -rw-r--r-- | embassy-nrf-examples/src/bin/gpiote_port.rs | 4 | ||||
| -rw-r--r-- | embassy-nrf-examples/src/bin/qspi.rs | 25 | ||||
| -rw-r--r-- | embassy-nrf-examples/src/bin/spim.rs | 12 | ||||
| -rw-r--r-- | embassy-nrf-examples/src/bin/uart.rs | 11 | ||||
| -rw-r--r-- | embassy-nrf/src/buffered_uarte.rs | 8 | ||||
| -rw-r--r-- | embassy-nrf/src/gpiote.rs | 5 | ||||
| -rw-r--r-- | embassy-nrf/src/qspi.rs | 99 | ||||
| -rw-r--r-- | embassy-nrf/src/spim.rs | 61 | ||||
| -rw-r--r-- | embassy-nrf/src/uarte.rs | 86 | ||||
| -rw-r--r-- | embassy-stm32-examples/src/bin/exti.rs | 5 | ||||
| -rw-r--r-- | embassy-stm32/src/exti.rs | 41 | ||||
| -rw-r--r-- | embassy-stm32/src/f4/serial.rs | 45 | ||||
| -rw-r--r-- | embassy-stm32/src/f4/spi.rs | 56 | ||||
| -rw-r--r-- | embassy-traits/src/delay.rs | 5 | ||||
| -rw-r--r-- | embassy-traits/src/flash.rs | 7 | ||||
| -rw-r--r-- | embassy-traits/src/gpio.rs | 10 | ||||
| -rw-r--r-- | embassy-traits/src/i2c.rs | 7 | ||||
| -rw-r--r-- | embassy-traits/src/spi.rs | 6 | ||||
| -rw-r--r-- | embassy-traits/src/uart.rs | 9 | ||||
| -rw-r--r-- | embassy/src/executor/timer.rs | 4 |
20 files changed, 248 insertions, 258 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-examples/src/bin/qspi.rs b/embassy-nrf-examples/src/bin/qspi.rs index 27dd28d72..28cde6e51 100644 --- a/embassy-nrf-examples/src/bin/qspi.rs +++ b/embassy-nrf-examples/src/bin/qspi.rs | |||
| @@ -14,7 +14,6 @@ use embassy::traits::flash::Flash; | |||
| 14 | use embassy_nrf::Peripherals; | 14 | use embassy_nrf::Peripherals; |
| 15 | use embassy_nrf::{interrupt, qspi}; | 15 | use embassy_nrf::{interrupt, qspi}; |
| 16 | use example_common::*; | 16 | use example_common::*; |
| 17 | use futures::pin_mut; | ||
| 18 | 17 | ||
| 19 | const PAGE_SIZE: usize = 4096; | 18 | const PAGE_SIZE: usize = 4096; |
| 20 | 19 | ||
| @@ -36,32 +35,22 @@ async fn main(spawner: Spawner) { | |||
| 36 | 35 | ||
| 37 | let config = qspi::Config::default(); | 36 | let config = qspi::Config::default(); |
| 38 | let irq = interrupt::take!(QSPI); | 37 | let irq = interrupt::take!(QSPI); |
| 39 | let q = qspi::Qspi::new(p.QSPI, irq, sck, csn, io0, io1, io2, io3, config); | 38 | let mut q = qspi::Qspi::new(p.QSPI, irq, sck, csn, io0, io1, io2, io3, config); |
| 40 | pin_mut!(q); | ||
| 41 | 39 | ||
| 42 | let mut id = [1; 3]; | 40 | let mut id = [1; 3]; |
| 43 | q.as_mut() | 41 | q.custom_instruction(0x9F, &[], &mut id).await.unwrap(); |
| 44 | .custom_instruction(0x9F, &[], &mut id) | ||
| 45 | .await | ||
| 46 | .unwrap(); | ||
| 47 | info!("id: {}", id); | 42 | info!("id: {}", id); |
| 48 | 43 | ||
| 49 | // Read status register | 44 | // Read status register |
| 50 | let mut status = [4; 1]; | 45 | let mut status = [4; 1]; |
| 51 | q.as_mut() | 46 | q.custom_instruction(0x05, &[], &mut status).await.unwrap(); |
| 52 | .custom_instruction(0x05, &[], &mut status) | ||
| 53 | .await | ||
| 54 | .unwrap(); | ||
| 55 | 47 | ||
| 56 | info!("status: {:?}", status[0]); | 48 | info!("status: {:?}", status[0]); |
| 57 | 49 | ||
| 58 | if status[0] & 0x40 == 0 { | 50 | if status[0] & 0x40 == 0 { |
| 59 | status[0] |= 0x40; | 51 | status[0] |= 0x40; |
| 60 | 52 | ||
| 61 | q.as_mut() | 53 | q.custom_instruction(0x01, &status, &mut []).await.unwrap(); |
| 62 | .custom_instruction(0x01, &status, &mut []) | ||
| 63 | .await | ||
| 64 | .unwrap(); | ||
| 65 | 54 | ||
| 66 | info!("enabled quad in status"); | 55 | info!("enabled quad in status"); |
| 67 | } | 56 | } |
| @@ -72,19 +61,19 @@ async fn main(spawner: Spawner) { | |||
| 72 | 61 | ||
| 73 | for i in 0..8 { | 62 | for i in 0..8 { |
| 74 | info!("page {:?}: erasing... ", i); | 63 | info!("page {:?}: erasing... ", i); |
| 75 | q.as_mut().erase(i * PAGE_SIZE).await.unwrap(); | 64 | q.erase(i * PAGE_SIZE).await.unwrap(); |
| 76 | 65 | ||
| 77 | for j in 0..PAGE_SIZE { | 66 | for j in 0..PAGE_SIZE { |
| 78 | buf.0[j] = pattern((j + i * PAGE_SIZE) as u32); | 67 | buf.0[j] = pattern((j + i * PAGE_SIZE) as u32); |
| 79 | } | 68 | } |
| 80 | 69 | ||
| 81 | info!("programming..."); | 70 | info!("programming..."); |
| 82 | q.as_mut().write(i * PAGE_SIZE, &buf.0).await.unwrap(); | 71 | q.write(i * PAGE_SIZE, &buf.0).await.unwrap(); |
| 83 | } | 72 | } |
| 84 | 73 | ||
| 85 | for i in 0..8 { | 74 | for i in 0..8 { |
| 86 | info!("page {:?}: reading... ", i); | 75 | info!("page {:?}: reading... ", i); |
| 87 | q.as_mut().read(i * PAGE_SIZE, &mut buf.0).await.unwrap(); | 76 | q.read(i * PAGE_SIZE, &mut buf.0).await.unwrap(); |
| 88 | 77 | ||
| 89 | info!("verifying..."); | 78 | info!("verifying..."); |
| 90 | for j in 0..PAGE_SIZE { | 79 | for j in 0..PAGE_SIZE { |
diff --git a/embassy-nrf-examples/src/bin/spim.rs b/embassy-nrf-examples/src/bin/spim.rs index 6d0fa93d0..27486374f 100644 --- a/embassy-nrf-examples/src/bin/spim.rs +++ b/embassy-nrf-examples/src/bin/spim.rs | |||
| @@ -17,7 +17,6 @@ use embassy_nrf::{interrupt, spim}; | |||
| 17 | use embassy_traits::spi::FullDuplex; | 17 | use embassy_traits::spi::FullDuplex; |
| 18 | use embedded_hal::digital::v2::*; | 18 | use embedded_hal::digital::v2::*; |
| 19 | use example_common::*; | 19 | use example_common::*; |
| 20 | use futures::pin_mut; | ||
| 21 | 20 | ||
| 22 | #[embassy::main] | 21 | #[embassy::main] |
| 23 | async fn main(spawner: Spawner) { | 22 | async fn main(spawner: Spawner) { |
| @@ -32,8 +31,7 @@ async fn main(spawner: Spawner) { | |||
| 32 | }; | 31 | }; |
| 33 | 32 | ||
| 34 | let irq = interrupt::take!(SPIM3); | 33 | let irq = interrupt::take!(SPIM3); |
| 35 | let spim = spim::Spim::new(p.SPIM3, irq, p.P0_29, p.P0_28, p.P0_30, config); | 34 | let mut spim = spim::Spim::new(p.SPIM3, irq, p.P0_29, p.P0_28, p.P0_30, config); |
| 36 | pin_mut!(spim); | ||
| 37 | 35 | ||
| 38 | let mut ncs = Output::new(p.P0_31, Level::High, OutputDrive::Standard); | 36 | let mut ncs = Output::new(p.P0_31, Level::High, OutputDrive::Standard); |
| 39 | 37 | ||
| @@ -44,7 +42,7 @@ async fn main(spawner: Spawner) { | |||
| 44 | ncs.set_low().unwrap(); | 42 | ncs.set_low().unwrap(); |
| 45 | cortex_m::asm::delay(5); | 43 | cortex_m::asm::delay(5); |
| 46 | let tx = [0xFF]; | 44 | let tx = [0xFF]; |
| 47 | unwrap!(spim.as_mut().read_write(&mut [], &tx).await); | 45 | unwrap!(spim.read_write(&mut [], &tx).await); |
| 48 | cortex_m::asm::delay(10); | 46 | cortex_m::asm::delay(10); |
| 49 | ncs.set_high().unwrap(); | 47 | ncs.set_high().unwrap(); |
| 50 | 48 | ||
| @@ -57,7 +55,7 @@ async fn main(spawner: Spawner) { | |||
| 57 | ncs.set_low().unwrap(); | 55 | ncs.set_low().unwrap(); |
| 58 | cortex_m::asm::delay(5000); | 56 | cortex_m::asm::delay(5000); |
| 59 | let tx = [0b000_11101, 0]; | 57 | let tx = [0b000_11101, 0]; |
| 60 | unwrap!(spim.as_mut().read_write(&mut rx, &tx).await); | 58 | unwrap!(spim.read_write(&mut rx, &tx).await); |
| 61 | cortex_m::asm::delay(5000); | 59 | cortex_m::asm::delay(5000); |
| 62 | ncs.set_high().unwrap(); | 60 | ncs.set_high().unwrap(); |
| 63 | info!("estat: {=[?]}", rx); | 61 | info!("estat: {=[?]}", rx); |
| @@ -67,7 +65,7 @@ async fn main(spawner: Spawner) { | |||
| 67 | ncs.set_low().unwrap(); | 65 | ncs.set_low().unwrap(); |
| 68 | cortex_m::asm::delay(5); | 66 | cortex_m::asm::delay(5); |
| 69 | let tx = [0b100_11111, 0b11]; | 67 | let tx = [0b100_11111, 0b11]; |
| 70 | unwrap!(spim.as_mut().read_write(&mut rx, &tx).await); | 68 | unwrap!(spim.read_write(&mut rx, &tx).await); |
| 71 | cortex_m::asm::delay(10); | 69 | cortex_m::asm::delay(10); |
| 72 | ncs.set_high().unwrap(); | 70 | ncs.set_high().unwrap(); |
| 73 | 71 | ||
| @@ -76,7 +74,7 @@ async fn main(spawner: Spawner) { | |||
| 76 | ncs.set_low().unwrap(); | 74 | ncs.set_low().unwrap(); |
| 77 | cortex_m::asm::delay(5); | 75 | cortex_m::asm::delay(5); |
| 78 | let tx = [0b000_10010, 0]; | 76 | let tx = [0b000_10010, 0]; |
| 79 | unwrap!(spim.as_mut().read_write(&mut rx, &tx).await); | 77 | unwrap!(spim.read_write(&mut rx, &tx).await); |
| 80 | cortex_m::asm::delay(10); | 78 | cortex_m::asm::delay(10); |
| 81 | ncs.set_high().unwrap(); | 79 | ncs.set_high().unwrap(); |
| 82 | 80 | ||
diff --git a/embassy-nrf-examples/src/bin/uart.rs b/embassy-nrf-examples/src/bin/uart.rs index b3d32814b..23fc89312 100644 --- a/embassy-nrf-examples/src/bin/uart.rs +++ b/embassy-nrf-examples/src/bin/uart.rs | |||
| @@ -15,7 +15,6 @@ use embassy::traits::uart::{Read, Write}; | |||
| 15 | use embassy::util::Steal; | 15 | use embassy::util::Steal; |
| 16 | use embassy_nrf::gpio::NoPin; | 16 | use embassy_nrf::gpio::NoPin; |
| 17 | use embassy_nrf::{interrupt, uarte, Peripherals}; | 17 | use embassy_nrf::{interrupt, uarte, Peripherals}; |
| 18 | use futures::pin_mut; | ||
| 19 | 18 | ||
| 20 | #[embassy::main] | 19 | #[embassy::main] |
| 21 | async fn main(spawner: Spawner) { | 20 | async fn main(spawner: Spawner) { |
| @@ -26,8 +25,8 @@ async fn main(spawner: Spawner) { | |||
| 26 | config.baudrate = uarte::Baudrate::BAUD115200; | 25 | config.baudrate = uarte::Baudrate::BAUD115200; |
| 27 | 26 | ||
| 28 | let irq = interrupt::take!(UARTE0_UART0); | 27 | let irq = interrupt::take!(UARTE0_UART0); |
| 29 | let uart = unsafe { uarte::Uarte::new(p.UARTE0, irq, p.P0_08, p.P0_06, NoPin, NoPin, config) }; | 28 | let mut uart = |
| 30 | pin_mut!(uart); | 29 | unsafe { uarte::Uarte::new(p.UARTE0, irq, p.P0_08, p.P0_06, NoPin, NoPin, config) }; |
| 31 | 30 | ||
| 32 | info!("uarte initialized!"); | 31 | info!("uarte initialized!"); |
| 33 | 32 | ||
| @@ -35,14 +34,14 @@ async fn main(spawner: Spawner) { | |||
| 35 | let mut buf = [0; 8]; | 34 | let mut buf = [0; 8]; |
| 36 | buf.copy_from_slice(b"Hello!\r\n"); | 35 | buf.copy_from_slice(b"Hello!\r\n"); |
| 37 | 36 | ||
| 38 | unwrap!(uart.as_mut().write(&buf).await); | 37 | unwrap!(uart.write(&buf).await); |
| 39 | info!("wrote hello in uart!"); | 38 | info!("wrote hello in uart!"); |
| 40 | 39 | ||
| 41 | loop { | 40 | loop { |
| 42 | info!("reading..."); | 41 | info!("reading..."); |
| 43 | unwrap!(uart.as_mut().read(&mut buf).await); | 42 | unwrap!(uart.read(&mut buf).await); |
| 44 | info!("writing..."); | 43 | info!("writing..."); |
| 45 | unwrap!(uart.as_mut().write(&buf).await); | 44 | unwrap!(uart.write(&buf).await); |
| 46 | 45 | ||
| 47 | /* | 46 | /* |
| 48 | // `receive()` doesn't return until the buffer has been completely filled with | 47 | // `receive()` doesn't return until the buffer has been completely filled with |
diff --git a/embassy-nrf/src/buffered_uarte.rs b/embassy-nrf/src/buffered_uarte.rs index 702ccde0e..9e67aaef6 100644 --- a/embassy-nrf/src/buffered_uarte.rs +++ b/embassy-nrf/src/buffered_uarte.rs | |||
| @@ -78,7 +78,7 @@ impl<'d, U: UarteInstance, T: TimerInstance> BufferedUarte<'d, U, T> { | |||
| 78 | ) -> Self { | 78 | ) -> Self { |
| 79 | unborrow!(uarte, timer, ppi_ch1, ppi_ch2, irq, rxd, txd, cts, rts); | 79 | unborrow!(uarte, timer, ppi_ch1, ppi_ch2, irq, rxd, txd, cts, rts); |
| 80 | 80 | ||
| 81 | let r = uarte.regs(); | 81 | let r = U::regs(); |
| 82 | let rt = timer.regs(); | 82 | let rt = timer.regs(); |
| 83 | 83 | ||
| 84 | rxd.conf().write(|w| w.input().connect().drive().h0h1()); | 84 | rxd.conf().write(|w| w.input().connect().drive().h0h1()); |
| @@ -178,7 +178,7 @@ impl<'d, U: UarteInstance, T: TimerInstance> BufferedUarte<'d, U, T> { | |||
| 178 | 178 | ||
| 179 | pub fn set_baudrate(self: Pin<&mut Self>, baudrate: Baudrate) { | 179 | pub fn set_baudrate(self: Pin<&mut Self>, baudrate: Baudrate) { |
| 180 | self.inner().with(|state, _irq| { | 180 | self.inner().with(|state, _irq| { |
| 181 | let r = state.uarte.regs(); | 181 | let r = U::regs(); |
| 182 | let rt = state.timer.regs(); | 182 | let rt = state.timer.regs(); |
| 183 | 183 | ||
| 184 | let timeout = 0x8000_0000 / (baudrate as u32 / 40); | 184 | let timeout = 0x8000_0000 / (baudrate as u32 / 40); |
| @@ -265,7 +265,7 @@ impl<'d, U: UarteInstance, T: TimerInstance> AsyncWrite for BufferedUarte<'d, U, | |||
| 265 | 265 | ||
| 266 | impl<'a, U: UarteInstance, T: TimerInstance> Drop for State<'a, U, T> { | 266 | impl<'a, U: UarteInstance, T: TimerInstance> Drop for State<'a, U, T> { |
| 267 | fn drop(&mut self) { | 267 | fn drop(&mut self) { |
| 268 | let r = self.uarte.regs(); | 268 | let r = U::regs(); |
| 269 | let rt = self.timer.regs(); | 269 | let rt = self.timer.regs(); |
| 270 | 270 | ||
| 271 | // TODO this probably deadlocks. do like Uarte instead. | 271 | // TODO this probably deadlocks. do like Uarte instead. |
| @@ -290,7 +290,7 @@ impl<'a, U: UarteInstance, T: TimerInstance> PeripheralState for State<'a, U, T> | |||
| 290 | type Interrupt = U::Interrupt; | 290 | type Interrupt = U::Interrupt; |
| 291 | fn on_interrupt(&mut self) { | 291 | fn on_interrupt(&mut self) { |
| 292 | trace!("irq: start"); | 292 | trace!("irq: start"); |
| 293 | let r = self.uarte.regs(); | 293 | let r = U::regs(); |
| 294 | let rt = self.timer.regs(); | 294 | let rt = self.timer.regs(); |
| 295 | 295 | ||
| 296 | loop { | 296 | loop { |
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-nrf/src/qspi.rs b/embassy-nrf/src/qspi.rs index 033c9e012..d682c1b8c 100644 --- a/embassy-nrf/src/qspi.rs +++ b/embassy-nrf/src/qspi.rs | |||
| @@ -1,11 +1,11 @@ | |||
| 1 | use core::future::Future; | 1 | use core::future::Future; |
| 2 | use core::marker::PhantomData; | 2 | use core::marker::PhantomData; |
| 3 | use core::pin::Pin; | ||
| 4 | use core::task::Poll; | 3 | use core::task::Poll; |
| 5 | 4 | use embassy::interrupt::{Interrupt, InterruptExt}; | |
| 6 | use embassy::interrupt::Interrupt; | 5 | use embassy::traits::flash::{Error, Flash}; |
| 7 | use embassy_extras::peripheral::{PeripheralMutex, PeripheralState}; | 6 | use embassy::util::{AtomicWaker, DropBomb, PeripheralBorrow}; |
| 8 | use embassy_extras::unborrow; | 7 | use embassy_extras::unborrow; |
| 8 | use futures::future::poll_fn; | ||
| 9 | 9 | ||
| 10 | use crate::fmt::{assert, assert_eq, *}; | 10 | use crate::fmt::{assert, assert_eq, *}; |
| 11 | use crate::gpio::Pin as GpioPin; | 11 | use crate::gpio::Pin as GpioPin; |
| @@ -27,10 +27,6 @@ pub use crate::pac::qspi::ifconfig0::WRITEOC_A as WriteOpcode; | |||
| 27 | // - activate/deactivate | 27 | // - activate/deactivate |
| 28 | // - set gpio in high drive | 28 | // - set gpio in high drive |
| 29 | 29 | ||
| 30 | use embassy::traits::flash::{Error, Flash}; | ||
| 31 | use embassy::util::{wake_on_interrupt, DropBomb, PeripheralBorrow, WakerRegistration}; | ||
| 32 | use futures::future::poll_fn; | ||
| 33 | |||
| 34 | pub struct DeepPowerDownConfig { | 30 | pub struct DeepPowerDownConfig { |
| 35 | pub enter_time: u16, | 31 | pub enter_time: u16, |
| 36 | pub exit_time: u16, | 32 | pub exit_time: u16, |
| @@ -77,7 +73,7 @@ impl<'d, T: Instance> Qspi<'d, T> { | |||
| 77 | ) -> Self { | 73 | ) -> Self { |
| 78 | unborrow!(qspi, irq, sck, csn, io0, io1, io2, io3); | 74 | unborrow!(qspi, irq, sck, csn, io0, io1, io2, io3); |
| 79 | 75 | ||
| 80 | let r = qspi.regs(); | 76 | let r = T::regs(); |
| 81 | 77 | ||
| 82 | for cnf in &[ | 78 | for cnf in &[ |
| 83 | sck.conf(), | 79 | sck.conf(), |
| @@ -137,6 +133,10 @@ impl<'d, T: Instance> Qspi<'d, T> { | |||
| 137 | while r.events_ready.read().bits() == 0 {} | 133 | while r.events_ready.read().bits() == 0 {} |
| 138 | r.events_ready.reset(); | 134 | r.events_ready.reset(); |
| 139 | 135 | ||
| 136 | irq.set_handler(Self::on_interrupt); | ||
| 137 | irq.unpend(); | ||
| 138 | irq.enable(); | ||
| 139 | |||
| 140 | Self { | 140 | Self { |
| 141 | peri: qspi, | 141 | peri: qspi, |
| 142 | irq, | 142 | irq, |
| @@ -144,8 +144,18 @@ impl<'d, T: Instance> Qspi<'d, T> { | |||
| 144 | } | 144 | } |
| 145 | } | 145 | } |
| 146 | 146 | ||
| 147 | pub fn sleep(mut self: Pin<&mut Self>) { | 147 | fn on_interrupt(_: *mut ()) { |
| 148 | let r = unsafe { self.as_mut().get_unchecked_mut() }.peri.regs(); | 148 | let r = T::regs(); |
| 149 | let s = T::state(); | ||
| 150 | |||
| 151 | if r.events_ready.read().bits() != 0 { | ||
| 152 | s.ready_waker.wake(); | ||
| 153 | r.intenclr.write(|w| w.ready().clear()); | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | pub fn sleep(&mut self) { | ||
| 158 | let r = T::regs(); | ||
| 149 | 159 | ||
| 150 | info!("flash: sleeping"); | 160 | info!("flash: sleeping"); |
| 151 | info!("flash: state = {:?}", r.status.read().bits()); | 161 | info!("flash: state = {:?}", r.status.read().bits()); |
| @@ -158,7 +168,7 @@ impl<'d, T: Instance> Qspi<'d, T> { | |||
| 158 | } | 168 | } |
| 159 | 169 | ||
| 160 | pub async fn custom_instruction( | 170 | pub async fn custom_instruction( |
| 161 | mut self: Pin<&mut Self>, | 171 | &mut self, |
| 162 | opcode: u8, | 172 | opcode: u8, |
| 163 | req: &[u8], | 173 | req: &[u8], |
| 164 | resp: &mut [u8], | 174 | resp: &mut [u8], |
| @@ -184,7 +194,7 @@ impl<'d, T: Instance> Qspi<'d, T> { | |||
| 184 | 194 | ||
| 185 | let len = core::cmp::max(req.len(), resp.len()) as u8; | 195 | let len = core::cmp::max(req.len(), resp.len()) as u8; |
| 186 | 196 | ||
| 187 | let r = unsafe { self.as_mut().get_unchecked_mut() }.peri.regs(); | 197 | let r = T::regs(); |
| 188 | r.cinstrdat0.write(|w| unsafe { w.bits(dat0) }); | 198 | r.cinstrdat0.write(|w| unsafe { w.bits(dat0) }); |
| 189 | r.cinstrdat1.write(|w| unsafe { w.bits(dat1) }); | 199 | r.cinstrdat1.write(|w| unsafe { w.bits(dat1) }); |
| 190 | 200 | ||
| @@ -203,9 +213,9 @@ impl<'d, T: Instance> Qspi<'d, T> { | |||
| 203 | w | 213 | w |
| 204 | }); | 214 | }); |
| 205 | 215 | ||
| 206 | self.as_mut().wait_ready().await; | 216 | self.wait_ready().await; |
| 207 | 217 | ||
| 208 | let r = unsafe { self.as_mut().get_unchecked_mut() }.peri.regs(); | 218 | let r = T::regs(); |
| 209 | 219 | ||
| 210 | let dat0 = r.cinstrdat0.read().bits(); | 220 | let dat0 = r.cinstrdat0.read().bits(); |
| 211 | let dat1 = r.cinstrdat1.read().bits(); | 221 | let dat1 = r.cinstrdat1.read().bits(); |
| @@ -225,19 +235,14 @@ impl<'d, T: Instance> Qspi<'d, T> { | |||
| 225 | Ok(()) | 235 | Ok(()) |
| 226 | } | 236 | } |
| 227 | 237 | ||
| 228 | async fn wait_ready(self: Pin<&mut Self>) { | 238 | async fn wait_ready(&mut self) { |
| 229 | let this = unsafe { self.get_unchecked_mut() }; | ||
| 230 | |||
| 231 | poll_fn(move |cx| { | 239 | poll_fn(move |cx| { |
| 232 | let r = this.peri.regs(); | 240 | let r = T::regs(); |
| 233 | 241 | let s = T::state(); | |
| 242 | s.ready_waker.register(cx.waker()); | ||
| 234 | if r.events_ready.read().bits() != 0 { | 243 | if r.events_ready.read().bits() != 0 { |
| 235 | r.events_ready.reset(); | ||
| 236 | return Poll::Ready(()); | 244 | return Poll::Ready(()); |
| 237 | } | 245 | } |
| 238 | |||
| 239 | wake_on_interrupt(&mut this.irq, cx.waker()); | ||
| 240 | |||
| 241 | Poll::Pending | 246 | Poll::Pending |
| 242 | }) | 247 | }) |
| 243 | .await | 248 | .await |
| @@ -252,11 +257,7 @@ impl<'d, T: Instance> Flash for Qspi<'d, T> { | |||
| 252 | #[rustfmt::skip] | 257 | #[rustfmt::skip] |
| 253 | type ErasePageFuture<'a> where Self: 'a = impl Future<Output = Result<(), Error>> + 'a; | 258 | type ErasePageFuture<'a> where Self: 'a = impl Future<Output = Result<(), Error>> + 'a; |
| 254 | 259 | ||
| 255 | fn read<'a>( | 260 | fn read<'a>(&'a mut self, address: usize, data: &'a mut [u8]) -> Self::ReadFuture<'a> { |
| 256 | mut self: Pin<&'a mut Self>, | ||
| 257 | address: usize, | ||
| 258 | data: &'a mut [u8], | ||
| 259 | ) -> Self::ReadFuture<'a> { | ||
| 260 | async move { | 261 | async move { |
| 261 | let bomb = DropBomb::new(); | 262 | let bomb = DropBomb::new(); |
| 262 | 263 | ||
| @@ -264,7 +265,7 @@ impl<'d, T: Instance> Flash for Qspi<'d, T> { | |||
| 264 | assert_eq!(data.len() as u32 % 4, 0); | 265 | assert_eq!(data.len() as u32 % 4, 0); |
| 265 | assert_eq!(address as u32 % 4, 0); | 266 | assert_eq!(address as u32 % 4, 0); |
| 266 | 267 | ||
| 267 | let r = unsafe { self.as_mut().get_unchecked_mut() }.peri.regs(); | 268 | let r = T::regs(); |
| 268 | 269 | ||
| 269 | r.read | 270 | r.read |
| 270 | .src | 271 | .src |
| @@ -280,7 +281,7 @@ impl<'d, T: Instance> Flash for Qspi<'d, T> { | |||
| 280 | r.intenset.write(|w| w.ready().set()); | 281 | r.intenset.write(|w| w.ready().set()); |
| 281 | r.tasks_readstart.write(|w| w.tasks_readstart().bit(true)); | 282 | r.tasks_readstart.write(|w| w.tasks_readstart().bit(true)); |
| 282 | 283 | ||
| 283 | self.as_mut().wait_ready().await; | 284 | self.wait_ready().await; |
| 284 | 285 | ||
| 285 | bomb.defuse(); | 286 | bomb.defuse(); |
| 286 | 287 | ||
| @@ -288,11 +289,7 @@ impl<'d, T: Instance> Flash for Qspi<'d, T> { | |||
| 288 | } | 289 | } |
| 289 | } | 290 | } |
| 290 | 291 | ||
| 291 | fn write<'a>( | 292 | fn write<'a>(&'a mut self, address: usize, data: &'a [u8]) -> Self::WriteFuture<'a> { |
| 292 | mut self: Pin<&'a mut Self>, | ||
| 293 | address: usize, | ||
| 294 | data: &'a [u8], | ||
| 295 | ) -> Self::WriteFuture<'a> { | ||
| 296 | async move { | 293 | async move { |
| 297 | let bomb = DropBomb::new(); | 294 | let bomb = DropBomb::new(); |
| 298 | 295 | ||
| @@ -300,7 +297,7 @@ impl<'d, T: Instance> Flash for Qspi<'d, T> { | |||
| 300 | assert_eq!(data.len() as u32 % 4, 0); | 297 | assert_eq!(data.len() as u32 % 4, 0); |
| 301 | assert_eq!(address as u32 % 4, 0); | 298 | assert_eq!(address as u32 % 4, 0); |
| 302 | 299 | ||
| 303 | let r = unsafe { self.as_mut().get_unchecked_mut() }.peri.regs(); | 300 | let r = T::regs(); |
| 304 | r.write | 301 | r.write |
| 305 | .src | 302 | .src |
| 306 | .write(|w| unsafe { w.src().bits(data.as_ptr() as u32) }); | 303 | .write(|w| unsafe { w.src().bits(data.as_ptr() as u32) }); |
| @@ -315,7 +312,7 @@ impl<'d, T: Instance> Flash for Qspi<'d, T> { | |||
| 315 | r.intenset.write(|w| w.ready().set()); | 312 | r.intenset.write(|w| w.ready().set()); |
| 316 | r.tasks_writestart.write(|w| w.tasks_writestart().bit(true)); | 313 | r.tasks_writestart.write(|w| w.tasks_writestart().bit(true)); |
| 317 | 314 | ||
| 318 | self.as_mut().wait_ready().await; | 315 | self.wait_ready().await; |
| 319 | 316 | ||
| 320 | bomb.defuse(); | 317 | bomb.defuse(); |
| 321 | 318 | ||
| @@ -323,13 +320,13 @@ impl<'d, T: Instance> Flash for Qspi<'d, T> { | |||
| 323 | } | 320 | } |
| 324 | } | 321 | } |
| 325 | 322 | ||
| 326 | fn erase<'a>(mut self: Pin<&'a mut Self>, address: usize) -> Self::ErasePageFuture<'a> { | 323 | fn erase<'a>(&'a mut self, address: usize) -> Self::ErasePageFuture<'a> { |
| 327 | async move { | 324 | async move { |
| 328 | let bomb = DropBomb::new(); | 325 | let bomb = DropBomb::new(); |
| 329 | 326 | ||
| 330 | assert_eq!(address as u32 % 4096, 0); | 327 | assert_eq!(address as u32 % 4096, 0); |
| 331 | 328 | ||
| 332 | let r = unsafe { self.as_mut().get_unchecked_mut() }.peri.regs(); | 329 | let r = T::regs(); |
| 333 | r.erase | 330 | r.erase |
| 334 | .ptr | 331 | .ptr |
| 335 | .write(|w| unsafe { w.ptr().bits(address as u32) }); | 332 | .write(|w| unsafe { w.ptr().bits(address as u32) }); |
| @@ -339,7 +336,7 @@ impl<'d, T: Instance> Flash for Qspi<'d, T> { | |||
| 339 | r.intenset.write(|w| w.ready().set()); | 336 | r.intenset.write(|w| w.ready().set()); |
| 340 | r.tasks_erasestart.write(|w| w.tasks_erasestart().bit(true)); | 337 | r.tasks_erasestart.write(|w| w.tasks_erasestart().bit(true)); |
| 341 | 338 | ||
| 342 | self.as_mut().wait_ready().await; | 339 | self.wait_ready().await; |
| 343 | 340 | ||
| 344 | bomb.defuse(); | 341 | bomb.defuse(); |
| 345 | 342 | ||
| @@ -367,8 +364,20 @@ impl<'d, T: Instance> Flash for Qspi<'d, T> { | |||
| 367 | mod sealed { | 364 | mod sealed { |
| 368 | use super::*; | 365 | use super::*; |
| 369 | 366 | ||
| 367 | pub struct State { | ||
| 368 | pub ready_waker: AtomicWaker, | ||
| 369 | } | ||
| 370 | impl State { | ||
| 371 | pub const fn new() -> Self { | ||
| 372 | Self { | ||
| 373 | ready_waker: AtomicWaker::new(), | ||
| 374 | } | ||
| 375 | } | ||
| 376 | } | ||
| 377 | |||
| 370 | pub trait Instance { | 378 | pub trait Instance { |
| 371 | fn regs(&self) -> &pac::qspi::RegisterBlock; | 379 | fn regs() -> &'static pac::qspi::RegisterBlock; |
| 380 | fn state() -> &'static State; | ||
| 372 | } | 381 | } |
| 373 | } | 382 | } |
| 374 | 383 | ||
| @@ -379,9 +388,13 @@ pub trait Instance: sealed::Instance + 'static { | |||
| 379 | macro_rules! impl_instance { | 388 | macro_rules! impl_instance { |
| 380 | ($type:ident, $irq:ident) => { | 389 | ($type:ident, $irq:ident) => { |
| 381 | impl sealed::Instance for peripherals::$type { | 390 | impl sealed::Instance for peripherals::$type { |
| 382 | fn regs(&self) -> &pac::qspi::RegisterBlock { | 391 | fn regs() -> &'static pac::qspi::RegisterBlock { |
| 383 | unsafe { &*pac::$type::ptr() } | 392 | unsafe { &*pac::$type::ptr() } |
| 384 | } | 393 | } |
| 394 | fn state() -> &'static sealed::State { | ||
| 395 | static STATE: sealed::State = sealed::State::new(); | ||
| 396 | &STATE | ||
| 397 | } | ||
| 385 | } | 398 | } |
| 386 | impl Instance for peripherals::$type { | 399 | impl Instance for peripherals::$type { |
| 387 | type Interrupt = interrupt::$irq; | 400 | type Interrupt = interrupt::$irq; |
diff --git a/embassy-nrf/src/spim.rs b/embassy-nrf/src/spim.rs index 93ca52c63..bbe1eedf9 100644 --- a/embassy-nrf/src/spim.rs +++ b/embassy-nrf/src/spim.rs | |||
| @@ -1,10 +1,10 @@ | |||
| 1 | use core::future::Future; | 1 | use core::future::Future; |
| 2 | use core::marker::PhantomData; | 2 | use core::marker::PhantomData; |
| 3 | use core::pin::Pin; | ||
| 4 | use core::sync::atomic::{compiler_fence, Ordering}; | 3 | use core::sync::atomic::{compiler_fence, Ordering}; |
| 5 | use core::task::Poll; | 4 | use core::task::Poll; |
| 5 | use embassy::interrupt::InterruptExt; | ||
| 6 | use embassy::traits; | 6 | use embassy::traits; |
| 7 | use embassy::util::{wake_on_interrupt, PeripheralBorrow}; | 7 | use embassy::util::{AtomicWaker, PeripheralBorrow}; |
| 8 | use embassy_extras::unborrow; | 8 | use embassy_extras::unborrow; |
| 9 | use futures::future::poll_fn; | 9 | use futures::future::poll_fn; |
| 10 | use traits::spi::FullDuplex; | 10 | use traits::spi::FullDuplex; |
| @@ -50,7 +50,7 @@ impl<'d, T: Instance> Spim<'d, T> { | |||
| 50 | ) -> Self { | 50 | ) -> Self { |
| 51 | unborrow!(spim, irq, sck, miso, mosi); | 51 | unborrow!(spim, irq, sck, miso, mosi); |
| 52 | 52 | ||
| 53 | let r = spim.regs(); | 53 | let r = T::regs(); |
| 54 | 54 | ||
| 55 | // Configure pins | 55 | // Configure pins |
| 56 | sck.conf().write(|w| w.dir().output().drive().h0h1()); | 56 | sck.conf().write(|w| w.dir().output().drive().h0h1()); |
| @@ -122,12 +122,26 @@ impl<'d, T: Instance> Spim<'d, T> { | |||
| 122 | // Disable all events interrupts | 122 | // Disable all events interrupts |
| 123 | r.intenclr.write(|w| unsafe { w.bits(0xFFFF_FFFF) }); | 123 | r.intenclr.write(|w| unsafe { w.bits(0xFFFF_FFFF) }); |
| 124 | 124 | ||
| 125 | irq.set_handler(Self::on_interrupt); | ||
| 126 | irq.unpend(); | ||
| 127 | irq.enable(); | ||
| 128 | |||
| 125 | Self { | 129 | Self { |
| 126 | peri: spim, | 130 | peri: spim, |
| 127 | irq, | 131 | irq, |
| 128 | phantom: PhantomData, | 132 | phantom: PhantomData, |
| 129 | } | 133 | } |
| 130 | } | 134 | } |
| 135 | |||
| 136 | fn on_interrupt(_: *mut ()) { | ||
| 137 | let r = T::regs(); | ||
| 138 | let s = T::state(); | ||
| 139 | |||
| 140 | if r.events_end.read().bits() != 0 { | ||
| 141 | s.end_waker.wake(); | ||
| 142 | r.intenclr.write(|w| w.end().clear()); | ||
| 143 | } | ||
| 144 | } | ||
| 131 | } | 145 | } |
| 132 | 146 | ||
| 133 | impl<'d, T: Instance> FullDuplex<u8> for Spim<'d, T> { | 147 | impl<'d, T: Instance> FullDuplex<u8> for Spim<'d, T> { |
| @@ -140,20 +154,15 @@ impl<'d, T: Instance> FullDuplex<u8> for Spim<'d, T> { | |||
| 140 | #[rustfmt::skip] | 154 | #[rustfmt::skip] |
| 141 | type WriteReadFuture<'a> where Self: 'a = impl Future<Output = Result<(), Self::Error>> + 'a; | 155 | type WriteReadFuture<'a> where Self: 'a = impl Future<Output = Result<(), Self::Error>> + 'a; |
| 142 | 156 | ||
| 143 | fn read<'a>(self: Pin<&'a mut Self>, data: &'a mut [u8]) -> Self::ReadFuture<'a> { | 157 | fn read<'a>(&'a mut self, data: &'a mut [u8]) -> Self::ReadFuture<'a> { |
| 144 | self.read_write(data, &[]) | 158 | self.read_write(data, &[]) |
| 145 | } | 159 | } |
| 146 | fn write<'a>(self: Pin<&'a mut Self>, data: &'a [u8]) -> Self::WriteFuture<'a> { | 160 | fn write<'a>(&'a mut self, data: &'a [u8]) -> Self::WriteFuture<'a> { |
| 147 | self.read_write(&mut [], data) | 161 | self.read_write(&mut [], data) |
| 148 | } | 162 | } |
| 149 | 163 | ||
| 150 | fn read_write<'a>( | 164 | fn read_write<'a>(&'a mut self, rx: &'a mut [u8], tx: &'a [u8]) -> Self::WriteReadFuture<'a> { |
| 151 | self: Pin<&'a mut Self>, | ||
| 152 | rx: &'a mut [u8], | ||
| 153 | tx: &'a [u8], | ||
| 154 | ) -> Self::WriteReadFuture<'a> { | ||
| 155 | async move { | 165 | async move { |
| 156 | let this = unsafe { self.get_unchecked_mut() }; | ||
| 157 | slice_in_ram_or(rx, Error::DMABufferNotInDataMemory)?; | 166 | slice_in_ram_or(rx, Error::DMABufferNotInDataMemory)?; |
| 158 | slice_in_ram_or(tx, Error::DMABufferNotInDataMemory)?; | 167 | slice_in_ram_or(tx, Error::DMABufferNotInDataMemory)?; |
| 159 | 168 | ||
| @@ -162,7 +171,8 @@ impl<'d, T: Instance> FullDuplex<u8> for Spim<'d, T> { | |||
| 162 | // before any DMA action has started. | 171 | // before any DMA action has started. |
| 163 | compiler_fence(Ordering::SeqCst); | 172 | compiler_fence(Ordering::SeqCst); |
| 164 | 173 | ||
| 165 | let r = this.peri.regs(); | 174 | let r = T::regs(); |
| 175 | let s = T::state(); | ||
| 166 | 176 | ||
| 167 | // Set up the DMA write. | 177 | // Set up the DMA write. |
| 168 | r.txd | 178 | r.txd |
| @@ -194,15 +204,11 @@ impl<'d, T: Instance> FullDuplex<u8> for Spim<'d, T> { | |||
| 194 | 204 | ||
| 195 | // Wait for 'end' event. | 205 | // Wait for 'end' event. |
| 196 | poll_fn(|cx| { | 206 | poll_fn(|cx| { |
| 197 | let r = this.peri.regs(); | 207 | s.end_waker.register(cx.waker()); |
| 198 | |||
| 199 | if r.events_end.read().bits() != 0 { | 208 | if r.events_end.read().bits() != 0 { |
| 200 | r.events_end.reset(); | ||
| 201 | return Poll::Ready(()); | 209 | return Poll::Ready(()); |
| 202 | } | 210 | } |
| 203 | 211 | ||
| 204 | wake_on_interrupt(&mut this.irq, cx.waker()); | ||
| 205 | |||
| 206 | Poll::Pending | 212 | Poll::Pending |
| 207 | }) | 213 | }) |
| 208 | .await; | 214 | .await; |
| @@ -215,8 +221,21 @@ impl<'d, T: Instance> FullDuplex<u8> for Spim<'d, T> { | |||
| 215 | mod sealed { | 221 | mod sealed { |
| 216 | use super::*; | 222 | use super::*; |
| 217 | 223 | ||
| 224 | pub struct State { | ||
| 225 | pub end_waker: AtomicWaker, | ||
| 226 | } | ||
| 227 | |||
| 228 | impl State { | ||
| 229 | pub const fn new() -> Self { | ||
| 230 | Self { | ||
| 231 | end_waker: AtomicWaker::new(), | ||
| 232 | } | ||
| 233 | } | ||
| 234 | } | ||
| 235 | |||
| 218 | pub trait Instance { | 236 | pub trait Instance { |
| 219 | fn regs(&self) -> &pac::spim0::RegisterBlock; | 237 | fn regs() -> &'static pac::spim0::RegisterBlock; |
| 238 | fn state() -> &'static State; | ||
| 220 | } | 239 | } |
| 221 | } | 240 | } |
| 222 | 241 | ||
| @@ -227,9 +246,13 @@ pub trait Instance: sealed::Instance + 'static { | |||
| 227 | macro_rules! impl_instance { | 246 | macro_rules! impl_instance { |
| 228 | ($type:ident, $irq:ident) => { | 247 | ($type:ident, $irq:ident) => { |
| 229 | impl sealed::Instance for peripherals::$type { | 248 | impl sealed::Instance for peripherals::$type { |
| 230 | fn regs(&self) -> &pac::spim0::RegisterBlock { | 249 | fn regs() -> &'static pac::spim0::RegisterBlock { |
| 231 | unsafe { &*pac::$type::ptr() } | 250 | unsafe { &*pac::$type::ptr() } |
| 232 | } | 251 | } |
| 252 | fn state() -> &'static sealed::State { | ||
| 253 | static STATE: sealed::State = sealed::State::new(); | ||
| 254 | &STATE | ||
| 255 | } | ||
| 233 | } | 256 | } |
| 234 | impl Instance for peripherals::$type { | 257 | impl Instance for peripherals::$type { |
| 235 | type Interrupt = interrupt::$irq; | 258 | type Interrupt = interrupt::$irq; |
diff --git a/embassy-nrf/src/uarte.rs b/embassy-nrf/src/uarte.rs index 957fa4c71..9e485907c 100644 --- a/embassy-nrf/src/uarte.rs +++ b/embassy-nrf/src/uarte.rs | |||
| @@ -2,12 +2,11 @@ | |||
| 2 | 2 | ||
| 3 | use core::future::Future; | 3 | use core::future::Future; |
| 4 | use core::marker::PhantomData; | 4 | use core::marker::PhantomData; |
| 5 | use core::pin::Pin; | 5 | use core::sync::atomic::{compiler_fence, Ordering}; |
| 6 | use core::sync::atomic::{compiler_fence, AtomicBool, Ordering}; | ||
| 7 | use core::task::Poll; | 6 | use core::task::Poll; |
| 7 | use embassy::interrupt::InterruptExt; | ||
| 8 | use embassy::traits::uart::{Error, Read, Write}; | 8 | use embassy::traits::uart::{Error, Read, Write}; |
| 9 | use embassy::util::{AtomicWaker, OnDrop, PeripheralBorrow}; | 9 | use embassy::util::{AtomicWaker, OnDrop, PeripheralBorrow}; |
| 10 | use embassy_extras::peripheral_shared::{Peripheral, PeripheralState}; | ||
| 11 | use embassy_extras::unborrow; | 10 | use embassy_extras::unborrow; |
| 12 | use futures::future::poll_fn; | 11 | use futures::future::poll_fn; |
| 13 | 12 | ||
| @@ -38,16 +37,9 @@ impl Default for Config { | |||
| 38 | } | 37 | } |
| 39 | } | 38 | } |
| 40 | 39 | ||
| 41 | struct State<T: Instance> { | ||
| 42 | peri: T, | ||
| 43 | |||
| 44 | endrx_waker: AtomicWaker, | ||
| 45 | endtx_waker: AtomicWaker, | ||
| 46 | } | ||
| 47 | |||
| 48 | /// Interface to the UARTE peripheral | 40 | /// Interface to the UARTE peripheral |
| 49 | pub struct Uarte<'d, T: Instance> { | 41 | pub struct Uarte<'d, T: Instance> { |
| 50 | inner: Peripheral<State<T>>, | 42 | peri: T, |
| 51 | phantom: PhantomData<&'d mut T>, | 43 | phantom: PhantomData<&'d mut T>, |
| 52 | } | 44 | } |
| 53 | 45 | ||
| @@ -72,7 +64,7 @@ impl<'d, T: Instance> Uarte<'d, T> { | |||
| 72 | ) -> Self { | 64 | ) -> Self { |
| 73 | unborrow!(uarte, irq, rxd, txd, cts, rts); | 65 | unborrow!(uarte, irq, rxd, txd, cts, rts); |
| 74 | 66 | ||
| 75 | let r = uarte.regs(); | 67 | let r = T::regs(); |
| 76 | 68 | ||
| 77 | assert!(r.enable.read().enable().is_disabled()); | 69 | assert!(r.enable.read().enable().is_disabled()); |
| 78 | 70 | ||
| @@ -115,38 +107,29 @@ impl<'d, T: Instance> Uarte<'d, T> { | |||
| 115 | r.events_rxstarted.reset(); | 107 | r.events_rxstarted.reset(); |
| 116 | r.events_txstarted.reset(); | 108 | r.events_txstarted.reset(); |
| 117 | 109 | ||
| 110 | irq.set_handler(Self::on_interrupt); | ||
| 111 | irq.unpend(); | ||
| 112 | irq.enable(); | ||
| 113 | |||
| 118 | // Enable | 114 | // Enable |
| 119 | r.enable.write(|w| w.enable().enabled()); | 115 | r.enable.write(|w| w.enable().enabled()); |
| 120 | 116 | ||
| 121 | Self { | 117 | Self { |
| 122 | inner: Peripheral::new( | 118 | peri: uarte, |
| 123 | irq, | ||
| 124 | State { | ||
| 125 | peri: uarte, | ||
| 126 | endrx_waker: AtomicWaker::new(), | ||
| 127 | endtx_waker: AtomicWaker::new(), | ||
| 128 | }, | ||
| 129 | ), | ||
| 130 | phantom: PhantomData, | 119 | phantom: PhantomData, |
| 131 | } | 120 | } |
| 132 | } | 121 | } |
| 133 | 122 | ||
| 134 | fn inner(self: Pin<&mut Self>) -> Pin<&mut Peripheral<State<T>>> { | 123 | fn on_interrupt(_: *mut ()) { |
| 135 | unsafe { Pin::new_unchecked(&mut self.get_unchecked_mut().inner) } | 124 | let r = T::regs(); |
| 136 | } | 125 | let s = T::state(); |
| 137 | } | ||
| 138 | |||
| 139 | impl<T: Instance> PeripheralState for State<T> { | ||
| 140 | type Interrupt = T::Interrupt; | ||
| 141 | 126 | ||
| 142 | fn on_interrupt(&self) { | ||
| 143 | let r = self.peri.regs(); | ||
| 144 | if r.events_endrx.read().bits() != 0 { | 127 | if r.events_endrx.read().bits() != 0 { |
| 145 | self.endrx_waker.wake(); | 128 | s.endrx_waker.wake(); |
| 146 | r.intenclr.write(|w| w.endrx().clear()); | 129 | r.intenclr.write(|w| w.endrx().clear()); |
| 147 | } | 130 | } |
| 148 | if r.events_endtx.read().bits() != 0 { | 131 | if r.events_endtx.read().bits() != 0 { |
| 149 | self.endtx_waker.wake(); | 132 | s.endtx_waker.wake(); |
| 150 | r.intenclr.write(|w| w.endtx().clear()); | 133 | r.intenclr.write(|w| w.endtx().clear()); |
| 151 | } | 134 | } |
| 152 | 135 | ||
| @@ -163,8 +146,7 @@ impl<'a, T: Instance> Drop for Uarte<'a, T> { | |||
| 163 | fn drop(&mut self) { | 146 | fn drop(&mut self) { |
| 164 | info!("uarte drop"); | 147 | info!("uarte drop"); |
| 165 | 148 | ||
| 166 | let s = unsafe { Pin::new_unchecked(&mut self.inner) }.state(); | 149 | let r = T::regs(); |
| 167 | let r = s.peri.regs(); | ||
| 168 | 150 | ||
| 169 | let did_stoprx = r.events_rxstarted.read().bits() != 0; | 151 | let did_stoprx = r.events_rxstarted.read().bits() != 0; |
| 170 | let did_stoptx = r.events_txstarted.read().bits() != 0; | 152 | let did_stoptx = r.events_txstarted.read().bits() != 0; |
| @@ -194,16 +176,14 @@ impl<'d, T: Instance> Read for Uarte<'d, T> { | |||
| 194 | #[rustfmt::skip] | 176 | #[rustfmt::skip] |
| 195 | type ReadFuture<'a> where Self: 'a = impl Future<Output = Result<(), Error>> + 'a; | 177 | type ReadFuture<'a> where Self: 'a = impl Future<Output = Result<(), Error>> + 'a; |
| 196 | 178 | ||
| 197 | fn read<'a>(mut self: Pin<&'a mut Self>, rx_buffer: &'a mut [u8]) -> Self::ReadFuture<'a> { | 179 | fn read<'a>(&'a mut self, rx_buffer: &'a mut [u8]) -> Self::ReadFuture<'a> { |
| 198 | self.as_mut().inner().register_interrupt(); | ||
| 199 | |||
| 200 | async move { | 180 | async move { |
| 201 | let ptr = rx_buffer.as_ptr(); | 181 | let ptr = rx_buffer.as_ptr(); |
| 202 | let len = rx_buffer.len(); | 182 | let len = rx_buffer.len(); |
| 203 | assert!(len <= EASY_DMA_SIZE); | 183 | assert!(len <= EASY_DMA_SIZE); |
| 204 | 184 | ||
| 205 | let s = self.inner().state(); | 185 | let r = T::regs(); |
| 206 | let r = s.peri.regs(); | 186 | let s = T::state(); |
| 207 | 187 | ||
| 208 | let drop = OnDrop::new(move || { | 188 | let drop = OnDrop::new(move || { |
| 209 | info!("read drop: stopping"); | 189 | info!("read drop: stopping"); |
| @@ -250,17 +230,15 @@ impl<'d, T: Instance> Write for Uarte<'d, T> { | |||
| 250 | #[rustfmt::skip] | 230 | #[rustfmt::skip] |
| 251 | type WriteFuture<'a> where Self: 'a = impl Future<Output = Result<(), Error>> + 'a; | 231 | type WriteFuture<'a> where Self: 'a = impl Future<Output = Result<(), Error>> + 'a; |
| 252 | 232 | ||
| 253 | fn write<'a>(mut self: Pin<&'a mut Self>, tx_buffer: &'a [u8]) -> Self::WriteFuture<'a> { | 233 | fn write<'a>(&'a mut self, tx_buffer: &'a [u8]) -> Self::WriteFuture<'a> { |
| 254 | self.as_mut().inner().register_interrupt(); | ||
| 255 | |||
| 256 | async move { | 234 | async move { |
| 257 | let ptr = tx_buffer.as_ptr(); | 235 | let ptr = tx_buffer.as_ptr(); |
| 258 | let len = tx_buffer.len(); | 236 | let len = tx_buffer.len(); |
| 259 | assert!(len <= EASY_DMA_SIZE); | 237 | assert!(len <= EASY_DMA_SIZE); |
| 260 | // TODO: panic if buffer is not in SRAM | 238 | // TODO: panic if buffer is not in SRAM |
| 261 | 239 | ||
| 262 | let s = self.inner().state(); | 240 | let r = T::regs(); |
| 263 | let r = s.peri.regs(); | 241 | let s = T::state(); |
| 264 | 242 | ||
| 265 | let drop = OnDrop::new(move || { | 243 | let drop = OnDrop::new(move || { |
| 266 | info!("write drop: stopping"); | 244 | info!("write drop: stopping"); |
| @@ -306,8 +284,22 @@ impl<'d, T: Instance> Write for Uarte<'d, T> { | |||
| 306 | mod sealed { | 284 | mod sealed { |
| 307 | use super::*; | 285 | use super::*; |
| 308 | 286 | ||
| 287 | pub struct State { | ||
| 288 | pub endrx_waker: AtomicWaker, | ||
| 289 | pub endtx_waker: AtomicWaker, | ||
| 290 | } | ||
| 291 | impl State { | ||
| 292 | pub const fn new() -> Self { | ||
| 293 | Self { | ||
| 294 | endrx_waker: AtomicWaker::new(), | ||
| 295 | endtx_waker: AtomicWaker::new(), | ||
| 296 | } | ||
| 297 | } | ||
| 298 | } | ||
| 299 | |||
| 309 | pub trait Instance { | 300 | pub trait Instance { |
| 310 | fn regs(&self) -> &pac::uarte0::RegisterBlock; | 301 | fn regs() -> &'static pac::uarte0::RegisterBlock; |
| 302 | fn state() -> &'static State; | ||
| 311 | } | 303 | } |
| 312 | } | 304 | } |
| 313 | 305 | ||
| @@ -318,9 +310,13 @@ pub trait Instance: sealed::Instance + 'static { | |||
| 318 | macro_rules! impl_instance { | 310 | macro_rules! impl_instance { |
| 319 | ($type:ident, $irq:ident) => { | 311 | ($type:ident, $irq:ident) => { |
| 320 | impl sealed::Instance for peripherals::$type { | 312 | impl sealed::Instance for peripherals::$type { |
| 321 | fn regs(&self) -> &pac::uarte0::RegisterBlock { | 313 | fn regs() -> &'static pac::uarte0::RegisterBlock { |
| 322 | unsafe { &*pac::$type::ptr() } | 314 | unsafe { &*pac::$type::ptr() } |
| 323 | } | 315 | } |
| 316 | fn state() -> &'static sealed::State { | ||
| 317 | static STATE: sealed::State = sealed::State::new(); | ||
| 318 | &STATE | ||
| 319 | } | ||
| 324 | } | 320 | } |
| 325 | impl Instance for peripherals::$type { | 321 | impl Instance for peripherals::$type { |
| 326 | type Interrupt = interrupt::$irq; | 322 | type Interrupt = interrupt::$irq; |
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-stm32/src/f4/serial.rs b/embassy-stm32/src/f4/serial.rs index 7539abf51..78aaa8944 100644 --- a/embassy-stm32/src/f4/serial.rs +++ b/embassy-stm32/src/f4/serial.rs | |||
| @@ -2,7 +2,6 @@ | |||
| 2 | 2 | ||
| 3 | use core::future::Future; | 3 | use core::future::Future; |
| 4 | use core::marker::PhantomData; | 4 | use core::marker::PhantomData; |
| 5 | use core::pin::Pin; | ||
| 6 | use embassy::interrupt::Interrupt; | 5 | use embassy::interrupt::Interrupt; |
| 7 | use embassy::traits::uart::{Error, Read, ReadUntilIdle, Write}; | 6 | use embassy::traits::uart::{Error, Read, ReadUntilIdle, Write}; |
| 8 | use embassy::util::InterruptFuture; | 7 | use embassy::util::InterruptFuture; |
| @@ -101,13 +100,12 @@ where | |||
| 101 | /// Receives serial data. | 100 | /// Receives serial data. |
| 102 | /// | 101 | /// |
| 103 | /// The future is pending until the buffer is completely filled. | 102 | /// The future is pending until the buffer is completely filled. |
| 104 | fn read<'a>(self: Pin<&'a mut Self>, buf: &'a mut [u8]) -> Self::ReadFuture<'a> { | 103 | fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::ReadFuture<'a> { |
| 105 | let this = unsafe { self.get_unchecked_mut() }; | ||
| 106 | let static_buf = unsafe { core::mem::transmute::<&'a mut [u8], &'static mut [u8]>(buf) }; | 104 | let static_buf = unsafe { core::mem::transmute::<&'a mut [u8], &'static mut [u8]>(buf) }; |
| 107 | 105 | ||
| 108 | async move { | 106 | async move { |
| 109 | let rx_stream = this.rx_stream.take().unwrap(); | 107 | let rx_stream = self.rx_stream.take().unwrap(); |
| 110 | let usart = this.usart.take().unwrap(); | 108 | let usart = self.usart.take().unwrap(); |
| 111 | 109 | ||
| 112 | let mut rx_transfer = Transfer::init( | 110 | let mut rx_transfer = Transfer::init( |
| 113 | rx_stream, | 111 | rx_stream, |
| @@ -120,13 +118,13 @@ where | |||
| 120 | .double_buffer(false), | 118 | .double_buffer(false), |
| 121 | ); | 119 | ); |
| 122 | 120 | ||
| 123 | let fut = InterruptFuture::new(&mut this.rx_int); | 121 | let fut = InterruptFuture::new(&mut self.rx_int); |
| 124 | rx_transfer.start(|_usart| {}); | 122 | rx_transfer.start(|_usart| {}); |
| 125 | fut.await; | 123 | fut.await; |
| 126 | 124 | ||
| 127 | let (rx_stream, usart, _, _) = rx_transfer.free(); | 125 | let (rx_stream, usart, _, _) = rx_transfer.free(); |
| 128 | this.rx_stream.replace(rx_stream); | 126 | self.rx_stream.replace(rx_stream); |
| 129 | this.usart.replace(usart); | 127 | self.usart.replace(usart); |
| 130 | 128 | ||
| 131 | Ok(()) | 129 | Ok(()) |
| 132 | } | 130 | } |
| @@ -148,14 +146,13 @@ where | |||
| 148 | type WriteFuture<'a> = impl Future<Output = Result<(), Error>> + 'a; | 146 | type WriteFuture<'a> = impl Future<Output = Result<(), Error>> + 'a; |
| 149 | 147 | ||
| 150 | /// Sends serial data. | 148 | /// Sends serial data. |
| 151 | fn write<'a>(self: Pin<&'a mut Self>, buf: &'a [u8]) -> Self::WriteFuture<'a> { | 149 | fn write<'a>(&'a mut self, buf: &'a [u8]) -> Self::WriteFuture<'a> { |
| 152 | let this = unsafe { self.get_unchecked_mut() }; | ||
| 153 | #[allow(mutable_transmutes)] | 150 | #[allow(mutable_transmutes)] |
| 154 | let static_buf = unsafe { core::mem::transmute::<&'a [u8], &'static mut [u8]>(buf) }; | 151 | let static_buf = unsafe { core::mem::transmute::<&'a [u8], &'static mut [u8]>(buf) }; |
| 155 | 152 | ||
| 156 | async move { | 153 | async move { |
| 157 | let tx_stream = this.tx_stream.take().unwrap(); | 154 | let tx_stream = self.tx_stream.take().unwrap(); |
| 158 | let usart = this.usart.take().unwrap(); | 155 | let usart = self.usart.take().unwrap(); |
| 159 | 156 | ||
| 160 | let mut tx_transfer = Transfer::init( | 157 | let mut tx_transfer = Transfer::init( |
| 161 | tx_stream, | 158 | tx_stream, |
| @@ -168,15 +165,15 @@ where | |||
| 168 | .double_buffer(false), | 165 | .double_buffer(false), |
| 169 | ); | 166 | ); |
| 170 | 167 | ||
| 171 | let fut = InterruptFuture::new(&mut this.tx_int); | 168 | let fut = InterruptFuture::new(&mut self.tx_int); |
| 172 | 169 | ||
| 173 | tx_transfer.start(|_usart| {}); | 170 | tx_transfer.start(|_usart| {}); |
| 174 | fut.await; | 171 | fut.await; |
| 175 | 172 | ||
| 176 | let (tx_stream, usart, _buf, _) = tx_transfer.free(); | 173 | let (tx_stream, usart, _buf, _) = tx_transfer.free(); |
| 177 | 174 | ||
| 178 | this.tx_stream.replace(tx_stream); | 175 | self.tx_stream.replace(tx_stream); |
| 179 | this.usart.replace(usart); | 176 | self.usart.replace(usart); |
| 180 | 177 | ||
| 181 | Ok(()) | 178 | Ok(()) |
| 182 | } | 179 | } |
| @@ -202,16 +199,12 @@ where | |||
| 202 | /// The future is pending until either the buffer is completely full, or the RX line falls idle after receiving some data. | 199 | /// The future is pending until either the buffer is completely full, or the RX line falls idle after receiving some data. |
| 203 | /// | 200 | /// |
| 204 | /// Returns the number of bytes read. | 201 | /// Returns the number of bytes read. |
| 205 | fn read_until_idle<'a>( | 202 | fn read_until_idle<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::ReadUntilIdleFuture<'a> { |
| 206 | self: Pin<&'a mut Self>, | ||
| 207 | buf: &'a mut [u8], | ||
| 208 | ) -> Self::ReadUntilIdleFuture<'a> { | ||
| 209 | let this = unsafe { self.get_unchecked_mut() }; | ||
| 210 | let static_buf = unsafe { core::mem::transmute::<&'a mut [u8], &'static mut [u8]>(buf) }; | 203 | let static_buf = unsafe { core::mem::transmute::<&'a mut [u8], &'static mut [u8]>(buf) }; |
| 211 | 204 | ||
| 212 | async move { | 205 | async move { |
| 213 | let rx_stream = this.rx_stream.take().unwrap(); | 206 | let rx_stream = self.rx_stream.take().unwrap(); |
| 214 | let usart = this.usart.take().unwrap(); | 207 | let usart = self.usart.take().unwrap(); |
| 215 | 208 | ||
| 216 | unsafe { | 209 | unsafe { |
| 217 | /* __HAL_UART_ENABLE_IT(&uart->UartHandle, UART_IT_IDLE); */ | 210 | /* __HAL_UART_ENABLE_IT(&uart->UartHandle, UART_IT_IDLE); */ |
| @@ -235,8 +228,8 @@ where | |||
| 235 | 228 | ||
| 236 | let total_bytes = RSTREAM::get_number_of_transfers() as usize; | 229 | let total_bytes = RSTREAM::get_number_of_transfers() as usize; |
| 237 | 230 | ||
| 238 | let fut = InterruptFuture::new(&mut this.rx_int); | 231 | let fut = InterruptFuture::new(&mut self.rx_int); |
| 239 | let fut_idle = InterruptFuture::new(&mut this.usart_int); | 232 | let fut_idle = InterruptFuture::new(&mut self.usart_int); |
| 240 | 233 | ||
| 241 | rx_transfer.start(|_usart| {}); | 234 | rx_transfer.start(|_usart| {}); |
| 242 | 235 | ||
| @@ -249,8 +242,8 @@ where | |||
| 249 | unsafe { | 242 | unsafe { |
| 250 | (*USART::ptr()).cr1.modify(|_, w| w.idleie().clear_bit()); | 243 | (*USART::ptr()).cr1.modify(|_, w| w.idleie().clear_bit()); |
| 251 | } | 244 | } |
| 252 | this.rx_stream.replace(rx_stream); | 245 | self.rx_stream.replace(rx_stream); |
| 253 | this.usart.replace(usart); | 246 | self.usart.replace(usart); |
| 254 | 247 | ||
| 255 | Ok(total_bytes - remaining_bytes) | 248 | Ok(total_bytes - remaining_bytes) |
| 256 | } | 249 | } |
diff --git a/embassy-stm32/src/f4/spi.rs b/embassy-stm32/src/f4/spi.rs index bc73611fd..65bf7287a 100644 --- a/embassy-stm32/src/f4/spi.rs +++ b/embassy-stm32/src/f4/spi.rs | |||
| @@ -214,14 +214,13 @@ where | |||
| 214 | type ReadFuture<'a> = impl Future<Output = Result<(), Error>> + 'a; | 214 | type ReadFuture<'a> = impl Future<Output = Result<(), Error>> + 'a; |
| 215 | type WriteReadFuture<'a> = impl Future<Output = Result<(), Error>> + 'a; | 215 | type WriteReadFuture<'a> = impl Future<Output = Result<(), Error>> + 'a; |
| 216 | 216 | ||
| 217 | fn read<'a>(self: Pin<&'a mut Self>, buf: &'a mut [u8]) -> Self::ReadFuture<'a> { | 217 | fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::ReadFuture<'a> { |
| 218 | let this = unsafe { self.get_unchecked_mut() }; | ||
| 219 | #[allow(mutable_transmutes)] | 218 | #[allow(mutable_transmutes)] |
| 220 | let static_buf: &'static mut [u8] = unsafe { mem::transmute(buf) }; | 219 | let static_buf: &'static mut [u8] = unsafe { mem::transmute(buf) }; |
| 221 | 220 | ||
| 222 | async move { | 221 | async move { |
| 223 | let rx_stream = this.rx_stream.take().unwrap(); | 222 | let rx_stream = self.rx_stream.take().unwrap(); |
| 224 | let spi = this.spi.take().unwrap(); | 223 | let spi = self.spi.take().unwrap(); |
| 225 | 224 | ||
| 226 | spi.cr2.modify(|_, w| w.errie().set_bit()); | 225 | spi.cr2.modify(|_, w| w.errie().set_bit()); |
| 227 | 226 | ||
| @@ -236,8 +235,8 @@ where | |||
| 236 | .double_buffer(false), | 235 | .double_buffer(false), |
| 237 | ); | 236 | ); |
| 238 | 237 | ||
| 239 | let fut = InterruptFuture::new(&mut this.rx_int); | 238 | let fut = InterruptFuture::new(&mut self.rx_int); |
| 240 | let fut_err = InterruptFuture::new(&mut this.spi_int); | 239 | let fut_err = InterruptFuture::new(&mut self.spi_int); |
| 241 | 240 | ||
| 242 | rx_transfer.start(|_spi| {}); | 241 | rx_transfer.start(|_spi| {}); |
| 243 | future::select(fut, fut_err).await; | 242 | future::select(fut, fut_err).await; |
| @@ -245,21 +244,20 @@ where | |||
| 245 | let (rx_stream, spi, _buf, _) = rx_transfer.free(); | 244 | let (rx_stream, spi, _buf, _) = rx_transfer.free(); |
| 246 | 245 | ||
| 247 | spi.cr2.modify(|_, w| w.errie().clear_bit()); | 246 | spi.cr2.modify(|_, w| w.errie().clear_bit()); |
| 248 | this.rx_stream.replace(rx_stream); | 247 | self.rx_stream.replace(rx_stream); |
| 249 | this.spi.replace(spi); | 248 | self.spi.replace(spi); |
| 250 | 249 | ||
| 251 | Ok(()) | 250 | Ok(()) |
| 252 | } | 251 | } |
| 253 | } | 252 | } |
| 254 | 253 | ||
| 255 | fn write<'a>(self: Pin<&'a mut Self>, buf: &'a [u8]) -> Self::WriteFuture<'a> { | 254 | fn write<'a>(&'a mut self, buf: &'a [u8]) -> Self::WriteFuture<'a> { |
| 256 | let this = unsafe { self.get_unchecked_mut() }; | ||
| 257 | #[allow(mutable_transmutes)] | 255 | #[allow(mutable_transmutes)] |
| 258 | let static_buf: &'static mut [u8] = unsafe { mem::transmute(buf) }; | 256 | let static_buf: &'static mut [u8] = unsafe { mem::transmute(buf) }; |
| 259 | 257 | ||
| 260 | async move { | 258 | async move { |
| 261 | let tx_stream = this.tx_stream.take().unwrap(); | 259 | let tx_stream = self.tx_stream.take().unwrap(); |
| 262 | let spi = this.spi.take().unwrap(); | 260 | let spi = self.spi.take().unwrap(); |
| 263 | 261 | ||
| 264 | // let mut tx_transfer = Transfer::init( | 262 | // let mut tx_transfer = Transfer::init( |
| 265 | // tx_stream, | 263 | // tx_stream, |
| @@ -272,7 +270,7 @@ where | |||
| 272 | // .double_buffer(false), | 270 | // .double_buffer(false), |
| 273 | // ); | 271 | // ); |
| 274 | // | 272 | // |
| 275 | // let fut = InterruptFuture::new(&mut this.tx_int); | 273 | // let fut = InterruptFuture::new(&mut self.tx_int); |
| 276 | // | 274 | // |
| 277 | // tx_transfer.start(|_spi| {}); | 275 | // tx_transfer.start(|_spi| {}); |
| 278 | // fut.await; | 276 | // fut.await; |
| @@ -284,28 +282,26 @@ where | |||
| 284 | nb::block!(write_sr(&spi, byte)); | 282 | nb::block!(write_sr(&spi, byte)); |
| 285 | } | 283 | } |
| 286 | 284 | ||
| 287 | this.tx_stream.replace(tx_stream); | 285 | self.tx_stream.replace(tx_stream); |
| 288 | this.spi.replace(spi); | 286 | self.spi.replace(spi); |
| 289 | 287 | ||
| 290 | Ok(()) | 288 | Ok(()) |
| 291 | } | 289 | } |
| 292 | } | 290 | } |
| 293 | 291 | ||
| 294 | fn read_write<'a>( | 292 | fn read_write<'a>( |
| 295 | self: Pin<&'a mut Self>, | 293 | &'a mut self, |
| 296 | read_buf: &'a mut [u8], | 294 | read_buf: &'a mut [u8], |
| 297 | write_buf: &'a [u8], | 295 | write_buf: &'a [u8], |
| 298 | ) -> Self::WriteReadFuture<'a> { | 296 | ) -> Self::WriteReadFuture<'a> { |
| 299 | let this = unsafe { self.get_unchecked_mut() }; | ||
| 300 | |||
| 301 | #[allow(mutable_transmutes)] | 297 | #[allow(mutable_transmutes)] |
| 302 | let write_static_buf: &'static mut [u8] = unsafe { mem::transmute(write_buf) }; | 298 | let write_static_buf: &'static mut [u8] = unsafe { mem::transmute(write_buf) }; |
| 303 | let read_static_buf: &'static mut [u8] = unsafe { mem::transmute(read_buf) }; | 299 | let read_static_buf: &'static mut [u8] = unsafe { mem::transmute(read_buf) }; |
| 304 | 300 | ||
| 305 | async move { | 301 | async move { |
| 306 | let tx_stream = this.tx_stream.take().unwrap(); | 302 | let tx_stream = self.tx_stream.take().unwrap(); |
| 307 | let rx_stream = this.rx_stream.take().unwrap(); | 303 | let rx_stream = self.rx_stream.take().unwrap(); |
| 308 | let spi_tx = this.spi.take().unwrap(); | 304 | let spi_tx = self.spi.take().unwrap(); |
| 309 | let spi_rx: SPI = unsafe { mem::transmute_copy(&spi_tx) }; | 305 | let spi_rx: SPI = unsafe { mem::transmute_copy(&spi_tx) }; |
| 310 | 306 | ||
| 311 | spi_rx | 307 | spi_rx |
| @@ -334,9 +330,9 @@ where | |||
| 334 | // .double_buffer(false), | 330 | // .double_buffer(false), |
| 335 | // ); | 331 | // ); |
| 336 | // | 332 | // |
| 337 | // let tx_fut = InterruptFuture::new(&mut this.tx_int); | 333 | // let tx_fut = InterruptFuture::new(&mut self.tx_int); |
| 338 | // let rx_fut = InterruptFuture::new(&mut this.rx_int); | 334 | // let rx_fut = InterruptFuture::new(&mut self.rx_int); |
| 339 | // let rx_fut_err = InterruptFuture::new(&mut this.spi_int); | 335 | // let rx_fut_err = InterruptFuture::new(&mut self.spi_int); |
| 340 | // | 336 | // |
| 341 | // rx_transfer.start(|_spi| {}); | 337 | // rx_transfer.start(|_spi| {}); |
| 342 | // tx_transfer.start(|_spi| {}); | 338 | // tx_transfer.start(|_spi| {}); |
| @@ -352,7 +348,7 @@ where | |||
| 352 | for i in 0..(read_static_buf.len() - 1) { | 348 | for i in 0..(read_static_buf.len() - 1) { |
| 353 | let byte = write_static_buf[i]; | 349 | let byte = write_static_buf[i]; |
| 354 | loop { | 350 | loop { |
| 355 | let fut = InterruptFuture::new(&mut this.spi_int); | 351 | let fut = InterruptFuture::new(&mut self.spi_int); |
| 356 | match write_sr(&spi_tx, byte) { | 352 | match write_sr(&spi_tx, byte) { |
| 357 | Ok(()) => break, | 353 | Ok(()) => break, |
| 358 | _ => {} | 354 | _ => {} |
| @@ -361,7 +357,7 @@ where | |||
| 361 | } | 357 | } |
| 362 | 358 | ||
| 363 | loop { | 359 | loop { |
| 364 | let fut = InterruptFuture::new(&mut this.spi_int); | 360 | let fut = InterruptFuture::new(&mut self.spi_int); |
| 365 | match read_sr(&spi_tx) { | 361 | match read_sr(&spi_tx) { |
| 366 | Ok(byte) => { | 362 | Ok(byte) => { |
| 367 | read_static_buf[i] = byte; | 363 | read_static_buf[i] = byte; |
| @@ -381,9 +377,9 @@ where | |||
| 381 | .rxneie() | 377 | .rxneie() |
| 382 | .clear_bit() | 378 | .clear_bit() |
| 383 | }); | 379 | }); |
| 384 | this.rx_stream.replace(rx_stream); | 380 | self.rx_stream.replace(rx_stream); |
| 385 | this.tx_stream.replace(tx_stream); | 381 | self.tx_stream.replace(tx_stream); |
| 386 | this.spi.replace(spi_rx); | 382 | self.spi.replace(spi_rx); |
| 387 | 383 | ||
| 388 | Ok(()) | 384 | Ok(()) |
| 389 | } | 385 | } |
| @@ -421,7 +417,7 @@ macro_rules! spi { | |||
| 421 | impl Instance for pac::$SPI { | 417 | impl Instance for pac::$SPI { |
| 422 | unsafe fn enable_clock() { | 418 | unsafe fn enable_clock() { |
| 423 | const EN_BIT: u8 = $en; | 419 | const EN_BIT: u8 = $en; |
| 424 | // NOTE(unsafe) this reference will only be used for atomic writes with no side effects. | 420 | // NOTE(unsafe) self reference will only be used for atomic writes with no side effects. |
| 425 | let rcc = &(*pac::RCC::ptr()); | 421 | let rcc = &(*pac::RCC::ptr()); |
| 426 | // Enable clock. | 422 | // Enable clock. |
| 427 | bb::set(&rcc.$apbXenr, EN_BIT); | 423 | bb::set(&rcc.$apbXenr, EN_BIT); |
diff --git a/embassy-traits/src/delay.rs b/embassy-traits/src/delay.rs index 1e763350b..31239d319 100644 --- a/embassy-traits/src/delay.rs +++ b/embassy-traits/src/delay.rs | |||
| @@ -1,12 +1,11 @@ | |||
| 1 | use core::future::Future; | 1 | use core::future::Future; |
| 2 | use core::pin::Pin; | ||
| 3 | 2 | ||
| 4 | pub trait Delay { | 3 | pub trait Delay { |
| 5 | type DelayFuture<'a>: Future<Output = ()> + 'a; | 4 | type DelayFuture<'a>: Future<Output = ()> + 'a; |
| 6 | 5 | ||
| 7 | /// Future that completes after now + millis | 6 | /// Future that completes after now + millis |
| 8 | fn delay_ms<'a>(self: Pin<&'a mut Self>, millis: u64) -> Self::DelayFuture<'a>; | 7 | fn delay_ms<'a>(&'a mut self, millis: u64) -> Self::DelayFuture<'a>; |
| 9 | 8 | ||
| 10 | /// Future that completes after now + micros | 9 | /// Future that completes after now + micros |
| 11 | fn delay_us<'a>(self: Pin<&'a mut Self>, micros: u64) -> Self::DelayFuture<'a>; | 10 | fn delay_us<'a>(&'a mut self, micros: u64) -> Self::DelayFuture<'a>; |
| 12 | } | 11 | } |
diff --git a/embassy-traits/src/flash.rs b/embassy-traits/src/flash.rs index 3adaa3a0a..c9b14a390 100644 --- a/embassy-traits/src/flash.rs +++ b/embassy-traits/src/flash.rs | |||
| @@ -27,19 +27,18 @@ pub trait Flash { | |||
| 27 | /// | 27 | /// |
| 28 | /// address must be a multiple of self.read_size(). | 28 | /// address must be a multiple of self.read_size(). |
| 29 | /// buf.len() must be a multiple of self.read_size(). | 29 | /// buf.len() must be a multiple of self.read_size(). |
| 30 | fn read<'a>(self: Pin<&'a mut Self>, address: usize, buf: &'a mut [u8]) | 30 | fn read<'a>(&'a mut self, address: usize, buf: &'a mut [u8]) -> Self::ReadFuture<'a>; |
| 31 | -> Self::ReadFuture<'a>; | ||
| 32 | 31 | ||
| 33 | /// Writes data to the flash device. | 32 | /// Writes data to the flash device. |
| 34 | /// | 33 | /// |
| 35 | /// address must be a multiple of self.write_size(). | 34 | /// address must be a multiple of self.write_size(). |
| 36 | /// buf.len() must be a multiple of self.write_size(). | 35 | /// buf.len() must be a multiple of self.write_size(). |
| 37 | fn write<'a>(self: Pin<&'a mut Self>, address: usize, buf: &'a [u8]) -> Self::WriteFuture<'a>; | 36 | fn write<'a>(&'a mut self, address: usize, buf: &'a [u8]) -> Self::WriteFuture<'a>; |
| 38 | 37 | ||
| 39 | /// Erases a single page from the flash device. | 38 | /// Erases a single page from the flash device. |
| 40 | /// | 39 | /// |
| 41 | /// address must be a multiple of self.erase_size(). | 40 | /// address must be a multiple of self.erase_size(). |
| 42 | fn erase<'a>(self: Pin<&'a mut Self>, address: usize) -> Self::ErasePageFuture<'a>; | 41 | fn erase<'a>(&'a mut self, address: usize) -> Self::ErasePageFuture<'a>; |
| 43 | 42 | ||
| 44 | /// Returns the total size, in bytes. | 43 | /// Returns the total size, in bytes. |
| 45 | /// This is not guaranteed to be a power of 2. | 44 | /// This is not guaranteed to be a power of 2. |
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 | } |
diff --git a/embassy-traits/src/i2c.rs b/embassy-traits/src/i2c.rs index 704203885..abe932d9c 100644 --- a/embassy-traits/src/i2c.rs +++ b/embassy-traits/src/i2c.rs | |||
| @@ -67,7 +67,6 @@ | |||
| 67 | //! ``` | 67 | //! ``` |
| 68 | 68 | ||
| 69 | use core::future::Future; | 69 | use core::future::Future; |
| 70 | use core::pin::Pin; | ||
| 71 | 70 | ||
| 72 | mod private { | 71 | mod private { |
| 73 | pub trait Sealed {} | 72 | pub trait Sealed {} |
| @@ -117,7 +116,7 @@ pub trait I2c<A: AddressMode = SevenBitAddress> { | |||
| 117 | /// - `MAK` = master acknowledge | 116 | /// - `MAK` = master acknowledge |
| 118 | /// - `NMAK` = master no acknowledge | 117 | /// - `NMAK` = master no acknowledge |
| 119 | /// - `SP` = stop condition | 118 | /// - `SP` = stop condition |
| 120 | fn read<'a>(self: Pin<&'a mut Self>, address: A, buffer: &mut [u8]) -> Self::ReadFuture<'a>; | 119 | fn read<'a>(&'a mut self, address: A, buffer: &mut [u8]) -> Self::ReadFuture<'a>; |
| 121 | 120 | ||
| 122 | /// Sends bytes to slave with address `address` | 121 | /// Sends bytes to slave with address `address` |
| 123 | /// | 122 | /// |
| @@ -135,7 +134,7 @@ pub trait I2c<A: AddressMode = SevenBitAddress> { | |||
| 135 | /// - `SAK` = slave acknowledge | 134 | /// - `SAK` = slave acknowledge |
| 136 | /// - `Bi` = ith byte of data | 135 | /// - `Bi` = ith byte of data |
| 137 | /// - `SP` = stop condition | 136 | /// - `SP` = stop condition |
| 138 | fn write<'a>(self: Pin<&'a mut Self>, address: A, bytes: &[u8]) -> Self::WriteFuture<'a>; | 137 | fn write<'a>(&'a mut self, address: A, bytes: &[u8]) -> Self::WriteFuture<'a>; |
| 139 | 138 | ||
| 140 | /// Sends bytes to slave with address `address` and then reads enough bytes to fill `buffer` *in a | 139 | /// Sends bytes to slave with address `address` and then reads enough bytes to fill `buffer` *in a |
| 141 | /// single transaction* | 140 | /// single transaction* |
| @@ -160,7 +159,7 @@ pub trait I2c<A: AddressMode = SevenBitAddress> { | |||
| 160 | /// - `NMAK` = master no acknowledge | 159 | /// - `NMAK` = master no acknowledge |
| 161 | /// - `SP` = stop condition | 160 | /// - `SP` = stop condition |
| 162 | fn write_read<'a>( | 161 | fn write_read<'a>( |
| 163 | self: Pin<&'a mut Self>, | 162 | &'a mut self, |
| 164 | address: A, | 163 | address: A, |
| 165 | bytes: &[u8], | 164 | bytes: &[u8], |
| 166 | buffer: &mut [u8], | 165 | buffer: &mut [u8], |
diff --git a/embassy-traits/src/spi.rs b/embassy-traits/src/spi.rs index 9f08e7402..771ebf2f0 100644 --- a/embassy-traits/src/spi.rs +++ b/embassy-traits/src/spi.rs | |||
| @@ -33,10 +33,10 @@ pub trait FullDuplex<Word> { | |||
| 33 | where | 33 | where |
| 34 | Self: 'a; | 34 | Self: 'a; |
| 35 | 35 | ||
| 36 | fn read<'a>(self: Pin<&'a mut Self>, data: &'a mut [Word]) -> Self::ReadFuture<'a>; | 36 | fn read<'a>(&'a mut self, data: &'a mut [Word]) -> Self::ReadFuture<'a>; |
| 37 | fn write<'a>(self: Pin<&'a mut Self>, data: &'a [Word]) -> Self::WriteFuture<'a>; | 37 | fn write<'a>(&'a mut self, data: &'a [Word]) -> Self::WriteFuture<'a>; |
| 38 | fn read_write<'a>( | 38 | fn read_write<'a>( |
| 39 | self: Pin<&'a mut Self>, | 39 | &'a mut self, |
| 40 | read: &'a mut [Word], | 40 | read: &'a mut [Word], |
| 41 | write: &'a [Word], | 41 | write: &'a [Word], |
| 42 | ) -> Self::WriteReadFuture<'a>; | 42 | ) -> Self::WriteReadFuture<'a>; |
diff --git a/embassy-traits/src/uart.rs b/embassy-traits/src/uart.rs index 5676e3fca..9e76306b0 100644 --- a/embassy-traits/src/uart.rs +++ b/embassy-traits/src/uart.rs | |||
| @@ -13,7 +13,7 @@ pub trait Read { | |||
| 13 | where | 13 | where |
| 14 | Self: 'a; | 14 | Self: 'a; |
| 15 | 15 | ||
| 16 | fn read<'a>(self: Pin<&'a mut Self>, buf: &'a mut [u8]) -> Self::ReadFuture<'a>; | 16 | fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::ReadFuture<'a>; |
| 17 | } | 17 | } |
| 18 | 18 | ||
| 19 | pub trait ReadUntilIdle { | 19 | pub trait ReadUntilIdle { |
| @@ -23,10 +23,7 @@ pub trait ReadUntilIdle { | |||
| 23 | 23 | ||
| 24 | /// Receive into the buffer until the buffer is full or the line is idle after some bytes are received | 24 | /// Receive into the buffer until the buffer is full or the line is idle after some bytes are received |
| 25 | /// Return the number of bytes received | 25 | /// Return the number of bytes received |
| 26 | fn read_until_idle<'a>( | 26 | fn read_until_idle<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::ReadUntilIdleFuture<'a>; |
| 27 | self: Pin<&'a mut Self>, | ||
| 28 | buf: &'a mut [u8], | ||
| 29 | ) -> Self::ReadUntilIdleFuture<'a>; | ||
| 30 | } | 27 | } |
| 31 | 28 | ||
| 32 | pub trait Write { | 29 | pub trait Write { |
| @@ -34,5 +31,5 @@ pub trait Write { | |||
| 34 | where | 31 | where |
| 35 | Self: 'a; | 32 | Self: 'a; |
| 36 | 33 | ||
| 37 | fn write<'a>(self: Pin<&'a mut Self>, buf: &'a [u8]) -> Self::WriteFuture<'a>; | 34 | fn write<'a>(&'a mut self, buf: &'a [u8]) -> Self::WriteFuture<'a>; |
| 38 | } | 35 | } |
diff --git a/embassy/src/executor/timer.rs b/embassy/src/executor/timer.rs index 8297564a0..d66c7cae5 100644 --- a/embassy/src/executor/timer.rs +++ b/embassy/src/executor/timer.rs | |||
| @@ -23,10 +23,10 @@ impl Delay { | |||
| 23 | impl crate::traits::delay::Delay for Delay { | 23 | impl crate::traits::delay::Delay for Delay { |
| 24 | type DelayFuture<'a> = impl Future<Output = ()> + 'a; | 24 | type DelayFuture<'a> = impl Future<Output = ()> + 'a; |
| 25 | 25 | ||
| 26 | fn delay_ms<'a>(self: Pin<&'a mut Self>, millis: u64) -> Self::DelayFuture<'a> { | 26 | fn delay_ms<'a>(&'a mut self, millis: u64) -> Self::DelayFuture<'a> { |
| 27 | Timer::after(Duration::from_millis(millis)) | 27 | Timer::after(Duration::from_millis(millis)) |
| 28 | } | 28 | } |
| 29 | fn delay_us<'a>(self: Pin<&'a mut Self>, micros: u64) -> Self::DelayFuture<'a> { | 29 | fn delay_us<'a>(&'a mut self, micros: u64) -> Self::DelayFuture<'a> { |
| 30 | Timer::after(Duration::from_micros(micros)) | 30 | Timer::after(Duration::from_micros(micros)) |
| 31 | } | 31 | } |
| 32 | } | 32 | } |
