From ddef3db5fbc800e5762a30587be3502d0ac2a6a9 Mon Sep 17 00:00:00 2001 From: Priec Date: Thu, 28 May 2026 07:47:54 +0200 Subject: [PATCH] pin level agnostic --- 2sem_sem2/.gitignore | 1 + 2sem_sem2/src/bin/main.rs | 11 ++++++++- 2sem_sem2/src/lib.rs | 10 ++++++++ 2sem_sem2/src/receive.rs | 52 +++++++++++++++++++-------------------- 2sem_sem2/src/send.rs | 29 +++++++++++----------- 5 files changed, 61 insertions(+), 42 deletions(-) diff --git a/2sem_sem2/.gitignore b/2sem_sem2/.gitignore index ea8c4bf..4586ff0 100644 --- a/2sem_sem2/.gitignore +++ b/2sem_sem2/.gitignore @@ -1 +1,2 @@ /target +x.txt diff --git a/2sem_sem2/src/bin/main.rs b/2sem_sem2/src/bin/main.rs index 16089de..c1e2510 100644 --- a/2sem_sem2/src/bin/main.rs +++ b/2sem_sem2/src/bin/main.rs @@ -11,6 +11,7 @@ use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; use embassy_sync::channel::Channel; use embassy_time::Duration; use semestralka2::receive::bit_receive_and_decode; +use semestralka2::level_for_bit; use semestralka2::send::Tx; use semestralka2::send::{bit_send, msg_encode}; @@ -25,7 +26,15 @@ async fn main(spawner: Spawner) { info!("tititititi"); let p = embassy_stm32::init(Default::default()); - let tx = Output::new(p.PF2, gpio::Level::High, gpio::Speed::VeryHigh); + let tx = Output::new( + p.PF2, + if level_for_bit(1) { + gpio::Level::High + } else { + gpio::Level::Low + }, + gpio::Speed::VeryHigh, + ); let rx = ExtiInput::new(p.PA3, p.EXTI3, Pull::Up); let sender = PIPE_SEND.sender(); diff --git a/2sem_sem2/src/lib.rs b/2sem_sem2/src/lib.rs index 8eddb7f..305d2a1 100644 --- a/2sem_sem2/src/lib.rs +++ b/2sem_sem2/src/lib.rs @@ -9,3 +9,13 @@ pub enum Encoding { Nrzi, Manchester, } + +pub const LOGIC_ONE_HIGH: bool = true; + +pub fn level_for_bit(bit: u8) -> bool { + (bit != 0) == LOGIC_ONE_HIGH +} + +pub fn bit_for_level(level: bool) -> u8 { + if level == LOGIC_ONE_HIGH { 1 } else { 0 } +} diff --git a/2sem_sem2/src/receive.rs b/2sem_sem2/src/receive.rs index c5ceef0..26ce669 100644 --- a/2sem_sem2/src/receive.rs +++ b/2sem_sem2/src/receive.rs @@ -2,7 +2,7 @@ use defmt::info; use embassy_stm32::exti::ExtiInput; use embassy_time::{Ticker, Timer}; -use crate::Encoding; +use crate::{Encoding, bit_for_level, level_for_bit}; const START: u8 = 0x7E; const STOP: u8 = 0x81; @@ -32,13 +32,9 @@ async fn sample_byte( let current_level = pin.is_high(); let bit = match encoding { Encoding::Nrz => { - if pin.is_high() { - ticker.next().await; - 1 - } else { - ticker.next().await; - 0 - } + let bit = bit_for_level(pin.is_high()); + ticker.next().await; + bit } Encoding::Nrzi => { let bit = if current_level != *last_physical_level { @@ -59,7 +55,11 @@ async fn sample_byte( Timer::after(bit_time / 4).await; - if first_half && !second_half { 1 } else { 0 } + if first_half == level_for_bit(1) && second_half == level_for_bit(0) { + 1 + } else { + 0 + } } }; byte = (byte << 1) | bit; @@ -82,19 +82,15 @@ pub async fn bit_receive_and_decode(mut pin: ExtiInput<'static>, encoding: Encod } let mut ticker = Ticker::every(bit_time); - let mut last_level = true; + let mut last_level = level_for_bit(1); let mut start_byte = 0u8; for _ in 0..7 { let bit = match encoding { Encoding::Nrz => { - if pin.is_high() { - ticker.next().await; - 1 - } else { - ticker.next().await; - 0 - } + let bit = bit_for_level(pin.is_high()); + ticker.next().await; + bit } Encoding::Nrzi => { let current_level = pin.is_high(); @@ -109,7 +105,11 @@ pub async fn bit_receive_and_decode(mut pin: ExtiInput<'static>, encoding: Encod Timer::after(bit_time / 2).await; let second_pol = pin.is_high(); Timer::after(bit_time / 4).await; - if first_pol && !second_pol { 1 } else { 0 } + if first_pol == level_for_bit(1) && second_pol == level_for_bit(0) { + 1 + } else { + 0 + } } }; start_byte = (start_byte << 1) | bit; @@ -141,13 +141,9 @@ pub async fn bit_receive_and_decode(mut pin: ExtiInput<'static>, encoding: Encod // parita let parity_recv = match encoding { Encoding::Nrz => { - if pin.is_high() { - ticker.next().await; - 1 - } else { - ticker.next().await; - 0 - } + let bit = bit_for_level(pin.is_high()); + ticker.next().await; + bit } Encoding::Nrzi => { let current_p = pin.is_high(); @@ -162,7 +158,11 @@ pub async fn bit_receive_and_decode(mut pin: ExtiInput<'static>, encoding: Encod Timer::after(bit_time / 2).await; let s = pin.is_high(); Timer::after(bit_time / 4).await; - if f && !s { 1 } else { 0 } + if f == level_for_bit(1) && s == level_for_bit(0) { + 1 + } else { + 0 + } } }; diff --git a/2sem_sem2/src/send.rs b/2sem_sem2/src/send.rs index 8b0d1d0..6867619 100644 --- a/2sem_sem2/src/send.rs +++ b/2sem_sem2/src/send.rs @@ -1,13 +1,13 @@ // src/send.rs -use embassy_stm32::gpio::Output; +use embassy_stm32::gpio::{Level, Output}; use embassy_sync::{ blocking_mutex::raw::CriticalSectionRawMutex, channel::{Receiver, Sender}, }; use embassy_time::{Duration, Ticker}; -use crate::Encoding; +use crate::{level_for_bit, Encoding}; const START: u8 = 0x7E; const STOP: u8 = 0x81; @@ -15,6 +15,10 @@ const BIT_PERIOD: Duration = Duration::from_millis(10); pub type Tx = Sender<'static, CriticalSectionRawMutex, u8, 128>; +fn set_level(pin: &mut Output<'static>, is_high: bool) { + pin.set_level(if is_high { Level::High } else { Level::Low }); +} + async fn send_byte(byte: u8, tx: &Tx) { for i in (0..8).rev() { tx.send((byte >> i) & 1).await; @@ -71,20 +75,15 @@ pub async fn bit_send( encoding: Encoding, ) { let mut ticker = Ticker::every(BIT_PERIOD / 2); - let mut is_high = true; - pin.set_high(); + let mut is_high = level_for_bit(1); + set_level(&mut pin, is_high); loop { let bit = rx.receive().await; match encoding { Encoding::Nrz => { - if bit == 1 { - is_high = true; - pin.set_high(); - } else { - is_high = false; - pin.set_low(); - } + is_high = level_for_bit(bit); + set_level(&mut pin, is_high); ticker.next().await; } // toggle, ak sme v 1, inac sa nedeje nic @@ -103,15 +102,15 @@ pub async fn bit_send( Encoding::Manchester => { if bit == 1 { // high -> low - pin.set_high(); + set_level(&mut pin, level_for_bit(1)); ticker.next().await; - pin.set_low(); + set_level(&mut pin, level_for_bit(0)); ticker.next().await; } else { // low -> high - pin.set_low(); + set_level(&mut pin, level_for_bit(0)); ticker.next().await; - pin.set_high(); + set_level(&mut pin, level_for_bit(1)); ticker.next().await; } }