From 0119ea809dba3a0b002174f615650a5a8fd9c22e Mon Sep 17 00:00:00 2001 From: Bob McWhirter Date: Fri, 16 Jul 2021 14:40:25 -0400 Subject: [PATCH] Get DMA on H7 working, add usart_dma example for H7. --- embassy-stm32/src/dma/bdma.rs | 45 +----------- embassy-stm32/src/dma/dma.rs | 13 +++- examples/stm32h7/src/bin/usart_dma.rs | 100 ++++++++++++++++++++++++++ stm32-data | 2 +- 4 files changed, 115 insertions(+), 45 deletions(-) create mode 100644 examples/stm32h7/src/bin/usart_dma.rs diff --git a/embassy-stm32/src/dma/bdma.rs b/embassy-stm32/src/dma/bdma.rs index c6e77488..26adda08 100644 --- a/embassy-stm32/src/dma/bdma.rs +++ b/embassy-stm32/src/dma/bdma.rs @@ -139,7 +139,7 @@ unsafe fn on_irq() { /// safety: must be called only once pub(crate) unsafe fn init() { pac::interrupts! { - (DMA, $irq:ident) => { + (BDMA, $irq:ident) => { crate::interrupt::$irq::steal().enable(); }; } @@ -209,52 +209,11 @@ pac::bdma_channels! { }; } -// HACK: on stm32h7 "DMA" interrupts are for DMA, not BDMA, so this -// would cause duplicate interrupt definitions. Do it manually insted -#[cfg(not(rcc_h7))] pac::interrupts! { - (DMA, $irq:ident) => { + (BDMA, $irq:ident) => { #[crate::interrupt] unsafe fn $irq () { on_irq() } }; } - -#[cfg(rcc_h7)] -mod _if_h7 { - use super::*; - - #[crate::interrupt] - unsafe fn BDMA_CHANNEL0() { - on_irq() - } - #[crate::interrupt] - unsafe fn BDMA_CHANNEL1() { - on_irq() - } - #[crate::interrupt] - unsafe fn BDMA_CHANNEL2() { - on_irq() - } - #[crate::interrupt] - unsafe fn BDMA_CHANNEL3() { - on_irq() - } - #[crate::interrupt] - unsafe fn BDMA_CHANNEL4() { - on_irq() - } - #[crate::interrupt] - unsafe fn BDMA_CHANNEL5() { - on_irq() - } - #[crate::interrupt] - unsafe fn BDMA_CHANNEL6() { - on_irq() - } - #[crate::interrupt] - unsafe fn BDMA_CHANNEL7() { - on_irq() - } -} diff --git a/embassy-stm32/src/dma/dma.rs b/embassy-stm32/src/dma/dma.rs index b0df6ddc..1d6f7aee 100644 --- a/embassy-stm32/src/dma/dma.rs +++ b/embassy-stm32/src/dma/dma.rs @@ -3,7 +3,7 @@ use core::task::Poll; use atomic_polyfill::{AtomicU8, Ordering}; use core::future::Future; use embassy::interrupt::{Interrupt, InterruptExt}; -use embassy::util::AtomicWaker; +use embassy::util::{AtomicWaker, OnDrop}; use futures::future::poll_fn; use crate::interrupt; @@ -38,6 +38,7 @@ static STATE: State = State::new(); //async unsafe fn do_transfer(ch: &mut impl Channel, ch_func: u8, src: *const u8, dst: &mut [u8]) { +#[allow(unused)] pub(crate) async unsafe fn do_transfer( dma: pac::dma::Dma, channel_number: u8, @@ -59,6 +60,15 @@ pub(crate) async unsafe fn do_transfer( let ch = dma.st(channel_number as _); + let on_drop = OnDrop::new(|| unsafe { + ch.cr().modify(|w| { + w.set_tcie(false); + w.set_teie(false); + w.set_en(false); + }); + while ch.cr().read().en() {} + }); + #[cfg(dmamux)] super::dmamux::configure_dmamux(dmamux_regs, dmamux_ch_num, request); @@ -74,6 +84,7 @@ pub(crate) async unsafe fn do_transfer( w.set_pinc(vals::Inc::FIXED); w.set_teie(true); w.set_tcie(true); + w.set_trbuff(true); w.set_en(true); #[cfg(dma_v2)] diff --git a/examples/stm32h7/src/bin/usart_dma.rs b/examples/stm32h7/src/bin/usart_dma.rs new file mode 100644 index 00000000..fd5da8b3 --- /dev/null +++ b/examples/stm32h7/src/bin/usart_dma.rs @@ -0,0 +1,100 @@ +#![no_std] +#![no_main] +#![feature(trait_alias)] +#![feature(min_type_alias_impl_trait)] +#![feature(impl_trait_in_bindings)] +#![feature(type_alias_impl_trait)] +#![allow(incomplete_features)] + +#[path = "../example_common.rs"] +mod example_common; +use core::fmt::Write; +use embassy::executor::Executor; +use embassy::time::Clock; +use embassy::util::Forever; +use embassy_stm32::dma::NoDma; +use embassy_stm32::usart::{Config, Uart}; +use example_common::*; +use embassy_traits::uart::Write as _Write; + +use hal::prelude::*; +use stm32h7xx_hal as hal; + +use cortex_m_rt::entry; +use stm32h7::stm32h743 as pac; +use heapless::String; + +#[embassy::task] +async fn main_task() { + let p = embassy_stm32::init(Default::default()); + + let config = Config::default(); + let mut usart = Uart::new(p.UART7, p.PF6, p.PF7, p.DMA1_0, NoDma, config); + + for n in 0u32.. { + let mut s: String<128> = String::new(); + core::write!(&mut s, "Hello DMA World {}!\r\n", n).unwrap(); + + usart.write(s.as_bytes()).await.ok(); + + info!("wrote DMA"); + } + +} + +struct ZeroClock; + +impl Clock for ZeroClock { + fn now(&self) -> u64 { + 0 + } +} + +static EXECUTOR: Forever = Forever::new(); + +#[entry] +fn main() -> ! { + info!("Hello World!"); + + let pp = pac::Peripherals::take().unwrap(); + + let pwrcfg = pp.PWR.constrain().freeze(); + + let rcc = pp.RCC.constrain(); + + rcc.sys_ck(96.mhz()) + .pclk1(48.mhz()) + .pclk2(48.mhz()) + .pclk3(48.mhz()) + .pclk4(48.mhz()) + .pll1_q_ck(48.mhz()) + .freeze(pwrcfg, &pp.SYSCFG); + + let pp = unsafe { pac::Peripherals::steal() }; + + pp.DBGMCU.cr.modify(|_, w| { + w.dbgsleep_d1().set_bit(); + w.dbgstby_d1().set_bit(); + w.dbgstop_d1().set_bit(); + w.d1dbgcken().set_bit(); + w + }); + + pp.RCC.ahb4enr.modify(|_, w| { + w.gpioaen().set_bit(); + w.gpioben().set_bit(); + w.gpiocen().set_bit(); + w.gpioden().set_bit(); + w.gpioeen().set_bit(); + w.gpiofen().set_bit(); + w + }); + + unsafe { embassy::time::set_clock(&ZeroClock) }; + + let executor = EXECUTOR.put(Executor::new()); + + executor.run(|spawner| { + unwrap!(spawner.spawn(main_task())); + }) +} diff --git a/stm32-data b/stm32-data index 32ca7902..ddd0f206 160000 --- a/stm32-data +++ b/stm32-data @@ -1 +1 @@ -Subproject commit 32ca79020ec7523fe4c3fcfc02006cb1ea637a19 +Subproject commit ddd0f206e22143436230dca6d3fcfc0e02e55dfd