aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorUlf Lilleengen <[email protected]>2021-06-09 15:22:52 +0200
committerDario Nieuwenhuis <[email protected]>2021-06-09 23:09:48 +0200
commit1bb712315646a333bca0fb3724dab9c17e3a2b0f (patch)
tree33fb06edc0fb702a251e66c6ece3b68dda85f1cd /examples
parent3d16e922d5fc87453f9c8eb43c6414b959e3da80 (diff)
Add examples for STM32L0
Diffstat (limited to 'examples')
-rw-r--r--examples/stm32l0/.cargo/config.toml21
-rw-r--r--examples/stm32l0/Cargo.toml34
-rw-r--r--examples/stm32l0/build.rs31
-rw-r--r--examples/stm32l0/memory.x5
-rw-r--r--examples/stm32l0/src/bin/blinky.rs36
-rw-r--r--examples/stm32l0/src/bin/button.rs40
-rw-r--r--examples/stm32l0/src/bin/button_exti.rs64
-rw-r--r--examples/stm32l0/src/bin/spi.rs49
-rw-r--r--examples/stm32l0/src/example_common.rs17
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]
2build-std = ["core"]
3
4[target.'cfg(all(target_arch = "arm", target_os = "none"))']
5# replace your chip as listed in `probe-run --list-chips`
6runner = "probe-run --chip STM32L072CZ"
7
8rustflags = [
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]
21target = "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]
2authors = ["Dario Nieuwenhuis <[email protected]>", "Ulf Lilleengen <[email protected]>"]
3edition = "2018"
4name = "embassy-stm32l0-examples"
5version = "0.1.0"
6resolver = "2"
7
8[features]
9default = [
10 "defmt-default",
11]
12defmt-default = []
13defmt-trace = []
14defmt-debug = []
15defmt-info = []
16defmt-warn = []
17defmt-error = []
18
19[dependencies]
20embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-trace"] }
21embassy-traits = { version = "0.1.0", path = "../../embassy-traits", features = ["defmt"] }
22embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "defmt-trace", "stm32l072cz"] }
23embassy-extras = {version = "0.1.0", path = "../../embassy-extras" }
24
25defmt = "0.2.0"
26defmt-rtt = "0.2.0"
27
28cortex-m = "0.7.1"
29cortex-m-rt = "0.6.14"
30embedded-hal = { version = "0.2.4" }
31panic-probe = { version = "0.2.0", features= ["print-defmt"] }
32futures = { version = "0.3.8", default-features = false, features = ["async-await"] }
33rtt-target = { version = "0.3", features = ["cortex-m"] }
34heapless = { 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
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/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 @@
1MEMORY
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"]
10mod example_common;
11use embassy_stm32::{rcc::*, gpio::{Level, Output}};
12use embedded_hal::digital::v2::OutputPin;
13use example_common::*;
14
15use cortex_m_rt::entry;
16
17#[entry]
18fn 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"]
10mod example_common;
11use embassy_stm32::{rcc::*, gpio::{Input, Level, Output, Pull}};
12use embedded_hal::digital::v2::{InputPin, OutputPin};
13use example_common::*;
14
15use cortex_m_rt::entry;
16
17
18#[entry]
19fn 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"]
10mod example_common;
11use embassy::executor::Executor;
12use embassy::time::Clock;
13use embassy::util::Forever;
14use embassy_stm32::exti::ExtiInput;
15use embassy_stm32::gpio::{Input, Pull};
16use embassy_stm32::rcc;
17use embassy_traits::gpio::{WaitForFallingEdge, WaitForRisingEdge};
18use example_common::*;
19
20use cortex_m_rt::entry;
21
22#[embassy::task]
23async 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
43struct ZeroClock;
44
45impl Clock for ZeroClock {
46 fn now(&self) -> u64 {
47 0
48 }
49}
50
51static EXECUTOR: Forever<Executor> = Forever::new();
52
53#[entry]
54fn 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"]
10mod example_common;
11
12use embassy_stm32::gpio::{Level, Output};
13use embedded_hal::digital::v2::OutputPin;
14use example_common::*;
15
16use cortex_m_rt::entry;
17use embassy_stm32::rcc;
18use embassy_stm32::spi::{Config, Spi};
19use embassy_stm32::time::Hertz;
20use embedded_hal::blocking::spi::Transfer;
21
22#[entry]
23fn 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
3use defmt_rtt as _; // global logger
4use panic_probe as _;
5
6pub use defmt::*;
7
8use core::sync::atomic::{AtomicUsize, Ordering};
9
10defmt::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}