diff options
| author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2022-05-27 16:03:19 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-05-27 16:03:19 +0000 |
| commit | 82e873d920c843d458c23dbd1b3e631006a29d0b (patch) | |
| tree | f67133f80abd90c1db9dcff4895c35fc3f42c860 | |
| parent | e10fc2bada1c59420431f09a35f7aa09a5b45623 (diff) | |
| parent | 9772645718ae245c13d3905b2e70db8cb85443f6 (diff) | |
Merge #783
783: Reimplement BufRead for BufferedUart r=Dirbaio a=chemicstry
The `AsyncBufRead` implementation for `BufferedUart` was removed in https://github.com/embassy-rs/embassy/pull/752, this PR reimplements it from `embedded-io`. This allows reading `BufferedUart` without copying slices.
Co-authored-by: chemicstry <[email protected]>
| -rw-r--r-- | embassy-nrf/src/buffered_uarte.rs | 42 | ||||
| -rw-r--r-- | embassy-stm32/src/usart/buffered.rs | 37 | ||||
| -rw-r--r-- | examples/nrf/src/bin/buffered_uart.rs | 20 | ||||
| -rw-r--r-- | examples/stm32f4/Cargo.toml | 1 | ||||
| -rw-r--r-- | examples/stm32f4/src/bin/usart_buffered.rs | 36 |
5 files changed, 121 insertions, 15 deletions
diff --git a/embassy-nrf/src/buffered_uarte.rs b/embassy-nrf/src/buffered_uarte.rs index c42fa1138..e1d32a311 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 | |||
| 236 | } | 236 | } |
| 237 | } | 237 | } |
| 238 | 238 | ||
| 239 | impl<'d, U: UarteInstance, T: TimerInstance> embedded_io::asynch::BufRead | ||
| 240 | for BufferedUarte<'d, U, T> | ||
| 241 | { | ||
| 242 | type FillBufFuture<'a> = impl Future<Output = Result<&'a [u8], Self::Error>> | ||
| 243 | where | ||
| 244 | Self: 'a; | ||
| 245 | |||
| 246 | fn fill_buf<'a>(&'a mut self) -> Self::FillBufFuture<'a> { | ||
| 247 | poll_fn(move |cx| { | ||
| 248 | self.inner.with(|state| { | ||
| 249 | compiler_fence(Ordering::SeqCst); | ||
| 250 | trace!("fill_buf"); | ||
| 251 | |||
| 252 | // We have data ready in buffer? Return it. | ||
| 253 | let buf = state.rx.pop_buf(); | ||
| 254 | if !buf.is_empty() { | ||
| 255 | trace!(" got {:?} {:?}", buf.as_ptr() as u32, buf.len()); | ||
| 256 | let buf: &[u8] = buf; | ||
| 257 | // Safety: buffer lives as long as uart | ||
| 258 | let buf: &[u8] = unsafe { core::mem::transmute(buf) }; | ||
| 259 | return Poll::Ready(Ok(buf)); | ||
| 260 | } | ||
| 261 | |||
| 262 | trace!(" empty"); | ||
| 263 | state.rx_waker.register(cx.waker()); | ||
| 264 | Poll::<Result<&[u8], Self::Error>>::Pending | ||
| 265 | }) | ||
| 266 | }) | ||
| 267 | } | ||
| 268 | |||
| 269 | fn consume(&mut self, amt: usize) { | ||
| 270 | let signal = self.inner.with(|state| { | ||
| 271 | let full = state.rx.is_full(); | ||
| 272 | state.rx.pop(amt); | ||
| 273 | full | ||
| 274 | }); | ||
| 275 | if signal { | ||
| 276 | self.inner.pend(); | ||
| 277 | } | ||
| 278 | } | ||
| 279 | } | ||
| 280 | |||
| 239 | impl<'d, U: UarteInstance, T: TimerInstance> embedded_io::asynch::Write | 281 | impl<'d, U: UarteInstance, T: TimerInstance> embedded_io::asynch::Write |
| 240 | for BufferedUarte<'d, U, T> | 282 | for BufferedUarte<'d, U, T> |
| 241 | { | 283 | { |
diff --git a/embassy-stm32/src/usart/buffered.rs b/embassy-stm32/src/usart/buffered.rs index b2fcb504e..7b63638a0 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> { | |||
| 191 | } | 191 | } |
| 192 | } | 192 | } |
| 193 | 193 | ||
| 194 | impl<'d, T: Instance> embedded_io::asynch::BufRead for BufferedUart<'d, T> { | ||
| 195 | type FillBufFuture<'a> = impl Future<Output = Result<&'a [u8], Self::Error>> | ||
| 196 | where | ||
| 197 | Self: 'a; | ||
| 198 | |||
| 199 | fn fill_buf<'a>(&'a mut self) -> Self::FillBufFuture<'a> { | ||
| 200 | poll_fn(move |cx| { | ||
| 201 | self.inner.with(|state| { | ||
| 202 | compiler_fence(Ordering::SeqCst); | ||
| 203 | |||
| 204 | // We have data ready in buffer? Return it. | ||
| 205 | let buf = state.rx.pop_buf(); | ||
| 206 | if !buf.is_empty() { | ||
| 207 | let buf: &[u8] = buf; | ||
| 208 | // Safety: buffer lives as long as uart | ||
| 209 | let buf: &[u8] = unsafe { core::mem::transmute(buf) }; | ||
| 210 | return Poll::Ready(Ok(buf)); | ||
| 211 | } | ||
| 212 | |||
| 213 | state.rx_waker.register(cx.waker()); | ||
| 214 | Poll::<Result<&[u8], Self::Error>>::Pending | ||
| 215 | }) | ||
| 216 | }) | ||
| 217 | } | ||
| 218 | |||
| 219 | fn consume(&mut self, amt: usize) { | ||
| 220 | let signal = self.inner.with(|state| { | ||
| 221 | let full = state.rx.is_full(); | ||
| 222 | state.rx.pop(amt); | ||
| 223 | full | ||
| 224 | }); | ||
| 225 | if signal { | ||
| 226 | self.inner.pend(); | ||
| 227 | } | ||
| 228 | } | ||
| 229 | } | ||
| 230 | |||
| 194 | impl<'d, T: Instance> embedded_io::asynch::Write for BufferedUart<'d, T> { | 231 | impl<'d, T: Instance> embedded_io::asynch::Write for BufferedUart<'d, T> { |
| 195 | type WriteFuture<'a> = impl Future<Output = Result<usize, Self::Error>> | 232 | type WriteFuture<'a> = impl Future<Output = Result<usize, Self::Error>> |
| 196 | where | 233 | where |
diff --git a/examples/nrf/src/bin/buffered_uart.rs b/examples/nrf/src/bin/buffered_uart.rs index a64c5821b..782c39499 100644 --- a/examples/nrf/src/bin/buffered_uart.rs +++ b/examples/nrf/src/bin/buffered_uart.rs | |||
| @@ -6,7 +6,7 @@ use defmt::*; | |||
| 6 | use embassy::executor::Spawner; | 6 | use embassy::executor::Spawner; |
| 7 | use embassy_nrf::buffered_uarte::State; | 7 | use embassy_nrf::buffered_uarte::State; |
| 8 | use embassy_nrf::{buffered_uarte::BufferedUarte, interrupt, uarte, Peripherals}; | 8 | use embassy_nrf::{buffered_uarte::BufferedUarte, interrupt, uarte, Peripherals}; |
| 9 | use embedded_io::asynch::{Read, Write}; | 9 | use embedded_io::asynch::{BufRead, Write}; |
| 10 | use futures::pin_mut; | 10 | use futures::pin_mut; |
| 11 | 11 | ||
| 12 | use defmt_rtt as _; // global logger | 12 | use defmt_rtt as _; // global logger |
| @@ -46,23 +46,13 @@ async fn main(_spawner: Spawner, p: Peripherals) { | |||
| 46 | unwrap!(u.write_all(b"Hello!\r\n").await); | 46 | unwrap!(u.write_all(b"Hello!\r\n").await); |
| 47 | info!("wrote hello in uart!"); | 47 | info!("wrote hello in uart!"); |
| 48 | 48 | ||
| 49 | // Simple demo, reading 8-char chunks and echoing them back reversed. | ||
| 50 | loop { | 49 | loop { |
| 51 | info!("reading..."); | 50 | info!("reading..."); |
| 52 | let mut buf = [0u8; 8]; | 51 | let buf = unwrap!(u.fill_buf().await); |
| 53 | unwrap!(u.read_exact(&mut buf).await); | ||
| 54 | info!("read done, got {}", buf); | 52 | info!("read done, got {}", buf); |
| 55 | 53 | ||
| 56 | // Reverse buf | 54 | // Read bytes have to be explicitly consumed, otherwise fill_buf() will return them again |
| 57 | for i in 0..4 { | 55 | let n = buf.len(); |
| 58 | buf.swap(i, 7 - i); | 56 | u.consume(n); |
| 59 | } | ||
| 60 | |||
| 61 | info!("writing..."); | ||
| 62 | unwrap!(u.write_all(&buf).await); | ||
| 63 | info!("write done"); | ||
| 64 | |||
| 65 | // Wait until the bytes are actually finished being transmitted | ||
| 66 | unwrap!(u.flush().await); | ||
| 67 | } | 57 | } |
| 68 | } | 58 | } |
diff --git a/examples/stm32f4/Cargo.toml b/examples/stm32f4/Cargo.toml index e2065bed9..651d01388 100644 --- a/examples/stm32f4/Cargo.toml +++ b/examples/stm32f4/Cargo.toml | |||
| @@ -16,6 +16,7 @@ defmt-rtt = "0.3" | |||
| 16 | cortex-m = "0.7.3" | 16 | cortex-m = "0.7.3" |
| 17 | cortex-m-rt = "0.7.0" | 17 | cortex-m-rt = "0.7.0" |
| 18 | embedded-hal = "0.2.6" | 18 | embedded-hal = "0.2.6" |
| 19 | embedded-io = "0.3.0" | ||
| 19 | panic-probe = { version = "0.3", features = ["print-defmt"] } | 20 | panic-probe = { version = "0.3", features = ["print-defmt"] } |
| 20 | futures = { version = "0.3.17", default-features = false, features = ["async-await"] } | 21 | futures = { version = "0.3.17", default-features = false, features = ["async-await"] } |
| 21 | heapless = { version = "0.7.5", default-features = false } | 22 | 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 000000000..80b65f0d4 --- /dev/null +++ b/examples/stm32f4/src/bin/usart_buffered.rs | |||
| @@ -0,0 +1,36 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::*; | ||
| 6 | use defmt_rtt as _; // global logger | ||
| 7 | use embassy::executor::Spawner; | ||
| 8 | use embassy_stm32::dma::NoDma; | ||
| 9 | use embassy_stm32::usart::{BufferedUart, Config, State, Uart}; | ||
| 10 | use embassy_stm32::{interrupt, Peripherals}; | ||
| 11 | use embedded_io::asynch::BufRead; | ||
| 12 | use panic_probe as _; | ||
| 13 | |||
| 14 | #[embassy::main] | ||
| 15 | async fn main(_spawner: Spawner, p: Peripherals) { | ||
| 16 | info!("Hello World!"); | ||
| 17 | |||
| 18 | let config = Config::default(); | ||
| 19 | let usart = Uart::new(p.USART3, p.PD9, p.PD8, NoDma, NoDma, config); | ||
| 20 | |||
| 21 | let mut state = State::new(); | ||
| 22 | let irq = interrupt::take!(USART3); | ||
| 23 | let mut tx_buf = [0u8; 32]; | ||
| 24 | let mut rx_buf = [0u8; 32]; | ||
| 25 | let mut buf_usart = | ||
| 26 | unsafe { BufferedUart::new(&mut state, usart, irq, &mut tx_buf, &mut rx_buf) }; | ||
| 27 | |||
| 28 | loop { | ||
| 29 | let buf = buf_usart.fill_buf().await.unwrap(); | ||
| 30 | info!("Received: {}", buf); | ||
| 31 | |||
| 32 | // Read bytes have to be explicitly consumed, otherwise fill_buf() will return them again | ||
| 33 | let n = buf.len(); | ||
| 34 | buf_usart.consume(n); | ||
| 35 | } | ||
| 36 | } | ||
