4 Commits

Author SHA1 Message Date
Priec
416fdb0e7f unused pipe 2026-05-28 09:13:09 +02:00
Priec
a514098d00 level in enum 2026-05-28 08:09:27 +02:00
Priec
ec72b2dc8f fix pokial 0 a 1 je predefinovana pouzivatelom 2026-05-28 08:02:12 +02:00
Priec
8c95b3343d cargo fmt + max size of a payload at send 2026-05-28 07:57:25 +02:00
4 changed files with 36 additions and 13 deletions

View File

@@ -10,15 +10,14 @@ use embassy_stm32::gpio::{Output, Pull};
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::receive::bit_receive_and_decode;
use semestralka2::send::{bit_send, msg_encode};
use {defmt_rtt as _, panic_probe as _};
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);
#[embassy_executor::main]
@@ -46,11 +45,11 @@ async fn main(spawner: Spawner) {
info!("starting loop");
loop {
msg_encode(b"ahoj", &sender).await;
msg_encode(b"hello", &sender).await;
let _ = msg_encode(b"ahoj", &sender).await;
let _ = msg_encode(b"hello", &sender).await;
let data = [0x01, 0x02, 0x03, 0x04];
msg_encode(&data, &sender).await;
let _ = msg_encode(&data, &sender).await;
info!("frame sent");
embassy_time::Timer::after(Duration::from_secs(1)).await;

View File

@@ -10,12 +10,20 @@ pub enum Encoding {
Manchester,
}
pub const LOGIC_ONE_HIGH: bool = true;
#[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 {
(bit != 0) == LOGIC_ONE_HIGH
let level = if bit == 0 { LOGIC_ZERO } else { LOGIC_ONE };
level == LogicLevel::High
}
pub fn bit_for_level(level: bool) -> u8 {
if level == LOGIC_ONE_HIGH { 1 } else { 0 }
if level_for_bit(1) == level { 1 } else { 0 }
}

View File

@@ -70,10 +70,18 @@ async fn sample_byte(
#[embassy_executor::task]
pub async fn bit_receive_and_decode(mut pin: ExtiInput<'static>, encoding: Encoding) {
loop {
if level_for_bit(1) {
pin.wait_for_falling_edge().await;
} else {
pin.wait_for_rising_edge().await;
}
let t1 = embassy_time::Instant::now();
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);
// stred data bitu
@@ -84,6 +92,7 @@ pub async fn bit_receive_and_decode(mut pin: ExtiInput<'static>, encoding: Encod
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;
for _ in 0..7 {
let bit = match encoding {

View File

@@ -7,10 +7,11 @@ use embassy_sync::{
};
use embassy_time::{Duration, Ticker};
use crate::{level_for_bit, Encoding};
use crate::{Encoding, level_for_bit};
const START: u8 = 0x7E;
const STOP: u8 = 0x81;
const MAX_PAYLOAD: usize = 64;
const BIT_PERIOD: Duration = Duration::from_millis(10);
pub type Tx = Sender<'static, CriticalSectionRawMutex, u8, 128>;
@@ -26,7 +27,11 @@ async fn send_byte(byte: u8, tx: &Tx) {
}
/// [START] [LEN] [PAYLOAD ...] [CRC] [P] [STOP]
pub async fn msg_encode(payload: &[u8], tx: &Tx) {
pub async fn msg_encode(payload: &[u8], tx: &Tx) -> Result<(), ()> {
if payload.len() > MAX_PAYLOAD {
return Err(());
}
// start
send_byte(START, tx).await;
@@ -54,6 +59,8 @@ pub async fn msg_encode(payload: &[u8], tx: &Tx) {
// idle
tx.send(1).await;
Ok(())
}
fn crc8(crc: u8, byte: u8) -> u8 {