manchester is also working now
This commit is contained in:
@@ -7,4 +7,5 @@ pub mod send;
|
|||||||
pub enum Encoding {
|
pub enum Encoding {
|
||||||
Nrz,
|
Nrz,
|
||||||
Nrzi,
|
Nrzi,
|
||||||
|
Manchester,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use defmt::info;
|
use defmt::info;
|
||||||
use embassy_stm32::{exti::ExtiInput, sai::FrameSyncDefinition};
|
use embassy_stm32::exti::ExtiInput;
|
||||||
use embassy_time::{Ticker, Timer};
|
use embassy_time::{Ticker, Timer};
|
||||||
|
|
||||||
use crate::Encoding;
|
use crate::Encoding;
|
||||||
@@ -25,16 +25,18 @@ async fn sample_byte(
|
|||||||
encoding: Encoding,
|
encoding: Encoding,
|
||||||
ticker: &mut Ticker,
|
ticker: &mut Ticker,
|
||||||
last_physical_level: &mut bool,
|
last_physical_level: &mut bool,
|
||||||
|
bit_time: embassy_time::Duration,
|
||||||
) -> u8 {
|
) -> u8 {
|
||||||
let mut byte = 0u8;
|
let mut byte = 0u8;
|
||||||
for _ in 0..8 {
|
for _ in 0..8 {
|
||||||
let current_level = pin.is_high();
|
let current_level = pin.is_high();
|
||||||
|
|
||||||
let bit = match encoding {
|
let bit = match encoding {
|
||||||
Encoding::Nrz => {
|
Encoding::Nrz => {
|
||||||
if pin.is_high() {
|
if pin.is_high() {
|
||||||
|
ticker.next().await;
|
||||||
1
|
1
|
||||||
} else {
|
} else {
|
||||||
|
ticker.next().await;
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -45,11 +47,22 @@ async fn sample_byte(
|
|||||||
0
|
0
|
||||||
};
|
};
|
||||||
*last_physical_level = current_level;
|
*last_physical_level = current_level;
|
||||||
|
ticker.next().await;
|
||||||
bit
|
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;
|
byte = (byte << 1) | bit;
|
||||||
ticker.next().await;
|
|
||||||
}
|
}
|
||||||
byte
|
byte
|
||||||
}
|
}
|
||||||
@@ -63,30 +76,43 @@ pub async fn bit_receive_and_decode(mut pin: ExtiInput<'static>, encoding: Encod
|
|||||||
pin.wait_for_rising_edge().await;
|
pin.wait_for_rising_edge().await;
|
||||||
let bit_time = embassy_time::Instant::now().duration_since(t1);
|
let bit_time = embassy_time::Instant::now().duration_since(t1);
|
||||||
|
|
||||||
|
// stred data bitu
|
||||||
|
if encoding != Encoding::Manchester {
|
||||||
Timer::after(bit_time / 2).await;
|
Timer::after(bit_time / 2).await;
|
||||||
let mut ticker = Ticker::every(bit_time);
|
}
|
||||||
|
|
||||||
|
let mut ticker = Ticker::every(bit_time);
|
||||||
let mut last_level = true;
|
let mut last_level = true;
|
||||||
|
|
||||||
let mut start_byte = 0u8;
|
let mut start_byte = 0u8;
|
||||||
for _ in 0..7 {
|
for _ in 0..7 {
|
||||||
let current_level = pin.is_high();
|
|
||||||
let bit = match encoding {
|
let bit = match encoding {
|
||||||
Encoding::Nrz => {
|
Encoding::Nrz => {
|
||||||
if pin.is_high() {
|
if pin.is_high() {
|
||||||
|
ticker.next().await;
|
||||||
1
|
1
|
||||||
} else {
|
} else {
|
||||||
|
ticker.next().await;
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Encoding::Nrzi => {
|
Encoding::Nrzi => {
|
||||||
|
let current_level = pin.is_high();
|
||||||
let b = if current_level != last_level { 1 } else { 0 };
|
let b = if current_level != last_level { 1 } else { 0 };
|
||||||
last_level = current_level;
|
last_level = current_level;
|
||||||
|
ticker.next().await;
|
||||||
b
|
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;
|
start_byte = (start_byte << 1) | bit;
|
||||||
ticker.next().await;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if start_byte != START {
|
if start_byte != START {
|
||||||
@@ -95,7 +121,7 @@ pub async fn bit_receive_and_decode(mut pin: ExtiInput<'static>, encoding: Encod
|
|||||||
}
|
}
|
||||||
|
|
||||||
// len
|
// 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 {
|
if len as usize > MAX_PAYLOAD {
|
||||||
info!("bad len {}", len);
|
info!("bad len {}", len);
|
||||||
continue;
|
continue;
|
||||||
@@ -104,32 +130,44 @@ pub async fn bit_receive_and_decode(mut pin: ExtiInput<'static>, encoding: Encod
|
|||||||
// payload
|
// payload
|
||||||
let mut payload = [0u8; MAX_PAYLOAD];
|
let mut payload = [0u8; MAX_PAYLOAD];
|
||||||
for i in 0..(len as usize) {
|
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
|
// 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
|
// parita
|
||||||
let current_p = pin.is_high();
|
|
||||||
let parity_recv = match encoding {
|
let parity_recv = match encoding {
|
||||||
Encoding::Nrz => {
|
Encoding::Nrz => {
|
||||||
if current_p {
|
if pin.is_high() {
|
||||||
|
ticker.next().await;
|
||||||
1
|
1
|
||||||
} else {
|
} else {
|
||||||
|
ticker.next().await;
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Encoding::Nrzi => {
|
Encoding::Nrzi => {
|
||||||
|
let current_p = pin.is_high();
|
||||||
let b = if current_p != last_level { 1 } else { 0 };
|
let b = if current_p != last_level { 1 } else { 0 };
|
||||||
last_level = current_p;
|
last_level = current_p;
|
||||||
|
ticker.next().await;
|
||||||
b
|
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
|
// 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 {
|
if stop != STOP {
|
||||||
info!("bad stop 0x{:02X}", stop);
|
info!("bad stop 0x{:02X}", stop);
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ pub async fn bit_send(
|
|||||||
rx: Receiver<'static, CriticalSectionRawMutex, u8, 128>,
|
rx: Receiver<'static, CriticalSectionRawMutex, u8, 128>,
|
||||||
encoding: Encoding,
|
encoding: Encoding,
|
||||||
) {
|
) {
|
||||||
let mut ticker = Ticker::every(BIT_PERIOD);
|
let mut ticker = Ticker::every(BIT_PERIOD / 2);
|
||||||
let mut is_high = true;
|
let mut is_high = true;
|
||||||
pin.set_high();
|
pin.set_high();
|
||||||
|
|
||||||
@@ -85,6 +85,7 @@ pub async fn bit_send(
|
|||||||
is_high = false;
|
is_high = false;
|
||||||
pin.set_low();
|
pin.set_low();
|
||||||
}
|
}
|
||||||
|
ticker.next().await;
|
||||||
}
|
}
|
||||||
// toggle, ak sme v 1, inac sa nedeje nic
|
// toggle, ak sme v 1, inac sa nedeje nic
|
||||||
Encoding::Nrzi => {
|
Encoding::Nrzi => {
|
||||||
@@ -96,10 +97,24 @@ pub async fn bit_send(
|
|||||||
pin.set_low();
|
pin.set_low();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// bitrate
|
|
||||||
ticker.next().await;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user