From d098952077b8fdc8426754151bb8064214a046fa Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Thu, 21 Jan 2021 19:00:43 +0100 Subject: [PATCH] stm32f4/examples: add config and linker script so they're runnable. --- embassy-stm32f4-examples/.cargo/config | 28 ++++++++++ embassy-stm32f4-examples/build.rs | 31 ++++++++++++ embassy-stm32f4-examples/memory.x | 5 ++ embassy-stm32f4-examples/src/bin/exti.rs | 59 ++++++++++++++++++++++ embassy-stm32f4-examples/src/bin/hello.rs | 40 +++++++++++++++ embassy-stm32f4-examples/src/bin/serial.rs | 2 +- 6 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 embassy-stm32f4-examples/.cargo/config create mode 100644 embassy-stm32f4-examples/build.rs create mode 100644 embassy-stm32f4-examples/memory.x create mode 100644 embassy-stm32f4-examples/src/bin/exti.rs create mode 100644 embassy-stm32f4-examples/src/bin/hello.rs diff --git a/embassy-stm32f4-examples/.cargo/config b/embassy-stm32f4-examples/.cargo/config new file mode 100644 index 00000000..707494d6 --- /dev/null +++ b/embassy-stm32f4-examples/.cargo/config @@ -0,0 +1,28 @@ +[target.'cfg(all(target_arch = "arm", target_os = "none"))'] +runner = "probe-run --chip STM32F411CEUx --defmt" + +rustflags = [ + # LLD (shipped with the Rust toolchain) is used as the default linker + "-C", "link-arg=--nmagic", + "-C", "link-arg=-Tlink.x", + "-C", "link-arg=-Tdefmt.x", + + # if you run into problems with LLD switch to the GNU linker by commenting out + # this line + # "-C", "linker=arm-none-eabi-ld", + + # if you need to link to pre-compiled C libraries provided by a C toolchain + # use GCC as the linker by commenting out both lines above and then + # uncommenting the three lines below + # "-C", "linker=arm-none-eabi-gcc", + # "-C", "link-arg=-Wl,-Tlink.x", + # "-C", "link-arg=-nostartfiles", + + # Code-size optimizations. + "-Z", "trap-unreachable=no", + "-C", "inline-threshold=5", + "-C", "no-vectorize-loops", +] + +[build] +target = "thumbv7em-none-eabi" diff --git a/embassy-stm32f4-examples/build.rs b/embassy-stm32f4-examples/build.rs new file mode 100644 index 00000000..d534cc3d --- /dev/null +++ b/embassy-stm32f4-examples/build.rs @@ -0,0 +1,31 @@ +//! This build script copies the `memory.x` file from the crate root into +//! a directory where the linker can always find it at build time. +//! For many projects this is optional, as the linker always searches the +//! project root directory -- wherever `Cargo.toml` is. However, if you +//! are using a workspace or have a more complicated build setup, this +//! build script becomes required. Additionally, by requesting that +//! Cargo re-run the build script whenever `memory.x` is changed, +//! updating `memory.x` ensures a rebuild of the application with the +//! new memory settings. + +use std::env; +use std::fs::File; +use std::io::Write; +use std::path::PathBuf; + +fn main() { + // Put `memory.x` in our output directory and ensure it's + // on the linker search path. + let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap()); + File::create(out.join("memory.x")) + .unwrap() + .write_all(include_bytes!("memory.x")) + .unwrap(); + println!("cargo:rustc-link-search={}", out.display()); + + // By default, Cargo will re-run a build script whenever + // any file in the project changes. By specifying `memory.x` + // here, we ensure the build script is only re-run when + // `memory.x` is changed. + println!("cargo:rerun-if-changed=memory.x"); +} diff --git a/embassy-stm32f4-examples/memory.x b/embassy-stm32f4-examples/memory.x new file mode 100644 index 00000000..ee4d4d59 --- /dev/null +++ b/embassy-stm32f4-examples/memory.x @@ -0,0 +1,5 @@ +MEMORY +{ + FLASH : ORIGIN = 0x08000000, LENGTH = 64K + RAM : ORIGIN = 0x20000000, LENGTH = 32K +} diff --git a/embassy-stm32f4-examples/src/bin/exti.rs b/embassy-stm32f4-examples/src/bin/exti.rs new file mode 100644 index 00000000..879d0fa2 --- /dev/null +++ b/embassy-stm32f4-examples/src/bin/exti.rs @@ -0,0 +1,59 @@ +#![no_std] +#![no_main] +#![feature(trait_alias)] +#![feature(type_alias_impl_trait)] + +#[path = "../example_common.rs"] +mod example_common; +use example_common::{panic, *}; + +use cortex_m::singleton; +use cortex_m_rt::entry; +use embassy::executor::{task, Executor}; +use embassy::gpio::*; +use embassy::util::Forever; +use embassy_stm32f4::exti; +use embassy_stm32f4::exti::*; +use embassy_stm32f4::interrupt; +use embassy_stm32f4::serial; +use futures::pin_mut; +use stm32f4xx_hal::serial::config::Config; +use stm32f4xx_hal::stm32; +use stm32f4xx_hal::syscfg; +use stm32f4xx_hal::{prelude::*, serial::config}; + +static EXTI: Forever = Forever::new(); + +#[task] +async fn run(dp: stm32::Peripherals, cp: cortex_m::Peripherals) { + let gpioa = dp.GPIOA.split(); + + let button = gpioa.pa0.into_pull_up_input(); + + let exti = EXTI.put(exti::ExtiManager::new(dp.EXTI, dp.SYSCFG.constrain())); + let pin = exti.new_pin(button, interrupt::take!(EXTI0)); + pin_mut!(pin); + + info!("Starting loop"); + + loop { + pin.as_mut().wait_for_rising_edge().await; + info!("edge detected!"); + } +} + +static EXECUTOR: Forever = Forever::new(); + +#[entry] +fn main() -> ! { + let dp = stm32::Peripherals::take().unwrap(); + let cp = cortex_m::peripheral::Peripherals::take().unwrap(); + + let executor = EXECUTOR.put(Executor::new(cortex_m::asm::sev)); + executor.spawn(run(dp, cp)).unwrap(); + + loop { + executor.run(); + //cortex_m::asm::wfe(); // wfe causes RTT to stop working on stm32 + } +} diff --git a/embassy-stm32f4-examples/src/bin/hello.rs b/embassy-stm32f4-examples/src/bin/hello.rs new file mode 100644 index 00000000..706501e3 --- /dev/null +++ b/embassy-stm32f4-examples/src/bin/hello.rs @@ -0,0 +1,40 @@ +#![no_std] +#![no_main] +#![feature(trait_alias)] +#![feature(type_alias_impl_trait)] + +#[path = "../example_common.rs"] +mod example_common; +use example_common::{panic, *}; + +use cortex_m::singleton; +use cortex_m_rt::entry; +use embassy::executor::{task, Executor}; +use embassy::uart::Uart; +use embassy::util::Forever; +use embassy_stm32f4::interrupt; +use embassy_stm32f4::serial; +use stm32f4xx_hal::serial::config::Config; +use stm32f4xx_hal::stm32; +use stm32f4xx_hal::{prelude::*, serial::config}; + +#[entry] +fn main() -> ! { + info!("Hello World!"); + + let p = stm32f4xx_hal::stm32::Peripherals::take().unwrap(); + let gpioa = p.GPIOA.split(); + let gpioc = p.GPIOC.split(); + + let mut led = gpioc.pc13.into_push_pull_output(); + let button = gpioa.pa0.into_pull_up_input(); + led.set_low().unwrap(); + + loop { + if button.is_high().unwrap() { + led.set_low().unwrap(); + } else { + led.set_high().unwrap(); + } + } +} diff --git a/embassy-stm32f4-examples/src/bin/serial.rs b/embassy-stm32f4-examples/src/bin/serial.rs index c0c71bec..93c32b3f 100644 --- a/embassy-stm32f4-examples/src/bin/serial.rs +++ b/embassy-stm32f4-examples/src/bin/serial.rs @@ -64,6 +64,6 @@ fn main() -> ! { loop { executor.run(); - cortex_m::asm::wfe(); + //cortex_m::asm::wfe(); // wfe causes RTT to stop working on stm32 } }