uart rx working fully

This commit is contained in:
Priec
2025-11-19 15:59:14 +01:00
parent 46486a6e74
commit 0cd40eb5e2
4 changed files with 165 additions and 30 deletions

View File

@@ -26,6 +26,8 @@ use dma_gpio::config::{PIPE_HW_TX, PIPE_HW_RX, PIPE_SW_TX, PIPE_SW_RX};
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, pipe::Pipe};
use dma_gpio::hw_uart_internal::usart2;
use dma_gpio::hw_uart_internal::driver::uart_task as uart_task_internal;
use dma_gpio::software_uart::decode_uart_samples;
use dma_gpio::config::UART_CFG;
use dma_gpio::config::{PIPE_INT_TX, PIPE_INT_RX};
use embassy_time::{Duration, Timer};
use embassy_stm32::pac;
@@ -120,13 +122,44 @@ async fn main(spawner: Spawner) {
// EDN OF SOFTWARE UART
unsafe { cortex_m::peripheral::NVIC::unmask(pac::Interrupt::TIM7); }
let mut last_bit: Option<u8> = None;
let mut rx_samples = [1u8; 1024];
let mut rx_count = 0usize;
loop {
// 1. Drain the channel into the local buffer
while let Ok(bit) = PD6_BITS.try_receive() {
if Some(bit) != last_bit {
// PD6 level changed since the last sample → print one 'c'
info!("c");
last_bit = Some(bit);
if rx_count < rx_samples.len() {
rx_samples[rx_count] = bit;
rx_count += 1;
} else {
// Buffer full: discarding oldest data by rotating
// Simple naive strategy: just clear half
// Ideally we should prevent this by processing faster
warn!("RX Buffer overflow, resetting");
rx_count = 0;
}
}
// 2. Try to decode UART frames from the buffer
if rx_count > 0 {
// Try to decode
let (decoded, consumed) = decode_uart_samples(
&rx_samples[..rx_count],
RX_OVERSAMPLE,
&UART_CFG
);
// Print received characters
for b in decoded {
info!("Received: {}", b as char);
}
// 3. Remove processed samples from the buffer
if consumed > 0 {
// Shift remaining data to the front
// copy_within is efficient for slices
rx_samples.copy_within(consumed..rx_count, 0);
rx_count -= consumed;
}
}
yield_now().await;