From 7e93daf194c68b5f4a10653b1c865c5179685f9a Mon Sep 17 00:00:00 2001 From: akeamc Date: Sun, 2 Jun 2024 15:53:26 +0200 Subject: [PATCH] Add RP2040 example --- Cargo.toml | 3 ++ examples/rp2040/.cargo/config.toml | 11 +++++ examples/rp2040/Cargo.toml | 53 ++++++++++++++++++++++++ examples/rp2040/build.rs | 36 ++++++++++++++++ examples/rp2040/firmware/43439A0.bin | 0 examples/rp2040/firmware/43439A0_clm.bin | 0 examples/rp2040/memory.x | 17 ++++++++ examples/rp2040/src/main.rs | 27 ++++++++++++ 8 files changed, 147 insertions(+) create mode 100644 examples/rp2040/.cargo/config.toml create mode 100644 examples/rp2040/Cargo.toml create mode 100644 examples/rp2040/build.rs create mode 100644 examples/rp2040/firmware/43439A0.bin create mode 100644 examples/rp2040/firmware/43439A0_clm.bin create mode 100644 examples/rp2040/memory.x create mode 100644 examples/rp2040/src/main.rs diff --git a/Cargo.toml b/Cargo.toml index be413f1..676b1f2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,6 +24,9 @@ std = ["embedded-io-async/std"] [dev-dependencies] tokio = { version = "1.36.0", features = ["macros", "rt"] } +[workspace] +members = [".", "examples/rp2040"] + [package.metadata.docs.rs] all-features = true rustdoc-args = ["--cfg", "docsrs"] diff --git a/examples/rp2040/.cargo/config.toml b/examples/rp2040/.cargo/config.toml new file mode 100644 index 0000000..b45134f --- /dev/null +++ b/examples/rp2040/.cargo/config.toml @@ -0,0 +1,11 @@ +[target.'cfg(all(target_arch = "arm", target_os = "none"))'] +runner = "probe-rs run --chip RP2040" + +[build] +target = "thumbv6m-none-eabi" # Cortex-M0 and Cortex-M0+ + +[env] +DEFMT_LOG = "trace" + +[profile.release] +debug = 2 diff --git a/examples/rp2040/Cargo.toml b/examples/rp2040/Cargo.toml new file mode 100644 index 0000000..e514ceb --- /dev/null +++ b/examples/rp2040/Cargo.toml @@ -0,0 +1,53 @@ +[package] +name = "rp2040" +version = "0.1.0" +edition = "2021" + +[dependencies] +cortex-m = { version = "0.7.6", features = ["inline-asm"] } +cortex-m-rt = "0.7.0" +cyw43 = { version = "0.1.0", git = "https://github.com/embassy-rs/embassy", features = [ + "defmt", + "firmware-logs", +] } +cyw43-pio = { version = "0.1.0", git = "https://github.com/embassy-rs/embassy", features = [ + "defmt", + "overclock", +] } +defmt = "0.3" +defmt-rtt = "0.4" +embassy-embedded-hal = { version = "0.1.0", git = "https://github.com/embassy-rs/embassy", features = [ + "defmt", +] } +embassy-executor = { version = "0.5.0", git = "https://github.com/embassy-rs/embassy", features = [ + "task-arena-size-32768", + "arch-cortex-m", + "executor-thread", + "executor-interrupt", + "defmt", + "integrated-timers", +] } +embassy-rp = { version = "0.1.0", git = "https://github.com/embassy-rs/embassy", features = [ + "defmt", + "unstable-pac", + "time-driver", + "critical-section-impl", +] } +embassy-sync = "0.5.0" +embassy-time = { version = "0.3.0", git = "https://github.com/embassy-rs/embassy", features = [ + "defmt", + "defmt-timestamp-uptime", +] } +embedded-hal-async = "1.0.0" +fixed = "1.23.1" +fixed-macro = "1.2" +panic-probe = { version = "0.3", features = ["print-defmt"] } +pio = "0.2.1" +pio-proc = "0.2" +portable-atomic = { version = "1.5", features = ["critical-section"] } +static_cell = "2" +han = { path = "../..", features = [ + "defmt-03", + "embedded-io-async", +], default-features = false } +embedded-io-async = { version = "0.6.1", features = ["defmt-03"] } diff --git a/examples/rp2040/build.rs b/examples/rp2040/build.rs new file mode 100644 index 0000000..3f915f9 --- /dev/null +++ b/examples/rp2040/build.rs @@ -0,0 +1,36 @@ +//! This build script copies the `memory.x` file from the crate root into +//! a directory where the linker can always find it at build time. +//! For many projects this is optional, as the linker always searches the +//! project root directory -- wherever `Cargo.toml` is. However, if you +//! are using a workspace or have a more complicated build setup, this +//! build script becomes required. Additionally, by requesting that +//! Cargo re-run the build script whenever `memory.x` is changed, +//! updating `memory.x` ensures a rebuild of the application with the +//! new memory settings. + +use std::env; +use std::fs::File; +use std::io::Write; +use std::path::PathBuf; + +fn main() { + // Put `memory.x` in our output directory and ensure it's + // on the linker search path. + let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap()); + File::create(out.join("memory.x")) + .unwrap() + .write_all(include_bytes!("memory.x")) + .unwrap(); + println!("cargo:rustc-link-search={}", out.display()); + + // By default, Cargo will re-run a build script whenever + // any file in the project changes. By specifying `memory.x` + // here, we ensure the build script is only re-run when + // `memory.x` is changed. + println!("cargo:rerun-if-changed=memory.x"); + + println!("cargo:rustc-link-arg-bins=--nmagic"); + println!("cargo:rustc-link-arg-bins=-Tlink.x"); + println!("cargo:rustc-link-arg-bins=-Tlink-rp.x"); + println!("cargo:rustc-link-arg-bins=-Tdefmt.x"); +} diff --git a/examples/rp2040/firmware/43439A0.bin b/examples/rp2040/firmware/43439A0.bin new file mode 100644 index 0000000..e69de29 diff --git a/examples/rp2040/firmware/43439A0_clm.bin b/examples/rp2040/firmware/43439A0_clm.bin new file mode 100644 index 0000000..e69de29 diff --git a/examples/rp2040/memory.x b/examples/rp2040/memory.x new file mode 100644 index 0000000..ef19dff --- /dev/null +++ b/examples/rp2040/memory.x @@ -0,0 +1,17 @@ +MEMORY { + BOOT2 : ORIGIN = 0x10000000, LENGTH = 0x100 + FLASH : ORIGIN = 0x10000100, LENGTH = 2048K - 0x100 + + /* Pick one of the two options for RAM layout */ + + /* OPTION A: Use all RAM banks as one big block */ + /* Reasonable, unless you are doing something */ + /* really particular with DMA or other concurrent */ + /* access that would benefit from striping */ + RAM : ORIGIN = 0x20000000, LENGTH = 264K + + /* OPTION B: Keep the unstriped sections separate */ + /* RAM: ORIGIN = 0x20000000, LENGTH = 256K */ + /* SCRATCH_A: ORIGIN = 0x20040000, LENGTH = 4K */ + /* SCRATCH_B: ORIGIN = 0x20041000, LENGTH = 4K */ +} diff --git a/examples/rp2040/src/main.rs b/examples/rp2040/src/main.rs new file mode 100644 index 0000000..a2868a2 --- /dev/null +++ b/examples/rp2040/src/main.rs @@ -0,0 +1,27 @@ +//! This example shows how to use UART (Universal asynchronous receiver-transmitter) in the RP2040 chip. +//! +//! No specific hardware is specified in this example. Only output on pin 0 is tested. +//! The Raspberry Pi Debug Probe (https://www.raspberrypi.com/products/debug-probe/) could be used +//! with its UART port. + +#![no_std] +#![no_main] + +use embassy_executor::Spawner; +use embassy_rp::uart; +use {defmt_rtt as _, panic_probe as _}; + +#[embassy_executor::main] +async fn main(_spawner: Spawner) { + let p = embassy_rp::init(Default::default()); + let mut config = uart::Config::default(); + config.invert_rx = true; + config.baudrate = 115_200; + let mut uart = uart::UartRx::new_blocking(p.UART1, p.PIN_5, config); + + loop { + defmt::info!("read"); + let mut buf = [0; 1]; + let n = uart.blocking_read(&mut buf).unwrap(); + } +}