input via exti
This commit is contained in:
@@ -12,7 +12,7 @@ panic-halt = "1.0.0"
|
|||||||
|
|
||||||
embassy-executor = { version = "0.9.1", features = ["arch-cortex-m", "executor-thread"] }
|
embassy-executor = { version = "0.9.1", features = ["arch-cortex-m", "executor-thread"] }
|
||||||
embassy-futures = "0.1.2"
|
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-sync = "0.7.2"
|
||||||
embassy-time = { version = "0.5.0", features = ["tick-hz-32_768"] }
|
embassy-time = { version = "0.5.0", features = ["tick-hz-32_768"] }
|
||||||
embassy-usb = "0.5.1"
|
embassy-usb = "0.5.1"
|
||||||
|
|||||||
@@ -5,10 +5,9 @@
|
|||||||
use core::sync::atomic::{AtomicBool, Ordering};
|
use core::sync::atomic::{AtomicBool, Ordering};
|
||||||
use defmt::info;
|
use defmt::info;
|
||||||
use embassy_executor::Spawner;
|
use embassy_executor::Spawner;
|
||||||
use embassy_stm32::exti;
|
use embassy_stm32::exti::ExtiInput;
|
||||||
use embassy_stm32::gpio;
|
use embassy_stm32::gpio;
|
||||||
use embassy_stm32::gpio::{Input, Output, Pull};
|
use embassy_stm32::gpio::{Output, Pull};
|
||||||
use embassy_stm32::usart::{Config, InterruptHandler, Uart, UartRx, UartTx};
|
|
||||||
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
|
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
|
||||||
use embassy_sync::channel::Channel;
|
use embassy_sync::channel::Channel;
|
||||||
use embassy_sync::channel::Sender;
|
use embassy_sync::channel::Sender;
|
||||||
@@ -18,6 +17,7 @@ use heapless::Vec;
|
|||||||
use {defmt_rtt as _, panic_probe as _};
|
use {defmt_rtt as _, panic_probe as _};
|
||||||
|
|
||||||
static PIPE: Channel<CriticalSectionRawMutex, u8, 64> = Channel::new();
|
static PIPE: Channel<CriticalSectionRawMutex, u8, 64> = Channel::new();
|
||||||
|
static PIPE_REC: Channel<CriticalSectionRawMutex, u8, 64> = Channel::new();
|
||||||
static SEND_ALLOWED: AtomicBool = AtomicBool::new(true);
|
static SEND_ALLOWED: AtomicBool = AtomicBool::new(true);
|
||||||
// static REC_ALLOWED: AtomicBool = AtomicBool::new(true);
|
// static REC_ALLOWED: AtomicBool = AtomicBool::new(true);
|
||||||
|
|
||||||
@@ -26,17 +26,19 @@ async fn main(spawner: Spawner) {
|
|||||||
info!("tititititi");
|
info!("tititititi");
|
||||||
|
|
||||||
let p = embassy_stm32::init(Default::default());
|
let p = embassy_stm32::init(Default::default());
|
||||||
let mut tx = Output::new(p.PF2, gpio::Level::High, gpio::Speed::VeryHigh);
|
let tx = Output::new(p.PF2, gpio::Level::High, gpio::Speed::VeryHigh);
|
||||||
let rx = Input::new(p.PA3, Pull::Up);
|
let rx = ExtiInput::new(p.PA3, p.EXTI3, Pull::Up);
|
||||||
|
|
||||||
let config = Config::default();
|
// let config = Config::default();
|
||||||
let sender = PIPE.sender();
|
let sender = PIPE.sender();
|
||||||
|
let receiver_sender = PIPE_REC.sender();
|
||||||
// let receiver = PIPE.receiver();
|
// let receiver = PIPE.receiver();
|
||||||
|
|
||||||
spawner.spawn(bit_send(tx)).unwrap();
|
spawner.spawn(bit_send(tx)).unwrap();
|
||||||
|
spawner.spawn(bit_receive(rx, receiver_sender)).unwrap();
|
||||||
|
|
||||||
info!("starting echo");
|
info!("starting echo");
|
||||||
let mut v: Vec<u8, 38, u8> = Vec::new();
|
let v: Vec<u8, 38, u8> = Vec::new();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let slovko = "ahoj";
|
let slovko = "ahoj";
|
||||||
@@ -108,10 +110,38 @@ async fn bit_send(mut pin: Output<'static>) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[embassy_executor::task]
|
#[embassy_executor::task]
|
||||||
async fn bit_receive(mut pin: Input<'static>) {
|
async fn bit_receive(
|
||||||
let mut ticker = Ticker::every(Duration::from_millis(10));
|
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 {
|
loop {
|
||||||
// bitrate
|
pin.wait_for_any_edge().await;
|
||||||
ticker.next().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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user