aboutsummaryrefslogtreecommitdiff
path: root/examples/stm32f4
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2021-06-02 01:30:07 +0200
committerDario Nieuwenhuis <[email protected]>2021-06-02 01:32:19 +0200
commitdff03ecfc74d6af716637888338ebfa99ab7a027 (patch)
treec06bf2b0a2e6657c3427c956dbd27a4e45211aaa /examples/stm32f4
parenta0c5f7137fe0c45b8db0aad2a116aea91e6a93f7 (diff)
Move examples to a subdirectory
Diffstat (limited to 'examples/stm32f4')
-rw-r--r--examples/stm32f4/.cargo/config.toml21
-rw-r--r--examples/stm32f4/Cargo.toml35
-rw-r--r--examples/stm32f4/build.rs31
-rw-r--r--examples/stm32f4/memory.x7
-rw-r--r--examples/stm32f4/src/bin/blinky.rs54
-rw-r--r--examples/stm32f4/src/bin/button.rs59
-rw-r--r--examples/stm32f4/src/bin/button_exti.rs83
-rw-r--r--examples/stm32f4/src/bin/spi.rs71
-rw-r--r--examples/stm32f4/src/bin/usart.rs86
-rw-r--r--examples/stm32f4/src/bin/usart_dma.rs90
-rw-r--r--examples/stm32f4/src/example_common.rs17
11 files changed, 554 insertions, 0 deletions
diff --git a/examples/stm32f4/.cargo/config.toml b/examples/stm32f4/.cargo/config.toml
new file mode 100644
index 000000000..8704a9ba5
--- /dev/null
+++ b/examples/stm32f4/.cargo/config.toml
@@ -0,0 +1,21 @@
1[unstable]
2build-std = ["core"]
3
4[target.'cfg(all(target_arch = "arm", target_os = "none"))']
5# replace STM32F429ZITx with your chip as listed in `probe-run --list-chips`
6runner = "probe-run --chip STM32F429ZITx"
7
8rustflags = [
9 # LLD (shipped with the Rust toolchain) is used as the default linker
10 "-C", "link-arg=--nmagic",
11 "-C", "link-arg=-Tlink.x",
12 "-C", "link-arg=-Tdefmt.x",
13
14 # Code-size optimizations.
15 "-Z", "trap-unreachable=no",
16 "-C", "inline-threshold=5",
17 "-C", "no-vectorize-loops",
18]
19
20[build]
21target = "thumbv7em-none-eabi"
diff --git a/examples/stm32f4/Cargo.toml b/examples/stm32f4/Cargo.toml
new file mode 100644
index 000000000..c5c8d9ae6
--- /dev/null
+++ b/examples/stm32f4/Cargo.toml
@@ -0,0 +1,35 @@
1[package]
2authors = ["Dario Nieuwenhuis <[email protected]>"]
3edition = "2018"
4name = "embassy-stm32f4-examples"
5version = "0.1.0"
6resolver = "2"
7
8[features]
9default = [
10 "defmt-default",
11]
12defmt-default = []
13defmt-trace = []
14defmt-debug = []
15defmt-info = []
16defmt-warn = []
17defmt-error = []
18
19[dependencies]
20embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-trace"] }
21embassy-traits = { version = "0.1.0", path = "../../embassy-traits", features = ["defmt"] }
22embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "defmt-trace", "stm32f429zi"] }
23embassy-extras = {version = "0.1.0", path = "../../embassy-extras" }
24stm32f4 = { version = "0.13", features = ["stm32f429"] }
25
26defmt = "0.2.0"
27defmt-rtt = "0.2.0"
28
29cortex-m = "0.7.1"
30cortex-m-rt = "0.6.14"
31embedded-hal = { version = "0.2.4" }
32panic-probe = { version = "0.2.0", features= ["print-defmt"] }
33futures = { version = "0.3.8", default-features = false, features = ["async-await"] }
34rtt-target = { version = "0.3", features = ["cortex-m"] }
35heapless = "0.7" \ No newline at end of file
diff --git a/examples/stm32f4/build.rs b/examples/stm32f4/build.rs
new file mode 100644
index 000000000..d534cc3df
--- /dev/null
+++ b/examples/stm32f4/build.rs
@@ -0,0 +1,31 @@
1//! This build script copies the `memory.x` file from the crate root into
2//! a directory where the linker can always find it at build time.
3//! For many projects this is optional, as the linker always searches the
4//! project root directory -- wherever `Cargo.toml` is. However, if you
5//! are using a workspace or have a more complicated build setup, this
6//! build script becomes required. Additionally, by requesting that
7//! Cargo re-run the build script whenever `memory.x` is changed,
8//! updating `memory.x` ensures a rebuild of the application with the
9//! new memory settings.
10
11use std::env;
12use std::fs::File;
13use std::io::Write;
14use std::path::PathBuf;
15
16fn main() {
17 // Put `memory.x` in our output directory and ensure it's
18 // on the linker search path.
19 let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
20 File::create(out.join("memory.x"))
21 .unwrap()
22 .write_all(include_bytes!("memory.x"))
23 .unwrap();
24 println!("cargo:rustc-link-search={}", out.display());
25
26 // By default, Cargo will re-run a build script whenever
27 // any file in the project changes. By specifying `memory.x`
28 // here, we ensure the build script is only re-run when
29 // `memory.x` is changed.
30 println!("cargo:rerun-if-changed=memory.x");
31}
diff --git a/examples/stm32f4/memory.x b/examples/stm32f4/memory.x
new file mode 100644
index 000000000..f21e32572
--- /dev/null
+++ b/examples/stm32f4/memory.x
@@ -0,0 +1,7 @@
1MEMORY
2{
3 /* NOTE 1 K = 1 KiBi = 1024 bytes */
4 /* These values correspond to the STM32F429ZI */
5 FLASH : ORIGIN = 0x08000000, LENGTH = 2048K
6 RAM : ORIGIN = 0x20000000, LENGTH = 192K
7}
diff --git a/examples/stm32f4/src/bin/blinky.rs b/examples/stm32f4/src/bin/blinky.rs
new file mode 100644
index 000000000..7590764d8
--- /dev/null
+++ b/examples/stm32f4/src/bin/blinky.rs
@@ -0,0 +1,54 @@
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;
11use embassy_stm32::gpio::{Level, Output};
12use embedded_hal::digital::v2::OutputPin;
13use example_common::*;
14
15use cortex_m_rt::entry;
16use stm32f4::stm32f429 as pac;
17
18#[entry]
19fn main() -> ! {
20 info!("Hello World!");
21
22 let pp = pac::Peripherals::take().unwrap();
23
24 pp.DBGMCU.cr.modify(|_, w| {
25 w.dbg_sleep().set_bit();
26 w.dbg_standby().set_bit();
27 w.dbg_stop().set_bit()
28 });
29 pp.RCC.ahb1enr.modify(|_, w| w.dma1en().enabled());
30
31 pp.RCC.ahb1enr.modify(|_, w| {
32 w.gpioaen().enabled();
33 w.gpioben().enabled();
34 w.gpiocen().enabled();
35 w.gpioden().enabled();
36 w.gpioeen().enabled();
37 w.gpiofen().enabled();
38 w
39 });
40
41 let p = embassy_stm32::init(Default::default());
42
43 let mut led = Output::new(p.PB7, Level::High);
44
45 loop {
46 info!("high");
47 led.set_high().unwrap();
48 cortex_m::asm::delay(10_000_000);
49
50 info!("low");
51 led.set_low().unwrap();
52 cortex_m::asm::delay(10_000_000);
53 }
54}
diff --git a/examples/stm32f4/src/bin/button.rs b/examples/stm32f4/src/bin/button.rs
new file mode 100644
index 000000000..1ee99f527
--- /dev/null
+++ b/examples/stm32f4/src/bin/button.rs
@@ -0,0 +1,59 @@
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;
11use embassy_stm32::gpio::{Input, Level, Output, Pull};
12use embedded_hal::digital::v2::{InputPin, OutputPin};
13use example_common::*;
14
15use cortex_m_rt::entry;
16use stm32f4::stm32f429 as pac;
17
18#[entry]
19fn main() -> ! {
20 info!("Hello World!");
21
22 let pp = pac::Peripherals::take().unwrap();
23
24 pp.DBGMCU.cr.modify(|_, w| {
25 w.dbg_sleep().set_bit();
26 w.dbg_standby().set_bit();
27 w.dbg_stop().set_bit()
28 });
29 pp.RCC.ahb1enr.modify(|_, w| w.dma1en().enabled());
30
31 pp.RCC.ahb1enr.modify(|_, w| {
32 w.gpioaen().enabled();
33 w.gpioben().enabled();
34 w.gpiocen().enabled();
35 w.gpioden().enabled();
36 w.gpioeen().enabled();
37 w.gpiofen().enabled();
38 w
39 });
40
41 let p = embassy_stm32::init(Default::default());
42
43 let button = Input::new(p.PC13, Pull::Down);
44 let mut led1 = Output::new(p.PB0, Level::High);
45 let _led2 = Output::new(p.PB7, Level::High);
46 let mut led3 = Output::new(p.PB14, Level::High);
47
48 loop {
49 if button.is_high().unwrap() {
50 info!("high");
51 led1.set_high().unwrap();
52 led3.set_low().unwrap();
53 } else {
54 info!("low");
55 led1.set_low().unwrap();
56 led3.set_high().unwrap();
57 }
58 }
59}
diff --git a/examples/stm32f4/src/bin/button_exti.rs b/examples/stm32f4/src/bin/button_exti.rs
new file mode 100644
index 000000000..8fc889dad
--- /dev/null
+++ b/examples/stm32f4/src/bin/button_exti.rs
@@ -0,0 +1,83 @@
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;
11use embassy::executor::Executor;
12use embassy::time::Clock;
13use embassy::util::Forever;
14use embassy_stm32::exti::ExtiInput;
15use embassy_stm32::gpio::{Input, Pull};
16use embassy_traits::gpio::{WaitForFallingEdge, WaitForRisingEdge};
17use example_common::*;
18
19use cortex_m_rt::entry;
20use stm32f4::stm32f429 as pac;
21
22#[embassy::task]
23async fn main_task() {
24 let p = embassy_stm32::init(Default::default());
25
26 let button = Input::new(p.PC13, Pull::Down);
27 let mut button = ExtiInput::new(button, p.EXTI13);
28
29 info!("Press the USER button...");
30
31 loop {
32 button.wait_for_rising_edge().await;
33 info!("Pressed!");
34 button.wait_for_falling_edge().await;
35 info!("Released!");
36 }
37}
38
39struct ZeroClock;
40
41impl Clock for ZeroClock {
42 fn now(&self) -> u64 {
43 0
44 }
45}
46
47static EXECUTOR: Forever<Executor> = Forever::new();
48
49#[entry]
50fn main() -> ! {
51 info!("Hello World!");
52
53 let pp = pac::Peripherals::take().unwrap();
54
55 pp.DBGMCU.cr.modify(|_, w| {
56 w.dbg_sleep().set_bit();
57 w.dbg_standby().set_bit();
58 w.dbg_stop().set_bit()
59 });
60 pp.RCC.ahb1enr.modify(|_, w| w.dma1en().enabled());
61
62 pp.RCC.ahb1enr.modify(|_, w| {
63 w.gpioaen().enabled();
64 w.gpioben().enabled();
65 w.gpiocen().enabled();
66 w.gpioden().enabled();
67 w.gpioeen().enabled();
68 w.gpiofen().enabled();
69 w
70 });
71 pp.RCC.apb2enr.modify(|_, w| {
72 w.syscfgen().enabled();
73 w
74 });
75
76 unsafe { embassy::time::set_clock(&ZeroClock) };
77
78 let executor = EXECUTOR.put(Executor::new());
79
80 executor.run(|spawner| {
81 unwrap!(spawner.spawn(main_task()));
82 })
83}
diff --git a/examples/stm32f4/src/bin/spi.rs b/examples/stm32f4/src/bin/spi.rs
new file mode 100644
index 000000000..af0d57412
--- /dev/null
+++ b/examples/stm32f4/src/bin/spi.rs
@@ -0,0 +1,71 @@
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 embassy_stm32::gpio::{Level, Output};
13use embedded_hal::digital::v2::OutputPin;
14use example_common::*;
15
16use cortex_m_rt::entry;
17use embassy_stm32::spi::{Config, Spi};
18use embassy_stm32::time::Hertz;
19use embedded_hal::blocking::spi::Transfer;
20use stm32f4::stm32f429 as pac;
21
22#[entry]
23fn main() -> ! {
24 info!("Hello World, dude!");
25
26 let pp = pac::Peripherals::take().unwrap();
27
28 pp.DBGMCU.cr.modify(|_, w| {
29 w.dbg_sleep().set_bit();
30 w.dbg_standby().set_bit();
31 w.dbg_stop().set_bit()
32 });
33 pp.RCC.ahb1enr.modify(|_, w| w.dma1en().set_bit());
34
35 pp.RCC.apb1enr.modify(|_, w| {
36 w.spi3en().enabled();
37 w
38 });
39
40 pp.RCC.ahb1enr.modify(|_, w| {
41 w.gpioaen().enabled();
42 w.gpioben().enabled();
43 w.gpiocen().enabled();
44 w.gpioden().enabled();
45 w.gpioeen().enabled();
46 w.gpiofen().enabled();
47 w
48 });
49
50 let p = embassy_stm32::init(Default::default());
51
52 let mut spi = Spi::new(
53 Hertz(16_000_000),
54 p.SPI3,
55 p.PC10,
56 p.PC12,
57 p.PC11,
58 Hertz(1_000_000),
59 Config::default(),
60 );
61
62 let mut cs = Output::new(p.PE0, Level::High);
63
64 loop {
65 let mut buf = [0x0A; 4];
66 unwrap!(cs.set_low());
67 unwrap!(spi.transfer(&mut buf));
68 unwrap!(cs.set_high());
69 info!("xfer {=[u8]:x}", buf);
70 }
71}
diff --git a/examples/stm32f4/src/bin/usart.rs b/examples/stm32f4/src/bin/usart.rs
new file mode 100644
index 000000000..f7b66f86b
--- /dev/null
+++ b/examples/stm32f4/src/bin/usart.rs
@@ -0,0 +1,86 @@
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;
11use cortex_m::prelude::_embedded_hal_blocking_serial_Write;
12use embassy::executor::Executor;
13use embassy::time::Clock;
14use embassy::util::Forever;
15use embassy_stm32::usart::{Config, Uart};
16use example_common::*;
17
18use cortex_m_rt::entry;
19use stm32f4::stm32f429 as pac;
20
21#[embassy::task]
22async fn main_task() {
23 let p = embassy_stm32::init(Default::default());
24
25 let config = Config::default();
26 let mut usart = Uart::new(p.USART3, p.PD9, p.PD8, config, 16_000_000);
27
28 usart.bwrite_all(b"Hello Embassy World!\r\n").unwrap();
29 info!("wrote Hello, starting echo");
30
31 let mut buf = [0u8; 1];
32 loop {
33 usart.read(&mut buf).unwrap();
34 usart.bwrite_all(&buf).unwrap();
35 }
36}
37
38struct ZeroClock;
39
40impl Clock for ZeroClock {
41 fn now(&self) -> u64 {
42 0
43 }
44}
45
46static EXECUTOR: Forever<Executor> = Forever::new();
47
48#[entry]
49fn main() -> ! {
50 info!("Hello World!");
51
52 let pp = pac::Peripherals::take().unwrap();
53
54 pp.DBGMCU.cr.modify(|_, w| {
55 w.dbg_sleep().set_bit();
56 w.dbg_standby().set_bit();
57 w.dbg_stop().set_bit()
58 });
59 pp.RCC.ahb1enr.modify(|_, w| w.dma1en().enabled());
60
61 pp.RCC.ahb1enr.modify(|_, w| {
62 w.gpioaen().enabled();
63 w.gpioben().enabled();
64 w.gpiocen().enabled();
65 w.gpioden().enabled();
66 w.gpioeen().enabled();
67 w.gpiofen().enabled();
68 w
69 });
70 pp.RCC.apb2enr.modify(|_, w| {
71 w.syscfgen().enabled();
72 w
73 });
74 pp.RCC.apb1enr.modify(|_, w| {
75 w.usart3en().enabled();
76 w
77 });
78
79 unsafe { embassy::time::set_clock(&ZeroClock) };
80
81 let executor = EXECUTOR.put(Executor::new());
82
83 executor.run(|spawner| {
84 unwrap!(spawner.spawn(main_task()));
85 })
86}
diff --git a/examples/stm32f4/src/bin/usart_dma.rs b/examples/stm32f4/src/bin/usart_dma.rs
new file mode 100644
index 000000000..fae05b607
--- /dev/null
+++ b/examples/stm32f4/src/bin/usart_dma.rs
@@ -0,0 +1,90 @@
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;
11use core::fmt::Write;
12use cortex_m_rt::entry;
13use embassy::executor::Executor;
14use embassy::time::Clock;
15use embassy::util::Forever;
16use embassy_stm32::usart::{Config, Uart};
17use example_common::*;
18use heapless::String;
19use stm32f4::stm32f429 as pac;
20
21#[embassy::task]
22async fn main_task() {
23 let mut p = embassy_stm32::init(Default::default());
24
25 let config = Config::default();
26 let mut usart = Uart::new(p.USART3, p.PD9, p.PD8, config, 16_000_000);
27
28 for n in 0u32.. {
29 let mut s: String<128> = String::new();
30 core::write!(&mut s, "Hello DMA World {}!\r\n", n).unwrap();
31
32 usart
33 .write_dma(&mut p.DMA1_CH3, s.as_bytes())
34 .await
35 .unwrap();
36 info!("wrote DMA");
37 }
38}
39
40struct ZeroClock;
41
42impl Clock for ZeroClock {
43 fn now(&self) -> u64 {
44 0
45 }
46}
47
48static EXECUTOR: Forever<Executor> = Forever::new();
49
50#[entry]
51fn main() -> ! {
52 info!("Hello World!");
53
54 let pp = pac::Peripherals::take().unwrap();
55
56 pp.DBGMCU.cr.modify(|_, w| {
57 w.dbg_sleep().set_bit();
58 w.dbg_standby().set_bit();
59 w.dbg_stop().set_bit()
60 });
61 pp.RCC.ahb1enr.modify(|_, w| w.dma1en().enabled());
62
63 pp.RCC.ahb1enr.modify(|_, w| {
64 w.gpioaen().enabled();
65 w.gpioben().enabled();
66 w.gpiocen().enabled();
67 w.gpioden().enabled();
68 w.gpioeen().enabled();
69 w.gpiofen().enabled();
70 w.dma1en().enabled();
71 w.dma2en().enabled();
72 w
73 });
74 pp.RCC.apb2enr.modify(|_, w| {
75 w.syscfgen().enabled();
76 w
77 });
78 pp.RCC.apb1enr.modify(|_, w| {
79 w.usart3en().enabled();
80 w
81 });
82
83 unsafe { embassy::time::set_clock(&ZeroClock) };
84
85 let executor = EXECUTOR.put(Executor::new());
86
87 executor.run(|spawner| {
88 unwrap!(spawner.spawn(main_task()));
89 })
90}
diff --git a/examples/stm32f4/src/example_common.rs b/examples/stm32f4/src/example_common.rs
new file mode 100644
index 000000000..54d633837
--- /dev/null
+++ b/examples/stm32f4/src/example_common.rs
@@ -0,0 +1,17 @@
1#![macro_use]
2
3use defmt_rtt as _; // global logger
4use panic_probe as _;
5
6pub use defmt::*;
7
8use core::sync::atomic::{AtomicUsize, Ordering};
9
10defmt::timestamp! {"{=u64}", {
11 static COUNT: AtomicUsize = AtomicUsize::new(0);
12 // NOTE(no-CAS) `timestamps` runs with interrupts disabled
13 let n = COUNT.load(Ordering::Relaxed);
14 COUNT.store(n + 1, Ordering::Relaxed);
15 n as u64
16 }
17}