Skip to content

Commit

Permalink
rp pico 2 w support
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonros authored and lulf committed Jan 11, 2025
1 parent 6b46414 commit cf71ade
Show file tree
Hide file tree
Showing 12 changed files with 364 additions and 2 deletions.
1 change: 1 addition & 0 deletions .dir-locals.el
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
;; "examples/esp32/Cargo.toml"
;; "examples/nrf-sdc/Cargo.toml"
;; "examples/rp-pico-w/Cargo.toml"
;; "examples/rp-pico-2-w/Cargo.toml"
;; "examples/serial-hci/Cargo.toml"
;; "examples/apache-nimble/Cargo.toml"
])))
Expand Down
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
// "examples/esp32/Cargo.toml",
// "examples/nrf-sdc/Cargo.toml",
// "examples/rp-pico-w/Cargo.toml",
// "examples/rp-pico-2-w/Cargo.toml",
// "examples/serial-hci/Cargo.toml",
// "examples/apache-nimble/Cargo.toml",
]
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ See `examples` for example applications for different BLE controllers.
* `apache-nimble` which uses the controller from the [NimBLE stack](https://github.com/apache/mynewt-nimble) through high-level bindings from the [`apache-nimble`](https://github.com/benbrittain/apache-nimble-sys) crate.
* `esp32` which uses the BLE controller in the [esp-hal](https://github.com/esp-rs/esp-hal).
* `rp-pico-w` which uses the BLE controller in the [Raspberry Pi Pico W](https://www.raspberrypi.com/documentation/microcontrollers/pico-series.html#raspberry-pi-pico-w).
* `rp-pico-2-w` which uses the BLE controller in the [Raspberry Pi Pico 2 W](https://www.raspberrypi.com/documentation/microcontrollers/pico-series.html#raspberry-pi-pico-2-w).

Since a lot of the examples demo the same BLE functionality, they only contain basic wiring specific to the BLE controller, and share the 'business logic' within the `examples/apps` folder.

Expand Down
5 changes: 3 additions & 2 deletions ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ cargo batch \
--- build --release --manifest-path examples/nrf-sdc/Cargo.toml --target thumbv7em-none-eabihf --features nrf52832 \
--- build --release --manifest-path examples/esp32/Cargo.toml --target riscv32imc-unknown-none-elf --out-dir tests/esp32 \
--- build --release --manifest-path examples/serial-hci/Cargo.toml \
--- build --release --manifest-path examples/rp-pico-w//Cargo.toml --target thumbv6m-none-eabi --features skip-cyw43-firmware
--- build --release --manifest-path examples/rp-pico-w/Cargo.toml --target thumbv6m-none-eabi --features skip-cyw43-firmware \
--- build --release --manifest-path examples/rp-pico-2-w/Cargo.toml --target thumbv8m.main-none-eabihf --features skip-cyw43-firmware
# --- build --release --manifest-path examples/apache-nimble/Cargo.toml --target thumbv7em-none-eabihf

cargo fmt --check --manifest-path ./host/Cargo.toml
cargo clippy --manifest-path ./host/Cargo.toml --features gatt,peripheral,central
cargo test --manifest-path ./host/Cargo.toml --lib -- --nocapture
cargo test --manifest-path ./host/Cargo.toml --lib -- --nocapture
9 changes: 9 additions & 0 deletions examples/rp-pico-2-w/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[target.'cfg(all(target_arch = "arm", target_os = "none"))']
#runner = "probe-rs run --chip RP2040"
runner = "picotool load -u -v -x -t elf"

[build]
target = "thumbv8m.main-none-eabihf"

[env]
DEFMT_LOG = "debug"
1 change: 1 addition & 0 deletions examples/rp-pico-2-w/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cyw43-firmware/
59 changes: 59 additions & 0 deletions examples/rp-pico-2-w/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
[package]
name = "trouble-rp23-examples"
version = "0.1.0"
edition = "2021"
resolver = "2"

[dependencies]
embassy-executor = { version = "0.6.1", default-features = false, features = ["task-arena-size-98304", "arch-cortex-m", "executor-thread", "executor-interrupt", "defmt"] }
embassy-time = { version = "0.4.0", default-features = false, features = ["defmt", "defmt-timestamp-uptime"] }
embassy-rp = { version = "0.2.0", features = ["defmt", "unstable-pac", "time-driver", "critical-section-impl", "rp235xa", "binary-info"] }
embassy-futures = "0.1.1"
embassy-sync = { version = "0.6.1", features = ["defmt"] }

futures = { version = "0.3", default-features = false, features = ["async-await"]}
bt-hci = { version = "0.2.0", default-features = false, features = ["defmt"] }
trouble-example-apps= { version = "0.1.0", path = "../apps", features = ["defmt"] }
cyw43 = { version = "0.2.0", features = ["defmt", "firmware-logs", "bluetooth"] }
cyw43-pio = { version = "0.2.0", features = ["defmt"] }

defmt = "0.3"
defmt-rtt = "0.4.0"

cortex-m = { version = "0.7.6" }
cortex-m-rt = "0.7.0"
panic-probe = { version = "0.3", features = ["print-defmt"] }
static_cell = "2"
portable-atomic = { version = "1.5", features = ["critical-section"] }

[build-dependencies]
reqwest = { version = "0.12.9", features = ["blocking"]}

[features]
skip-cyw43-firmware = []

[profile.release]
debug = 2

[patch.crates-io]
embassy-executor = { git = "https://github.com/embassy-rs/embassy.git", rev = "e68efc2d7cdea195aec112ecb61231e148a282c2" }
embassy-rp = { git = "https://github.com/embassy-rs/embassy.git", rev = "e68efc2d7cdea195aec112ecb61231e148a282c2" }
embassy-sync = { git = "https://github.com/embassy-rs/embassy.git", rev = "e68efc2d7cdea195aec112ecb61231e148a282c2" }
embassy-futures = { git = "https://github.com/embassy-rs/embassy.git", rev = "e68efc2d7cdea195aec112ecb61231e148a282c2" }
embassy-time = { git = "https://github.com/embassy-rs/embassy.git", rev = "e68efc2d7cdea195aec112ecb61231e148a282c2" }
embassy-time-driver = { git = "https://github.com/embassy-rs/embassy.git", rev = "e68efc2d7cdea195aec112ecb61231e148a282c2" }
embassy-embedded-hal = { git = "https://github.com/embassy-rs/embassy.git", rev = "e68efc2d7cdea195aec112ecb61231e148a282c2" }
cyw43 = { git = "https://github.com/embassy-rs/embassy.git", rev = "e68efc2d7cdea195aec112ecb61231e148a282c2" }
cyw43-pio = { git = "https://github.com/embassy-rs/embassy.git", rev = "e68efc2d7cdea195aec112ecb61231e148a282c2" }

#embassy-executor = {path = "../../../embassy/embassy-executor"}
#embassy-nrf = {path = "../../../embassy/embassy-nrf"}
#embassy-sync = {path = "../../../embassy/embassy-sync"}
#embassy-futures = {path = "../../../embassy/embassy-futures"}
#embassy-time = {path = "../../../embassy/embassy-time"}
#embassy-time-driver = {path = "../../../embassy/embassy-time-driver"}
#embassy-embedded-hal = {path = "../../../embassy/embassy-embedded-hal"}
#embassy-hal-internal = {path = "../../../embassy/embassy-hal-internal"}
#nrf-sdc = { path = "../../../nrf-sdc/nrf-sdc" }
#nrf-mpsl = { path = "../../../nrf-sdc/nrf-mpsl" }
#bt-hci = { path = "../../../bt-hci" }
75 changes: 75 additions & 0 deletions examples/rp-pico-2-w/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//! 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());

#[cfg(not(feature = "skip-cyw43-firmware"))]
download_cyw43_firmware();

// 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=-Tdefmt.x");
}

#[cfg(not(feature = "skip-cyw43-firmware"))]
fn download_cyw43_firmware() {
let download_folder = "cyw43-firmware";
let url_base = "https://github.com/embassy-rs/embassy/raw/refs/heads/main/cyw43-firmware";
let file_names = [
"43439A0.bin",
"43439A0_btfw.bin",
"43439A0_clm.bin",
"LICENSE-permissive-binary-license-1.0.txt",
"README.md",
];

println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed={}", download_folder);
std::fs::create_dir_all(download_folder).expect("Failed to create download directory");

// download each file into the folder "cyw43-firmware"
for file in file_names {
let url = format!("{}/{}", url_base, file);
// only fetch if it doesn't exist
if std::path::Path::new(download_folder).join(file).exists() {
continue;
}
match reqwest::blocking::get(&url) {
Ok(response) => {
let content = response.bytes().expect("Failed to read file content");
let file_path = PathBuf::from(download_folder).join(file);
std::fs::write(file_path, &content).expect("Failed to write file");
}
Err(err) => panic!(
"Failed to download the cyw43 firmware from {}: {}, required for pi-pico-w example",
url, err
),
}
}
}
75 changes: 75 additions & 0 deletions examples/rp-pico-2-w/memory.x
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
MEMORY {
/*
* The RP2350 has either external or internal flash.
*
* 2 MiB is a safe default here, although a Pico 2 has 4 MiB.
*/
FLASH : ORIGIN = 0x10000000, LENGTH = 2048K
/*
* RAM consists of 8 banks, SRAM0-SRAM7, with a striped mapping.
* This is usually good for performance, as it distributes load on
* those banks evenly.
*/
RAM : ORIGIN = 0x20000000, LENGTH = 512K
/*
* RAM banks 8 and 9 use a direct mapping. They can be used to have
* memory areas dedicated for some specific job, improving predictability
* of access times.
* Example: Separate stacks for core0 and core1.
*/
SRAM4 : ORIGIN = 0x20080000, LENGTH = 4K
SRAM5 : ORIGIN = 0x20081000, LENGTH = 4K
}

SECTIONS {
/* ### Boot ROM info
*
* Goes after .vector_table, to keep it in the first 4K of flash
* where the Boot ROM (and picotool) can find it
*/
.start_block : ALIGN(4)
{
__start_block_addr = .;
KEEP(*(.start_block));
KEEP(*(.boot_info));
} > FLASH

} INSERT AFTER .vector_table;

/* move .text to start /after/ the boot info */
_stext = ADDR(.start_block) + SIZEOF(.start_block);

SECTIONS {
/* ### Picotool 'Binary Info' Entries
*
* Picotool looks through this block (as we have pointers to it in our
* header) to find interesting information.
*/
.bi_entries : ALIGN(4)
{
/* We put this in the header */
__bi_entries_start = .;
/* Here are the entries */
KEEP(*(.bi_entries));
/* Keep this block a nice round size */
. = ALIGN(4);
/* We put this in the header */
__bi_entries_end = .;
} > FLASH
} INSERT AFTER .text;

SECTIONS {
/* ### Boot ROM extra info
*
* Goes after everything in our program, so it can contain a signature.
*/
.end_block : ALIGN(4)
{
__end_block_addr = .;
KEEP(*(.end_block));
} > FLASH

} INSERT AFTER .uninit;

PROVIDE(start_to_end = __end_block_addr - __start_block_addr);
PROVIDE(end_to_start = __start_block_addr - __end_block_addr);
69 changes: 69 additions & 0 deletions examples/rp-pico-2-w/src/bin/ble_bas_central.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#![no_std]
#![no_main]

use bt_hci::controller::ExternalController;
use cyw43_pio::{PioSpi, RM2_CLOCK_DIVIDER};
use defmt::*;
use embassy_executor::Spawner;
use embassy_rp::bind_interrupts;
use embassy_rp::gpio::{Level, Output};
use embassy_rp::peripherals::{DMA_CH0, PIO0};
use embassy_rp::pio::{InterruptHandler, Pio};
use static_cell::StaticCell;
use trouble_example_apps::ble_bas_central;
use {defmt_rtt as _, embassy_time as _, panic_probe as _};

bind_interrupts!(struct Irqs {
PIO0_IRQ_0 => InterruptHandler<PIO0>;
});

#[embassy_executor::task]
async fn cyw43_task(runner: cyw43::Runner<'static, Output<'static>, PioSpi<'static, PIO0, 0, DMA_CH0>>) -> ! {
runner.run().await
}

#[embassy_executor::main]
async fn main(spawner: Spawner) {
let p = embassy_rp::init(Default::default());

#[cfg(feature = "skip-cyw43-firmware")]
let (fw, clm, btfw) = (&[], &[], &[]);

#[cfg(not(feature = "skip-cyw43-firmware"))]
let (fw, clm, btfw) = {
// IMPORTANT
//
// Download and make sure these files from https://github.com/embassy-rs/embassy/tree/main/cyw43-firmware
// are available in `./examples/rp-pico-2-w`. (should be automatic)
//
// IMPORTANT
let fw = include_bytes!("../../cyw43-firmware/43439A0.bin");
let clm = include_bytes!("../../cyw43-firmware/43439A0_clm.bin");
let btfw = include_bytes!("../../cyw43-firmware/43439A0_btfw.bin");
(fw, clm, btfw)
};

let pwr = Output::new(p.PIN_23, Level::Low);
let cs = Output::new(p.PIN_25, Level::High);
let mut pio = Pio::new(p.PIO0, Irqs);
let spi = PioSpi::new(
&mut pio.common,
pio.sm0,
RM2_CLOCK_DIVIDER,
pio.irq0,
cs,
p.PIN_24,
p.PIN_29,
p.DMA_CH0,
);

static STATE: StaticCell<cyw43::State> = StaticCell::new();
let state = STATE.init(cyw43::State::new());
let (_net_device, bt_device, mut control, runner) = cyw43::new_with_bluetooth(state, pwr, spi, fw, btfw).await;
unwrap!(spawner.spawn(cyw43_task(runner)));
control.init(clm).await;

let controller: ExternalController<_, 10> = ExternalController::new(bt_device);

ble_bas_central::run(controller).await;
}
Loading

0 comments on commit cf71ade

Please sign in to comment.