2 Commits

Author SHA1 Message Date
Priec
3134399c4e direct solution works 2025-11-06 00:36:38 +01:00
Priec
9358f2e8ec not working, i have no clue why 2025-11-05 23:40:59 +01:00
2 changed files with 70 additions and 70 deletions

View File

@@ -1,95 +1,95 @@
// src/bin/main.rs // src/bin/main.rs
#![no_std] #![no_std]
#![no_main] #![no_main]
use defmt::*; use defmt::*;
use embassy_executor::Spawner; use embassy_executor::Spawner;
use embassy_futures::yield_now; use embassy_stm32::dma::Request;
use embassy_stm32::gpio::{Input, Output, Level, Pull, Speed}; use embassy_stm32::gpio::{Input, Output, Level, Pull, Speed};
use embassy_stm32::dma::{TransferOptions, Transfer, Priority, Request}; use embassy_time::{Duration, Timer};
use embassy_stm32::dma::{TransferOptions, WritableRingBuffer};
use dma_gpio::software_uart::{ use dma_gpio::software_uart::{
dma_timer::init_tim6_for_uart, dma_timer::init_tim6_for_uart,
gpio_dma_uart_tx::encode_uart_frames, gpio_dma_uart_tx::encode_uart_frames,
debug::dump_tim6_regs, debug::dump_tim6_regs,
}; };
use dma_gpio::config::{BAUD, TX_PIN_BIT, TX_OVERSAMPLE}; use dma_gpio::config::{BAUD, TX_PIN_BIT, TX_OVERSAMPLE, TX_RING_BYTES};
use static_cell::StaticCell; use static_cell::StaticCell;
use core::slice;
use {defmt_rtt as _, panic_probe as _}; use {defmt_rtt as _, panic_probe as _};
unsafe fn start_dma<'a>(
ch: embassy_hal_internal::Peri<'a, impl embassy_stm32::dma::Channel>,
request: Request,
odr_ptr: *mut u32,
buf: &'a [u32],
opts: TransferOptions,
) -> Transfer<'a> {
// new_write itself is unsafe
unsafe { Transfer::new_write(ch, request, buf, odr_ptr, opts) }
}
pub const TIM6_UP_REQ: Request = 4; pub const TIM6_UP_REQ: Request = 4;
const DMA_BUF_WORDS: usize = 256; static TX_RING: StaticCell<[u32; TX_RING_BYTES]> = StaticCell::new();
static BUF_A: StaticCell<[u32; DMA_BUF_WORDS]> = StaticCell::new();
static BUF_B: StaticCell<[u32; DMA_BUF_WORDS]> = StaticCell::new();
#[embassy_executor::main] #[embassy_executor::main]
async fn main(_spawner: Spawner) { async fn main(_spawner: Spawner) {
let p = embassy_stm32::init(Default::default()); let p = embassy_stm32::init(Default::default());
let odr_ptr = embassy_stm32::pac::GPIOA.odr().as_ptr() as *mut u32; info!("DMA UART TX - Direct Buffer Modification");
let _rx = Input::new(p.PA3, Pull::Up);
let _tx = Output::new(p.PA2, Level::High, Speed::VeryHigh);
init_tim6_for_uart(p.TIM6, BAUD, TX_OVERSAMPLE); init_tim6_for_uart(p.TIM6, BAUD, TX_OVERSAMPLE);
dump_tim6_regs(); dump_tim6_regs();
let buf_a = BUF_A.init([0u32; DMA_BUF_WORDS]); // Initialize buffer - keep reference for modification
let buf_b = BUF_B.init([0u32; DMA_BUF_WORDS]); let tx_ring_mem: &'static mut [u32; TX_RING_BYTES] = TX_RING.init([0; TX_RING_BYTES]);
// Pre-fill with idle high level (stop bit level)
let idle = 1u32 << TX_PIN_BIT; // Fill with UART idle state (pin high)
for w in buf_a.iter_mut() { let pin_mask = 1u32 << TX_PIN_BIT;
*w = idle; for word in tx_ring_mem.iter_mut() {
} *word = pin_mask;
for w in buf_b.iter_mut() {
*w = idle;
} }
let mut opts = TransferOptions::default(); // Create DMA ring buffer with a SEPARATE slice created from raw parts
opts.priority = Priority::VeryHigh; // This allows us to keep modifying tx_ring_mem
opts.complete_transfer_ir = true; let odr_ptr = embassy_stm32::pac::GPIOA.odr().as_ptr() as *mut u32;
let tx_opts = TransferOptions::default();
let mut ch0 = p.GPDMA1_CH0; let mut tx_ring = unsafe {
let mut using_a = true; // Create a separate mutable slice pointing to the same memory
let dma_slice = slice::from_raw_parts_mut(
tx_ring_mem.as_mut_ptr(),
tx_ring_mem.len()
);
WritableRingBuffer::new(
p.GPDMA1_CH0,
TIM6_UP_REQ,
odr_ptr,
dma_slice,
tx_opts,
)
};
tx_ring.start();
info!("DMA started - continuously reading from buffer");
// Temporary buffer for encoding
let mut frame_buf = [0u32; 4096];
loop { loop {
let (dma_buf, cpu_buf) = if using_a { Timer::after(Duration::from_millis(400)).await;
(&*buf_a, &mut *buf_b)
} else {
(&*buf_b, &mut *buf_a)
};
// Start from idle pattern // Encode the UART frames
let idle = 1u32 << TX_PIN_BIT; let frame_len = encode_uart_frames(
for w in cpu_buf.iter_mut() { TX_PIN_BIT,
*w = idle; b"Hello marshmallow\r\n",
&mut frame_buf
).await;
info!("Encoded {} words, copying to buffer...", frame_len);
// Copy directly into the ring buffer memory
// DMA will automatically read and transmit this!
let copy_len = frame_len.min(TX_RING_BYTES);
tx_ring_mem[..copy_len].copy_from_slice(&frame_buf[..copy_len]);
// Fill rest with idle state
for i in copy_len..TX_RING_BYTES {
tx_ring_mem[i] = pin_mask;
} }
let used = encode_uart_frames(TX_PIN_BIT, b"Hello marshmallow\r\n", cpu_buf).await; info!("Buffer updated - DMA transmitting now");
let len = if used == 0 { 1 } else { used };
// At least one word so DMA is always valid.
let transfer = unsafe {
start_dma(
ch0.reborrow(),
TIM6_UP_REQ,
odr_ptr,
&dma_buf[..len],
opts,
)
};
transfer.await;
using_a = !using_a;
yield_now().await;
} }
} }

View File

@@ -4,9 +4,9 @@ use crate::software_uart::uart_emulation::{Parity, StopBits, UartConfig};
pub const BAUD: u32 = 9_600; pub const BAUD: u32 = 9_600;
pub const TX_PIN_BIT: u8 = 2; // PA2 pub const TX_PIN_BIT: u8 = 2; // PA2
pub const TX_OVERSAMPLE: u16 = 1; pub const TX_OVERSAMPLE: u16 = 1;
pub const RX_OVERSAMPLE: u16 = 16; pub const RX_OVERSAMPLE: u16 = 1;
pub const RX_RING_BYTES: usize = 4096; pub const RX_RING_BYTES: usize = 4096;
pub const TX_RING_BYTES: usize = 4096; pub const TX_RING_BYTES: usize = 256;
pub const PIPE_RX_SIZE: usize = 256; pub const PIPE_RX_SIZE: usize = 256;
pub const UART_CFG: UartConfig = UartConfig { pub const UART_CFG: UartConfig = UartConfig {