From a93965eccf1478a05810e26c89d099d5a7deaf7f Mon Sep 17 00:00:00 2001 From: Priec Date: Sun, 17 May 2026 23:09:21 +0200 Subject: [PATCH] polish: --- 2sem_sem2/src/bin/main.rs | 4 ++-- 2sem_sem2/src/send.rs | 36 ++++++++++++++++-------------------- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/2sem_sem2/src/bin/main.rs b/2sem_sem2/src/bin/main.rs index 7940917..24f05d9 100644 --- a/2sem_sem2/src/bin/main.rs +++ b/2sem_sem2/src/bin/main.rs @@ -16,8 +16,8 @@ use semestralka2::send::*; use {defmt_rtt as _, panic_probe as _}; -static PIPE: Channel = Channel::new(); -static PIPE_REC: Channel = Channel::new(); +static PIPE: Channel = Channel::new(); +static PIPE_REC: Channel = Channel::new(); // static REC_ALLOWED: AtomicBool = AtomicBool::new(true); #[embassy_executor::main] diff --git a/2sem_sem2/src/send.rs b/2sem_sem2/src/send.rs index ebdf57c..b6a4fcb 100644 --- a/2sem_sem2/src/send.rs +++ b/2sem_sem2/src/send.rs @@ -1,14 +1,11 @@ // src/send.rs -use core::sync::atomic::{AtomicBool, Ordering}; -use defmt::info; use embassy_stm32::gpio::Output; use embassy_sync::{ blocking_mutex::raw::CriticalSectionRawMutex, channel::{Receiver, Sender}, }; use embassy_time::{Duration, Ticker}; -use heapless::Vec; const START: u8 = 0x7E; const STOP: u8 = 0x81; @@ -16,18 +13,6 @@ const BIT_PERIOD: Duration = Duration::from_millis(10); 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) { for i in (0..8).rev() { tx.send((byte >> i) & 1).await; @@ -46,12 +31,11 @@ pub async fn nrz(payload: &[u8], tx: &Tx) { send_byte(b, tx).await; } - let mut buf: Vec = Vec::new(); - let _ = buf.push(len); + // crc + let mut crc = crc8(0, len); for &b in payload { - let _ = buf.push(b); + crc = crc8(crc, b); } - let crc = crc8(&buf); send_byte(crc, tx).await; 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; } +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, 64>, + rx: Receiver<'static, CriticalSectionRawMutex, u8, 128>, ) { let mut ticker = Ticker::every(BIT_PERIOD); loop {