From 1d951a54beeee4184315260b3f2ba0a021f4f9d0 Mon Sep 17 00:00:00 2001 From: chemicstry Date: Thu, 26 May 2022 14:02:55 +0300 Subject: [PATCH 1/6] Reimplement BufRead for BufferedUart --- embassy-stm32/src/usart/buffered.rs | 37 +++++++++++++++++++++ examples/stm32f4/Cargo.toml | 2 +- examples/stm32f4/src/bin/usart_buffered.rs | 38 ++++++++++++++++++++++ 3 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 examples/stm32f4/src/bin/usart_buffered.rs diff --git a/embassy-stm32/src/usart/buffered.rs b/embassy-stm32/src/usart/buffered.rs index b2fcb504..7b63638a 100644 --- a/embassy-stm32/src/usart/buffered.rs +++ b/embassy-stm32/src/usart/buffered.rs @@ -191,6 +191,43 @@ impl<'d, T: Instance> embedded_io::asynch::Read for BufferedUart<'d, T> { } } +impl<'d, T: Instance> embedded_io::asynch::BufRead for BufferedUart<'d, T> { + type FillBufFuture<'a> = impl Future> + where + Self: 'a; + + fn fill_buf<'a>(&'a mut self) -> Self::FillBufFuture<'a> { + poll_fn(move |cx| { + self.inner.with(|state| { + compiler_fence(Ordering::SeqCst); + + // We have data ready in buffer? Return it. + let buf = state.rx.pop_buf(); + if !buf.is_empty() { + let buf: &[u8] = buf; + // Safety: buffer lives as long as uart + let buf: &[u8] = unsafe { core::mem::transmute(buf) }; + return Poll::Ready(Ok(buf)); + } + + state.rx_waker.register(cx.waker()); + Poll::>::Pending + }) + }) + } + + fn consume(&mut self, amt: usize) { + let signal = self.inner.with(|state| { + let full = state.rx.is_full(); + state.rx.pop(amt); + full + }); + if signal { + self.inner.pend(); + } + } +} + impl<'d, T: Instance> embedded_io::asynch::Write for BufferedUart<'d, T> { type WriteFuture<'a> = impl Future> where diff --git a/examples/stm32f4/Cargo.toml b/examples/stm32f4/Cargo.toml index e2065bed..4cd8b7a2 100644 --- a/examples/stm32f4/Cargo.toml +++ b/examples/stm32f4/Cargo.toml @@ -9,13 +9,13 @@ resolver = "2" [dependencies] embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-timestamp-uptime", "unstable-traits"] } embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "unstable-traits", "defmt", "stm32f429zi", "unstable-pac", "memory-x", "time-driver-any", "exti"] } - defmt = "0.3" defmt-rtt = "0.3" cortex-m = "0.7.3" cortex-m-rt = "0.7.0" embedded-hal = "0.2.6" +embedded-io = "0.3.0" panic-probe = { version = "0.3", features = ["print-defmt"] } futures = { version = "0.3.17", default-features = false, features = ["async-await"] } heapless = { version = "0.7.5", default-features = false } diff --git a/examples/stm32f4/src/bin/usart_buffered.rs b/examples/stm32f4/src/bin/usart_buffered.rs new file mode 100644 index 00000000..c5fbbbe5 --- /dev/null +++ b/examples/stm32f4/src/bin/usart_buffered.rs @@ -0,0 +1,38 @@ +#![no_std] +#![no_main] +#![feature(type_alias_impl_trait)] + +use defmt::*; +use defmt_rtt as _; // global logger +use embassy::executor::Spawner; +use embassy_stm32::dma::NoDma; +use embassy_stm32::usart::{BufferedUart, Config, State, Uart}; +use embassy_stm32::{interrupt, Peripherals}; +use embedded_io::asynch::BufRead; +use panic_probe as _; + +#[embassy::main] +async fn main(_spawner: Spawner, p: Peripherals) { + info!("Hello World!"); + + let config = Config::default(); + let usart = Uart::new(p.USART3, p.PD9, p.PD8, NoDma, NoDma, config); + + let mut state = State::new(); + let irq = interrupt::take!(USART3); + let mut tx_buf = [0u8; 32]; + let mut rx_buf = [0u8; 32]; + let mut buf_usart = + unsafe { BufferedUart::new(&mut state, usart, irq, &mut tx_buf, &mut rx_buf) }; + + loop { + let n = { + let buf = buf_usart.fill_buf().await.unwrap(); + info!("Received: {}", buf); + buf.len() + }; + + // Read bytes have to be explicitly consumed, otherwise fill_buf() will return them again + buf_usart.consume(n); + } +} From 4b6162694a65bf8629da62499046b8f22cf3175e Mon Sep 17 00:00:00 2001 From: chemicstry Date: Thu, 26 May 2022 14:05:56 +0300 Subject: [PATCH 2/6] Fix removed space --- examples/stm32f4/Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/stm32f4/Cargo.toml b/examples/stm32f4/Cargo.toml index 4cd8b7a2..651d0138 100644 --- a/examples/stm32f4/Cargo.toml +++ b/examples/stm32f4/Cargo.toml @@ -9,6 +9,7 @@ resolver = "2" [dependencies] embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-timestamp-uptime", "unstable-traits"] } embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "unstable-traits", "defmt", "stm32f429zi", "unstable-pac", "memory-x", "time-driver-any", "exti"] } + defmt = "0.3" defmt-rtt = "0.3" From 667abe6d1d78cc989fb57884643f4d2f5834ea52 Mon Sep 17 00:00:00 2001 From: chemicstry Date: Thu, 26 May 2022 14:11:15 +0300 Subject: [PATCH 3/6] Simplify example --- examples/stm32f4/src/bin/usart_buffered.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/examples/stm32f4/src/bin/usart_buffered.rs b/examples/stm32f4/src/bin/usart_buffered.rs index c5fbbbe5..80b65f0d 100644 --- a/examples/stm32f4/src/bin/usart_buffered.rs +++ b/examples/stm32f4/src/bin/usart_buffered.rs @@ -26,13 +26,11 @@ async fn main(_spawner: Spawner, p: Peripherals) { unsafe { BufferedUart::new(&mut state, usart, irq, &mut tx_buf, &mut rx_buf) }; loop { - let n = { - let buf = buf_usart.fill_buf().await.unwrap(); - info!("Received: {}", buf); - buf.len() - }; + let buf = buf_usart.fill_buf().await.unwrap(); + info!("Received: {}", buf); // Read bytes have to be explicitly consumed, otherwise fill_buf() will return them again + let n = buf.len(); buf_usart.consume(n); } } From c3b899c470bd84ef4f0b8bdeca992ca1069a546c Mon Sep 17 00:00:00 2001 From: chemicstry Date: Thu, 26 May 2022 23:15:06 +0300 Subject: [PATCH 4/6] Implement BufRead for nrf BufferedUarte --- embassy-nrf/src/buffered_uarte.rs | 42 +++++++++++++++++++++++++++ examples/nrf/src/bin/buffered_uart.rs | 20 ++++--------- 2 files changed, 47 insertions(+), 15 deletions(-) diff --git a/embassy-nrf/src/buffered_uarte.rs b/embassy-nrf/src/buffered_uarte.rs index c42fa113..e1d32a31 100644 --- a/embassy-nrf/src/buffered_uarte.rs +++ b/embassy-nrf/src/buffered_uarte.rs @@ -236,6 +236,48 @@ impl<'d, U: UarteInstance, T: TimerInstance> embedded_io::asynch::Read for Buffe } } +impl<'d, U: UarteInstance, T: TimerInstance> embedded_io::asynch::BufRead + for BufferedUarte<'d, U, T> +{ + type FillBufFuture<'a> = impl Future> + where + Self: 'a; + + fn fill_buf<'a>(&'a mut self) -> Self::FillBufFuture<'a> { + poll_fn(move |cx| { + self.inner.with(|state| { + compiler_fence(Ordering::SeqCst); + trace!("fill_buf"); + + // We have data ready in buffer? Return it. + let buf = state.rx.pop_buf(); + if !buf.is_empty() { + trace!(" got {:?} {:?}", buf.as_ptr() as u32, buf.len()); + let buf: &[u8] = buf; + // Safety: buffer lives as long as uart + let buf: &[u8] = unsafe { core::mem::transmute(buf) }; + return Poll::Ready(Ok(buf)); + } + + trace!(" empty"); + state.rx_waker.register(cx.waker()); + Poll::>::Pending + }) + }) + } + + fn consume(&mut self, amt: usize) { + let signal = self.inner.with(|state| { + let full = state.rx.is_full(); + state.rx.pop(amt); + full + }); + if signal { + self.inner.pend(); + } + } +} + impl<'d, U: UarteInstance, T: TimerInstance> embedded_io::asynch::Write for BufferedUarte<'d, U, T> { diff --git a/examples/nrf/src/bin/buffered_uart.rs b/examples/nrf/src/bin/buffered_uart.rs index a64c5821..782c3949 100644 --- a/examples/nrf/src/bin/buffered_uart.rs +++ b/examples/nrf/src/bin/buffered_uart.rs @@ -6,7 +6,7 @@ use defmt::*; use embassy::executor::Spawner; use embassy_nrf::buffered_uarte::State; use embassy_nrf::{buffered_uarte::BufferedUarte, interrupt, uarte, Peripherals}; -use embedded_io::asynch::{Read, Write}; +use embedded_io::asynch::{BufRead, Write}; use futures::pin_mut; use defmt_rtt as _; // global logger @@ -46,23 +46,13 @@ async fn main(_spawner: Spawner, p: Peripherals) { unwrap!(u.write_all(b"Hello!\r\n").await); info!("wrote hello in uart!"); - // Simple demo, reading 8-char chunks and echoing them back reversed. loop { info!("reading..."); - let mut buf = [0u8; 8]; - unwrap!(u.read_exact(&mut buf).await); + let buf = unwrap!(u.fill_buf().await); info!("read done, got {}", buf); - // Reverse buf - for i in 0..4 { - buf.swap(i, 7 - i); - } - - info!("writing..."); - unwrap!(u.write_all(&buf).await); - info!("write done"); - - // Wait until the bytes are actually finished being transmitted - unwrap!(u.flush().await); + // Read bytes have to be explicitly consumed, otherwise fill_buf() will return them again + let n = buf.len(); + u.consume(n); } } From 9a447f1359ebaabc486ed4eb364eb3dd7b5275c9 Mon Sep 17 00:00:00 2001 From: chemicstry Date: Thu, 26 May 2022 23:24:02 +0300 Subject: [PATCH 5/6] Fix irq pend behavior --- embassy-nrf/src/buffered_uarte.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/embassy-nrf/src/buffered_uarte.rs b/embassy-nrf/src/buffered_uarte.rs index e1d32a31..4f15bb26 100644 --- a/embassy-nrf/src/buffered_uarte.rs +++ b/embassy-nrf/src/buffered_uarte.rs @@ -268,9 +268,9 @@ impl<'d, U: UarteInstance, T: TimerInstance> embedded_io::asynch::BufRead fn consume(&mut self, amt: usize) { let signal = self.inner.with(|state| { - let full = state.rx.is_full(); + let empty = state.rx.is_empty(); state.rx.pop(amt); - full + !empty }); if signal { self.inner.pend(); From 9772645718ae245c13d3905b2e70db8cb85443f6 Mon Sep 17 00:00:00 2001 From: chemicstry Date: Thu, 26 May 2022 23:36:25 +0300 Subject: [PATCH 6/6] Revert "Fix irq pend behavior" This reverts commit 9a447f1359ebaabc486ed4eb364eb3dd7b5275c9. --- embassy-nrf/src/buffered_uarte.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/embassy-nrf/src/buffered_uarte.rs b/embassy-nrf/src/buffered_uarte.rs index 4f15bb26..e1d32a31 100644 --- a/embassy-nrf/src/buffered_uarte.rs +++ b/embassy-nrf/src/buffered_uarte.rs @@ -268,9 +268,9 @@ impl<'d, U: UarteInstance, T: TimerInstance> embedded_io::asynch::BufRead fn consume(&mut self, amt: usize) { let signal = self.inner.with(|state| { - let empty = state.rx.is_empty(); + let full = state.rx.is_full(); state.rx.pop(amt); - !empty + full }); if signal { self.inner.pend();