This commit is contained in:
Priec
2026-05-17 23:09:21 +02:00
parent 22b3b2307d
commit a93965eccf
2 changed files with 18 additions and 22 deletions

View File

@@ -16,8 +16,8 @@ use semestralka2::send::*;
use {defmt_rtt as _, panic_probe as _}; use {defmt_rtt as _, panic_probe as _};
static PIPE: Channel<CriticalSectionRawMutex, u8, 64> = Channel::new(); static PIPE: Channel<CriticalSectionRawMutex, u8, 128> = Channel::new();
static PIPE_REC: Channel<CriticalSectionRawMutex, u8, 64> = 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]

View File

@@ -1,14 +1,11 @@
// src/send.rs // src/send.rs
use core::sync::atomic::{AtomicBool, Ordering};
use defmt::info;
use embassy_stm32::gpio::Output; use embassy_stm32::gpio::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 heapless::Vec;
const START: u8 = 0x7E; const START: u8 = 0x7E;
const STOP: u8 = 0x81; const STOP: u8 = 0x81;
@@ -16,18 +13,6 @@ const BIT_PERIOD: Duration = Duration::from_millis(10);
type Tx = Sender<'static, CriticalSectionRawMutex, u8, 128>; type Tx = Sender<'static, CriticalSectionRawMutex, u8, 128>;
fn crc8(data: &[u8]) -> u8 {
let mut crc: u8 = 0;
for &b in data {
crc ^= b;
for _ in 0..8 {
let mask = if (crc & 0x80) != 0 { 0x07 } else { 0 };
crc = (crc << 1) ^ mask;
}
}
crc
}
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;
@@ -46,12 +31,11 @@ pub async fn nrz(payload: &[u8], tx: &Tx) {
send_byte(b, tx).await; send_byte(b, tx).await;
} }
let mut buf: Vec<u8, 64> = Vec::new(); // crc
let _ = buf.push(len); let mut crc = crc8(0, len);
for &b in payload { for &b in payload {
let _ = buf.push(b); crc = crc8(crc, b);
} }
let crc = crc8(&buf);
send_byte(crc, tx).await; send_byte(crc, tx).await;
let mut ones = len.count_ones() + crc.count_ones(); let mut ones = len.count_ones() + crc.count_ones();
@@ -66,10 +50,22 @@ pub async fn nrz(payload: &[u8], tx: &Tx) {
tx.send(1).await; 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] #[embassy_executor::task]
pub async fn bit_send( pub async fn bit_send(
mut pin: Output<'static>, mut pin: Output<'static>,
rx: Receiver<'static, CriticalSectionRawMutex, u8, 64>, rx: Receiver<'static, CriticalSectionRawMutex, u8, 128>,
) { ) {
let mut ticker = Ticker::every(BIT_PERIOD); let mut ticker = Ticker::every(BIT_PERIOD);
loop { loop {