aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2021-01-21 19:00:43 +0100
committerDario Nieuwenhuis <[email protected]>2021-01-21 19:00:43 +0100
commitd098952077b8fdc8426754151bb8064214a046fa (patch)
tree422c7bdb3c0ce833b8198409b2af26211668c861
parent9240a1f437a0c85de2cb0334dcc58168fe199053 (diff)
stm32f4/examples: add config and linker script so they're runnable.
-rw-r--r--embassy-stm32f4-examples/.cargo/config28
-rw-r--r--embassy-stm32f4-examples/build.rs31
-rw-r--r--embassy-stm32f4-examples/memory.x5
-rw-r--r--embassy-stm32f4-examples/src/bin/exti.rs59
-rw-r--r--embassy-stm32f4-examples/src/bin/hello.rs40
-rw-r--r--embassy-stm32f4-examples/src/bin/serial.rs2
6 files changed, 164 insertions, 1 deletions
diff --git a/embassy-stm32f4-examples/.cargo/config b/embassy-stm32f4-examples/.cargo/config
new file mode 100644
index 000000000..707494d68
--- /dev/null
+++ b/embassy-stm32f4-examples/.cargo/config
@@ -0,0 +1,28 @@
1[target.'cfg(all(target_arch = "arm", target_os = "none"))']
2runner = "probe-run --chip STM32F411CEUx --defmt"
3
4rustflags = [
5 # LLD (shipped with the Rust toolchain) is used as the default linker
6 "-C", "link-arg=--nmagic",
7 "-C", "link-arg=-Tlink.x",
8 "-C", "link-arg=-Tdefmt.x",
9
10 # if you run into problems with LLD switch to the GNU linker by commenting out
11 # this line
12 # "-C", "linker=arm-none-eabi-ld",
13
14 # if you need to link to pre-compiled C libraries provided by a C toolchain
15 # use GCC as the linker by commenting out both lines above and then
16 # uncommenting the three lines below
17 # "-C", "linker=arm-none-eabi-gcc",
18 # "-C", "link-arg=-Wl,-Tlink.x",
19 # "-C", "link-arg=-nostartfiles",
20
21 # Code-size optimizations.
22 "-Z", "trap-unreachable=no",
23 "-C", "inline-threshold=5",
24 "-C", "no-vectorize-loops",
25]
26
27[build]
28target = "thumbv7em-none-eabi"
diff --git a/embassy-stm32f4-examples/build.rs b/embassy-stm32f4-examples/build.rs
new file mode 100644
index 000000000..d534cc3df
--- /dev/null
+++ b/embassy-stm32f4-examples/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/embassy-stm32f4-examples/memory.x b/embassy-stm32f4-examples/memory.x
new file mode 100644
index 000000000..ee4d4d59c
--- /dev/null
+++ b/embassy-stm32f4-examples/memory.x
@@ -0,0 +1,5 @@
1MEMORY
2{
3 FLASH : ORIGIN = 0x08000000, LENGTH = 64K
4 RAM : ORIGIN = 0x20000000, LENGTH = 32K
5}
diff --git a/embassy-stm32f4-examples/src/bin/exti.rs b/embassy-stm32f4-examples/src/bin/exti.rs
new file mode 100644
index 000000000..879d0fa2a
--- /dev/null
+++ b/embassy-stm32f4-examples/src/bin/exti.rs
@@ -0,0 +1,59 @@
1#![no_std]
2#![no_main]
3#![feature(trait_alias)]
4#![feature(type_alias_impl_trait)]
5
6#[path = "../example_common.rs"]
7mod example_common;
8use example_common::{panic, *};
9
10use cortex_m::singleton;
11use cortex_m_rt::entry;
12use embassy::executor::{task, Executor};
13use embassy::gpio::*;
14use embassy::util::Forever;
15use embassy_stm32f4::exti;
16use embassy_stm32f4::exti::*;
17use embassy_stm32f4::interrupt;
18use embassy_stm32f4::serial;
19use futures::pin_mut;
20use stm32f4xx_hal::serial::config::Config;
21use stm32f4xx_hal::stm32;
22use stm32f4xx_hal::syscfg;
23use stm32f4xx_hal::{prelude::*, serial::config};
24
25static EXTI: Forever<exti::ExtiManager> = Forever::new();
26
27#[task]
28async fn run(dp: stm32::Peripherals, cp: cortex_m::Peripherals) {
29 let gpioa = dp.GPIOA.split();
30
31 let button = gpioa.pa0.into_pull_up_input();
32
33 let exti = EXTI.put(exti::ExtiManager::new(dp.EXTI, dp.SYSCFG.constrain()));
34 let pin = exti.new_pin(button, interrupt::take!(EXTI0));
35 pin_mut!(pin);
36
37 info!("Starting loop");
38
39 loop {
40 pin.as_mut().wait_for_rising_edge().await;
41 info!("edge detected!");
42 }
43}
44
45static EXECUTOR: Forever<Executor> = Forever::new();
46
47#[entry]
48fn main() -> ! {
49 let dp = stm32::Peripherals::take().unwrap();
50 let cp = cortex_m::peripheral::Peripherals::take().unwrap();
51
52 let executor = EXECUTOR.put(Executor::new(cortex_m::asm::sev));
53 executor.spawn(run(dp, cp)).unwrap();
54
55 loop {
56 executor.run();
57 //cortex_m::asm::wfe(); // wfe causes RTT to stop working on stm32
58 }
59}
diff --git a/embassy-stm32f4-examples/src/bin/hello.rs b/embassy-stm32f4-examples/src/bin/hello.rs
new file mode 100644
index 000000000..706501e35
--- /dev/null
+++ b/embassy-stm32f4-examples/src/bin/hello.rs
@@ -0,0 +1,40 @@
1#![no_std]
2#![no_main]
3#![feature(trait_alias)]
4#![feature(type_alias_impl_trait)]
5
6#[path = "../example_common.rs"]
7mod example_common;
8use example_common::{panic, *};
9
10use cortex_m::singleton;
11use cortex_m_rt::entry;
12use embassy::executor::{task, Executor};
13use embassy::uart::Uart;
14use embassy::util::Forever;
15use embassy_stm32f4::interrupt;
16use embassy_stm32f4::serial;
17use stm32f4xx_hal::serial::config::Config;
18use stm32f4xx_hal::stm32;
19use stm32f4xx_hal::{prelude::*, serial::config};
20
21#[entry]
22fn main() -> ! {
23 info!("Hello World!");
24
25 let p = stm32f4xx_hal::stm32::Peripherals::take().unwrap();
26 let gpioa = p.GPIOA.split();
27 let gpioc = p.GPIOC.split();
28
29 let mut led = gpioc.pc13.into_push_pull_output();
30 let button = gpioa.pa0.into_pull_up_input();
31 led.set_low().unwrap();
32
33 loop {
34 if button.is_high().unwrap() {
35 led.set_low().unwrap();
36 } else {
37 led.set_high().unwrap();
38 }
39 }
40}
diff --git a/embassy-stm32f4-examples/src/bin/serial.rs b/embassy-stm32f4-examples/src/bin/serial.rs
index c0c71bece..93c32b3f4 100644
--- a/embassy-stm32f4-examples/src/bin/serial.rs
+++ b/embassy-stm32f4-examples/src/bin/serial.rs
@@ -64,6 +64,6 @@ fn main() -> ! {
64 64
65 loop { 65 loop {
66 executor.run(); 66 executor.run();
67 cortex_m::asm::wfe(); 67 //cortex_m::asm::wfe(); // wfe causes RTT to stop working on stm32
68 } 68 }
69} 69}