aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2021-08-20 01:35:02 +0200
committerGitHub <[email protected]>2021-08-20 01:35:02 +0200
commit0be6e4a384b3c2fee34f6f6db5f86ecbf9cea5e5 (patch)
treef5b6608d18e0a49a28fd04389684cb81d7a17022 /examples
parent8e5f1f4b5e8ff20897ac86ca81605d50c3dfa966 (diff)
parentda707051b0bc963b63aea209dc47108de15543f5 (diff)
Merge pull request #333 from bgamari/stm32g0
Add support for STM32G0
Diffstat (limited to 'examples')
-rw-r--r--examples/stm32g0/.cargo/config.toml18
-rw-r--r--examples/stm32g0/Cargo.toml34
-rw-r--r--examples/stm32g0/build.rs31
-rw-r--r--examples/stm32g0/memory.x7
-rw-r--r--examples/stm32g0/src/bin/blinky.rs31
-rw-r--r--examples/stm32g0/src/bin/button.rs29
-rw-r--r--examples/stm32g0/src/bin/button_exti.rs31
-rw-r--r--examples/stm32g0/src/example_common.rs17
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`
3runner = "probe-run --chip STM32G071RBTx"
4
5rustflags = [
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]
18target = "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]
2authors = ["Dario Nieuwenhuis <[email protected]>", "Ben Gamari <[email protected]>"]
3edition = "2018"
4name = "embassy-stm32g0-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", "time-driver-tim2", "stm32g071rb", "unstable-pac"] }
23embassy-hal-common = {version = "0.1.0", path = "../../embassy-hal-common" }
24
25defmt = "0.2.0"
26defmt-rtt = "0.2.0"
27
28cortex-m = "0.7.1"
29cortex-m-rt = "0.7.0"
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/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
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/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 @@
1MEMORY
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"]
8mod example_common;
9use embassy::executor::Spawner;
10use embassy::time::{Duration, Timer};
11use embassy_stm32::gpio::{Level, Output, Speed};
12use embassy_stm32::Peripherals;
13use embedded_hal::digital::v2::OutputPin;
14use example_common::*;
15
16#[embassy::main]
17async 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"]
8mod example_common;
9use cortex_m_rt::entry;
10use embassy_stm32::gpio::{Input, Pull};
11use embedded_hal::digital::v2::InputPin;
12use example_common::*;
13
14#[entry]
15fn 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"]
8mod example_common;
9use embassy::executor::Spawner;
10use embassy_stm32::exti::ExtiInput;
11use embassy_stm32::gpio::{Input, Pull};
12use embassy_stm32::Peripherals;
13use embassy_traits::gpio::{WaitForFallingEdge, WaitForRisingEdge};
14use example_common::*;
15
16#[embassy::main]
17async 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
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}