aboutsummaryrefslogtreecommitdiff
path: root/examples/rp
diff options
context:
space:
mode:
Diffstat (limited to 'examples/rp')
-rw-r--r--examples/rp/.cargo/config.toml19
-rw-r--r--examples/rp/Cargo.toml32
-rw-r--r--examples/rp/build.rs31
-rw-r--r--examples/rp/memory.x5
-rw-r--r--examples/rp/src/bin/blinky.rs31
-rw-r--r--examples/rp/src/bin/button.rs29
-rw-r--r--examples/rp/src/bin/uart.rs25
-rw-r--r--examples/rp/src/example_common.rs12
8 files changed, 184 insertions, 0 deletions
diff --git a/examples/rp/.cargo/config.toml b/examples/rp/.cargo/config.toml
new file mode 100644
index 000000000..1bbbe97da
--- /dev/null
+++ b/examples/rp/.cargo/config.toml
@@ -0,0 +1,19 @@
1[unstable]
2build-std = ["core"]
3
4[target.'cfg(all(target_arch = "arm", target_os = "none"))']
5runner = "probe-run-rp --chip RP2040"
6
7rustflags = [
8 # LLD (shipped with the Rust toolchain) is used as the default linker
9 "-C", "link-arg=--nmagic",
10 "-C", "link-arg=-Tlink.x",
11 "-C", "link-arg=-Tlink-rp.x",
12 "-C", "link-arg=-Tdefmt.x",
13
14 # Code-size optimizations.
15 "-Z", "trap-unreachable=no",
16]
17
18[build]
19target = "thumbv6m-none-eabi" # Cortex-M0 and Cortex-M0+
diff --git a/examples/rp/Cargo.toml b/examples/rp/Cargo.toml
new file mode 100644
index 000000000..71b48129f
--- /dev/null
+++ b/examples/rp/Cargo.toml
@@ -0,0 +1,32 @@
1[package]
2authors = ["Dario Nieuwenhuis <[email protected]>"]
3edition = "2018"
4name = "embassy-rp-examples"
5version = "0.1.0"
6
7[features]
8default = [
9 "defmt-default",
10]
11defmt-default = []
12defmt-trace = []
13defmt-debug = []
14defmt-info = []
15defmt-warn = []
16defmt-error = []
17
18
19[dependencies]
20embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-trace"] }
21embassy-rp = { version = "0.1.0", path = "../../embassy-rp", features = ["defmt", "defmt-trace"] }
22rp2040-pac2 = { git = "https://github.com/Dirbaio/rp2040-pac", rev="254f4677937801155ca3cb17c7bb9d38eb62683e" }
23atomic-polyfill = { version = "0.1.1" }
24
25defmt = "0.2.0"
26defmt-rtt = "0.2.0"
27
28cortex-m = { version = "0.7.1", features = ["inline-asm"] }
29cortex-m-rt = "0.6.13"
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", "cfg-target-has-atomic", "unstable"] }
diff --git a/examples/rp/build.rs b/examples/rp/build.rs
new file mode 100644
index 000000000..d534cc3df
--- /dev/null
+++ b/examples/rp/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/rp/memory.x b/examples/rp/memory.x
new file mode 100644
index 000000000..aba861aae
--- /dev/null
+++ b/examples/rp/memory.x
@@ -0,0 +1,5 @@
1MEMORY {
2 BOOT2 : ORIGIN = 0x10000000, LENGTH = 0x100
3 FLASH : ORIGIN = 0x10000100, LENGTH = 2048K - 0x100
4 RAM : ORIGIN = 0x20000000, LENGTH = 256K
5} \ No newline at end of file
diff --git a/examples/rp/src/bin/blinky.rs b/examples/rp/src/bin/blinky.rs
new file mode 100644
index 000000000..e42999912
--- /dev/null
+++ b/examples/rp/src/bin/blinky.rs
@@ -0,0 +1,31 @@
1#![no_std]
2#![no_main]
3#![feature(asm)]
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 defmt::*;
13use embassy::executor::Spawner;
14use embassy_rp::{gpio, Peripherals};
15use embedded_hal::digital::v2::OutputPin;
16use gpio::{Level, Output};
17
18#[embassy::main]
19async fn main(_spawner: Spawner, p: Peripherals) {
20 let mut led = Output::new(p.PIN_25, Level::Low);
21
22 loop {
23 info!("led on!");
24 led.set_high().unwrap();
25 cortex_m::asm::delay(1_000_000);
26
27 info!("led off!");
28 led.set_low().unwrap();
29 cortex_m::asm::delay(1_000_000);
30 }
31}
diff --git a/examples/rp/src/bin/button.rs b/examples/rp/src/bin/button.rs
new file mode 100644
index 000000000..c4d942ff5
--- /dev/null
+++ b/examples/rp/src/bin/button.rs
@@ -0,0 +1,29 @@
1#![no_std]
2#![no_main]
3#![feature(asm)]
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::executor::Spawner;
13use embassy_rp::gpio::{Input, Level, Output, Pull};
14use embassy_rp::Peripherals;
15use embedded_hal::digital::v2::{InputPin, OutputPin};
16
17#[embassy::main]
18async fn main(_spawner: Spawner, p: Peripherals) {
19 let button = Input::new(p.PIN_28, Pull::Up);
20 let mut led = Output::new(p.PIN_25, Level::Low);
21
22 loop {
23 if button.is_high().unwrap() {
24 led.set_high().unwrap();
25 } else {
26 led.set_low().unwrap();
27 }
28 }
29}
diff --git a/examples/rp/src/bin/uart.rs b/examples/rp/src/bin/uart.rs
new file mode 100644
index 000000000..8b5f2a53b
--- /dev/null
+++ b/examples/rp/src/bin/uart.rs
@@ -0,0 +1,25 @@
1#![no_std]
2#![no_main]
3#![feature(asm)]
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::executor::Spawner;
13use embassy_rp::{uart, Peripherals};
14
15#[embassy::main]
16async fn main(_spawner: Spawner, p: Peripherals) {
17 let config = uart::Config::default();
18 let mut uart = uart::Uart::new(p.UART0, p.PIN_0, p.PIN_1, p.PIN_2, p.PIN_3, config);
19 uart.send("Hello World!\r\n".as_bytes());
20
21 loop {
22 uart.send("hello there!\r\n".as_bytes());
23 cortex_m::asm::delay(1_000_000);
24 }
25}
diff --git a/examples/rp/src/example_common.rs b/examples/rp/src/example_common.rs
new file mode 100644
index 000000000..f7c4ef57a
--- /dev/null
+++ b/examples/rp/src/example_common.rs
@@ -0,0 +1,12 @@
1use core::sync::atomic::{AtomicUsize, Ordering};
2use defmt_rtt as _;
3use panic_probe as _;
4
5defmt::timestamp! {"{=u64}", {
6 static COUNT: AtomicUsize = AtomicUsize::new(0);
7 // NOTE(no-CAS) `timestamps` runs with interrupts disabled
8 let n = COUNT.load(Ordering::Relaxed);
9 COUNT.store(n + 1, Ordering::Relaxed);
10 n as u64
11}
12}