Compare commits
8 Commits
cb99c9b89b
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
416fdb0e7f | ||
|
|
a514098d00 | ||
|
|
ec72b2dc8f | ||
|
|
8c95b3343d | ||
|
|
ddef3db5fb | ||
|
|
6334c95e05 | ||
|
|
92d69e875a | ||
|
|
6dae8c727a |
1
2sem_sem2/.gitignore
vendored
1
2sem_sem2/.gitignore
vendored
@@ -1 +1,2 @@
|
|||||||
/target
|
/target
|
||||||
|
x.txt
|
||||||
|
|||||||
@@ -10,14 +10,14 @@ use embassy_stm32::gpio::{Output, Pull};
|
|||||||
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_time::Duration;
|
use embassy_time::Duration;
|
||||||
|
use semestralka2::level_for_bit;
|
||||||
use semestralka2::receive::bit_receive_and_decode;
|
use semestralka2::receive::bit_receive_and_decode;
|
||||||
use semestralka2::send::Tx;
|
use semestralka2::send::{bit_send, msg_encode};
|
||||||
use semestralka2::send::{bit_send, nrz};
|
|
||||||
|
|
||||||
use {defmt_rtt as _, panic_probe as _};
|
use {defmt_rtt as _, panic_probe as _};
|
||||||
|
|
||||||
static PIPE: Channel<CriticalSectionRawMutex, u8, 128> = Channel::new();
|
static PIPE_SEND: Channel<CriticalSectionRawMutex, u8, 128> = Channel::new();
|
||||||
static PIPE_REC: Channel<CriticalSectionRawMutex, u8, 128> = Channel::new();
|
// static PIPE_REC: Channel<CriticalSectionRawMutex, u8, 128> = Channel::new();
|
||||||
// static REC_ALLOWED: AtomicBool = AtomicBool::new(true);
|
// static REC_ALLOWED: AtomicBool = AtomicBool::new(true);
|
||||||
|
|
||||||
#[embassy_executor::main]
|
#[embassy_executor::main]
|
||||||
@@ -25,22 +25,31 @@ async fn main(spawner: Spawner) {
|
|||||||
info!("tititititi");
|
info!("tititititi");
|
||||||
|
|
||||||
let p = embassy_stm32::init(Default::default());
|
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 rx = ExtiInput::new(p.PA3, p.EXTI3, Pull::Up);
|
||||||
|
|
||||||
let sender = PIPE.sender();
|
let sender = PIPE_SEND.sender();
|
||||||
let receiver = PIPE.receiver();
|
let receiver = PIPE_SEND.receiver();
|
||||||
|
|
||||||
spawner.spawn(bit_send(tx, receiver)).unwrap();
|
let e_nrz = semestralka2::Encoding::Nrz;
|
||||||
spawner.spawn(bit_receive_and_decode(rx)).unwrap();
|
spawner.spawn(bit_send(tx, receiver, e_nrz)).unwrap();
|
||||||
|
spawner.spawn(bit_receive_and_decode(rx, e_nrz)).unwrap();
|
||||||
|
|
||||||
info!("starting loop");
|
info!("starting loop");
|
||||||
loop {
|
loop {
|
||||||
nrz(b"ahoj", &sender).await;
|
let _ = msg_encode(b"ahoj", &sender).await;
|
||||||
nrz(b"hello", &sender).await;
|
let _ = msg_encode(b"hello", &sender).await;
|
||||||
|
|
||||||
let data = [0x01, 0x02, 0x03, 0x04];
|
let data = [0x01, 0x02, 0x03, 0x04];
|
||||||
nrz(&data, &sender).await;
|
let _ = msg_encode(&data, &sender).await;
|
||||||
|
|
||||||
info!("frame sent");
|
info!("frame sent");
|
||||||
embassy_time::Timer::after(Duration::from_secs(1)).await;
|
embassy_time::Timer::after(Duration::from_secs(1)).await;
|
||||||
|
|||||||
@@ -2,3 +2,28 @@
|
|||||||
|
|
||||||
pub mod receive;
|
pub mod receive;
|
||||||
pub mod send;
|
pub mod send;
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, PartialEq)]
|
||||||
|
pub enum Encoding {
|
||||||
|
Nrz,
|
||||||
|
Nrzi,
|
||||||
|
Manchester,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, PartialEq)]
|
||||||
|
pub enum LogicLevel {
|
||||||
|
Low,
|
||||||
|
High,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const LOGIC_ZERO: LogicLevel = LogicLevel::Low;
|
||||||
|
pub const LOGIC_ONE: LogicLevel = LogicLevel::High;
|
||||||
|
|
||||||
|
pub fn level_for_bit(bit: u8) -> bool {
|
||||||
|
let level = if bit == 0 { LOGIC_ZERO } else { LOGIC_ONE };
|
||||||
|
level == LogicLevel::High
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn bit_for_level(level: bool) -> u8 {
|
||||||
|
if level_for_bit(1) == level { 1 } else { 0 }
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
use defmt::info;
|
use defmt::info;
|
||||||
use embassy_stm32::exti::ExtiInput;
|
use embassy_stm32::exti::ExtiInput;
|
||||||
use embassy_time::{Duration, Timer};
|
use embassy_time::{Ticker, Timer};
|
||||||
|
|
||||||
|
use crate::{Encoding, bit_for_level, level_for_bit};
|
||||||
|
|
||||||
const START: u8 = 0x7E;
|
const START: u8 = 0x7E;
|
||||||
const STOP: u8 = 0x81;
|
const STOP: u8 = 0x81;
|
||||||
@@ -18,32 +20,108 @@ fn crc8(crc: u8, byte: u8) -> u8 {
|
|||||||
c
|
c
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn sample_byte(pin: &mut ExtiInput<'static>, bit_time: Duration) -> u8 {
|
async fn sample_byte(
|
||||||
|
pin: &mut ExtiInput<'static>,
|
||||||
|
encoding: Encoding,
|
||||||
|
ticker: &mut Ticker,
|
||||||
|
last_physical_level: &mut bool,
|
||||||
|
bit_time: embassy_time::Duration,
|
||||||
|
) -> u8 {
|
||||||
let mut byte = 0u8;
|
let mut byte = 0u8;
|
||||||
for _ in 0..8 {
|
for _ in 0..8 {
|
||||||
let bit = if pin.is_high() { 1 } else { 0 };
|
let current_level = pin.is_high();
|
||||||
|
let bit = match encoding {
|
||||||
|
Encoding::Nrz => {
|
||||||
|
let bit = bit_for_level(pin.is_high());
|
||||||
|
ticker.next().await;
|
||||||
|
bit
|
||||||
|
}
|
||||||
|
Encoding::Nrzi => {
|
||||||
|
let bit = if current_level != *last_physical_level {
|
||||||
|
1
|
||||||
|
} else {
|
||||||
|
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 == level_for_bit(1) && second_half == level_for_bit(0) {
|
||||||
|
1
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
byte = (byte << 1) | bit;
|
byte = (byte << 1) | bit;
|
||||||
Timer::after(bit_time).await;
|
|
||||||
}
|
}
|
||||||
byte
|
byte
|
||||||
}
|
}
|
||||||
|
|
||||||
#[embassy_executor::task]
|
#[embassy_executor::task]
|
||||||
pub async fn bit_receive_and_decode(mut pin: ExtiInput<'static>) {
|
pub async fn bit_receive_and_decode(mut pin: ExtiInput<'static>, encoding: Encoding) {
|
||||||
loop {
|
loop {
|
||||||
pin.wait_for_falling_edge().await;
|
if level_for_bit(1) {
|
||||||
|
pin.wait_for_falling_edge().await;
|
||||||
|
} else {
|
||||||
|
pin.wait_for_rising_edge().await;
|
||||||
|
}
|
||||||
let t1 = embassy_time::Instant::now();
|
let t1 = embassy_time::Instant::now();
|
||||||
|
|
||||||
pin.wait_for_rising_edge().await;
|
if level_for_bit(1) {
|
||||||
|
pin.wait_for_rising_edge().await;
|
||||||
|
} else {
|
||||||
|
pin.wait_for_falling_edge().await;
|
||||||
|
}
|
||||||
let bit_time = embassy_time::Instant::now().duration_since(t1);
|
let bit_time = embassy_time::Instant::now().duration_since(t1);
|
||||||
|
|
||||||
Timer::after(bit_time / 2).await;
|
// stred data bitu
|
||||||
|
if encoding != Encoding::Manchester {
|
||||||
|
Timer::after(bit_time / 2).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut ticker = Ticker::every(bit_time);
|
||||||
|
let mut last_level = level_for_bit(1);
|
||||||
|
|
||||||
|
// First START bit is the already detected logical 0, sample the remaining 7 bits.
|
||||||
let mut start_byte = 0u8;
|
let mut start_byte = 0u8;
|
||||||
for _ in 0..7 {
|
for _ in 0..7 {
|
||||||
let bit = if pin.is_high() { 1 } else { 0 };
|
let bit = match encoding {
|
||||||
|
Encoding::Nrz => {
|
||||||
|
let bit = bit_for_level(pin.is_high());
|
||||||
|
ticker.next().await;
|
||||||
|
bit
|
||||||
|
}
|
||||||
|
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 == level_for_bit(1) && second_pol == level_for_bit(0) {
|
||||||
|
1
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
start_byte = (start_byte << 1) | bit;
|
start_byte = (start_byte << 1) | bit;
|
||||||
Timer::after(bit_time).await;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if start_byte != START {
|
if start_byte != START {
|
||||||
@@ -52,7 +130,7 @@ pub async fn bit_receive_and_decode(mut pin: ExtiInput<'static>) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// len
|
// len
|
||||||
let len = sample_byte(&mut pin, bit_time).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;
|
||||||
@@ -61,18 +139,44 @@ pub async fn bit_receive_and_decode(mut pin: ExtiInput<'static>) {
|
|||||||
// 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, bit_time).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, bit_time).await;
|
let crc_recv =
|
||||||
|
sample_byte(&mut pin, encoding, &mut ticker, &mut last_level, bit_time).await;
|
||||||
|
|
||||||
// parita
|
// parita
|
||||||
let parity_recv = if pin.is_high() { 1 } else { 0 };
|
let parity_recv = match encoding {
|
||||||
Timer::after(bit_time).await;
|
Encoding::Nrz => {
|
||||||
|
let bit = bit_for_level(pin.is_high());
|
||||||
|
ticker.next().await;
|
||||||
|
bit
|
||||||
|
}
|
||||||
|
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 == level_for_bit(1) && s == level_for_bit(0) {
|
||||||
|
1
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// stop
|
// stop
|
||||||
let stop = sample_byte(&mut pin, bit_time).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;
|
||||||
|
|||||||
@@ -1,18 +1,25 @@
|
|||||||
// src/send.rs
|
// src/send.rs
|
||||||
|
|
||||||
use embassy_stm32::gpio::Output;
|
use embassy_stm32::gpio::{Level, Output};
|
||||||
use embassy_sync::{
|
use embassy_sync::{
|
||||||
blocking_mutex::raw::CriticalSectionRawMutex,
|
blocking_mutex::raw::CriticalSectionRawMutex,
|
||||||
channel::{Receiver, Sender},
|
channel::{Receiver, Sender},
|
||||||
};
|
};
|
||||||
use embassy_time::{Duration, Ticker};
|
use embassy_time::{Duration, Ticker};
|
||||||
|
|
||||||
|
use crate::{Encoding, level_for_bit};
|
||||||
|
|
||||||
const START: u8 = 0x7E;
|
const START: u8 = 0x7E;
|
||||||
const STOP: u8 = 0x81;
|
const STOP: u8 = 0x81;
|
||||||
|
const MAX_PAYLOAD: usize = 64;
|
||||||
const BIT_PERIOD: Duration = Duration::from_millis(10);
|
const BIT_PERIOD: Duration = Duration::from_millis(10);
|
||||||
|
|
||||||
pub type Tx = Sender<'static, CriticalSectionRawMutex, u8, 128>;
|
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) {
|
async fn send_byte(byte: u8, tx: &Tx) {
|
||||||
for i in (0..8).rev() {
|
for i in (0..8).rev() {
|
||||||
tx.send((byte >> i) & 1).await;
|
tx.send((byte >> i) & 1).await;
|
||||||
@@ -20,7 +27,11 @@ async fn send_byte(byte: u8, tx: &Tx) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// [START] [LEN] [PAYLOAD ...] [CRC] [P] [STOP]
|
/// [START] [LEN] [PAYLOAD ...] [CRC] [P] [STOP]
|
||||||
pub async fn nrz(payload: &[u8], tx: &Tx) {
|
pub async fn msg_encode(payload: &[u8], tx: &Tx) -> Result<(), ()> {
|
||||||
|
if payload.len() > MAX_PAYLOAD {
|
||||||
|
return Err(());
|
||||||
|
}
|
||||||
|
|
||||||
// start
|
// start
|
||||||
send_byte(START, tx).await;
|
send_byte(START, tx).await;
|
||||||
|
|
||||||
@@ -48,6 +59,8 @@ pub async fn nrz(payload: &[u8], tx: &Tx) {
|
|||||||
|
|
||||||
// idle
|
// idle
|
||||||
tx.send(1).await;
|
tx.send(1).await;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn crc8(crc: u8, byte: u8) -> u8 {
|
fn crc8(crc: u8, byte: u8) -> u8 {
|
||||||
@@ -66,16 +79,48 @@ fn crc8(crc: u8, byte: u8) -> u8 {
|
|||||||
pub async fn bit_send(
|
pub async fn bit_send(
|
||||||
mut pin: Output<'static>,
|
mut pin: Output<'static>,
|
||||||
rx: Receiver<'static, CriticalSectionRawMutex, u8, 128>,
|
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 = level_for_bit(1);
|
||||||
|
set_level(&mut pin, is_high);
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let bit = rx.receive().await;
|
let bit = rx.receive().await;
|
||||||
if bit == 1 {
|
match encoding {
|
||||||
pin.set_high();
|
Encoding::Nrz => {
|
||||||
} else {
|
is_high = level_for_bit(bit);
|
||||||
pin.set_low();
|
set_level(&mut pin, is_high);
|
||||||
}
|
ticker.next().await;
|
||||||
// bitrate
|
}
|
||||||
ticker.next().await;
|
// toggle, ak sme v 1, inac sa nedeje nic
|
||||||
|
Encoding::Nrzi => {
|
||||||
|
if bit == 1 {
|
||||||
|
is_high = !is_high;
|
||||||
|
if is_high {
|
||||||
|
pin.set_high();
|
||||||
|
} else {
|
||||||
|
pin.set_low();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ticker.next().await;
|
||||||
|
}
|
||||||
|
// manchester xor tabulka
|
||||||
|
Encoding::Manchester => {
|
||||||
|
if bit == 1 {
|
||||||
|
// high -> low
|
||||||
|
set_level(&mut pin, level_for_bit(1));
|
||||||
|
ticker.next().await;
|
||||||
|
set_level(&mut pin, level_for_bit(0));
|
||||||
|
ticker.next().await;
|
||||||
|
} else {
|
||||||
|
// low -> high
|
||||||
|
set_level(&mut pin, level_for_bit(0));
|
||||||
|
ticker.next().await;
|
||||||
|
set_level(&mut pin, level_for_bit(1));
|
||||||
|
ticker.next().await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user