diff options
| author | Bob McWhirter <[email protected]> | 2021-06-07 11:57:27 -0400 |
|---|---|---|
| committer | Bob McWhirter <[email protected]> | 2021-06-08 10:37:11 -0400 |
| commit | b8690e5f5d7b6996e833e9866629f298f88289fe (patch) | |
| tree | 85a037614dabb3f0e01cf4c184639154470ffada /examples | |
| parent | e6bd02d40eb2d2bdde1da463a6e79eebb63987b8 (diff) | |
Add blinky example.
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/stm32l4/Cargo.toml | 35 | ||||
| -rw-r--r-- | examples/stm32l4/build.rs | 31 | ||||
| -rw-r--r-- | examples/stm32l4/memory.x | 7 | ||||
| -rw-r--r-- | examples/stm32l4/src/bin/blinky.rs | 53 | ||||
| -rw-r--r-- | examples/stm32l4/src/example_common.rs | 17 |
5 files changed, 143 insertions, 0 deletions
diff --git a/examples/stm32l4/Cargo.toml b/examples/stm32l4/Cargo.toml new file mode 100644 index 000000000..ca53f08b1 --- /dev/null +++ b/examples/stm32l4/Cargo.toml | |||
| @@ -0,0 +1,35 @@ | |||
| 1 | [package] | ||
| 2 | authors = ["Dario Nieuwenhuis <[email protected]>"] | ||
| 3 | edition = "2018" | ||
| 4 | name = "embassy-stm32f4-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", "stm32l4s5vi"] } | ||
| 23 | embassy-extras = {version = "0.1.0", path = "../../embassy-extras" } | ||
| 24 | stm32l4 = { version = "0.13", features = ["stm32l4x5" ] } | ||
| 25 | |||
| 26 | defmt = "0.2.0" | ||
| 27 | defmt-rtt = "0.2.0" | ||
| 28 | |||
| 29 | cortex-m = "0.7.1" | ||
| 30 | cortex-m-rt = "0.6.14" | ||
| 31 | embedded-hal = { version = "0.2.4" } | ||
| 32 | panic-probe = { version = "0.2.0", features= ["print-defmt"] } | ||
| 33 | futures = { version = "0.3.8", default-features = false, features = ["async-await"] } | ||
| 34 | rtt-target = { version = "0.3", features = ["cortex-m"] } | ||
| 35 | heapless = { version = "0.7.1", default-features = false } | ||
diff --git a/examples/stm32l4/build.rs b/examples/stm32l4/build.rs new file mode 100644 index 000000000..d534cc3df --- /dev/null +++ b/examples/stm32l4/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/stm32l4/memory.x b/examples/stm32l4/memory.x new file mode 100644 index 000000000..22618dfbc --- /dev/null +++ b/examples/stm32l4/memory.x | |||
| @@ -0,0 +1,7 @@ | |||
| 1 | MEMORY | ||
| 2 | { | ||
| 3 | /* NOTE 1 K = 1 KiBi = 1024 bytes */ | ||
| 4 | /* These values correspond to the STM32L4S5 */ | ||
| 5 | FLASH : ORIGIN = 0x08000000, LENGTH = 2048K | ||
| 6 | RAM : ORIGIN = 0x20000000, LENGTH = 640K | ||
| 7 | } | ||
diff --git a/examples/stm32l4/src/bin/blinky.rs b/examples/stm32l4/src/bin/blinky.rs new file mode 100644 index 000000000..42c9333f4 --- /dev/null +++ b/examples/stm32l4/src/bin/blinky.rs | |||
| @@ -0,0 +1,53 @@ | |||
| 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"] | ||
| 10 | mod example_common; | ||
| 11 | use embassy_stm32::gpio::{Level, Output}; | ||
| 12 | use embedded_hal::digital::v2::OutputPin; | ||
| 13 | use example_common::*; | ||
| 14 | |||
| 15 | use cortex_m_rt::entry; | ||
| 16 | use stm32l4::stm32l4x5 as pac; | ||
| 17 | |||
| 18 | #[entry] | ||
| 19 | fn 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 | |||
| 30 | pp.RCC.ahb2enr.modify(|_, w| { | ||
| 31 | w.gpioaen().set_bit(); | ||
| 32 | w.gpioben().set_bit(); | ||
| 33 | w.gpiocen().set_bit(); | ||
| 34 | w.gpioden().set_bit(); | ||
| 35 | w.gpioeen().set_bit(); | ||
| 36 | w.gpiofen().set_bit(); | ||
| 37 | w | ||
| 38 | }); | ||
| 39 | |||
| 40 | let p = embassy_stm32::init(Default::default()); | ||
| 41 | |||
| 42 | let mut led = Output::new(p.PB14, Level::High); | ||
| 43 | |||
| 44 | loop { | ||
| 45 | info!("high"); | ||
| 46 | led.set_high().unwrap(); | ||
| 47 | cortex_m::asm::delay(10_000_000); | ||
| 48 | |||
| 49 | info!("low"); | ||
| 50 | led.set_low().unwrap(); | ||
| 51 | cortex_m::asm::delay(10_000_000); | ||
| 52 | } | ||
| 53 | } | ||
diff --git a/examples/stm32l4/src/example_common.rs b/examples/stm32l4/src/example_common.rs new file mode 100644 index 000000000..54d633837 --- /dev/null +++ b/examples/stm32l4/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 | } | ||
