From 0d2051243ef62aac7210dd68c7569912fa315fb2 Mon Sep 17 00:00:00 2001 From: Bob McWhirter Date: Wed, 21 Jul 2021 10:42:22 -0400 Subject: SPIv2 + DMA. --- examples/stm32l4/src/bin/spi.rs | 3 ++ examples/stm32l4/src/bin/spi_dma.rs | 103 ++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 examples/stm32l4/src/bin/spi_dma.rs (limited to 'examples') diff --git a/examples/stm32l4/src/bin/spi.rs b/examples/stm32l4/src/bin/spi.rs index 8702fe0cc..14605283b 100644 --- a/examples/stm32l4/src/bin/spi.rs +++ b/examples/stm32l4/src/bin/spi.rs @@ -17,6 +17,7 @@ use embassy_stm32::time::Hertz; use embedded_hal::blocking::spi::Transfer; use embedded_hal::digital::v2::OutputPin; use example_common::*; +use embassy_stm32::dma::NoDma; #[entry] fn main() -> ! { @@ -41,6 +42,8 @@ fn main() -> ! { p.PC10, p.PC12, p.PC11, + NoDma, + NoDma, Hertz(1_000_000), Config::default(), ); diff --git a/examples/stm32l4/src/bin/spi_dma.rs b/examples/stm32l4/src/bin/spi_dma.rs new file mode 100644 index 000000000..ca77c2f9b --- /dev/null +++ b/examples/stm32l4/src/bin/spi_dma.rs @@ -0,0 +1,103 @@ +#![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 cortex_m_rt::entry; +use embassy::executor::Executor; +use embassy::time::Clock; +use embassy::util::Forever; +use embassy_stm32::pac; +use example_common::*; +use embassy_stm32::spi::{Spi, Config}; +use embassy_traits::spi::FullDuplex; +use embassy_stm32::time::Hertz; +use embassy_stm32::gpio::{Output, Level, Speed}; +use embedded_hal::digital::v2::OutputPin; + +#[embassy::task] +async fn main_task() { + let p = embassy_stm32::init(Default::default()); + + let mut spi = Spi::new( + p.SPI3, + p.PC10, + p.PC12, + p.PC11, + p.DMA1_CH0, + p.DMA1_CH1, + Hertz(1_000_000), + Config::default(), + ); + + let mut cs = Output::new(p.PE0, Level::High, Speed::VeryHigh); + + loop { + let write = [0x0A; 10]; + let mut read = [0; 10]; + unwrap!(cs.set_low()); + spi.read_write(&mut read, &write).await.ok(); + unwrap!(cs.set_high()); + info!("xfer {=[u8]:x}", read); + } +} + +struct ZeroClock; + +impl Clock for ZeroClock { + fn now(&self) -> u64 { + 0 + } +} + +static EXECUTOR: Forever = Forever::new(); + +#[entry] +fn main() -> ! { + info!("Hello World!"); + + unsafe { + pac::DBGMCU.cr().modify(|w| { + w.set_dbg_sleep(true); + w.set_dbg_standby(true); + w.set_dbg_stop(true); + }); + + //pac::RCC.apbenr().modify(|w| { + //w.set_spi3en(true); + // }); + + pac::RCC.apb2enr().modify(|w| { + w.set_syscfgen(true); + }); + + pac::RCC.ahb1enr().modify(|w| { + w.set_dmamux1en(true); + w.set_dma1en(true); + w.set_dma2en(true); + }); + + pac::RCC.ahb2enr().modify(|w| { + w.set_gpioaen(true); + w.set_gpioben(true); + w.set_gpiocen(true); + w.set_gpioden(true); + w.set_gpioeen(true); + w.set_gpiofen(true); + }); + } + + unsafe { embassy::time::set_clock(&ZeroClock) }; + + let executor = EXECUTOR.put(Executor::new()); + + executor.run(|spawner| { + unwrap!(spawner.spawn(main_task())); + }) +} -- cgit