embassy/examples/stm32f4/src/bin/usart_buffered.rs

43 lines
1.0 KiB
Rust
Raw Normal View History

2022-05-26 13:02:55 +02:00
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
use defmt::*;
use embassy_executor::Spawner;
use embassy_stm32::interrupt;
use embassy_stm32::usart::{BufferedUart, Config, State};
2022-05-26 13:02:55 +02:00
use embedded_io::asynch::BufRead;
2022-06-12 22:15:44 +02:00
use {defmt_rtt as _, panic_probe as _};
2022-05-26 13:02:55 +02:00
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let p = embassy_stm32::init(Default::default());
2022-05-26 13:02:55 +02:00
info!("Hello World!");
let config = Config::default();
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 = BufferedUart::new(
&mut state,
p.USART3,
p.PD9,
p.PD8,
irq,
&mut tx_buf,
&mut rx_buf,
config,
);
2022-05-26 13:02:55 +02:00
loop {
2022-05-26 13:11:15 +02:00
let buf = buf_usart.fill_buf().await.unwrap();
info!("Received: {}", buf);
2022-05-26 13:02:55 +02:00
// Read bytes have to be explicitly consumed, otherwise fill_buf() will return them again
2022-05-26 13:11:15 +02:00
let n = buf.len();
2022-05-26 13:02:55 +02:00
buf_usart.consume(n);
}
}