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
3 changed files with 112 additions and 65 deletions

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

@@ -7,7 +7,6 @@ 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_sync::{blocking_mutex::raw::CriticalSectionRawMutex, pipe::Pipe};
use embassy_time::{Duration, Timer};
use embassy_stm32::dma::{TransferOptions, WritableRingBuffer};
use dma_gpio::software_uart::{
@@ -15,57 +14,36 @@ use dma_gpio::software_uart::{
gpio_dma_uart_tx::encode_uart_frames,
debug::dump_tim6_regs,
};
use dma_gpio::config::{BAUD, TX_PIN_BIT, RX_OVERSAMPLE, TX_OVERSAMPLE};
use dma_gpio::config::{TX_RING_BYTES, RX_RING_BYTES, PIPE_RX_SIZE};
use dma_gpio::config::{BAUD, TX_PIN_BIT, TX_OVERSAMPLE, TX_RING_BYTES};
use static_cell::StaticCell;
use {defmt_rtt as _, panic_probe as _};
// kapitola 17.4.11 - 2 casovace pre 2 DMA
pub const TIM6_UP_REQ: Request = 4; // Table 137: tim6_upd_dma, strana 687 STM32U5xx datasheet
pub const TIM6_UP_REQ: Request = 4;
static TX_RING: StaticCell<[u32; TX_RING_BYTES]> = StaticCell::new();
use core::future::poll_fn;
use core::task::Poll;
async fn wait_for_space<'a, W: embassy_stm32::dma::word::Word>(
ring: &mut embassy_stm32::dma::WritableRingBuffer<'a, W>,
min_free: usize,
) {
poll_fn(|cx| {
let used = ring.len().unwrap_or(0);
let cap = ring.capacity();
if cap - used > min_free {
Poll::Ready(())
} else {
ring.set_waker(cx.waker());
Poll::Pending
}
}).await
}
#[embassy_executor::main]
async fn main(spawner: Spawner) {
async fn main(_spawner: Spawner) {
let p = embassy_stm32::init(Default::default());
info!("Hehe");
let _rx = Input::new(p.PA3, Pull::Up);
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();
// Safe one-time init from StaticCell
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]);
// Create and start the TX DMA ring in main.
// let bsrr_ptr = embassy_stm32::pac::GPIOA.bsrr().as_ptr() as *mut u32;
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;
// SAFETY: tx_ring_mem is exclusive
let mut tx_ring = unsafe {
WritableRingBuffer::new(
p.GPDMA1_CH0,
@@ -75,28 +53,113 @@ async fn main(spawner: Spawner) {
tx_opts,
)
};
// Start DMA
{
// 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()
);
}
tx_ring.start();
info!("TX DMA ring started");
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 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 {
info!("tick start");
Timer::after(Duration::from_millis(400)).await;
info!("tick end");
Timer::after(Duration::from_millis(1021)).await;
//info!("tick end");
let used = encode_uart_frames(TX_PIN_BIT, b"Hello marshmallow\r\n", &mut frame_buf).await;
let used = encode_uart_frames(
TX_PIN_BIT,
b"H\r\n",
&mut frame_buf,
)
.await;
// Wait for DMA to free space, async style
wait_for_space(&mut tx_ring, used / 2).await;
if let Err(e) = tx_ring.write_exact(&frame_buf[..used]).await {
warn!("DMA ring write error: {:?}", e);
} else {
info!("Frame queued to DMA ring");
if used == 0 {
info!("encode_uart_frames() produced 0 words, skipping write");
yield_now().await;
continue;
}
// Debug: confirm encoded content.
let preview = core::cmp::min(used, 32);
info!(
"TX frame used={} words, head={=[?]}",
used,
&frame_buf[..preview]
);
// 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;
}
}