summaryrefslogtreecommitdiff
path: root/examples/stm32f4/src/bin
diff options
context:
space:
mode:
authorchemicstry <chemicstry@gmail.com>2022-05-26 14:02:55 +0300
committerchemicstry <chemicstry@gmail.com>2022-05-26 14:02:55 +0300
commit1d951a54beeee4184315260b3f2ba0a021f4f9d0 (patch)
tree2c3444eb1f8c15ce6be53ece689080b19d65020e /examples/stm32f4/src/bin
parent36a1f203648dcb402727ea3eb5d30cf1f6993795 (diff)
downloadembassy-1d951a54beeee4184315260b3f2ba0a021f4f9d0.zip
Reimplement BufRead for BufferedUart
Diffstat (limited to 'examples/stm32f4/src/bin')
-rw-r--r--examples/stm32f4/src/bin/usart_buffered.rs38
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 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);
+ }
+}