Compare commits
2 Commits
master
...
dd978ec65c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dd978ec65c | ||
|
|
3930716ac3 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1 @@
|
|||||||
*.pdf
|
*.pdf
|
||||||
dma_example/
|
|
||||||
|
|||||||
@@ -1,95 +1,71 @@
|
|||||||
// 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_stm32::dma::Request;
|
use embassy_futures::yield_now;
|
||||||
use embassy_stm32::gpio::{Input, Output, Level, Pull, Speed};
|
use embassy_stm32::gpio::{Input, Output, Level, Pull, Speed};
|
||||||
use embassy_time::{Duration, Timer};
|
use embassy_stm32::dma::{TransferOptions, Transfer, Priority, Request};
|
||||||
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, TX_RING_BYTES};
|
use dma_gpio::config::{BAUD, TX_PIN_BIT, TX_OVERSAMPLE};
|
||||||
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;
|
||||||
static TX_RING: StaticCell<[u32; TX_RING_BYTES]> = StaticCell::new();
|
|
||||||
|
static BUF_A: StaticCell<[u32; 8]> = StaticCell::new();
|
||||||
|
static BUF_B: StaticCell<[u32; 8]> = 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());
|
||||||
info!("DMA UART TX - Direct Buffer Modification");
|
let odr_ptr = embassy_stm32::pac::GPIOA.odr().as_ptr() as *mut u32;
|
||||||
|
|
||||||
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();
|
||||||
|
|
||||||
// Initialize buffer - keep reference for modification
|
let buf_a = BUF_A.init([0u32; 8]);
|
||||||
let tx_ring_mem: &'static mut [u32; TX_RING_BYTES] = TX_RING.init([0; TX_RING_BYTES]);
|
let buf_b = BUF_B.init([0u32; 8]);
|
||||||
|
|
||||||
// Fill with UART idle state (pin high)
|
let mut opts = TransferOptions::default();
|
||||||
let pin_mask = 1u32 << TX_PIN_BIT;
|
opts.priority = Priority::VeryHigh;
|
||||||
for word in tx_ring_mem.iter_mut() {
|
opts.complete_transfer_ir = true;
|
||||||
*word = pin_mask;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create DMA ring buffer with a SEPARATE slice created from raw parts
|
let mut ch0 = p.GPDMA1_CH0;
|
||||||
// This allows us to keep modifying tx_ring_mem
|
let mut using_a = true;
|
||||||
let odr_ptr = embassy_stm32::pac::GPIOA.odr().as_ptr() as *mut u32;
|
|
||||||
let tx_opts = TransferOptions::default();
|
|
||||||
|
|
||||||
let mut tx_ring = unsafe {
|
|
||||||
// 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 {
|
||||||
Timer::after(Duration::from_millis(400)).await;
|
let (dma_buf, cpu_buf) = if using_a {
|
||||||
|
(&*buf_a, &mut *buf_b)
|
||||||
|
} else {
|
||||||
|
(&*buf_b, &mut *buf_a)
|
||||||
|
};
|
||||||
|
|
||||||
// Encode the UART frames
|
let used = encode_uart_frames(TX_PIN_BIT, b"Hello marshmallow\r\n", cpu_buf).await;
|
||||||
let frame_len = encode_uart_frames(
|
|
||||||
TX_PIN_BIT,
|
|
||||||
b"Hello marshmallow\r\n",
|
|
||||||
&mut frame_buf
|
|
||||||
).await;
|
|
||||||
|
|
||||||
info!("Encoded {} words, copying to buffer...", frame_len);
|
let transfer = unsafe {
|
||||||
|
start_dma(ch0.reborrow(), TIM6_UP_REQ, odr_ptr, &dma_buf[..used], opts)
|
||||||
|
};
|
||||||
|
transfer.await;
|
||||||
|
using_a = !using_a;
|
||||||
|
|
||||||
// Copy directly into the ring buffer memory
|
yield_now().await;
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
info!("Buffer updated - DMA transmitting now");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 = 1;
|
pub const RX_OVERSAMPLE: u16 = 16;
|
||||||
pub const RX_RING_BYTES: usize = 4096;
|
pub const RX_RING_BYTES: usize = 4096;
|
||||||
pub const TX_RING_BYTES: usize = 256;
|
pub const TX_RING_BYTES: usize = 4096;
|
||||||
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 {
|
||||||
|
|||||||
Reference in New Issue
Block a user