diff options
| author | Dario Nieuwenhuis <[email protected]> | 2021-08-20 01:35:02 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-08-20 01:35:02 +0200 |
| commit | 0be6e4a384b3c2fee34f6f6db5f86ecbf9cea5e5 (patch) | |
| tree | f5b6608d18e0a49a28fd04389684cb81d7a17022 /examples | |
| parent | 8e5f1f4b5e8ff20897ac86ca81605d50c3dfa966 (diff) | |
| parent | da707051b0bc963b63aea209dc47108de15543f5 (diff) | |
Merge pull request #333 from bgamari/stm32g0
Add support for STM32G0
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/stm32g0/.cargo/config.toml | 18 | ||||
| -rw-r--r-- | examples/stm32g0/Cargo.toml | 34 | ||||
| -rw-r--r-- | examples/stm32g0/build.rs | 31 | ||||
| -rw-r--r-- | examples/stm32g0/memory.x | 7 | ||||
| -rw-r--r-- | examples/stm32g0/src/bin/blinky.rs | 31 | ||||
| -rw-r--r-- | examples/stm32g0/src/bin/button.rs | 29 | ||||
| -rw-r--r-- | examples/stm32g0/src/bin/button_exti.rs | 31 | ||||
| -rw-r--r-- | examples/stm32g0/src/example_common.rs | 17 |
8 files changed, 198 insertions, 0 deletions
diff --git a/examples/stm32g0/.cargo/config.toml b/examples/stm32g0/.cargo/config.toml new file mode 100644 index 000000000..1f7c3ecdd --- /dev/null +++ b/examples/stm32g0/.cargo/config.toml | |||
| @@ -0,0 +1,18 @@ | |||
| 1 | [target.'cfg(all(target_arch = "arm", target_os = "none"))'] | ||
| 2 | # replace STM32G071C8Rx with your chip as listed in `probe-run --list-chips` | ||
| 3 | runner = "probe-run --chip STM32G071RBTx" | ||
| 4 | |||
| 5 | rustflags = [ | ||
| 6 | # LLD (shipped with the Rust toolchain) is used as the default linker | ||
| 7 | "-C", "link-arg=--nmagic", | ||
| 8 | "-C", "link-arg=-Tlink.x", | ||
| 9 | "-C", "link-arg=-Tdefmt.x", | ||
| 10 | |||
| 11 | # Code-size optimizations. | ||
| 12 | "-Z", "trap-unreachable=no", | ||
| 13 | "-C", "inline-threshold=5", | ||
| 14 | "-C", "no-vectorize-loops", | ||
| 15 | ] | ||
| 16 | |||
| 17 | [build] | ||
| 18 | target = "thumbv6m-none-eabi" | ||
diff --git a/examples/stm32g0/Cargo.toml b/examples/stm32g0/Cargo.toml new file mode 100644 index 000000000..83f535b91 --- /dev/null +++ b/examples/stm32g0/Cargo.toml | |||
| @@ -0,0 +1,34 @@ | |||
| 1 | [package] | ||
| 2 | authors = ["Dario Nieuwenhuis <[email protected]>", "Ben Gamari <[email protected]>"] | ||
| 3 | edition = "2018" | ||
| 4 | name = "embassy-stm32g0-examples" | ||
| 5 | version = "0.1.0" | ||
| 6 | resolver = "2" | ||
| 7 | |||
| 8 | [features] | ||
| 9 | default = [ | ||
| 10 | "defmt-default", | ||
| 11 | ] | ||
| 12 | defmt-default = [] | ||
| 13 | defmt-trace = [] | ||
| 14 | defmt-debug = [] | ||
| 15 | defmt-info = [] | ||
| 16 | defmt-warn = [] | ||
| 17 | defmt-error = [] | ||
| 18 | |||
| 19 | [dependencies] | ||
| 20 | embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-trace"] } | ||
| 21 | embassy-traits = { version = "0.1.0", path = "../../embassy-traits", features = ["defmt"] } | ||
| 22 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "defmt-trace", "time-driver-tim2", "stm32g071rb", "unstable-pac"] } | ||
| 23 | embassy-hal-common = {version = "0.1.0", path = "../../embassy-hal-common" } | ||
| 24 | |||
| 25 | defmt = "0.2.0" | ||
| 26 | defmt-rtt = "0.2.0" | ||
| 27 | |||
| 28 | cortex-m = "0.7.1" | ||
| 29 | cortex-m-rt = "0.7.0" | ||
| 30 | embedded-hal = { version = "0.2.4" } | ||
| 31 | panic-probe = { version = "0.2.0", features= ["print-defmt"] } | ||
| 32 | futures = { version = "0.3.8", default-features = false, features = ["async-await"] } | ||
| 33 | rtt-target = { version = "0.3", features = ["cortex-m"] } | ||
| 34 | heapless = { version = "0.7.1", default-features = false } | ||
diff --git a/examples/stm32g0/build.rs b/examples/stm32g0/build.rs new file mode 100644 index 000000000..d534cc3df --- /dev/null +++ b/examples/stm32g0/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 | |||
| 11 | use std::env; | ||
| 12 | use std::fs::File; | ||
| 13 | use std::io::Write; | ||
| 14 | use std::path::PathBuf; | ||
| 15 | |||
| 16 | fn 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/stm32g0/memory.x b/examples/stm32g0/memory.x new file mode 100644 index 000000000..02d59c83f --- /dev/null +++ b/examples/stm32g0/memory.x | |||
| @@ -0,0 +1,7 @@ | |||
| 1 | MEMORY | ||
| 2 | { | ||
| 3 | /* NOTE 1 K = 1 KiBi = 1024 bytes */ | ||
| 4 | /* These values correspond to the STM32G071C8 */ | ||
| 5 | FLASH : ORIGIN = 0x08000000, LENGTH = 64K | ||
| 6 | RAM : ORIGIN = 0x20000000, LENGTH = 36K | ||
| 7 | } | ||
diff --git a/examples/stm32g0/src/bin/blinky.rs b/examples/stm32g0/src/bin/blinky.rs new file mode 100644 index 000000000..a30887f7d --- /dev/null +++ b/examples/stm32g0/src/bin/blinky.rs | |||
| @@ -0,0 +1,31 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(trait_alias)] | ||
| 4 | #![feature(type_alias_impl_trait)] | ||
| 5 | #![allow(incomplete_features)] | ||
| 6 | |||
| 7 | #[path = "../example_common.rs"] | ||
| 8 | mod example_common; | ||
| 9 | use embassy::executor::Spawner; | ||
| 10 | use embassy::time::{Duration, Timer}; | ||
| 11 | use embassy_stm32::gpio::{Level, Output, Speed}; | ||
| 12 | use embassy_stm32::Peripherals; | ||
| 13 | use embedded_hal::digital::v2::OutputPin; | ||
| 14 | use example_common::*; | ||
| 15 | |||
| 16 | #[embassy::main] | ||
| 17 | async fn main(_spawner: Spawner, p: Peripherals) { | ||
| 18 | info!("Hello World!"); | ||
| 19 | |||
| 20 | let mut led = Output::new(p.PB7, Level::High, Speed::Low); | ||
| 21 | |||
| 22 | loop { | ||
| 23 | info!("high"); | ||
| 24 | unwrap!(led.set_high()); | ||
| 25 | Timer::after(Duration::from_millis(300)).await; | ||
| 26 | |||
| 27 | info!("low"); | ||
| 28 | unwrap!(led.set_low()); | ||
| 29 | Timer::after(Duration::from_millis(300)).await; | ||
| 30 | } | ||
| 31 | } | ||
diff --git a/examples/stm32g0/src/bin/button.rs b/examples/stm32g0/src/bin/button.rs new file mode 100644 index 000000000..a834b2fce --- /dev/null +++ b/examples/stm32g0/src/bin/button.rs | |||
| @@ -0,0 +1,29 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(trait_alias)] | ||
| 4 | #![feature(type_alias_impl_trait)] | ||
| 5 | #![allow(incomplete_features)] | ||
| 6 | |||
| 7 | #[path = "../example_common.rs"] | ||
| 8 | mod example_common; | ||
| 9 | use cortex_m_rt::entry; | ||
| 10 | use embassy_stm32::gpio::{Input, Pull}; | ||
| 11 | use embedded_hal::digital::v2::InputPin; | ||
| 12 | use example_common::*; | ||
| 13 | |||
| 14 | #[entry] | ||
| 15 | fn main() -> ! { | ||
| 16 | info!("Hello World!"); | ||
| 17 | |||
| 18 | let p = embassy_stm32::init(Default::default()); | ||
| 19 | |||
| 20 | let button = Input::new(p.PC13, Pull::Up); | ||
| 21 | |||
| 22 | loop { | ||
| 23 | if unwrap!(button.is_high()) { | ||
| 24 | info!("high"); | ||
| 25 | } else { | ||
| 26 | info!("low"); | ||
| 27 | } | ||
| 28 | } | ||
| 29 | } | ||
diff --git a/examples/stm32g0/src/bin/button_exti.rs b/examples/stm32g0/src/bin/button_exti.rs new file mode 100644 index 000000000..c55d6408c --- /dev/null +++ b/examples/stm32g0/src/bin/button_exti.rs | |||
| @@ -0,0 +1,31 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(trait_alias)] | ||
| 4 | #![feature(type_alias_impl_trait)] | ||
| 5 | #![allow(incomplete_features)] | ||
| 6 | |||
| 7 | #[path = "../example_common.rs"] | ||
| 8 | mod example_common; | ||
| 9 | use embassy::executor::Spawner; | ||
| 10 | use embassy_stm32::exti::ExtiInput; | ||
| 11 | use embassy_stm32::gpio::{Input, Pull}; | ||
| 12 | use embassy_stm32::Peripherals; | ||
| 13 | use embassy_traits::gpio::{WaitForFallingEdge, WaitForRisingEdge}; | ||
| 14 | use example_common::*; | ||
| 15 | |||
| 16 | #[embassy::main] | ||
| 17 | async fn main(_spawner: Spawner, p: Peripherals) { | ||
| 18 | info!("Hello World!"); | ||
| 19 | |||
| 20 | let button = Input::new(p.PC13, Pull::Up); | ||
| 21 | let mut button = ExtiInput::new(button, p.EXTI13); | ||
| 22 | |||
| 23 | info!("Press the USER button..."); | ||
| 24 | |||
| 25 | loop { | ||
| 26 | button.wait_for_falling_edge().await; | ||
| 27 | info!("Pressed!"); | ||
| 28 | button.wait_for_rising_edge().await; | ||
| 29 | info!("Released!"); | ||
| 30 | } | ||
| 31 | } | ||
diff --git a/examples/stm32g0/src/example_common.rs b/examples/stm32g0/src/example_common.rs new file mode 100644 index 000000000..54d633837 --- /dev/null +++ b/examples/stm32g0/src/example_common.rs | |||
| @@ -0,0 +1,17 @@ | |||
| 1 | #![macro_use] | ||
| 2 | |||
| 3 | use defmt_rtt as _; // global logger | ||
| 4 | use panic_probe as _; | ||
| 5 | |||
| 6 | pub use defmt::*; | ||
| 7 | |||
| 8 | use core::sync::atomic::{AtomicUsize, Ordering}; | ||
| 9 | |||
| 10 | defmt::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 | } | ||
