aboutsummaryrefslogtreecommitdiff
path: root/examples/stm32l4
diff options
context:
space:
mode:
authorBob McWhirter <[email protected]>2021-07-21 10:42:22 -0400
committerBob McWhirter <[email protected]>2021-07-23 13:22:39 -0400
commit0d2051243ef62aac7210dd68c7569912fa315fb2 (patch)
tree2dbd366bd03b3bf23efd7575f46670c0529c9219 /examples/stm32l4
parent1a03f00b56061dbef8a3aae6e499e5e635b3fd4d (diff)
SPIv2 + DMA.
Diffstat (limited to 'examples/stm32l4')
-rw-r--r--examples/stm32l4/src/bin/spi.rs3
-rw-r--r--examples/stm32l4/src/bin/spi_dma.rs103
2 files changed, 106 insertions, 0 deletions
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;
17use embedded_hal::blocking::spi::Transfer; 17use embedded_hal::blocking::spi::Transfer;
18use embedded_hal::digital::v2::OutputPin; 18use embedded_hal::digital::v2::OutputPin;
19use example_common::*; 19use example_common::*;
20use embassy_stm32::dma::NoDma;
20 21
21#[entry] 22#[entry]
22fn main() -> ! { 23fn main() -> ! {
@@ -41,6 +42,8 @@ fn main() -> ! {
41 p.PC10, 42 p.PC10,
42 p.PC12, 43 p.PC12,
43 p.PC11, 44 p.PC11,
45 NoDma,
46 NoDma,
44 Hertz(1_000_000), 47 Hertz(1_000_000),
45 Config::default(), 48 Config::default(),
46 ); 49 );
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 @@
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"]
10mod example_common;
11
12use cortex_m_rt::entry;
13use embassy::executor::Executor;
14use embassy::time::Clock;
15use embassy::util::Forever;
16use embassy_stm32::pac;
17use example_common::*;
18use embassy_stm32::spi::{Spi, Config};
19use embassy_traits::spi::FullDuplex;
20use embassy_stm32::time::Hertz;
21use embassy_stm32::gpio::{Output, Level, Speed};
22use embedded_hal::digital::v2::OutputPin;
23
24#[embassy::task]
25async fn main_task() {
26 let p = embassy_stm32::init(Default::default());
27
28 let mut spi = Spi::new(
29 p.SPI3,
30 p.PC10,
31 p.PC12,
32 p.PC11,
33 p.DMA1_CH0,
34 p.DMA1_CH1,
35 Hertz(1_000_000),
36 Config::default(),
37 );
38
39 let mut cs = Output::new(p.PE0, Level::High, Speed::VeryHigh);
40
41 loop {
42 let write = [0x0A; 10];
43 let mut read = [0; 10];
44 unwrap!(cs.set_low());
45 spi.read_write(&mut read, &write).await.ok();
46 unwrap!(cs.set_high());
47 info!("xfer {=[u8]:x}", read);
48 }
49}
50
51struct ZeroClock;
52
53impl Clock for ZeroClock {
54 fn now(&self) -> u64 {
55 0
56 }
57}
58
59static EXECUTOR: Forever<Executor> = Forever::new();
60
61#[entry]
62fn main() -> ! {
63 info!("Hello World!");
64
65 unsafe {
66 pac::DBGMCU.cr().modify(|w| {
67 w.set_dbg_sleep(true);
68 w.set_dbg_standby(true);
69 w.set_dbg_stop(true);
70 });
71
72 //pac::RCC.apbenr().modify(|w| {
73 //w.set_spi3en(true);
74 // });
75
76 pac::RCC.apb2enr().modify(|w| {
77 w.set_syscfgen(true);
78 });
79
80 pac::RCC.ahb1enr().modify(|w| {
81 w.set_dmamux1en(true);
82 w.set_dma1en(true);
83 w.set_dma2en(true);
84 });
85
86 pac::RCC.ahb2enr().modify(|w| {
87 w.set_gpioaen(true);
88 w.set_gpioben(true);
89 w.set_gpiocen(true);
90 w.set_gpioden(true);
91 w.set_gpioeen(true);
92 w.set_gpiofen(true);
93 });
94 }
95
96 unsafe { embassy::time::set_clock(&ZeroClock) };
97
98 let executor = EXECUTOR.put(Executor::new());
99
100 executor.run(|spawner| {
101 unwrap!(spawner.spawn(main_task()));
102 })
103}