diff options
| author | Bob McWhirter <[email protected]> | 2021-07-21 14:09:24 -0400 |
|---|---|---|
| committer | Bob McWhirter <[email protected]> | 2021-07-23 13:22:39 -0400 |
| commit | a1dac21bdfdecfe24bcf7890c116fc122667dcc0 (patch) | |
| tree | 6b053c3861eba452dd2b1735d95284983a63950c /examples/stm32h7/src | |
| parent | 34dfe28d3a80192ed69e8f49c41172d0738afda8 (diff) | |
Make SPIv3 work with DMA.
Add both DMA and non-DMA example to H7.
Diffstat (limited to 'examples/stm32h7/src')
| -rw-r--r-- | examples/stm32h7/src/bin/spi.rs | 112 | ||||
| -rw-r--r-- | examples/stm32h7/src/bin/spi_dma.rs | 109 |
2 files changed, 221 insertions, 0 deletions
diff --git a/examples/stm32h7/src/bin/spi.rs b/examples/stm32h7/src/bin/spi.rs new file mode 100644 index 000000000..ac483a311 --- /dev/null +++ b/examples/stm32h7/src/bin/spi.rs | |||
| @@ -0,0 +1,112 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(trait_alias)] | ||
| 4 | #![feature(min_type_alias_impl_trait)] | ||
| 5 | #![feature(impl_trait_in_bindings)] | ||
| 6 | #![feature(type_alias_impl_trait)] | ||
| 7 | #![allow(incomplete_features)] | ||
| 8 | |||
| 9 | #[path = "../example_common.rs"] | ||
| 10 | mod example_common; | ||
| 11 | |||
| 12 | use core::fmt::Write; | ||
| 13 | use embassy::executor::Executor; | ||
| 14 | use embassy::time::Clock; | ||
| 15 | use embassy::util::Forever; | ||
| 16 | use embassy_stm32::dma::NoDma; | ||
| 17 | use example_common::*; | ||
| 18 | use embedded_hal::blocking::spi::Transfer; | ||
| 19 | |||
| 20 | use hal::prelude::*; | ||
| 21 | use stm32h7xx_hal as hal; | ||
| 22 | |||
| 23 | use cortex_m_rt::entry; | ||
| 24 | use stm32h7::stm32h743 as pac; | ||
| 25 | use heapless::String; | ||
| 26 | use embassy_stm32::spi::{Spi, Config}; | ||
| 27 | use embassy_stm32::time::Hertz; | ||
| 28 | |||
| 29 | #[embassy::task] | ||
| 30 | async fn main_task() { | ||
| 31 | let p = embassy_stm32::init(Default::default()); | ||
| 32 | |||
| 33 | let mut spi = Spi::new( | ||
| 34 | p.SPI3, | ||
| 35 | p.PB3, | ||
| 36 | p.PB5, | ||
| 37 | p.PB4, | ||
| 38 | NoDma, | ||
| 39 | NoDma, | ||
| 40 | Hertz(1_000_000), | ||
| 41 | Config::default(), | ||
| 42 | ); | ||
| 43 | |||
| 44 | for n in 0u32.. { | ||
| 45 | let mut write: String<128> = String::new(); | ||
| 46 | core::write!(&mut write, "Hello DMA World {}!\r\n", n).unwrap(); | ||
| 47 | unsafe { | ||
| 48 | let result = spi.transfer(write.as_bytes_mut()); | ||
| 49 | if let Err(_) = result { | ||
| 50 | defmt::panic!("crap"); | ||
| 51 | } | ||
| 52 | } | ||
| 53 | info!("read via spi: {}", write.as_bytes()); | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | struct ZeroClock; | ||
| 58 | |||
| 59 | impl Clock for ZeroClock { | ||
| 60 | fn now(&self) -> u64 { | ||
| 61 | 0 | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | static EXECUTOR: Forever<Executor> = Forever::new(); | ||
| 66 | |||
| 67 | #[entry] | ||
| 68 | fn main() -> ! { | ||
| 69 | info!("Hello World!"); | ||
| 70 | |||
| 71 | let pp = pac::Peripherals::take().unwrap(); | ||
| 72 | |||
| 73 | let pwrcfg = pp.PWR.constrain().freeze(); | ||
| 74 | |||
| 75 | let rcc = pp.RCC.constrain(); | ||
| 76 | |||
| 77 | rcc.sys_ck(96.mhz()) | ||
| 78 | .pclk1(48.mhz()) | ||
| 79 | .pclk2(48.mhz()) | ||
| 80 | .pclk3(48.mhz()) | ||
| 81 | .pclk4(48.mhz()) | ||
| 82 | .pll1_q_ck(48.mhz()) | ||
| 83 | .freeze(pwrcfg, &pp.SYSCFG); | ||
| 84 | |||
| 85 | let pp = unsafe { pac::Peripherals::steal() }; | ||
| 86 | |||
| 87 | pp.DBGMCU.cr.modify(|_, w| { | ||
| 88 | w.dbgsleep_d1().set_bit(); | ||
| 89 | w.dbgstby_d1().set_bit(); | ||
| 90 | w.dbgstop_d1().set_bit(); | ||
| 91 | w.d1dbgcken().set_bit(); | ||
| 92 | w | ||
| 93 | }); | ||
| 94 | |||
| 95 | pp.RCC.ahb4enr.modify(|_, w| { | ||
| 96 | w.gpioaen().set_bit(); | ||
| 97 | w.gpioben().set_bit(); | ||
| 98 | w.gpiocen().set_bit(); | ||
| 99 | w.gpioden().set_bit(); | ||
| 100 | w.gpioeen().set_bit(); | ||
| 101 | w.gpiofen().set_bit(); | ||
| 102 | w | ||
| 103 | }); | ||
| 104 | |||
| 105 | unsafe { embassy::time::set_clock(&ZeroClock) }; | ||
| 106 | |||
| 107 | let executor = EXECUTOR.put(Executor::new()); | ||
| 108 | |||
| 109 | executor.run(|spawner| { | ||
| 110 | unwrap!(spawner.spawn(main_task())); | ||
| 111 | }) | ||
| 112 | } | ||
diff --git a/examples/stm32h7/src/bin/spi_dma.rs b/examples/stm32h7/src/bin/spi_dma.rs new file mode 100644 index 000000000..9dbfd0960 --- /dev/null +++ b/examples/stm32h7/src/bin/spi_dma.rs | |||
| @@ -0,0 +1,109 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(trait_alias)] | ||
| 4 | #![feature(min_type_alias_impl_trait)] | ||
| 5 | #![feature(impl_trait_in_bindings)] | ||
| 6 | #![feature(type_alias_impl_trait)] | ||
| 7 | #![allow(incomplete_features)] | ||
| 8 | |||
| 9 | #[path = "../example_common.rs"] | ||
| 10 | mod example_common; | ||
| 11 | use core::fmt::Write; | ||
| 12 | use embassy::executor::Executor; | ||
| 13 | use embassy::time::Clock; | ||
| 14 | use embassy::util::Forever; | ||
| 15 | use example_common::*; | ||
| 16 | use embassy_traits::spi::FullDuplex; | ||
| 17 | |||
| 18 | use hal::prelude::*; | ||
| 19 | use stm32h7xx_hal as hal; | ||
| 20 | |||
| 21 | use cortex_m_rt::entry; | ||
| 22 | use stm32h7::stm32h743 as pac; | ||
| 23 | use heapless::String; | ||
| 24 | use embassy_stm32::spi::{Spi, Config}; | ||
| 25 | use embassy_stm32::time::Hertz; | ||
| 26 | use core::str::from_utf8; | ||
| 27 | |||
| 28 | #[embassy::task] | ||
| 29 | async fn main_task() { | ||
| 30 | let p = embassy_stm32::init(Default::default()); | ||
| 31 | |||
| 32 | let mut spi = Spi::new( | ||
| 33 | p.SPI3, | ||
| 34 | p.PB3, | ||
| 35 | p.PB5, | ||
| 36 | p.PB4, | ||
| 37 | p.DMA1_CH3, | ||
| 38 | p.DMA1_CH4, | ||
| 39 | Hertz(1_000_000), | ||
| 40 | Config::default(), | ||
| 41 | ); | ||
| 42 | |||
| 43 | for n in 0u32.. { | ||
| 44 | let mut write: String<128> = String::new(); | ||
| 45 | let mut read = [0;128]; | ||
| 46 | core::write!(&mut write, "Hello DMA World {}!\r\n", n).unwrap(); | ||
| 47 | // read_write will slice the &mut read down to &write's actual length. | ||
| 48 | spi.read_write(&mut read, write.as_bytes()).await.ok(); | ||
| 49 | info!("read via spi+dma: {}", from_utf8(&read).unwrap()); | ||
| 50 | } | ||
| 51 | |||
| 52 | } | ||
| 53 | |||
| 54 | struct ZeroClock; | ||
| 55 | |||
| 56 | impl Clock for ZeroClock { | ||
| 57 | fn now(&self) -> u64 { | ||
| 58 | 0 | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | static EXECUTOR: Forever<Executor> = Forever::new(); | ||
| 63 | |||
| 64 | #[entry] | ||
| 65 | fn main() -> ! { | ||
| 66 | info!("Hello World!"); | ||
| 67 | |||
| 68 | let pp = pac::Peripherals::take().unwrap(); | ||
| 69 | |||
| 70 | let pwrcfg = pp.PWR.constrain().freeze(); | ||
| 71 | |||
| 72 | let rcc = pp.RCC.constrain(); | ||
| 73 | |||
| 74 | rcc.sys_ck(96.mhz()) | ||
| 75 | .pclk1(48.mhz()) | ||
| 76 | .pclk2(48.mhz()) | ||
| 77 | .pclk3(48.mhz()) | ||
| 78 | .pclk4(48.mhz()) | ||
| 79 | .pll1_q_ck(48.mhz()) | ||
| 80 | .freeze(pwrcfg, &pp.SYSCFG); | ||
| 81 | |||
| 82 | let pp = unsafe { pac::Peripherals::steal() }; | ||
| 83 | |||
| 84 | pp.DBGMCU.cr.modify(|_, w| { | ||
| 85 | w.dbgsleep_d1().set_bit(); | ||
| 86 | w.dbgstby_d1().set_bit(); | ||
| 87 | w.dbgstop_d1().set_bit(); | ||
| 88 | w.d1dbgcken().set_bit(); | ||
| 89 | w | ||
| 90 | }); | ||
| 91 | |||
| 92 | pp.RCC.ahb4enr.modify(|_, w| { | ||
| 93 | w.gpioaen().set_bit(); | ||
| 94 | w.gpioben().set_bit(); | ||
| 95 | w.gpiocen().set_bit(); | ||
| 96 | w.gpioden().set_bit(); | ||
| 97 | w.gpioeen().set_bit(); | ||
| 98 | w.gpiofen().set_bit(); | ||
| 99 | w | ||
| 100 | }); | ||
| 101 | |||
| 102 | unsafe { embassy::time::set_clock(&ZeroClock) }; | ||
| 103 | |||
| 104 | let executor = EXECUTOR.put(Executor::new()); | ||
| 105 | |||
| 106 | executor.run(|spawner| { | ||
| 107 | unwrap!(spawner.spawn(main_task())); | ||
| 108 | }) | ||
| 109 | } | ||
