manchester is also working now

This commit is contained in:
Priec
2026-05-22 23:20:22 +02:00
parent 6dae8c727a
commit 92d69e875a
3 changed files with 72 additions and 18 deletions

View File

@@ -7,4 +7,5 @@ pub mod send;
pub enum Encoding {
Nrz,
Nrzi,
Manchester,
}

View File

@@ -1,5 +1,5 @@
use defmt::info;
use embassy_stm32::{exti::ExtiInput, sai::FrameSyncDefinition};
use embassy_stm32::exti::ExtiInput;
use embassy_time::{Ticker, Timer};
use crate::Encoding;
@@ -25,16 +25,18 @@ async fn sample_byte(
encoding: Encoding,
ticker: &mut Ticker,
last_physical_level: &mut bool,
bit_time: embassy_time::Duration,
) -> u8 {
let mut byte = 0u8;
for _ in 0..8 {
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
}
}
@@ -45,11 +47,22 @@ async fn sample_byte(
0
};
*last_physical_level = current_level;
ticker.next().await;
bit
}
Encoding::Manchester => {
Timer::after(bit_time / 4).await;
let first_half = pin.is_high();
Timer::after(bit_time / 2).await;
let second_half = pin.is_high();
Timer::after(bit_time / 4).await;
if first_half && !second_half { 1 } else { 0 }
}
};
byte = (byte << 1) | bit;
ticker.next().await;
}
byte
}
@@ -63,30 +76,43 @@ pub async fn bit_receive_and_decode(mut pin: ExtiInput<'static>, encoding: Encod
pin.wait_for_rising_edge().await;
let bit_time = embassy_time::Instant::now().duration_since(t1);
Timer::after(bit_time / 2).await;
let mut ticker = Ticker::every(bit_time);
// stred data bitu
if encoding != Encoding::Manchester {
Timer::after(bit_time / 2).await;
}
let mut ticker = Ticker::every(bit_time);
let mut last_level = true;
let mut start_byte = 0u8;
for _ in 0..7 {
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
}
}
Encoding::Nrzi => {
let current_level = pin.is_high();
let b = if current_level != last_level { 1 } else { 0 };
last_level = current_level;
ticker.next().await;
b
}
Encoding::Manchester => {
Timer::after(bit_time / 4).await;
let first_pol = pin.is_high();
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 }
}
};
start_byte = (start_byte << 1) | bit;
ticker.next().await;
}
if start_byte != START {
@@ -95,7 +121,7 @@ pub async fn bit_receive_and_decode(mut pin: ExtiInput<'static>, encoding: Encod
}
// len
let len = sample_byte(&mut pin, encoding, &mut ticker, &mut last_level).await;
let len = sample_byte(&mut pin, encoding, &mut ticker, &mut last_level, bit_time).await;
if len as usize > MAX_PAYLOAD {
info!("bad len {}", len);
continue;
@@ -104,32 +130,44 @@ pub async fn bit_receive_and_decode(mut pin: ExtiInput<'static>, encoding: Encod
// payload
let mut payload = [0u8; MAX_PAYLOAD];
for i in 0..(len as usize) {
payload[i] = sample_byte(&mut pin, encoding, &mut ticker, &mut last_level).await;
payload[i] =
sample_byte(&mut pin, encoding, &mut ticker, &mut last_level, bit_time).await;
}
// crc
let crc_recv = sample_byte(&mut pin, encoding, &mut ticker, &mut last_level).await;
let crc_recv =
sample_byte(&mut pin, encoding, &mut ticker, &mut last_level, bit_time).await;
// parita
let current_p = pin.is_high();
let parity_recv = match encoding {
Encoding::Nrz => {
if current_p {
if pin.is_high() {
ticker.next().await;
1
} else {
ticker.next().await;
0
}
}
Encoding::Nrzi => {
let current_p = pin.is_high();
let b = if current_p != last_level { 1 } else { 0 };
last_level = current_p;
ticker.next().await;
b
}
Encoding::Manchester => {
Timer::after(bit_time / 4).await;
let f = pin.is_high();
Timer::after(bit_time / 2).await;
let s = pin.is_high();
Timer::after(bit_time / 4).await;
if f && !s { 1 } else { 0 }
}
};
ticker.next().await;
// stop
let stop = sample_byte(&mut pin, encoding, &mut ticker, &mut last_level).await;
let stop = sample_byte(&mut pin, encoding, &mut ticker, &mut last_level, bit_time).await;
if stop != STOP {
info!("bad stop 0x{:02X}", stop);
continue;

View File

@@ -70,7 +70,7 @@ pub async fn bit_send(
rx: Receiver<'static, CriticalSectionRawMutex, u8, 128>,
encoding: Encoding,
) {
let mut ticker = Ticker::every(BIT_PERIOD);
let mut ticker = Ticker::every(BIT_PERIOD / 2);
let mut is_high = true;
pin.set_high();
@@ -85,6 +85,7 @@ pub async fn bit_send(
is_high = false;
pin.set_low();
}
ticker.next().await;
}
// toggle, ak sme v 1, inac sa nedeje nic
Encoding::Nrzi => {
@@ -96,10 +97,24 @@ pub async fn bit_send(
pin.set_low();
}
}
ticker.next().await;
}
// manchester xor tabulka
Encoding::Manchester => {
if bit == 1 {
// high -> low
pin.set_high();
ticker.next().await;
pin.set_low();
ticker.next().await;
} else {
// low -> high
pin.set_low();
ticker.next().await;
pin.set_high();
ticker.next().await;
}
}
};
// bitrate
ticker.next().await;
}
}