Compare commits
14 Commits
6992b46ed5
...
v2sem.sem2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ddef3db5fb | ||
|
|
6334c95e05 | ||
|
|
92d69e875a | ||
|
|
6dae8c727a | ||
|
|
cb99c9b89b | ||
|
|
c7d790f474 | ||
|
|
a93965eccf | ||
|
|
22b3b2307d | ||
|
|
5399983dfb | ||
|
|
34b10426c2 | ||
|
|
89ad23b3a0 | ||
|
|
d0fd820434 | ||
|
|
8cbd43bc3a | ||
|
|
86032fc821 |
1
2sem_sem2/.gitignore
vendored
1
2sem_sem2/.gitignore
vendored
@@ -1 +1,2 @@
|
|||||||
/target
|
/target
|
||||||
|
x.txt
|
||||||
|
|||||||
53
2sem_sem2/Cargo.lock
generated
53
2sem_sem2/Cargo.lock
generated
@@ -616,32 +616,6 @@ dependencies = [
|
|||||||
"pin-utils",
|
"pin-utils",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "hal_rng"
|
|
||||||
version = "0.1.0"
|
|
||||||
dependencies = [
|
|
||||||
"cortex-m",
|
|
||||||
"cortex-m-rt",
|
|
||||||
"defmt 1.0.1",
|
|
||||||
"defmt-rtt",
|
|
||||||
"embassy-executor",
|
|
||||||
"embassy-futures",
|
|
||||||
"embassy-stm32",
|
|
||||||
"embassy-sync",
|
|
||||||
"embassy-time",
|
|
||||||
"embassy-usb",
|
|
||||||
"embedded-graphics",
|
|
||||||
"embedded-hal 1.0.0",
|
|
||||||
"embedded-io",
|
|
||||||
"embedded-io-async",
|
|
||||||
"heapless 0.9.1",
|
|
||||||
"micromath",
|
|
||||||
"panic-halt",
|
|
||||||
"panic-probe",
|
|
||||||
"static_cell",
|
|
||||||
"tinybmp",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hash32"
|
name = "hash32"
|
||||||
version = "0.3.1"
|
version = "0.3.1"
|
||||||
@@ -676,6 +650,7 @@ version = "0.9.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b1edcd5a338e64688fbdcb7531a846cfd3476a54784dcb918a0844682bc7ada5"
|
checksum = "b1edcd5a338e64688fbdcb7531a846cfd3476a54784dcb918a0844682bc7ada5"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"defmt 1.0.1",
|
||||||
"hash32",
|
"hash32",
|
||||||
"stable_deref_trait",
|
"stable_deref_trait",
|
||||||
]
|
]
|
||||||
@@ -835,6 +810,32 @@ version = "0.9.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b328e2cb950eeccd55b7f55c3a963691455dcd044cfb5354f0c5e68d2c2d6ee2"
|
checksum = "b328e2cb950eeccd55b7f55c3a963691455dcd044cfb5354f0c5e68d2c2d6ee2"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "semestralka2"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"cortex-m",
|
||||||
|
"cortex-m-rt",
|
||||||
|
"defmt 1.0.1",
|
||||||
|
"defmt-rtt",
|
||||||
|
"embassy-executor",
|
||||||
|
"embassy-futures",
|
||||||
|
"embassy-stm32",
|
||||||
|
"embassy-sync",
|
||||||
|
"embassy-time",
|
||||||
|
"embassy-usb",
|
||||||
|
"embedded-graphics",
|
||||||
|
"embedded-hal 1.0.0",
|
||||||
|
"embedded-io",
|
||||||
|
"embedded-io-async",
|
||||||
|
"heapless 0.9.1",
|
||||||
|
"micromath",
|
||||||
|
"panic-halt",
|
||||||
|
"panic-probe",
|
||||||
|
"static_cell",
|
||||||
|
"tinybmp",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "semver"
|
name = "semver"
|
||||||
version = "0.9.0"
|
version = "0.9.0"
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
authors = ["Priec <filippriec@gmail.com>"]
|
authors = ["Priec <filippriec@gmail.com>"]
|
||||||
name = "hal_rng"
|
name = "semestralka2"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
|
||||||
@@ -12,14 +12,14 @@ panic-halt = "1.0.0"
|
|||||||
|
|
||||||
embassy-executor = { version = "0.9.1", features = ["arch-cortex-m", "executor-thread"] }
|
embassy-executor = { version = "0.9.1", features = ["arch-cortex-m", "executor-thread"] }
|
||||||
embassy-futures = "0.1.2"
|
embassy-futures = "0.1.2"
|
||||||
embassy-stm32 = { version = "0.4.0", features = ["unstable-pac", "stm32u575zi", "time-driver-any", "memory-x", "defmt"] }
|
embassy-stm32 = { version = "0.4.0", features = ["unstable-pac", "stm32u575zi", "time-driver-any", "memory-x", "defmt", "exti"] }
|
||||||
embassy-sync = "0.7.2"
|
embassy-sync = "0.7.2"
|
||||||
embassy-time = { version = "0.5.0", features = ["tick-hz-32_768"] }
|
embassy-time = { version = "0.5.0", features = ["tick-hz-32_768"] }
|
||||||
embassy-usb = "0.5.1"
|
embassy-usb = "0.5.1"
|
||||||
|
|
||||||
embedded-hal = "1.0.0"
|
embedded-hal = "1.0.0"
|
||||||
embedded-graphics = "0.8.1"
|
embedded-graphics = "0.8.1"
|
||||||
heapless = { version = "0.9.1", default-features = false }
|
heapless = { version = "0.9.1", default-features = false, features = ["defmt"] }
|
||||||
micromath = "2.1.0"
|
micromath = "2.1.0"
|
||||||
tinybmp = "0.6.0"
|
tinybmp = "0.6.0"
|
||||||
panic-probe = { version = "1.0.0", features = ["defmt"] }
|
panic-probe = { version = "1.0.0", features = ["defmt"] }
|
||||||
|
|||||||
@@ -2,125 +2,57 @@
|
|||||||
#![no_std]
|
#![no_std]
|
||||||
#![no_main]
|
#![no_main]
|
||||||
|
|
||||||
use defmt::*;
|
use defmt::info;
|
||||||
use core::sync::atomic::{AtomicBool, Ordering};
|
|
||||||
use embassy_executor::Spawner;
|
use embassy_executor::Spawner;
|
||||||
use embassy_stm32::usart::{Config, Uart, UartRx, UartTx, InterruptHandler};
|
use embassy_stm32::exti::ExtiInput;
|
||||||
use embassy_stm32::peripherals::{USART1, USART3};
|
use embassy_stm32::gpio;
|
||||||
use embassy_stm32::bind_interrupts;
|
use embassy_stm32::gpio::{Output, Pull};
|
||||||
use embassy_time::{Duration, Timer};
|
|
||||||
use embassy_sync::channel::Channel;
|
|
||||||
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
|
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};
|
||||||
|
|
||||||
use {defmt_rtt as _, panic_probe as _};
|
use {defmt_rtt as _, panic_probe as _};
|
||||||
|
|
||||||
bind_interrupts!(struct Irqs {
|
static PIPE_SEND: Channel<CriticalSectionRawMutex, u8, 128> = Channel::new();
|
||||||
USART1 => InterruptHandler<USART1>;
|
static PIPE_REC: Channel<CriticalSectionRawMutex, u8, 128> = Channel::new();
|
||||||
USART3 => InterruptHandler<USART3>;
|
// static REC_ALLOWED: AtomicBool = AtomicBool::new(true);
|
||||||
});
|
|
||||||
|
|
||||||
static PIPE: Channel<CriticalSectionRawMutex, u8, 64> = Channel::new();
|
|
||||||
static SEND_ALLOWED: AtomicBool = AtomicBool::new(true);
|
|
||||||
static REC_ALLOWED: AtomicBool = AtomicBool::new(true);
|
|
||||||
|
|
||||||
#[embassy_executor::main]
|
#[embassy_executor::main]
|
||||||
async fn main(spawner: Spawner) {
|
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,
|
||||||
|
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 config = Config::default();
|
let sender = PIPE_SEND.sender();
|
||||||
let dev_usart = Uart::new(
|
let receiver = PIPE_SEND.receiver();
|
||||||
p.USART1,
|
|
||||||
p.PA10,
|
|
||||||
p.PA9,
|
|
||||||
Irqs,
|
|
||||||
p.GPDMA1_CH0, // TX DMA
|
|
||||||
p.GPDMA1_CH1, // RX DMA
|
|
||||||
config,
|
|
||||||
).unwrap();
|
|
||||||
|
|
||||||
let uart = Uart::new(
|
let e_nrz = semestralka2::Encoding::Nrz;
|
||||||
p.USART3,
|
spawner.spawn(bit_send(tx, receiver, e_nrz)).unwrap();
|
||||||
p.PD9, // RX zo zariadenia
|
spawner.spawn(bit_receive_and_decode(rx, e_nrz)).unwrap();
|
||||||
p.PD8, // TX do zariadenia
|
|
||||||
Irqs,
|
|
||||||
p.GPDMA1_CH2,
|
|
||||||
p.GPDMA1_CH3,
|
|
||||||
config,
|
|
||||||
).unwrap();
|
|
||||||
let (mut tx, rx) = uart.split();
|
|
||||||
let (dev_tx, dev_rx) = dev_usart.split();
|
|
||||||
spawner.spawn(rx_task(rx, dev_tx)).unwrap();
|
|
||||||
spawner.spawn(toggle_rec_task()).unwrap();
|
|
||||||
|
|
||||||
let sender = PIPE.sender();
|
|
||||||
let receiver = PIPE.receiver();
|
|
||||||
|
|
||||||
info!("starting echo");
|
|
||||||
|
|
||||||
let data = b"filip\r\n";
|
|
||||||
let idle = &[0x13u8];
|
|
||||||
|
|
||||||
|
info!("starting loop");
|
||||||
loop {
|
loop {
|
||||||
// rec_allowed = (start_time.elapsed().as_secs() / 2) % 2 == 0;
|
msg_encode(b"ahoj", &sender).await;
|
||||||
let current_send_allowed = SEND_ALLOWED.load(Ordering::Relaxed);
|
msg_encode(b"hello", &sender).await;
|
||||||
let current_rec_allowed = REC_ALLOWED.load(Ordering::Relaxed);
|
|
||||||
if current_rec_allowed == true {
|
|
||||||
if current_send_allowed {
|
|
||||||
if let Ok(stored_byte) = receiver.try_receive() {
|
|
||||||
let _ = tx.write(&[stored_byte]).await;
|
|
||||||
} else {
|
|
||||||
let _ = tx.write(data).await;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
for b in data {
|
|
||||||
let _ = sender.try_send(*b);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
let _ = tx.write(idle).await;
|
|
||||||
}
|
|
||||||
let _ = Timer::after(Duration::from_millis(100));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[embassy_executor::task]
|
let data = [0x01, 0x02, 0x03, 0x04];
|
||||||
async fn rx_task(
|
msg_encode(&data, &sender).await;
|
||||||
mut rx: UartRx<'static, embassy_stm32::mode::Async>,
|
|
||||||
mut pc_tx: UartTx<'static, embassy_stm32::mode::Async>
|
|
||||||
) {
|
|
||||||
let mut buf = [0u8; 1];
|
|
||||||
loop {
|
|
||||||
if rx.read(&mut buf).await.is_ok() {
|
|
||||||
match buf[0] {
|
|
||||||
0x11 => {
|
|
||||||
SEND_ALLOWED.store(true, Ordering::Relaxed);
|
|
||||||
info!("Vysielanie povolené");
|
|
||||||
}
|
|
||||||
0x13 => {
|
|
||||||
SEND_ALLOWED.store(false, Ordering::Relaxed);
|
|
||||||
info!("Vysielanie zakázané");
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
let _ = pc_tx.write(&buf).await;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[embassy_executor::task]
|
info!("frame sent");
|
||||||
async fn toggle_rec_task() {
|
embassy_time::Timer::after(Duration::from_secs(1)).await;
|
||||||
loop {
|
|
||||||
Timer::after(Duration::from_secs(2)).await;
|
|
||||||
// Načítame aktuálnu hodnotu a uložíme jej opak
|
|
||||||
let current = REC_ALLOWED.load(Ordering::Relaxed);
|
|
||||||
REC_ALLOWED.store(!current, Ordering::Relaxed);
|
|
||||||
|
|
||||||
if !current {
|
|
||||||
info!("REC_ALLOWED: true");
|
|
||||||
} else {
|
|
||||||
info!("REC_ALLOWED: false");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1 +1,21 @@
|
|||||||
#![no_std]
|
#![no_std]
|
||||||
|
|
||||||
|
pub mod receive;
|
||||||
|
pub mod send;
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, PartialEq)]
|
||||||
|
pub enum Encoding {
|
||||||
|
Nrz,
|
||||||
|
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 }
|
||||||
|
}
|
||||||
|
|||||||
202
2sem_sem2/src/receive.rs
Normal file
202
2sem_sem2/src/receive.rs
Normal file
@@ -0,0 +1,202 @@
|
|||||||
|
use defmt::info;
|
||||||
|
use embassy_stm32::exti::ExtiInput;
|
||||||
|
use embassy_time::{Ticker, Timer};
|
||||||
|
|
||||||
|
use crate::{Encoding, bit_for_level, level_for_bit};
|
||||||
|
|
||||||
|
const START: u8 = 0x7E;
|
||||||
|
const STOP: u8 = 0x81;
|
||||||
|
const MAX_PAYLOAD: usize = 64;
|
||||||
|
|
||||||
|
fn crc8(crc: u8, byte: u8) -> u8 {
|
||||||
|
let mut c = crc ^ byte;
|
||||||
|
for _ in 0..8 {
|
||||||
|
c = if c & 0x80 != 0 {
|
||||||
|
(c << 1) ^ 0x07
|
||||||
|
} else {
|
||||||
|
c << 1
|
||||||
|
};
|
||||||
|
}
|
||||||
|
c
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
for _ in 0..8 {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
#[embassy_executor::task]
|
||||||
|
pub async fn bit_receive_and_decode(mut pin: ExtiInput<'static>, encoding: Encoding) {
|
||||||
|
loop {
|
||||||
|
pin.wait_for_falling_edge().await;
|
||||||
|
let t1 = embassy_time::Instant::now();
|
||||||
|
|
||||||
|
pin.wait_for_rising_edge().await;
|
||||||
|
let bit_time = embassy_time::Instant::now().duration_since(t1);
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
|
||||||
|
let mut start_byte = 0u8;
|
||||||
|
for _ in 0..7 {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
if start_byte != START {
|
||||||
|
info!("start fail: 0x{:02X}", start_byte);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// len
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, bit_time).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
// crc
|
||||||
|
let crc_recv =
|
||||||
|
sample_byte(&mut pin, encoding, &mut ticker, &mut last_level, bit_time).await;
|
||||||
|
|
||||||
|
// parita
|
||||||
|
let parity_recv = match encoding {
|
||||||
|
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
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// verify CRC
|
||||||
|
let mut crc_calc = crc8(0, len);
|
||||||
|
for i in 0..(len as usize) {
|
||||||
|
crc_calc = crc8(crc_calc, payload[i]);
|
||||||
|
}
|
||||||
|
if crc_calc != crc_recv {
|
||||||
|
info!("crc nesedi 0x{:02X} ma byt 0x{:02X}", crc_recv, crc_calc);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// verify parity
|
||||||
|
let mut ones = len.count_ones() + crc_recv.count_ones();
|
||||||
|
for i in 0..(len as usize) {
|
||||||
|
ones += payload[i].count_ones();
|
||||||
|
}
|
||||||
|
if (ones as u8 & 1) != parity_recv {
|
||||||
|
info!("nesedi parita");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Ok(s) = core::str::from_utf8(&payload[..len as usize]) {
|
||||||
|
info!("RX OK: \"{}\"", s);
|
||||||
|
} else {
|
||||||
|
info!("RX OK: {} bytes", len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
119
2sem_sem2/src/send.rs
Normal file
119
2sem_sem2/src/send.rs
Normal file
@@ -0,0 +1,119 @@
|
|||||||
|
// src/send.rs
|
||||||
|
|
||||||
|
use embassy_stm32::gpio::{Level, Output};
|
||||||
|
use embassy_sync::{
|
||||||
|
blocking_mutex::raw::CriticalSectionRawMutex,
|
||||||
|
channel::{Receiver, Sender},
|
||||||
|
};
|
||||||
|
use embassy_time::{Duration, Ticker};
|
||||||
|
|
||||||
|
use crate::{level_for_bit, Encoding};
|
||||||
|
|
||||||
|
const START: u8 = 0x7E;
|
||||||
|
const STOP: u8 = 0x81;
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// [START] [LEN] [PAYLOAD ...] [CRC] [P] [STOP]
|
||||||
|
pub async fn msg_encode(payload: &[u8], tx: &Tx) {
|
||||||
|
// start
|
||||||
|
send_byte(START, tx).await;
|
||||||
|
|
||||||
|
// len
|
||||||
|
let len = payload.len() as u8;
|
||||||
|
send_byte(len, tx).await;
|
||||||
|
for &b in payload {
|
||||||
|
send_byte(b, tx).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
// crc
|
||||||
|
let mut crc = crc8(0, len);
|
||||||
|
for &b in payload {
|
||||||
|
crc = crc8(crc, b);
|
||||||
|
}
|
||||||
|
send_byte(crc, tx).await;
|
||||||
|
|
||||||
|
let mut ones = len.count_ones() + crc.count_ones();
|
||||||
|
for &b in payload {
|
||||||
|
ones += b.count_ones();
|
||||||
|
}
|
||||||
|
tx.send((ones as u8) & 1).await;
|
||||||
|
|
||||||
|
send_byte(STOP, tx).await;
|
||||||
|
|
||||||
|
// idle
|
||||||
|
tx.send(1).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn crc8(crc: u8, byte: u8) -> u8 {
|
||||||
|
let mut c = crc ^ byte;
|
||||||
|
for _ in 0..8 {
|
||||||
|
c = if c & 0x80 != 0 {
|
||||||
|
(c << 1) ^ 0x07
|
||||||
|
} else {
|
||||||
|
c << 1
|
||||||
|
};
|
||||||
|
}
|
||||||
|
c
|
||||||
|
}
|
||||||
|
|
||||||
|
#[embassy_executor::task]
|
||||||
|
pub async fn bit_send(
|
||||||
|
mut pin: Output<'static>,
|
||||||
|
rx: Receiver<'static, CriticalSectionRawMutex, u8, 128>,
|
||||||
|
encoding: Encoding,
|
||||||
|
) {
|
||||||
|
let mut ticker = Ticker::every(BIT_PERIOD / 2);
|
||||||
|
let mut is_high = level_for_bit(1);
|
||||||
|
set_level(&mut pin, is_high);
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let bit = rx.receive().await;
|
||||||
|
match encoding {
|
||||||
|
Encoding::Nrz => {
|
||||||
|
is_high = level_for_bit(bit);
|
||||||
|
set_level(&mut pin, is_high);
|
||||||
|
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