diff options
| author | chemicstry <[email protected]> | 2022-05-26 14:02:55 +0300 |
|---|---|---|
| committer | chemicstry <[email protected]> | 2022-05-26 14:02:55 +0300 |
| commit | 1d951a54beeee4184315260b3f2ba0a021f4f9d0 (patch) | |
| tree | 2c3444eb1f8c15ce6be53ece689080b19d65020e | |
| parent | 36a1f203648dcb402727ea3eb5d30cf1f6993795 (diff) | |
Reimplement BufRead for BufferedUart
| -rw-r--r-- | embassy-stm32/src/usart/buffered.rs | 37 | ||||
| -rw-r--r-- | examples/stm32f4/Cargo.toml | 2 | ||||
| -rw-r--r-- | examples/stm32f4/src/bin/usart_buffered.rs | 38 |
3 files changed, 76 insertions, 1 deletions
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/stm32f4/Cargo.toml b/examples/stm32f4/Cargo.toml index e2065bed9..4cd8b7a28 100644 --- a/examples/stm32f4/Cargo.toml +++ b/examples/stm32f4/Cargo.toml | |||
| @@ -9,13 +9,13 @@ resolver = "2" | |||
| 9 | [dependencies] | 9 | [dependencies] |
| 10 | embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-timestamp-uptime", "unstable-traits"] } | 10 | embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-timestamp-uptime", "unstable-traits"] } |
| 11 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "unstable-traits", "defmt", "stm32f429zi", "unstable-pac", "memory-x", "time-driver-any", "exti"] } | 11 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "unstable-traits", "defmt", "stm32f429zi", "unstable-pac", "memory-x", "time-driver-any", "exti"] } |
| 12 | |||
| 13 | defmt = "0.3" | 12 | defmt = "0.3" |
| 14 | defmt-rtt = "0.3" | 13 | defmt-rtt = "0.3" |
| 15 | 14 | ||
| 16 | cortex-m = "0.7.3" | 15 | cortex-m = "0.7.3" |
| 17 | cortex-m-rt = "0.7.0" | 16 | cortex-m-rt = "0.7.0" |
| 18 | embedded-hal = "0.2.6" | 17 | embedded-hal = "0.2.6" |
| 18 | embedded-io = "0.3.0" | ||
| 19 | panic-probe = { version = "0.3", features = ["print-defmt"] } | 19 | panic-probe = { version = "0.3", features = ["print-defmt"] } |
| 20 | futures = { version = "0.3.17", default-features = false, features = ["async-await"] } | 20 | futures = { version = "0.3.17", default-features = false, features = ["async-await"] } |
| 21 | heapless = { version = "0.7.5", default-features = false } | 21 | 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..c5fbbbe5d --- /dev/null +++ b/examples/stm32f4/src/bin/usart_buffered.rs | |||
| @@ -0,0 +1,38 @@ | |||
| 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 n = { | ||
| 30 | let buf = buf_usart.fill_buf().await.unwrap(); | ||
| 31 | info!("Received: {}", buf); | ||
| 32 | buf.len() | ||
| 33 | }; | ||
| 34 | |||
| 35 | // Read bytes have to be explicitly consumed, otherwise fill_buf() will return them again | ||
| 36 | buf_usart.consume(n); | ||
| 37 | } | ||
| 38 | } | ||
