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 /examples/stm32f4/src/bin | |
| parent | 36a1f203648dcb402727ea3eb5d30cf1f6993795 (diff) | |
Reimplement BufRead for BufferedUart
Diffstat (limited to 'examples/stm32f4/src/bin')
| -rw-r--r-- | examples/stm32f4/src/bin/usart_buffered.rs | 38 |
1 files changed, 38 insertions, 0 deletions
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 | } | ||
