|
|
|
@@ -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_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();
|
|
|
|
const DMA_BUF_WORDS: usize = 256;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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());
|
|
|
|
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; DMA_BUF_WORDS]);
|
|
|
|
let tx_ring_mem: &'static mut [u32; TX_RING_BYTES] = TX_RING.init([0; TX_RING_BYTES]);
|
|
|
|
let buf_b = BUF_B.init([0u32; DMA_BUF_WORDS]);
|
|
|
|
|
|
|
|
// Pre-fill with idle high level (stop bit level)
|
|
|
|
// Fill with UART idle state (pin high)
|
|
|
|
let idle = 1u32 << TX_PIN_BIT;
|
|
|
|
let pin_mask = 1u32 << TX_PIN_BIT;
|
|
|
|
for w in buf_a.iter_mut() {
|
|
|
|
for word in tx_ring_mem.iter_mut() {
|
|
|
|
*w = idle;
|
|
|
|
*word = pin_mask;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for w in buf_b.iter_mut() {
|
|
|
|
// Create DMA ring buffer with a SEPARATE slice created from raw parts
|
|
|
|
*w = idle;
|
|
|
|
// This allows us to keep modifying tx_ring_mem
|
|
|
|
}
|
|
|
|
let odr_ptr = embassy_stm32::pac::GPIOA.odr().as_ptr() as *mut u32;
|
|
|
|
|
|
|
|
let tx_opts = TransferOptions::default();
|
|
|
|
let mut opts = TransferOptions::default();
|
|
|
|
|
|
|
|
opts.priority = Priority::VeryHigh;
|
|
|
|
let mut tx_ring = unsafe {
|
|
|
|
opts.complete_transfer_ir = true;
|
|
|
|
// Create a separate mutable slice pointing to the same memory
|
|
|
|
|
|
|
|
let dma_slice = slice::from_raw_parts_mut(
|
|
|
|
let mut ch0 = p.GPDMA1_CH0;
|
|
|
|
tx_ring_mem.as_mut_ptr(),
|
|
|
|
let mut using_a = true;
|
|
|
|
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)
|
|
|
|
// Encode the UART frames
|
|
|
|
} else {
|
|
|
|
let frame_len = encode_uart_frames(
|
|
|
|
(&*buf_b, &mut *buf_a)
|
|
|
|
TX_PIN_BIT,
|
|
|
|
};
|
|
|
|
b"Hello marshmallow\r\n",
|
|
|
|
|
|
|
|
&mut frame_buf
|
|
|
|
// Start from idle pattern
|
|
|
|
).await;
|
|
|
|
let idle = 1u32 << TX_PIN_BIT;
|
|
|
|
|
|
|
|
for w in cpu_buf.iter_mut() {
|
|
|
|
info!("Encoded {} words, copying to buffer...", frame_len);
|
|
|
|
*w = idle;
|
|
|
|
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
info!("Buffer updated - DMA transmitting now");
|
|
|
|
let used = encode_uart_frames(TX_PIN_BIT, b"Hello marshmallow\r\n", cpu_buf).await;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|