aboutsummaryrefslogtreecommitdiff
path: root/docs/examples
diff options
context:
space:
mode:
Diffstat (limited to 'docs/examples')
-rw-r--r--docs/examples/basic/.cargo/config.toml9
-rw-r--r--docs/examples/basic/Cargo.toml18
-rw-r--r--docs/examples/basic/build.rs35
-rw-r--r--docs/examples/basic/memory.x7
-rw-r--r--docs/examples/basic/src/main.rs26
l---------docs/examples/examples1
-rw-r--r--docs/examples/layer-by-layer/.cargo/config.toml14
-rw-r--r--docs/examples/layer-by-layer/Cargo.toml21
-rw-r--r--docs/examples/layer-by-layer/blinky-async/Cargo.toml15
-rw-r--r--docs/examples/layer-by-layer/blinky-async/src/main.rs23
-rw-r--r--docs/examples/layer-by-layer/blinky-hal/Cargo.toml14
-rw-r--r--docs/examples/layer-by-layer/blinky-hal/src/main.rs21
-rw-r--r--docs/examples/layer-by-layer/blinky-irq/Cargo.toml14
-rw-r--r--docs/examples/layer-by-layer/blinky-irq/src/main.rs93
-rw-r--r--docs/examples/layer-by-layer/blinky-pac/Cargo.toml14
-rw-r--r--docs/examples/layer-by-layer/blinky-pac/src/main.rs53
16 files changed, 378 insertions, 0 deletions
diff --git a/docs/examples/basic/.cargo/config.toml b/docs/examples/basic/.cargo/config.toml
new file mode 100644
index 000000000..8ca28df39
--- /dev/null
+++ b/docs/examples/basic/.cargo/config.toml
@@ -0,0 +1,9 @@
1[target.'cfg(all(target_arch = "arm", target_os = "none"))']
2# replace nRF82840_xxAA with your chip as listed in `probe-run --list-chips`
3runner = "probe-run --chip nRF52840_xxAA"
4
5[build]
6target = "thumbv7em-none-eabi"
7
8[env]
9DEFMT_LOG = "trace"
diff --git a/docs/examples/basic/Cargo.toml b/docs/examples/basic/Cargo.toml
new file mode 100644
index 000000000..2c282145d
--- /dev/null
+++ b/docs/examples/basic/Cargo.toml
@@ -0,0 +1,18 @@
1[package]
2authors = ["Dario Nieuwenhuis <[email protected]>"]
3edition = "2018"
4name = "embassy-basic-example"
5version = "0.1.0"
6license = "MIT OR Apache-2.0"
7
8[dependencies]
9embassy-executor = { version = "0.5.0", path = "../../../../../embassy-executor", features = ["defmt", "integrated-timers", "arch-cortex-m", "executor-thread"] }
10embassy-time = { version = "0.3.0", path = "../../../../../embassy-time", features = ["defmt"] }
11embassy-nrf = { version = "0.1.0", path = "../../../../../embassy-nrf", features = ["defmt", "nrf52840", "time-driver-rtc1", "gpiote"] }
12
13defmt = "0.3"
14defmt-rtt = "0.3"
15
16cortex-m = { version = "0.7.6", features = ["critical-section-single-core"] }
17cortex-m-rt = "0.7.0"
18panic-probe = { version = "0.3", features = ["print-defmt"] }
diff --git a/docs/examples/basic/build.rs b/docs/examples/basic/build.rs
new file mode 100644
index 000000000..30691aa97
--- /dev/null
+++ b/docs/examples/basic/build.rs
@@ -0,0 +1,35 @@
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
32 println!("cargo:rustc-link-arg-bins=--nmagic");
33 println!("cargo:rustc-link-arg-bins=-Tlink.x");
34 println!("cargo:rustc-link-arg-bins=-Tdefmt.x");
35}
diff --git a/docs/examples/basic/memory.x b/docs/examples/basic/memory.x
new file mode 100644
index 000000000..9b04edec0
--- /dev/null
+++ b/docs/examples/basic/memory.x
@@ -0,0 +1,7 @@
1MEMORY
2{
3 /* NOTE 1 K = 1 KiBi = 1024 bytes */
4 /* These values correspond to the NRF52840 with Softdevices S140 7.0.1 */
5 FLASH : ORIGIN = 0x00000000, LENGTH = 1024K
6 RAM : ORIGIN = 0x20000000, LENGTH = 256K
7}
diff --git a/docs/examples/basic/src/main.rs b/docs/examples/basic/src/main.rs
new file mode 100644
index 000000000..4412712c8
--- /dev/null
+++ b/docs/examples/basic/src/main.rs
@@ -0,0 +1,26 @@
1#![no_std]
2#![no_main]
3
4use defmt::*;
5use embassy_executor::Spawner;
6use embassy_nrf::gpio::{Level, Output, OutputDrive};
7use embassy_time::{Duration, Timer};
8use {defmt_rtt as _, panic_probe as _}; // global logger
9
10#[embassy_executor::task]
11async fn blinker(mut led: Output<'static>, interval: Duration) {
12 loop {
13 led.set_high();
14 Timer::after(interval).await;
15 led.set_low();
16 Timer::after(interval).await;
17 }
18}
19
20#[embassy_executor::main]
21async fn main(spawner: Spawner) {
22 let p = embassy_nrf::init(Default::default());
23
24 let led = Output::new(p.P0_13, Level::Low, OutputDrive::Standard);
25 unwrap!(spawner.spawn(blinker(led, Duration::from_millis(300))));
26}
diff --git a/docs/examples/examples b/docs/examples/examples
new file mode 120000
index 000000000..d15735c1d
--- /dev/null
+++ b/docs/examples/examples
@@ -0,0 +1 @@
../../examples \ No newline at end of file
diff --git a/docs/examples/layer-by-layer/.cargo/config.toml b/docs/examples/layer-by-layer/.cargo/config.toml
new file mode 100644
index 000000000..3012f05dc
--- /dev/null
+++ b/docs/examples/layer-by-layer/.cargo/config.toml
@@ -0,0 +1,14 @@
1[target.'cfg(all(target_arch = "arm", target_os = "none"))']
2runner = "probe-run --chip STM32L475VG"
3
4rustflags = [
5 "-C", "link-arg=--nmagic",
6 "-C", "link-arg=-Tlink.x",
7 "-C", "link-arg=-Tdefmt.x",
8]
9
10[build]
11target = "thumbv7em-none-eabihf"
12
13[env]
14DEFMT_LOG = "trace"
diff --git a/docs/examples/layer-by-layer/Cargo.toml b/docs/examples/layer-by-layer/Cargo.toml
new file mode 100644
index 000000000..943249a17
--- /dev/null
+++ b/docs/examples/layer-by-layer/Cargo.toml
@@ -0,0 +1,21 @@
1[workspace]
2resolver = "2"
3members = [
4 "blinky-pac",
5 "blinky-hal",
6 "blinky-irq",
7 "blinky-async",
8]
9
10[patch.crates-io]
11embassy-executor = { path = "../../../../../embassy-executor" }
12embassy-stm32 = { path = "../../../../../embassy-stm32" }
13
14[profile.release]
15codegen-units = 1
16debug = 2
17debug-assertions = false
18incremental = false
19lto = "fat"
20opt-level = 's'
21overflow-checks = false
diff --git a/docs/examples/layer-by-layer/blinky-async/Cargo.toml b/docs/examples/layer-by-layer/blinky-async/Cargo.toml
new file mode 100644
index 000000000..64f7e8403
--- /dev/null
+++ b/docs/examples/layer-by-layer/blinky-async/Cargo.toml
@@ -0,0 +1,15 @@
1[package]
2name = "blinky-async"
3version = "0.1.0"
4edition = "2021"
5license = "MIT OR Apache-2.0"
6
7[dependencies]
8cortex-m = "0.7"
9cortex-m-rt = "0.7"
10embassy-stm32 = { version = "0.1.0", features = ["stm32l475vg", "memory-x", "exti"] }
11embassy-executor = { version = "0.5.0", features = ["arch-cortex-m", "executor-thread"] }
12
13defmt = "0.3.0"
14defmt-rtt = "0.3.0"
15panic-probe = { version = "0.3.0", features = ["print-defmt"] }
diff --git a/docs/examples/layer-by-layer/blinky-async/src/main.rs b/docs/examples/layer-by-layer/blinky-async/src/main.rs
new file mode 100644
index 000000000..004602816
--- /dev/null
+++ b/docs/examples/layer-by-layer/blinky-async/src/main.rs
@@ -0,0 +1,23 @@
1#![no_std]
2#![no_main]
3
4use embassy_executor::Spawner;
5use embassy_stm32::exti::ExtiInput;
6use embassy_stm32::gpio::{Level, Output, Pull, Speed};
7use {defmt_rtt as _, panic_probe as _};
8
9#[embassy_executor::main]
10async fn main(_spawner: Spawner) {
11 let p = embassy_stm32::init(Default::default());
12 let mut led = Output::new(p.PB14, Level::Low, Speed::VeryHigh);
13 let mut button = ExtiInput::new(p.PC13, p.EXTI13, Pull::Up);
14
15 loop {
16 button.wait_for_any_edge().await;
17 if button.is_low() {
18 led.set_high();
19 } else {
20 led.set_low();
21 }
22 }
23}
diff --git a/docs/examples/layer-by-layer/blinky-hal/Cargo.toml b/docs/examples/layer-by-layer/blinky-hal/Cargo.toml
new file mode 100644
index 000000000..c15de2db2
--- /dev/null
+++ b/docs/examples/layer-by-layer/blinky-hal/Cargo.toml
@@ -0,0 +1,14 @@
1[package]
2name = "blinky-hal"
3version = "0.1.0"
4edition = "2021"
5license = "MIT OR Apache-2.0"
6
7[dependencies]
8cortex-m = "0.7"
9cortex-m-rt = "0.7"
10embassy-stm32 = { version = "0.1.0", features = ["stm32l475vg", "memory-x"] }
11
12defmt = "0.3.0"
13defmt-rtt = "0.3.0"
14panic-probe = { version = "0.3.0", features = ["print-defmt"] }
diff --git a/docs/examples/layer-by-layer/blinky-hal/src/main.rs b/docs/examples/layer-by-layer/blinky-hal/src/main.rs
new file mode 100644
index 000000000..d0c9f4907
--- /dev/null
+++ b/docs/examples/layer-by-layer/blinky-hal/src/main.rs
@@ -0,0 +1,21 @@
1#![no_std]
2#![no_main]
3
4use cortex_m_rt::entry;
5use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed};
6use {defmt_rtt as _, panic_probe as _};
7
8#[entry]
9fn main() -> ! {
10 let p = embassy_stm32::init(Default::default());
11 let mut led = Output::new(p.PB14, Level::High, Speed::VeryHigh);
12 let button = Input::new(p.PC13, Pull::Up);
13
14 loop {
15 if button.is_low() {
16 led.set_high();
17 } else {
18 led.set_low();
19 }
20 }
21}
diff --git a/docs/examples/layer-by-layer/blinky-irq/Cargo.toml b/docs/examples/layer-by-layer/blinky-irq/Cargo.toml
new file mode 100644
index 000000000..9733658b6
--- /dev/null
+++ b/docs/examples/layer-by-layer/blinky-irq/Cargo.toml
@@ -0,0 +1,14 @@
1[package]
2name = "blinky-irq"
3version = "0.1.0"
4edition = "2021"
5license = "MIT OR Apache-2.0"
6
7[dependencies]
8cortex-m = "0.7"
9cortex-m-rt = { version = "0.7" }
10embassy-stm32 = { version = "0.1.0", features = ["stm32l475vg", "memory-x", "unstable-pac"] }
11
12defmt = "0.3.0"
13defmt-rtt = "0.3.0"
14panic-probe = { version = "0.3.0", features = ["print-defmt"] }
diff --git a/docs/examples/layer-by-layer/blinky-irq/src/main.rs b/docs/examples/layer-by-layer/blinky-irq/src/main.rs
new file mode 100644
index 000000000..743c9d99b
--- /dev/null
+++ b/docs/examples/layer-by-layer/blinky-irq/src/main.rs
@@ -0,0 +1,93 @@
1#![no_std]
2#![no_main]
3
4use core::cell::RefCell;
5
6use cortex_m::interrupt::Mutex;
7use cortex_m::peripheral::NVIC;
8use cortex_m_rt::entry;
9use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed};
10use embassy_stm32::{interrupt, pac};
11use {defmt_rtt as _, panic_probe as _};
12
13static BUTTON: Mutex<RefCell<Option<Input<'static>>>> = Mutex::new(RefCell::new(None));
14static LED: Mutex<RefCell<Option<Output<'static>>>> = Mutex::new(RefCell::new(None));
15
16#[entry]
17fn main() -> ! {
18 let p = embassy_stm32::init(Default::default());
19 let led = Output::new(p.PB14, Level::Low, Speed::Low);
20 let mut button = Input::new(p.PC13, Pull::Up);
21
22 cortex_m::interrupt::free(|cs| {
23 enable_interrupt(&mut button);
24
25 LED.borrow(cs).borrow_mut().replace(led);
26 BUTTON.borrow(cs).borrow_mut().replace(button);
27
28 unsafe { NVIC::unmask(pac::Interrupt::EXTI15_10) };
29 });
30
31 loop {
32 cortex_m::asm::wfe();
33 }
34}
35
36#[interrupt]
37fn EXTI15_10() {
38 cortex_m::interrupt::free(|cs| {
39 let mut button = BUTTON.borrow(cs).borrow_mut();
40 let button = button.as_mut().unwrap();
41
42 let mut led = LED.borrow(cs).borrow_mut();
43 let led = led.as_mut().unwrap();
44 if check_interrupt(button) {
45 if button.is_low() {
46 led.set_high();
47 } else {
48 led.set_low();
49 }
50 }
51 clear_interrupt(button);
52 });
53}
54//
55//
56//
57//
58//
59//
60// "Hidden" HAL-like methods for doing interrupts with embassy. Hardcode pin just to give audience an idea of what it looks like
61
62const PORT: u8 = 2;
63const PIN: usize = 13;
64fn check_interrupt(_pin: &mut Input<'static>) -> bool {
65 let exti = pac::EXTI;
66 let pin = PIN;
67 let lines = exti.pr(0).read();
68 lines.line(pin)
69}
70
71fn clear_interrupt(_pin: &mut Input<'static>) {
72 let exti = pac::EXTI;
73 let pin = PIN;
74 let mut lines = exti.pr(0).read();
75 lines.set_line(pin, true);
76 exti.pr(0).write_value(lines);
77}
78
79fn enable_interrupt(_pin: &mut Input<'static>) {
80 cortex_m::interrupt::free(|_| {
81 let rcc = pac::RCC;
82 rcc.apb2enr().modify(|w| w.set_syscfgen(true));
83
84 let port = PORT;
85 let pin = PIN;
86 let syscfg = pac::SYSCFG;
87 let exti = pac::EXTI;
88 syscfg.exticr(pin / 4).modify(|w| w.set_exti(pin % 4, port));
89 exti.imr(0).modify(|w| w.set_line(pin, true));
90 exti.rtsr(0).modify(|w| w.set_line(pin, true));
91 exti.ftsr(0).modify(|w| w.set_line(pin, true));
92 });
93}
diff --git a/docs/examples/layer-by-layer/blinky-pac/Cargo.toml b/docs/examples/layer-by-layer/blinky-pac/Cargo.toml
new file mode 100644
index 000000000..f872b94cb
--- /dev/null
+++ b/docs/examples/layer-by-layer/blinky-pac/Cargo.toml
@@ -0,0 +1,14 @@
1[package]
2name = "blinky-pac"
3version = "0.1.0"
4edition = "2021"
5license = "MIT OR Apache-2.0"
6
7[dependencies]
8cortex-m = "0.7"
9cortex-m-rt = "0.7"
10stm32-metapac = { version = "1", features = ["stm32l475vg", "memory-x"] }
11
12defmt = "0.3.0"
13defmt-rtt = "0.3.0"
14panic-probe = { version = "0.3.0", features = ["print-defmt"] }
diff --git a/docs/examples/layer-by-layer/blinky-pac/src/main.rs b/docs/examples/layer-by-layer/blinky-pac/src/main.rs
new file mode 100644
index 000000000..990d46cb6
--- /dev/null
+++ b/docs/examples/layer-by-layer/blinky-pac/src/main.rs
@@ -0,0 +1,53 @@
1#![no_std]
2#![no_main]
3
4use pac::gpio::vals;
5use {defmt_rtt as _, panic_probe as _, stm32_metapac as pac};
6
7#[cortex_m_rt::entry]
8fn main() -> ! {
9 // Enable GPIO clock
10 let rcc = pac::RCC;
11 unsafe {
12 rcc.ahb2enr().modify(|w| {
13 w.set_gpioben(true);
14 w.set_gpiocen(true);
15 });
16
17 rcc.ahb2rstr().modify(|w| {
18 w.set_gpiobrst(true);
19 w.set_gpiocrst(true);
20 w.set_gpiobrst(false);
21 w.set_gpiocrst(false);
22 });
23 }
24
25 // Setup button
26 let gpioc = pac::GPIOC;
27 const BUTTON_PIN: usize = 13;
28 unsafe {
29 gpioc.pupdr().modify(|w| w.set_pupdr(BUTTON_PIN, vals::Pupdr::PULLUP));
30 gpioc.otyper().modify(|w| w.set_ot(BUTTON_PIN, vals::Ot::PUSHPULL));
31 gpioc.moder().modify(|w| w.set_moder(BUTTON_PIN, vals::Moder::INPUT));
32 }
33
34 // Setup LED
35 let gpiob = pac::GPIOB;
36 const LED_PIN: usize = 14;
37 unsafe {
38 gpiob.pupdr().modify(|w| w.set_pupdr(LED_PIN, vals::Pupdr::FLOATING));
39 gpiob.otyper().modify(|w| w.set_ot(LED_PIN, vals::Ot::PUSHPULL));
40 gpiob.moder().modify(|w| w.set_moder(LED_PIN, vals::Moder::OUTPUT));
41 }
42
43 // Main loop
44 loop {
45 unsafe {
46 if gpioc.idr().read().idr(BUTTON_PIN) == vals::Idr::LOW {
47 gpiob.bsrr().write(|w| w.set_bs(LED_PIN, true));
48 } else {
49 gpiob.bsrr().write(|w| w.set_br(LED_PIN, true));
50 }
51 }
52 }
53}