2sem_2
This commit is contained in:
@@ -2,125 +2,90 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use defmt::*;
|
||||
use core::sync::atomic::{AtomicBool, Ordering};
|
||||
use defmt::info;
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_stm32::usart::{Config, Uart, UartRx, UartTx, InterruptHandler};
|
||||
use embassy_stm32::peripherals::{USART1, USART3};
|
||||
use embassy_stm32::bind_interrupts;
|
||||
use embassy_time::{Duration, Timer};
|
||||
use embassy_sync::channel::Channel;
|
||||
use embassy_stm32::gpio;
|
||||
use embassy_stm32::gpio::Output;
|
||||
use embassy_stm32::usart::{Config, InterruptHandler, Uart, UartRx, UartTx};
|
||||
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
|
||||
use embassy_sync::channel::Channel;
|
||||
use embassy_sync::channel::Sender;
|
||||
use embassy_time::{Duration, Timer};
|
||||
use heapless::Vec;
|
||||
|
||||
use {defmt_rtt as _, panic_probe as _};
|
||||
|
||||
bind_interrupts!(struct Irqs {
|
||||
USART1 => InterruptHandler<USART1>;
|
||||
USART3 => InterruptHandler<USART3>;
|
||||
});
|
||||
|
||||
static PIPE: Channel<CriticalSectionRawMutex, u8, 64> = Channel::new();
|
||||
static SEND_ALLOWED: AtomicBool = AtomicBool::new(true);
|
||||
static REC_ALLOWED: AtomicBool = AtomicBool::new(true);
|
||||
// static REC_ALLOWED: AtomicBool = AtomicBool::new(true);
|
||||
|
||||
#[embassy_executor::main]
|
||||
async fn main(spawner: Spawner) {
|
||||
info!("tititititi");
|
||||
|
||||
let p = embassy_stm32::init(Default::default());
|
||||
let mut tx = Output::new(p.PF2, gpio::Level::High, gpio::Speed::High);
|
||||
|
||||
let config = Config::default();
|
||||
let dev_usart = Uart::new(
|
||||
p.USART1,
|
||||
p.PA10,
|
||||
p.PA9,
|
||||
Irqs,
|
||||
p.GPDMA1_CH0, // TX DMA
|
||||
p.GPDMA1_CH1, // RX DMA
|
||||
config,
|
||||
).unwrap();
|
||||
|
||||
let uart = Uart::new(
|
||||
p.USART3,
|
||||
p.PD9, // RX zo zariadenia
|
||||
p.PD8, // TX do zariadenia
|
||||
Irqs,
|
||||
p.GPDMA1_CH2,
|
||||
p.GPDMA1_CH3,
|
||||
config,
|
||||
).unwrap();
|
||||
let (mut tx, rx) = uart.split();
|
||||
let (dev_tx, dev_rx) = dev_usart.split();
|
||||
spawner.spawn(rx_task(rx, dev_tx)).unwrap();
|
||||
spawner.spawn(toggle_rec_task()).unwrap();
|
||||
|
||||
let sender = PIPE.sender();
|
||||
let receiver = PIPE.receiver();
|
||||
// let receiver = PIPE.receiver();
|
||||
|
||||
info!("starting echo");
|
||||
let mut v: Vec<u8, 38, u8> = Vec::new();
|
||||
|
||||
let data = b"filip\r\n";
|
||||
let idle = &[0x13u8];
|
||||
// let data = b"filip\r\n";
|
||||
// let idle = &[0x13u8];
|
||||
|
||||
loop {
|
||||
// rec_allowed = (start_time.elapsed().as_secs() / 2) % 2 == 0;
|
||||
let current_send_allowed = SEND_ALLOWED.load(Ordering::Relaxed);
|
||||
let current_rec_allowed = REC_ALLOWED.load(Ordering::Relaxed);
|
||||
if current_rec_allowed == true {
|
||||
if current_send_allowed {
|
||||
if let Ok(stored_byte) = receiver.try_receive() {
|
||||
let _ = tx.write(&[stored_byte]).await;
|
||||
} else {
|
||||
let _ = tx.write(data).await;
|
||||
}
|
||||
} else {
|
||||
for b in data {
|
||||
let _ = sender.try_send(*b);
|
||||
}
|
||||
let slovko = "ahoj";
|
||||
info!("slovo: {}", slovko);
|
||||
let vektorik = encode(slovko, v.clone());
|
||||
info!("slovo: {:#?}", vektorik);
|
||||
// let current_send_allowed = SEND_ALLOWED.load(Ordering::Relaxed);
|
||||
// let current_rec_allowed = REC_ALLOWED.load(Ordering::Relaxed);
|
||||
// if current_rec_allowed == true {
|
||||
// if current_send_allowed {
|
||||
// if let Ok(stored_byte) = receiver.try_receive() {
|
||||
// // TODO
|
||||
// // let _ = tx.write(&[stored_byte]).await;
|
||||
// } else {
|
||||
// // TODO
|
||||
// // let _ = tx.write(data).await;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
let _ = Timer::after(Duration::from_millis(1000));
|
||||
embassy_time::block_for(Duration::from_millis(1000));
|
||||
}
|
||||
}
|
||||
|
||||
fn encode(word: &str, mut v: Vec<u8, 38, u8>) {
|
||||
v.clear();
|
||||
for byte in word.bytes() {
|
||||
for bit in 0..8 {
|
||||
let bit_value = (byte >> (7 - bit)) & 1;
|
||||
if v.push(bit_value).is_err() {
|
||||
info!("we panick boyz");
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
let _ = tx.write(idle).await;
|
||||
}
|
||||
let _ = Timer::after(Duration::from_millis(100));
|
||||
}
|
||||
}
|
||||
|
||||
#[embassy_executor::task]
|
||||
async fn rx_task(
|
||||
mut rx: UartRx<'static, embassy_stm32::mode::Async>,
|
||||
mut pc_tx: UartTx<'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é");
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
let _ = pc_tx.write(&buf).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[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");
|
||||
fn nrz(enkodovany: &Vec<u8, 38, u8>, sender: &Sender<'_, CriticalSectionRawMutex, u8, 64>) {
|
||||
if !SEND_ALLOWED.load(Ordering::Relaxed) {
|
||||
return;
|
||||
}
|
||||
let preamble: u8 = 0xAA;
|
||||
let sfd: u8 = 0xAB;
|
||||
sender.push(preamble);
|
||||
sender.push(sfd);
|
||||
for &bit in enkodovany {
|
||||
if sender.try_send(bit).is_err() {
|
||||
info!("pipe full");
|
||||
break;
|
||||
}
|
||||
}
|
||||
sender.push(preamble);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user