compiled, config in src/config, but i get hardfault crash at runtime

This commit is contained in:
Priec
2025-11-05 09:44:41 +01:00
parent 44f154e289
commit d57d16935d
5 changed files with 86 additions and 22 deletions

View File

@@ -9,6 +9,8 @@ use embassy_stm32::dma::{
ReadableRingBuffer as DmaRingRx,
TransferOptions,
};
use crate::config::{RX_OVERSAMPLE, UART_CFG};
use crate::software_uart::decode_uart_samples;
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, pipe::Pipe};
// datasheet tabulka 137
@@ -34,6 +36,7 @@ pub async fn rx_dma_task(
let mut chunk = [0u8; 256];
loop {
let _ = rx.read_exact(&mut chunk).await;
pipe_rx.write(&chunk).await;
let decoded = decode_uart_samples(&chunk, RX_OVERSAMPLE, &UART_CFG);
pipe_rx.write(&decoded).await;
}
}

View File

@@ -1,4 +1,5 @@
// src/software_uart/uart_emulation.rs
use heapless::Vec;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Parity {
@@ -87,3 +88,63 @@ pub fn encode_uart_byte_cfg(
idx
}
/// Decode an oversampled stream of logic levels into UART bytes.
pub fn decode_uart_samples(
samples: &[u8],
oversample: u16,
cfg: &UartConfig,
) -> heapless::Vec<u8, 256> {
let mut out = Vec::<u8, 256>::new();
let mut idx = 0usize;
let nbits = cfg.data_bits as usize;
while idx + (oversample as usize * (nbits + 3)) < samples.len() {
// Wait for start bit (falling edge: high -> low)
if samples[idx] != 0 && samples[idx + 1] == 0 {
// Align to middle of start bit
idx += (oversample / 2) as usize;
// Sanity check start bit really low
if samples.get(idx).copied().unwrap_or(1) != 0 {
idx += 1;
continue;
}
// Sample data bits
let mut data: u8 = 0;
for bit in 0..nbits {
idx += oversample as usize;
let bit_val = samples
.get(idx)
.map(|&b| if b != 0 { 1u8 } else { 0u8 })
.unwrap_or(1);
data |= bit_val << bit;
}
// Parity: skip / verify
match cfg.parity {
Parity::None => {}
Parity::Even | Parity::Odd => {
idx += oversample as usize;
// You can optionally add parity check here if needed
}
}
// Move past stop bits
let stop_skip = match cfg.stop_bits {
StopBits::One => oversample as usize,
StopBits::Two => (oversample * 2) as usize,
};
idx += stop_skip;
// Push decoded byte
let _ = out.push(data);
} else {
idx += 1;
}
}
out
}