diff options
| author | Ulf Lilleengen <[email protected]> | 2021-06-09 15:22:52 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2021-06-09 23:09:48 +0200 |
| commit | 1bb712315646a333bca0fb3724dab9c17e3a2b0f (patch) | |
| tree | 33fb06edc0fb702a251e66c6ece3b68dda85f1cd /examples | |
| parent | 3d16e922d5fc87453f9c8eb43c6414b959e3da80 (diff) | |
Add examples for STM32L0
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/stm32l0/.cargo/config.toml | 21 | ||||
| -rw-r--r-- | examples/stm32l0/Cargo.toml | 34 | ||||
| -rw-r--r-- | examples/stm32l0/build.rs | 31 | ||||
| -rw-r--r-- | examples/stm32l0/memory.x | 5 | ||||
| -rw-r--r-- | examples/stm32l0/src/bin/blinky.rs | 36 | ||||
| -rw-r--r-- | examples/stm32l0/src/bin/button.rs | 40 | ||||
| -rw-r--r-- | examples/stm32l0/src/bin/button_exti.rs | 64 | ||||
| -rw-r--r-- | examples/stm32l0/src/bin/spi.rs | 49 | ||||
| -rw-r--r-- | examples/stm32l0/src/example_common.rs | 17 |
9 files changed, 297 insertions, 0 deletions
diff --git a/examples/stm32l0/.cargo/config.toml b/examples/stm32l0/.cargo/config.toml new file mode 100644 index 000000000..0528db9fb --- /dev/null +++ b/examples/stm32l0/.cargo/config.toml | |||
| @@ -0,0 +1,21 @@ | |||
| 1 | [unstable] | ||
| 2 | build-std = ["core"] | ||
| 3 | |||
| 4 | [target.'cfg(all(target_arch = "arm", target_os = "none"))'] | ||
| 5 | # replace your chip as listed in `probe-run --list-chips` | ||
| 6 | runner = "probe-run --chip STM32L072CZ" | ||
| 7 | |||
| 8 | rustflags = [ | ||
| 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] | ||
| 21 | target = "thumbv6m-none-eabi" | ||
diff --git a/examples/stm32l0/Cargo.toml b/examples/stm32l0/Cargo.toml new file mode 100644 index 000000000..68291b007 --- /dev/null +++ b/examples/stm32l0/Cargo.toml | |||
| @@ -0,0 +1,34 @@ | |||
| 1 | [package] | ||
| 2 | authors = ["Dario Nieuwenhuis <[email protected]>", "Ulf Lilleengen <[email protected]>"] | ||
| 3 | edition = "2018" | ||
| 4 | name = "embassy-stm32l0-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", "stm32l072cz"] } | ||
| 23 | embassy-extras = {version = "0.1.0", path = "../../embassy-extras" } | ||
| 24 | |||
| 25 | defmt = "0.2.0" | ||
| 26 | defmt-rtt = "0.2.0" | ||
| 27 | |||
| 28 | cortex-m = "0.7.1" | ||
| 29 | cortex-m-rt = "0.6.14" | ||
| 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/stm32l0/build.rs b/examples/stm32l0/build.rs new file mode 100644 index 000000000..d534cc3df --- /dev/null +++ b/examples/stm32l0/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/stm32l0/memory.x b/examples/stm32l0/memory.x new file mode 100644 index 000000000..67a50a5ff --- /dev/null +++ b/examples/stm32l0/memory.x | |||
| @@ -0,0 +1,5 @@ | |||
| 1 | MEMORY | ||
| 2 | { | ||
| 3 | FLASH : ORIGIN = 0x08000000, LENGTH = 192K | ||
| 4 | RAM : ORIGIN = 0x20000000, LENGTH = 20K | ||
| 5 | } | ||
diff --git a/examples/stm32l0/src/bin/blinky.rs b/examples/stm32l0/src/bin/blinky.rs new file mode 100644 index 000000000..e7433c26f --- /dev/null +++ b/examples/stm32l0/src/bin/blinky.rs | |||
| @@ -0,0 +1,36 @@ | |||
| 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::{rcc::*, gpio::{Level, Output}}; | ||
| 12 | use embedded_hal::digital::v2::OutputPin; | ||
| 13 | use example_common::*; | ||
| 14 | |||
| 15 | use cortex_m_rt::entry; | ||
| 16 | |||
| 17 | #[entry] | ||
| 18 | fn main() -> ! { | ||
| 19 | info!("Hello World!"); | ||
| 20 | |||
| 21 | let mut p = embassy_stm32::init(Default::default()); | ||
| 22 | |||
| 23 | Rcc::new(p.RCC).enable_debug_wfe(&mut p.DBGMCU, true); | ||
| 24 | |||
| 25 | let mut led = Output::new(p.PB5, Level::High); | ||
| 26 | |||
| 27 | loop { | ||
| 28 | info!("high"); | ||
| 29 | led.set_high().unwrap(); | ||
| 30 | cortex_m::asm::delay(1_000_000); | ||
| 31 | |||
| 32 | info!("low"); | ||
| 33 | led.set_low().unwrap(); | ||
| 34 | cortex_m::asm::delay(1_000_000); | ||
| 35 | } | ||
| 36 | } | ||
diff --git a/examples/stm32l0/src/bin/button.rs b/examples/stm32l0/src/bin/button.rs new file mode 100644 index 000000000..1a2e20b14 --- /dev/null +++ b/examples/stm32l0/src/bin/button.rs | |||
| @@ -0,0 +1,40 @@ | |||
| 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::{rcc::*, gpio::{Input, Level, Output, Pull}}; | ||
| 12 | use embedded_hal::digital::v2::{InputPin, OutputPin}; | ||
| 13 | use example_common::*; | ||
| 14 | |||
| 15 | use cortex_m_rt::entry; | ||
| 16 | |||
| 17 | |||
| 18 | #[entry] | ||
| 19 | fn main() -> ! { | ||
| 20 | info!("Hello World!"); | ||
| 21 | |||
| 22 | let mut p = embassy_stm32::init(Default::default()); | ||
| 23 | Rcc::new(p.RCC).enable_debug_wfe(&mut p.DBGMCU, true); | ||
| 24 | |||
| 25 | let button = Input::new(p.PB2, Pull::Up); | ||
| 26 | let mut led1 = Output::new(p.PA5, Level::High); | ||
| 27 | let mut led2 = Output::new(p.PB5, Level::High); | ||
| 28 | |||
| 29 | loop { | ||
| 30 | if button.is_high().unwrap() { | ||
| 31 | info!("high"); | ||
| 32 | led1.set_high().unwrap(); | ||
| 33 | led2.set_low().unwrap(); | ||
| 34 | } else { | ||
| 35 | info!("low"); | ||
| 36 | led1.set_low().unwrap(); | ||
| 37 | led2.set_high().unwrap(); | ||
| 38 | } | ||
| 39 | } | ||
| 40 | } | ||
diff --git a/examples/stm32l0/src/bin/button_exti.rs b/examples/stm32l0/src/bin/button_exti.rs new file mode 100644 index 000000000..a7f734942 --- /dev/null +++ b/examples/stm32l0/src/bin/button_exti.rs | |||
| @@ -0,0 +1,64 @@ | |||
| 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::executor::Executor; | ||
| 12 | use embassy::time::Clock; | ||
| 13 | use embassy::util::Forever; | ||
| 14 | use embassy_stm32::exti::ExtiInput; | ||
| 15 | use embassy_stm32::gpio::{Input, Pull}; | ||
| 16 | use embassy_stm32::rcc; | ||
| 17 | use embassy_traits::gpio::{WaitForFallingEdge, WaitForRisingEdge}; | ||
| 18 | use example_common::*; | ||
| 19 | |||
| 20 | use cortex_m_rt::entry; | ||
| 21 | |||
| 22 | #[embassy::task] | ||
| 23 | async fn main_task() { | ||
| 24 | let mut p = embassy_stm32::init(Default::default()); | ||
| 25 | let mut rcc = rcc::Rcc::new(p.RCC); | ||
| 26 | rcc.enable_debug_wfe(&mut p.DBGMCU, true); | ||
| 27 | // Enables SYSCFG | ||
| 28 | let _ = rcc.enable_hsi48(&mut p.SYSCFG, p.CRS); | ||
| 29 | |||
| 30 | let button = Input::new(p.PB2, Pull::Up); | ||
| 31 | let mut button = ExtiInput::new(button, p.EXTI2); | ||
| 32 | |||
| 33 | info!("Press the USER button..."); | ||
| 34 | |||
| 35 | loop { | ||
| 36 | button.wait_for_falling_edge().await; | ||
| 37 | info!("Pressed!"); | ||
| 38 | button.wait_for_rising_edge().await; | ||
| 39 | info!("Released!"); | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | struct ZeroClock; | ||
| 44 | |||
| 45 | impl Clock for ZeroClock { | ||
| 46 | fn now(&self) -> u64 { | ||
| 47 | 0 | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | static EXECUTOR: Forever<Executor> = Forever::new(); | ||
| 52 | |||
| 53 | #[entry] | ||
| 54 | fn main() -> ! { | ||
| 55 | info!("Hello World!"); | ||
| 56 | |||
| 57 | unsafe { embassy::time::set_clock(&ZeroClock) }; | ||
| 58 | |||
| 59 | let executor = EXECUTOR.put(Executor::new()); | ||
| 60 | |||
| 61 | executor.run(|spawner| { | ||
| 62 | unwrap!(spawner.spawn(main_task())); | ||
| 63 | }) | ||
| 64 | } | ||
diff --git a/examples/stm32l0/src/bin/spi.rs b/examples/stm32l0/src/bin/spi.rs new file mode 100644 index 000000000..4eb9bfdd2 --- /dev/null +++ b/examples/stm32l0/src/bin/spi.rs | |||
| @@ -0,0 +1,49 @@ | |||
| 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 | |||
| 12 | use embassy_stm32::gpio::{Level, Output}; | ||
| 13 | use embedded_hal::digital::v2::OutputPin; | ||
| 14 | use example_common::*; | ||
| 15 | |||
| 16 | use cortex_m_rt::entry; | ||
| 17 | use embassy_stm32::rcc; | ||
| 18 | use embassy_stm32::spi::{Config, Spi}; | ||
| 19 | use embassy_stm32::time::Hertz; | ||
| 20 | use embedded_hal::blocking::spi::Transfer; | ||
| 21 | |||
| 22 | #[entry] | ||
| 23 | fn main() -> ! { | ||
| 24 | info!("Hello World, dude!"); | ||
| 25 | |||
| 26 | let mut p = embassy_stm32::init(Default::default()); | ||
| 27 | let mut rcc = rcc::Rcc::new(p.RCC); | ||
| 28 | rcc.enable_debug_wfe(&mut p.DBGMCU, true); | ||
| 29 | |||
| 30 | let mut spi = Spi::new( | ||
| 31 | Hertz(16_000_000), | ||
| 32 | p.SPI1, | ||
| 33 | p.PB3, | ||
| 34 | p.PA7, | ||
| 35 | p.PA6, | ||
| 36 | Hertz(1_000_000), | ||
| 37 | Config::default(), | ||
| 38 | ); | ||
| 39 | |||
| 40 | let mut cs = Output::new(p.PA15, Level::High); | ||
| 41 | |||
| 42 | loop { | ||
| 43 | let mut buf = [0x0A; 4]; | ||
| 44 | unwrap!(cs.set_low()); | ||
| 45 | unwrap!(spi.transfer(&mut buf)); | ||
| 46 | unwrap!(cs.set_high()); | ||
| 47 | info!("xfer {=[u8]:x}", buf); | ||
| 48 | } | ||
| 49 | } | ||
diff --git a/examples/stm32l0/src/example_common.rs b/examples/stm32l0/src/example_common.rs new file mode 100644 index 000000000..54d633837 --- /dev/null +++ b/examples/stm32l0/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 | } | ||
