Compare commits
5 Commits
89ad23b3a0
...
c7d790f474
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c7d790f474 | ||
|
|
a93965eccf | ||
|
|
22b3b2307d | ||
|
|
5399983dfb | ||
|
|
34b10426c2 |
52
2sem_sem2/Cargo.lock
generated
52
2sem_sem2/Cargo.lock
generated
@@ -616,32 +616,6 @@ dependencies = [
|
||||
"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]]
|
||||
name = "hash32"
|
||||
version = "0.3.1"
|
||||
@@ -836,6 +810,32 @@ version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
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]]
|
||||
name = "semver"
|
||||
version = "0.9.0"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
authors = ["Priec <filippriec@gmail.com>"]
|
||||
name = "hal_rng"
|
||||
name = "semestralka2"
|
||||
edition = "2024"
|
||||
version = "0.1.0"
|
||||
|
||||
@@ -12,7 +12,7 @@ panic-halt = "1.0.0"
|
||||
|
||||
embassy-executor = { version = "0.9.1", features = ["arch-cortex-m", "executor-thread"] }
|
||||
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-time = { version = "0.5.0", features = ["tick-hz-32_768"] }
|
||||
embassy-usb = "0.5.1"
|
||||
|
||||
@@ -2,23 +2,22 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use core::sync::atomic::{AtomicBool, Ordering};
|
||||
use defmt::info;
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_stm32::exti;
|
||||
use embassy_stm32::exti::ExtiInput;
|
||||
use embassy_stm32::gpio;
|
||||
use embassy_stm32::gpio::{Input, Output, Pull};
|
||||
use embassy_stm32::usart::{Config, InterruptHandler, Uart, UartRx, UartTx};
|
||||
use embassy_stm32::gpio::{Output, Pull};
|
||||
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
|
||||
use embassy_sync::channel::Channel;
|
||||
use embassy_sync::channel::Sender;
|
||||
use embassy_time::{Duration, Ticker, Timer};
|
||||
use heapless::Vec;
|
||||
use embassy_time::Duration;
|
||||
use semestralka2::receive::bit_receive_and_decode;
|
||||
use semestralka2::send::nrz;
|
||||
use semestralka2::send::Tx;
|
||||
|
||||
use {defmt_rtt as _, panic_probe as _};
|
||||
|
||||
static PIPE: Channel<CriticalSectionRawMutex, u8, 64> = Channel::new();
|
||||
static SEND_ALLOWED: AtomicBool = AtomicBool::new(true);
|
||||
static PIPE: 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]
|
||||
@@ -26,92 +25,24 @@ async fn main(spawner: Spawner) {
|
||||
info!("tititititi");
|
||||
|
||||
let p = embassy_stm32::init(Default::default());
|
||||
let mut tx = Output::new(p.PF2, gpio::Level::High, gpio::Speed::VeryHigh);
|
||||
let rx = Input::new(p.PA3, Pull::Up);
|
||||
let tx = Output::new(p.PF2, gpio::Level::High, gpio::Speed::VeryHigh);
|
||||
let rx = ExtiInput::new(p.PA3, p.EXTI3, Pull::Up);
|
||||
|
||||
let config = Config::default();
|
||||
let sender = PIPE.sender();
|
||||
// let receiver = PIPE.receiver();
|
||||
|
||||
spawner.spawn(bit_send(tx)).unwrap();
|
||||
|
||||
info!("starting echo");
|
||||
let mut v: Vec<u8, 38, u8> = Vec::new();
|
||||
|
||||
loop {
|
||||
let slovko = "ahoj";
|
||||
info!("slovo: {}", slovko);
|
||||
let vektorik = encode(slovko, v.clone());
|
||||
info!("slovo: {:#?}", vektorik);
|
||||
|
||||
info!("enkodovane, posielame do pipy...");
|
||||
nrz(&v, &sender).await;
|
||||
|
||||
let _ = Timer::after(Duration::from_millis(1000));
|
||||
embassy_time::block_for(Duration::from_millis(1000));
|
||||
}
|
||||
}
|
||||
|
||||
fn encode(word: &str, mut v: Vec<u8, 38, u8>) {
|
||||
v.clear();
|
||||
for byte in word.bytes() {
|
||||
for bit in 0..8 {
|
||||
let bit_value = (byte >> (7 - bit)) & 1;
|
||||
if v.push(bit_value).is_err() {
|
||||
info!("we panick boyz");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn nrz(enkodovany: &Vec<u8, 38, u8>, sender: &Sender<'_, CriticalSectionRawMutex, u8, 64>) {
|
||||
if !SEND_ALLOWED.load(Ordering::Relaxed) {
|
||||
return;
|
||||
}
|
||||
let sync_hod: u8 = 0xAA;
|
||||
let start_bits: u8 = 0xAB;
|
||||
let sequence = [sync_hod, start_bits];
|
||||
|
||||
for byte in sequence {
|
||||
for i in (0..8).rev() {
|
||||
sender.send((byte >> i) & 1).await;
|
||||
}
|
||||
}
|
||||
|
||||
for &bit in enkodovany {
|
||||
sender.send(bit).await;
|
||||
}
|
||||
|
||||
for i in (0..8).rev() {
|
||||
sender.send((sync_hod >> i) & 1).await;
|
||||
}
|
||||
|
||||
// idle
|
||||
sender.send(1).await;
|
||||
}
|
||||
|
||||
#[embassy_executor::task]
|
||||
async fn bit_send(mut pin: Output<'static>) {
|
||||
let mut ticker = Ticker::every(Duration::from_millis(10));
|
||||
let receiver = PIPE.receiver();
|
||||
loop {
|
||||
let bit = receiver.receive().await;
|
||||
if bit == 1 {
|
||||
pin.set_high();
|
||||
} else {
|
||||
pin.set_low();
|
||||
}
|
||||
// bitrate
|
||||
ticker.next().await;
|
||||
}
|
||||
}
|
||||
|
||||
#[embassy_executor::task]
|
||||
async fn bit_receive(mut pin: Input<'static>) {
|
||||
let mut ticker = Ticker::every(Duration::from_millis(10));
|
||||
spawner.spawn(bit_send(tx, receiver)).unwrap();
|
||||
spawner.spawn(bit_receive_and_decode(rx)).unwrap();
|
||||
|
||||
info!("starting loop");
|
||||
loop {
|
||||
// bitrate
|
||||
ticker.next().await;
|
||||
nrz(b"ahoj", &sender).await;
|
||||
nrz(b"hello", &sender).await;
|
||||
|
||||
let data = [0x01, 0x02, 0x03, 0x04];
|
||||
nrz(&data, &sender).await;
|
||||
|
||||
info!("frame sent");
|
||||
embassy_time::Timer::after(Duration::from_secs(1)).await;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1 +1,4 @@
|
||||
#![no_std]
|
||||
|
||||
pub mod receive;
|
||||
pub mod send;
|
||||
|
||||
107
2sem_sem2/src/receive.rs
Normal file
107
2sem_sem2/src/receive.rs
Normal file
@@ -0,0 +1,107 @@
|
||||
use defmt::info;
|
||||
use embassy_stm32::exti::ExtiInput;
|
||||
use embassy_time::{Duration, Timer};
|
||||
|
||||
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>, bit_time: Duration) -> u8 {
|
||||
let mut byte = 0u8;
|
||||
for _ in 0..8 {
|
||||
let bit = if pin.is_high() { 1 } else { 0 };
|
||||
byte = (byte << 1) | bit;
|
||||
Timer::after(bit_time).await;
|
||||
}
|
||||
byte
|
||||
}
|
||||
|
||||
#[embassy_executor::task]
|
||||
pub async fn bit_receive_and_decode(mut pin: ExtiInput<'static>) {
|
||||
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);
|
||||
|
||||
Timer::after(bit_time / 2).await;
|
||||
let mut start_byte = 0u8;
|
||||
for _ in 0..7 {
|
||||
let bit = if pin.is_high() { 1 } else { 0 };
|
||||
start_byte = (start_byte << 1) | bit;
|
||||
Timer::after(bit_time).await;
|
||||
}
|
||||
|
||||
if start_byte != START {
|
||||
info!("start fail: 0x{:02X}", start_byte);
|
||||
continue;
|
||||
}
|
||||
|
||||
// len
|
||||
let len = sample_byte(&mut pin, 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, bit_time).await;
|
||||
}
|
||||
|
||||
// crc
|
||||
let crc_recv = sample_byte(&mut pin, bit_time).await;
|
||||
|
||||
// parita
|
||||
let parity_recv = if pin.is_high() { 1 } else { 0 };
|
||||
Timer::after(bit_time).await;
|
||||
|
||||
// stop
|
||||
let stop = sample_byte(&mut pin, 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
81
2sem_sem2/src/send.rs
Normal file
81
2sem_sem2/src/send.rs
Normal file
@@ -0,0 +1,81 @@
|
||||
// src/send.rs
|
||||
|
||||
use embassy_stm32::gpio::Output;
|
||||
use embassy_sync::{
|
||||
blocking_mutex::raw::CriticalSectionRawMutex,
|
||||
channel::{Receiver, Sender},
|
||||
};
|
||||
use embassy_time::{Duration, Ticker};
|
||||
|
||||
const START: u8 = 0x7E;
|
||||
const STOP: u8 = 0x81;
|
||||
const BIT_PERIOD: Duration = Duration::from_millis(10);
|
||||
|
||||
type Tx = Sender<'static, CriticalSectionRawMutex, u8, 128>;
|
||||
|
||||
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 nrz(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>,
|
||||
) {
|
||||
let mut ticker = Ticker::every(BIT_PERIOD);
|
||||
loop {
|
||||
let bit = rx.receive().await;
|
||||
if bit == 1 {
|
||||
pin.set_high();
|
||||
} else {
|
||||
pin.set_low();
|
||||
}
|
||||
// bitrate
|
||||
ticker.next().await;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user