async working

This commit is contained in:
Filipriec
2026-02-24 16:35:30 +01:00
parent 88f2ec3ae9
commit adc4d6f29e

View File

@@ -3,65 +3,108 @@
#![no_main] #![no_main]
use defmt::*; use defmt::*;
use embassy_stm32::usart::{Config, Uart}; use core::sync::atomic::{AtomicBool, Ordering};
use embassy_executor::Spawner;
use embassy_stm32::usart::{Config, Uart, UartRx, InterruptHandler};
use embassy_stm32::peripherals::USART1;
use embassy_stm32::bind_interrupts;
use embassy_time::{Duration, Timer}; use embassy_time::{Duration, Timer};
use embassy_sync::channel::Channel; use embassy_sync::channel::Channel;
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
use {defmt_rtt as _, panic_probe as _}; use {defmt_rtt as _, panic_probe as _};
static PIPE: Channel<CriticalSectionRawMutex, u8, 64> = Channel::new(); bind_interrupts!(struct Irqs {
USART1 => InterruptHandler<USART1>;
});
#[cortex_m_rt::entry] static PIPE: Channel<CriticalSectionRawMutex, u8, 64> = Channel::new();
fn main() -> ! { static SEND_ALLOWED: AtomicBool = AtomicBool::new(true);
static REC_ALLOWED: AtomicBool = AtomicBool::new(true);
#[embassy_executor::main]
async fn main(spawner: Spawner) {
info!("tititititi"); info!("tititititi");
let p = embassy_stm32::init(Default::default()); let p = embassy_stm32::init(Default::default());
let config = Config::default(); let config = Config::default();
let mut usart = Uart::new_blocking(p.USART1, p.PA10, p.PA9, config).unwrap(); let usart = Uart::new(
p.USART1,
p.PA10,
p.PA9,
Irqs,
p.GPDMA1_CH0, // TX DMA
p.GPDMA1_CH1, // RX DMA
config,
).unwrap();
let (mut tx, rx) = usart.split();
spawner.spawn(rx_task(rx)).unwrap();
spawner.spawn(toggle_rec_task()).unwrap();
let sender = PIPE.sender(); let sender = PIPE.sender();
let receiver = PIPE.receiver(); let receiver = PIPE.receiver();
info!("starting echo"); info!("starting echo");
let mut buf = [0u8; 1];
let mut send_allowed: bool = true;
let mut rec_allowed: bool = true;
let data = b"filip\r\n"; let data = b"filip\r\n";
let start_time = embassy_time::Instant::now(); let idle = &[0x13u8];
loop { loop {
rec_allowed = (start_time.elapsed().as_secs() / 2) % 2 == 0; // rec_allowed = (start_time.elapsed().as_secs() / 2) % 2 == 0;
if rec_allowed == true { let current_send_allowed = SEND_ALLOWED.load(Ordering::Relaxed);
if usart.blocking_read(&mut buf).is_ok() { let current_rec_allowed = REC_ALLOWED.load(Ordering::Relaxed);
let byte = buf[0]; if current_rec_allowed == true {
match byte { if current_send_allowed {
0x11 => {
send_allowed = true;
info!("Vysielanie povolené");
}
0x13 => {
send_allowed = false;
info!("Vysielanie zakázané");
}
_ => {
}
}
}
if send_allowed {
if let Ok(stored_byte) = receiver.try_receive() { if let Ok(stored_byte) = receiver.try_receive() {
unwrap!(usart.blocking_write(&[stored_byte])); let _ = tx.write(&[stored_byte]).await;
} else { } else {
unwrap!(usart.blocking_write(data)); let _ = tx.write(data).await;
} }
} else { } else {
for b in data { for b in data {
let _ = sender.try_send(*b); let _ = sender.try_send(*b);
} }
} }
} else {
let _ = tx.write(idle).await;
} }
let _ = Timer::after(Duration::from_millis(100)); let _ = Timer::after(Duration::from_millis(100));
} }
} }
#[embassy_executor::task]
async fn rx_task(mut rx: UartRx<'static, embassy_stm32::mode::Async>) {
let mut buf = [0u8; 1];
loop {
if rx.read(&mut buf).await.is_ok() {
match buf[0] {
0x11 => {
SEND_ALLOWED.store(true, Ordering::Relaxed);
info!("Vysielanie povolené");
}
0x13 => {
SEND_ALLOWED.store(false, Ordering::Relaxed);
info!("Vysielanie zakázané");
}
_ => {}
}
}
}
}
#[embassy_executor::task]
async fn toggle_rec_task() {
loop {
Timer::after(Duration::from_secs(2)).await;
// Načítame aktuálnu hodnotu a uložíme jej opak
let current = REC_ALLOWED.load(Ordering::Relaxed);
REC_ALLOWED.store(!current, Ordering::Relaxed);
if !current {
info!("REC_ALLOWED: true");
} else {
info!("REC_ALLOWED: false");
}
}
}