100% cpu workage showing safe results
This commit is contained in:
118
usart_async_buffered_generalized2/src/bin/main.rs
Normal file
118
usart_async_buffered_generalized2/src/bin/main.rs
Normal file
@@ -0,0 +1,118 @@
|
||||
// src/bin/main.rs
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use defmt::*;
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_stm32::bind_interrupts;
|
||||
use embassy_stm32::peripherals;
|
||||
use embassy_stm32::usart::{BufferedInterruptHandler, BufferedUart, Config};
|
||||
use embedded_io_async::{Read, Write};
|
||||
use embassy_time::{Timer, Duration};
|
||||
use static_cell::StaticCell;
|
||||
use embassy_futures::yield_now;
|
||||
use {defmt_rtt as _, panic_probe as _};
|
||||
|
||||
use embassy_futures::select::{select, Either};
|
||||
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
|
||||
use embassy_sync::pipe::Pipe;
|
||||
|
||||
const TX_PIPE_CAP: usize = 1024;
|
||||
const RX_PIPE_CAP: usize = 1024;
|
||||
|
||||
// Two pipes: one for RX, one for TX.
|
||||
static UART_TX: Pipe<CriticalSectionRawMutex, TX_PIPE_CAP> = Pipe::new();
|
||||
static UART_RX: Pipe<CriticalSectionRawMutex, RX_PIPE_CAP> = Pipe::new();
|
||||
|
||||
bind_interrupts!(
|
||||
struct Irqs {
|
||||
USART1 => BufferedInterruptHandler<peripherals::USART1>;
|
||||
}
|
||||
);
|
||||
|
||||
#[embassy_executor::task]
|
||||
async fn uart_task(mut uart: BufferedUart<'static>) {
|
||||
let mut rx_byte = [0u8; 1];
|
||||
let mut tx_buf = [0u8; 64];
|
||||
|
||||
loop {
|
||||
// Wait for either RX or TX events.
|
||||
let rx_fut = uart.read(&mut rx_byte);
|
||||
let tx_fut = async {
|
||||
// Wait until there's outgoing data in TX pipe
|
||||
let n = UART_TX.read(&mut tx_buf).await;
|
||||
n
|
||||
};
|
||||
|
||||
match select(rx_fut, tx_fut).await {
|
||||
// Incoming data from UART hardware
|
||||
Either::First(res) => {
|
||||
if let Ok(_) = res {
|
||||
// Forward to RX pipe (non-blocking)
|
||||
let _ = UART_RX.try_write(&rx_byte);
|
||||
let _ = UART_TX.try_write(&rx_byte);
|
||||
}
|
||||
}
|
||||
|
||||
// Outgoing data waiting in TX pipe
|
||||
Either::Second(n) => {
|
||||
unwrap!(uart.write(&tx_buf[..n]).await);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[embassy_executor::main]
|
||||
async fn main(spawner: Spawner) {
|
||||
info!("tititititi");
|
||||
let p = embassy_stm32::init(Default::default());
|
||||
|
||||
static TX_BUF: StaticCell<[u8; 256]> = StaticCell::new();
|
||||
static RX_BUF: StaticCell<[u8; 256]> = StaticCell::new();
|
||||
let tx_buf = TX_BUF.init([0; 256]);
|
||||
let rx_buf = RX_BUF.init([0; 256]);
|
||||
|
||||
let mut cfg = Config::default();
|
||||
cfg.baudrate = 230_400;
|
||||
|
||||
let usart = BufferedUart::new(
|
||||
p.USART1,
|
||||
p.PA10, // RX
|
||||
p.PA9, // TX
|
||||
tx_buf,
|
||||
rx_buf,
|
||||
Irqs,
|
||||
cfg,
|
||||
).unwrap();
|
||||
|
||||
info!("starting uart task");
|
||||
spawner.spawn(uart_task(usart)).unwrap();
|
||||
|
||||
let mut counter: u32 = 0;
|
||||
let mut rx_buf = [0u8; 64];
|
||||
|
||||
loop {
|
||||
counter = counter.wrapping_add(1);
|
||||
|
||||
if counter % 10000 == 0 {
|
||||
info!("CPU busy {}", counter);
|
||||
}
|
||||
if counter % 100000 == 0 {
|
||||
let _ = UART_TX.try_write(b"Hello\r\n");
|
||||
info!("Queued Hello");
|
||||
}
|
||||
|
||||
// Poll RX pipe for new data (non-blocking)
|
||||
if let Ok(n) = UART_RX.try_read(&mut rx_buf) {
|
||||
if n > 0 {
|
||||
info!("RX got: {:?}", &rx_buf[..n]);
|
||||
}
|
||||
}
|
||||
|
||||
if counter % 100000 == 0 {
|
||||
yield_now().await;
|
||||
}
|
||||
// Timer::after(Duration::from_micros(1)).await;
|
||||
Timer::after(Duration::from_secs(5)).await;
|
||||
}
|
||||
}
|
||||
1
usart_async_buffered_generalized2/src/lib.rs
Normal file
1
usart_async_buffered_generalized2/src/lib.rs
Normal file
@@ -0,0 +1 @@
|
||||
#![no_std]
|
||||
Reference in New Issue
Block a user