3 Commits

Author SHA1 Message Date
Priec
fa6b217bc4 working with the dma library 2025-11-09 21:24:58 +01:00
Priec
ef98b7e4e9 fixing and working nonstop without timout 2025-11-09 19:29:49 +01:00
Priec
6620f9ad2b working write immidiate 2025-11-07 21:23:31 +01:00
4 changed files with 133 additions and 80 deletions

1
.gitignore vendored
View File

@@ -1,2 +1 @@
*.pdf
dma_example/

16
dma_gpio2/Cargo.lock generated
View File

@@ -295,7 +295,6 @@ dependencies = [
[[package]]
name = "embassy-embedded-hal"
version = "0.5.0"
source = "git+https://github.com/embassy-rs/embassy.git?branch=main#9b1add3d83e98c6c9ae9230bf35cd89bba530a20"
dependencies = [
"defmt 1.0.1",
"embassy-futures",
@@ -313,7 +312,6 @@ dependencies = [
[[package]]
name = "embassy-executor"
version = "0.9.1"
source = "git+https://github.com/embassy-rs/embassy.git?branch=main#9b1add3d83e98c6c9ae9230bf35cd89bba530a20"
dependencies = [
"cordyceps",
"cortex-m",
@@ -326,7 +324,6 @@ dependencies = [
[[package]]
name = "embassy-executor-macros"
version = "0.7.0"
source = "git+https://github.com/embassy-rs/embassy.git?branch=main#9b1add3d83e98c6c9ae9230bf35cd89bba530a20"
dependencies = [
"darling",
"proc-macro2",
@@ -337,17 +334,14 @@ dependencies = [
[[package]]
name = "embassy-executor-timer-queue"
version = "0.1.0"
source = "git+https://github.com/embassy-rs/embassy.git?branch=main#9b1add3d83e98c6c9ae9230bf35cd89bba530a20"
[[package]]
name = "embassy-futures"
version = "0.1.2"
source = "git+https://github.com/embassy-rs/embassy.git?branch=main#9b1add3d83e98c6c9ae9230bf35cd89bba530a20"
[[package]]
name = "embassy-hal-internal"
version = "0.3.0"
source = "git+https://github.com/embassy-rs/embassy.git?branch=main#9b1add3d83e98c6c9ae9230bf35cd89bba530a20"
dependencies = [
"cortex-m",
"critical-section",
@@ -358,7 +352,6 @@ dependencies = [
[[package]]
name = "embassy-net-driver"
version = "0.2.0"
source = "git+https://github.com/embassy-rs/embassy.git?branch=main#9b1add3d83e98c6c9ae9230bf35cd89bba530a20"
dependencies = [
"defmt 1.0.1",
]
@@ -366,7 +359,6 @@ dependencies = [
[[package]]
name = "embassy-net-driver-channel"
version = "0.3.2"
source = "git+https://github.com/embassy-rs/embassy.git?branch=main#9b1add3d83e98c6c9ae9230bf35cd89bba530a20"
dependencies = [
"embassy-futures",
"embassy-net-driver",
@@ -376,7 +368,6 @@ dependencies = [
[[package]]
name = "embassy-stm32"
version = "0.4.0"
source = "git+https://github.com/embassy-rs/embassy.git?branch=main#9b1add3d83e98c6c9ae9230bf35cd89bba530a20"
dependencies = [
"aligned",
"bit_field",
@@ -425,7 +416,6 @@ dependencies = [
[[package]]
name = "embassy-sync"
version = "0.7.2"
source = "git+https://github.com/embassy-rs/embassy.git?branch=main#9b1add3d83e98c6c9ae9230bf35cd89bba530a20"
dependencies = [
"cfg-if",
"critical-section",
@@ -439,7 +429,6 @@ dependencies = [
[[package]]
name = "embassy-time"
version = "0.5.0"
source = "git+https://github.com/embassy-rs/embassy.git?branch=main#9b1add3d83e98c6c9ae9230bf35cd89bba530a20"
dependencies = [
"cfg-if",
"critical-section",
@@ -455,7 +444,6 @@ dependencies = [
[[package]]
name = "embassy-time-driver"
version = "0.2.1"
source = "git+https://github.com/embassy-rs/embassy.git?branch=main#9b1add3d83e98c6c9ae9230bf35cd89bba530a20"
dependencies = [
"document-features",
]
@@ -463,7 +451,6 @@ dependencies = [
[[package]]
name = "embassy-time-queue-utils"
version = "0.3.0"
source = "git+https://github.com/embassy-rs/embassy.git?branch=main#9b1add3d83e98c6c9ae9230bf35cd89bba530a20"
dependencies = [
"embassy-executor-timer-queue",
"heapless 0.8.0",
@@ -472,7 +459,6 @@ dependencies = [
[[package]]
name = "embassy-usb"
version = "0.5.1"
source = "git+https://github.com/embassy-rs/embassy.git?branch=main#9b1add3d83e98c6c9ae9230bf35cd89bba530a20"
dependencies = [
"embassy-futures",
"embassy-net-driver-channel",
@@ -487,7 +473,6 @@ dependencies = [
[[package]]
name = "embassy-usb-driver"
version = "0.2.0"
source = "git+https://github.com/embassy-rs/embassy.git?branch=main#9b1add3d83e98c6c9ae9230bf35cd89bba530a20"
dependencies = [
"defmt 1.0.1",
"embedded-io-async",
@@ -496,7 +481,6 @@ dependencies = [
[[package]]
name = "embassy-usb-synopsys-otg"
version = "0.3.1"
source = "git+https://github.com/embassy-rs/embassy.git?branch=main#9b1add3d83e98c6c9ae9230bf35cd89bba530a20"
dependencies = [
"critical-section",
"defmt 1.0.1",

View File

@@ -10,13 +10,13 @@ cortex-m = { version = "0.7.7", features = ["inline-asm", "critical-section-sing
cortex-m-rt = "0.7.5"
panic-halt = "1.0.0"
embassy-executor = { git = "https://github.com/embassy-rs/embassy.git", branch = "main", features = ["arch-cortex-m", "executor-thread"] }
embassy-futures = { git = "https://github.com/embassy-rs/embassy.git", branch = "main" }
embassy-sync = { git = "https://github.com/embassy-rs/embassy.git", branch = "main" }
embassy-time = { git = "https://github.com/embassy-rs/embassy.git", branch = "main", features = ["tick-hz-32_768"] }
embassy-hal-internal = { git = "https://github.com/embassy-rs/embassy.git", branch = "main" }
embassy-usb = { git = "https://github.com/embassy-rs/embassy.git", branch = "main" }
embassy-stm32 = { git = "https://github.com/embassy-rs/embassy.git", branch = "main", features = ["unstable-pac", "stm32u575zi", "time-driver-tim2", "memory-x", "defmt"] }
embassy-executor = { path = "/home/filip/programs/embassy/embassy-executor", features = ["arch-cortex-m", "executor-thread"] }
embassy-futures = { path = "/home/filip/programs/embassy/embassy-futures" }
embassy-sync = { path = "/home/filip/programs/embassy/embassy-sync" }
embassy-time = { path = "/home/filip/programs/embassy/embassy-time", features = ["tick-hz-32_768"] }
embassy-hal-internal = { path = "/home/filip/programs/embassy/embassy-hal-internal" }
embassy-usb = { path = "/home/filip/programs/embassy/embassy-usb" }
embassy-stm32 = { path = "/home/filip/programs/embassy/embassy-stm32", features = ["unstable-pac", "stm32u575zi", "time-driver-tim2", "memory-x", "defmt"] }
embedded-hal = "1.0.0"
embedded-graphics = "0.8.1"

View File

@@ -1,95 +1,165 @@
// src/bin/main.rs
#![no_std]
#![no_main]
use defmt::*;
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::dma::{TransferOptions, Transfer, Priority, Request};
use embassy_time::{Duration, Timer};
use embassy_stm32::dma::{TransferOptions, WritableRingBuffer};
use dma_gpio::software_uart::{
dma_timer::init_tim6_for_uart,
gpio_dma_uart_tx::encode_uart_frames,
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 {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;
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();
static TX_RING: StaticCell<[u32; TX_RING_BYTES]> = StaticCell::new();
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let p = embassy_stm32::init(Default::default());
let odr_ptr = embassy_stm32::pac::GPIOA.odr().as_ptr() as *mut u32;
info!("Hehe");
let _rx = Input::new(p.PA3, embassy_stm32::gpio::Pull::Up);
let _tx = Output::new(p.PA2, Level::High, Speed::VeryHigh);
init_tim6_for_uart(p.TIM6, BAUD, TX_OVERSAMPLE);
dump_tim6_regs();
let buf_a = BUF_A.init([0u32; DMA_BUF_WORDS]);
let buf_b = BUF_B.init([0u32; DMA_BUF_WORDS]);
// Pre-fill with idle high level (stop bit level)
let idle = 1u32 << TX_PIN_BIT;
for w in buf_a.iter_mut() {
*w = idle;
}
for w in buf_b.iter_mut() {
*w = idle;
let idle: u32 = 1u32 << TX_PIN_BIT;
// Ring initialized to idle (line high).
let tx_ring_mem: &mut [u32; TX_RING_BYTES] = TX_RING.init([0; TX_RING_BYTES]);
let odr_ptr = embassy_stm32::pac::GPIOA.odr().as_ptr() as *mut u32;
let mut tx_opts = TransferOptions::default();
tx_opts.half_transfer_ir = true;
tx_opts.complete_transfer_ir = true;
let mut tx_ring = unsafe {
WritableRingBuffer::new(
p.GPDMA1_CH0,
TIM6_UP_REQ,
odr_ptr,
tx_ring_mem,
tx_opts,
)
};
{
// Full idle buffer matching ring capacity.
let idle_buf = [idle; TX_RING_BYTES];
let written = tx_ring
.write_exact(&idle_buf)
.await
.expect("Failed to prefill TX ring with idle");
info!(
"TX ring prefilled with idle via write_exact: written={} cap={}",
written,
tx_ring.capacity()
);
}
let mut opts = TransferOptions::default();
opts.priority = Priority::VeryHigh;
opts.complete_transfer_ir = true;
tx_ring.start();
info!(
"TX DMA ring started: cap_words={}",
tx_ring.capacity()
);
unsafe {
use embassy_stm32::pac::gpdma::Gpdma;
let dma = Gpdma::from_ptr(0x4002_0000 as _); // GPDMA1 base for STM32U5
let ch = dma.ch(0); // Channel 0
let mut ch0 = p.GPDMA1_CH0;
let mut using_a = true;
let cr = ch.cr().read();
let tr1 = ch.tr1().read();
let tr2 = ch.tr2().read();
let br1 = ch.br1().read();
let sar = ch.sar().read();
let dar = ch.dar().read();
let llr = ch.llr().read();
let lbar = ch.lbar().read();
info!(
"GPDMA1_CH0: EN={} HTIE={} TCIE={} SDW={:?} DDW={:?} SINC={} DINC={} BNDT={} SAR=0x{:08x} DAR=0x{:08x}",
cr.en(),
cr.htie(),
cr.tcie(),
tr1.sdw(),
tr1.ddw(),
tr1.sinc(),
tr1.dinc(),
br1.bndt(),
sar, // already u32
dar, // already u32
);
info!(
"GPDMA1_CH0: LBAR=0x{:08x} LLR=0x{:08x}",
lbar.lba(),
llr.0
);
}
let mut frame_buf = [0u32; 4096];
loop {
let (dma_buf, cpu_buf) = if using_a {
(&*buf_a, &mut *buf_b)
} else {
(&*buf_b, &mut *buf_a)
};
info!("tick start");
Timer::after(Duration::from_millis(1021)).await;
//info!("tick end");
// Start from idle pattern
let idle = 1u32 << TX_PIN_BIT;
for w in cpu_buf.iter_mut() {
*w = idle;
let used = encode_uart_frames(
TX_PIN_BIT,
b"H\r\n",
&mut frame_buf,
)
.await;
if used == 0 {
info!("encode_uart_frames() produced 0 words, skipping write");
yield_now().await;
continue;
}
let used = encode_uart_frames(TX_PIN_BIT, b"Hello marshmallow\r\n", cpu_buf).await;
// Debug: confirm encoded content.
let preview = core::cmp::min(used, 32);
info!(
"TX frame used={} words, head={=[?]}",
used,
&frame_buf[..preview]
);
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;
// Now it is safe to call write_exact.
match tx_ring.write_exact(&frame_buf[..used]).await {
Ok(written) => {
let len = tx_ring.len().unwrap_or(0);
info!(
"write_exact ok: written={} ring_used={} ring_cap={}",
written,
len,
tx_ring.capacity()
);
}
Err(e) => {
let len = tx_ring.len().unwrap_or(0);
warn!(
"write_exact error: {:?}, ring_used={} ring_cap={}",
e,
len,
tx_ring.capacity()
);
}
}
info!("tick end");
yield_now().await;
}
}