diff --git a/2sem_sem2/Cargo.toml b/2sem_sem2/Cargo.toml index 6fe2371..f263386 100644 --- a/2sem_sem2/Cargo.toml +++ b/2sem_sem2/Cargo.toml @@ -12,7 +12,7 @@ panic-halt = "1.0.0" embassy-executor = { version = "0.9.1", features = ["arch-cortex-m", "executor-thread"] } embassy-futures = "0.1.2" -embassy-stm32 = { version = "0.4.0", features = ["unstable-pac", "stm32u575zi", "time-driver-any", "memory-x", "defmt"] } +embassy-stm32 = { version = "0.4.0", features = ["unstable-pac", "stm32u575zi", "time-driver-any", "memory-x", "defmt", "exti"] } embassy-sync = "0.7.2" embassy-time = { version = "0.5.0", features = ["tick-hz-32_768"] } embassy-usb = "0.5.1" diff --git a/2sem_sem2/src/bin/main.rs b/2sem_sem2/src/bin/main.rs index 35dede4..00db6e2 100644 --- a/2sem_sem2/src/bin/main.rs +++ b/2sem_sem2/src/bin/main.rs @@ -5,10 +5,9 @@ use core::sync::atomic::{AtomicBool, Ordering}; use defmt::info; use embassy_executor::Spawner; -use embassy_stm32::exti; +use embassy_stm32::exti::ExtiInput; use embassy_stm32::gpio; -use embassy_stm32::gpio::{Input, Output, Pull}; -use embassy_stm32::usart::{Config, InterruptHandler, Uart, UartRx, UartTx}; +use embassy_stm32::gpio::{Output, Pull}; use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; use embassy_sync::channel::Channel; use embassy_sync::channel::Sender; @@ -18,6 +17,7 @@ use heapless::Vec; use {defmt_rtt as _, panic_probe as _}; static PIPE: Channel = Channel::new(); +static PIPE_REC: Channel = Channel::new(); static SEND_ALLOWED: AtomicBool = AtomicBool::new(true); // static REC_ALLOWED: AtomicBool = AtomicBool::new(true); @@ -26,17 +26,19 @@ 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::VeryHigh); - let rx = Input::new(p.PA3, Pull::Up); + let tx = Output::new(p.PF2, gpio::Level::High, gpio::Speed::VeryHigh); + let rx = ExtiInput::new(p.PA3, p.EXTI3, Pull::Up); - let config = Config::default(); + // let config = Config::default(); let sender = PIPE.sender(); + let receiver_sender = PIPE_REC.sender(); // let receiver = PIPE.receiver(); spawner.spawn(bit_send(tx)).unwrap(); + spawner.spawn(bit_receive(rx, receiver_sender)).unwrap(); info!("starting echo"); - let mut v: Vec = Vec::new(); + let v: Vec = Vec::new(); loop { let slovko = "ahoj"; @@ -108,10 +110,38 @@ async fn bit_send(mut pin: Output<'static>) { } #[embassy_executor::task] -async fn bit_receive(mut pin: Input<'static>) { - let mut ticker = Ticker::every(Duration::from_millis(10)); +async fn bit_receive( + mut pin: ExtiInput<'static>, + sender: Sender<'static, CriticalSectionRawMutex, u8, 64>, +) { + let mut last_tick = embassy_time::Instant::now(); + let mut measured_bit_time = 0; + loop { - // bitrate - ticker.next().await; + pin.wait_for_any_edge().await; + + let now = embassy_time::Instant::now(); + let diff = now.duration_since(last_tick).as_ticks(); + last_tick = now; + + if diff == 0 { + continue; + } + + if measured_bit_time == 0 { + measured_bit_time = diff; + let bit_val = if pin.is_high() { 0 } else { 1 }; + sender.send(bit_val).await; + continue; + } + + let count = (diff + (measured_bit_time / 2)) / measured_bit_time; + + let bit_val = if pin.is_high() { 0 } else { 1 }; + + for _ in 0..count { + info!("{}", bit_val); + sender.send(bit_val).await; + } } }