From 391798c55de0a9b903cb4542e6d5542781755a9a Mon Sep 17 00:00:00 2001 From: 9names <60134748+9names@users.noreply.github.com> Date: Thu, 25 Apr 2024 14:52:40 +1000 Subject: [PATCH] Add blocking nunchuk example --- .../.cargo/config.toml | 15 ++++ .../nunchuk-blocking-rp2040-hal/.gitignore | 14 ++++ .../.vscode/settings.json | 8 ++ .../nunchuk-blocking-rp2040-hal/Cargo.toml | 23 ++++++ .../nunchuk-blocking-rp2040-hal/README.md | 4 + examples/nunchuk-blocking-rp2040-hal/build.rs | 31 ++++++++ examples/nunchuk-blocking-rp2040-hal/memory.x | 15 ++++ .../nunchuk-blocking-rp2040-hal/src/main.rs | 79 +++++++++++++++++++ 8 files changed, 189 insertions(+) create mode 100644 examples/nunchuk-blocking-rp2040-hal/.cargo/config.toml create mode 100644 examples/nunchuk-blocking-rp2040-hal/.gitignore create mode 100644 examples/nunchuk-blocking-rp2040-hal/.vscode/settings.json create mode 100644 examples/nunchuk-blocking-rp2040-hal/Cargo.toml create mode 100644 examples/nunchuk-blocking-rp2040-hal/README.md create mode 100644 examples/nunchuk-blocking-rp2040-hal/build.rs create mode 100644 examples/nunchuk-blocking-rp2040-hal/memory.x create mode 100644 examples/nunchuk-blocking-rp2040-hal/src/main.rs diff --git a/examples/nunchuk-blocking-rp2040-hal/.cargo/config.toml b/examples/nunchuk-blocking-rp2040-hal/.cargo/config.toml new file mode 100644 index 0000000..730b910 --- /dev/null +++ b/examples/nunchuk-blocking-rp2040-hal/.cargo/config.toml @@ -0,0 +1,15 @@ +[target.'cfg(all(target_arch = "arm", target_os = "none"))'] +runner = "probe-rs run --chip RP2040 --protocol swd" +# runner = "elf2uf2-rs -d" + +rustflags = [ + "-C", "link-arg=--nmagic", + "-C", "link-arg=-Tlink.x", + "-C", "link-arg=-Tdefmt.x", +] + +[build] +target = "thumbv6m-none-eabi" + +[env] +DEFMT_LOG = "debug" diff --git a/examples/nunchuk-blocking-rp2040-hal/.gitignore b/examples/nunchuk-blocking-rp2040-hal/.gitignore new file mode 100644 index 0000000..3a0e903 --- /dev/null +++ b/examples/nunchuk-blocking-rp2040-hal/.gitignore @@ -0,0 +1,14 @@ +**/*.rs.bk +.#* +.gdb_history +Cargo.lock +target/ + +# editor files +.vscode/* +!.vscode/*.md +!.vscode/*.svd +!.vscode/launch.json +!.vscode/tasks.json +!.vscode/extensions.json +!.vscode/settings.json \ No newline at end of file diff --git a/examples/nunchuk-blocking-rp2040-hal/.vscode/settings.json b/examples/nunchuk-blocking-rp2040-hal/.vscode/settings.json new file mode 100644 index 0000000..2c5e3da --- /dev/null +++ b/examples/nunchuk-blocking-rp2040-hal/.vscode/settings.json @@ -0,0 +1,8 @@ +{ + "rust-analyzer.cargo.target": "thumbv6m-none-eabi", + "rust-analyzer.checkOnSave.allTargets": false, + "editor.formatOnSave": true, + "[toml]": { + "editor.formatOnSave": false, + } +} \ No newline at end of file diff --git a/examples/nunchuk-blocking-rp2040-hal/Cargo.toml b/examples/nunchuk-blocking-rp2040-hal/Cargo.toml new file mode 100644 index 0000000..5853789 --- /dev/null +++ b/examples/nunchuk-blocking-rp2040-hal/Cargo.toml @@ -0,0 +1,23 @@ +[package] +authors = ["9names"] +edition = "2018" +readme = "README.md" +name = "wii-ext_blocking_demo" +version = "0.1.0" +resolver = "2" +publish = false + +[dependencies] +cortex-m = "0.7.3" +cortex-m-rt = "0.7.0" +embedded-hal = "1" +embedded-time = "0.12.0" +defmt = "0.3.0" +defmt-rtt = "0.4.0" +panic-probe = { version = "0.3.0", features = ["print-defmt"] } +fugit = "0.3.6" +wii-ext = { version = "0.4.0", features = ["defmt_print",], path = "../../wii-ext" } +rp-pico = "0.9.0" + +[profile.release] +debug = 2 \ No newline at end of file diff --git a/examples/nunchuk-blocking-rp2040-hal/README.md b/examples/nunchuk-blocking-rp2040-hal/README.md new file mode 100644 index 0000000..e535713 --- /dev/null +++ b/examples/nunchuk-blocking-rp2040-hal/README.md @@ -0,0 +1,4 @@ +# Example project for wii-ext using Raspberry Pi Pico + +Just a simple example of how to use wii-ext-rs. +Grab a pico, wire up a classic controller or nunchuk and test it out! diff --git a/examples/nunchuk-blocking-rp2040-hal/build.rs b/examples/nunchuk-blocking-rp2040-hal/build.rs new file mode 100644 index 0000000..d534cc3 --- /dev/null +++ b/examples/nunchuk-blocking-rp2040-hal/build.rs @@ -0,0 +1,31 @@ +//! 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"); +} diff --git a/examples/nunchuk-blocking-rp2040-hal/memory.x b/examples/nunchuk-blocking-rp2040-hal/memory.x new file mode 100644 index 0000000..070eac7 --- /dev/null +++ b/examples/nunchuk-blocking-rp2040-hal/memory.x @@ -0,0 +1,15 @@ +MEMORY { + BOOT2 : ORIGIN = 0x10000000, LENGTH = 0x100 + FLASH : ORIGIN = 0x10000100, LENGTH = 2048K - 0x100 + RAM : ORIGIN = 0x20000000, LENGTH = 256K +} + +EXTERN(BOOT2_FIRMWARE) + +SECTIONS { + /* ### Boot loader */ + .boot2 ORIGIN(BOOT2) : + { + KEEP(*(.boot2)); + } > BOOT2 +} INSERT BEFORE .text; \ No newline at end of file diff --git a/examples/nunchuk-blocking-rp2040-hal/src/main.rs b/examples/nunchuk-blocking-rp2040-hal/src/main.rs new file mode 100644 index 0000000..38dd235 --- /dev/null +++ b/examples/nunchuk-blocking-rp2040-hal/src/main.rs @@ -0,0 +1,79 @@ +//! Interact with a Wii extension controller via the wii-ext crate on a Pico board +//! +//! It will enumerate as a USB joystick, which you can use to control a game +#![no_std] +#![no_main] + +use defmt::*; +use defmt_rtt as _; +use panic_probe as _; + +use bsp::hal::{ + self, clocks::init_clocks_and_plls, entry, gpio, pac, sio::Sio, watchdog::Watchdog, Timer, +}; +use embedded_hal::delay::DelayNs; +use fugit::RateExtU32; +use rp_pico as bsp; +use wii_ext::blocking_impl::nunchuk::Nunchuk; + +#[entry] +fn main() -> ! { + info!("Program start"); + let mut pac = pac::Peripherals::take().unwrap(); + let mut watchdog = Watchdog::new(pac.WATCHDOG); + let sio = Sio::new(pac.SIO); + + // External high-speed crystal on the pico board is 12Mhz + let external_xtal_freq_hz = 12_000_000u32; + let clocks = init_clocks_and_plls( + external_xtal_freq_hz, + pac.XOSC, + pac.CLOCKS, + pac.PLL_SYS, + pac.PLL_USB, + &mut pac.RESETS, + &mut watchdog, + ) + .ok() + .unwrap(); + + let mut delay = Timer::new(pac.TIMER, &mut pac.RESETS, &clocks); + + let pins = bsp::Pins::new( + pac.IO_BANK0, + pac.PADS_BANK0, + sio.gpio_bank0, + &mut pac.RESETS, + ); + + let sda_pin: gpio::Pin<_, gpio::FunctionI2C, _> = pins.gpio8.reconfigure(); + let scl_pin: gpio::Pin<_, gpio::FunctionI2C, _> = pins.gpio9.reconfigure(); + + let i2c = hal::I2C::i2c0( + pac.I2C0, + sda_pin, + scl_pin, + 100.kHz(), + &mut pac.RESETS, + &clocks.peripheral_clock, + ); + + // Create, initialise and calibrate the controller + let mut controller = Nunchuk::new(i2c, delay).unwrap(); + + loop { + // Some controllers need a delay between reads or they become unhappy + delay.delay_ms(10); + + // Capture the current button and axis values + let input = controller.read(); + if let Ok(input) = input { + // Print inputs from the controller + debug!("{:?}", input); + } else { + let _ = controller.init(); + } + } +} + +// End of file