pin level agnostic
This commit is contained in:
1
2sem_sem2/.gitignore
vendored
1
2sem_sem2/.gitignore
vendored
@@ -1 +1,2 @@
|
||||
/target
|
||||
x.txt
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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 }
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user