-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add nunchuk async impl * Add nunchuk async example project
- Loading branch information
Showing
9 changed files
with
395 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[target.'cfg(all(target_arch = "arm", target_os = "none"))'] | ||
runner = "probe-rs run --chip RP2040 --protocol swd --speed 16000" | ||
|
||
[build] | ||
target = "thumbv6m-none-eabi" # Cortex-M0 and Cortex-M0+ | ||
|
||
[env] | ||
DEFMT_LOG = "debug" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// The format of this file is specified in https://probe.rs/docs/tools/vscode/#start-a-debug-session-with-minimum-configuration | ||
{ | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"preLaunchTask": "rust: cargo build", | ||
"type": "probe-rs-debug", | ||
"request": "launch", | ||
"name": "embassy_rp_async launch", | ||
"cwd": "${workspaceFolder}", | ||
"chip": "rp2040", | ||
// RP2040 doesn't support connectUnderReset | ||
"connectUnderReset": false, | ||
"speed": 4000, | ||
"runtimeExecutable": "probe-rs", | ||
"runtimeArgs": [ | ||
"dap-server" | ||
], | ||
"flashingConfig": { | ||
"flashingEnabled": true, | ||
"resetAfterFlashing": true, | ||
"haltAfterReset": true, | ||
}, | ||
"coreConfigs": [ | ||
{ | ||
"coreIndex": 0, | ||
"programBinary": "target/thumbv6m-none-eabi/debug/embassy_rp_async", | ||
"chip": "RP2040", | ||
// Uncomment this if you've downloaded the SVD from | ||
// https://github.com/raspberrypi/pico-sdk/raw/1.3.1/src/rp2040/hardware_regs/rp2040.svd | ||
// and placed it in the .vscode directory | ||
// "svdFile": "./.vscode/rp2040.svd", | ||
"rttEnabled": true, | ||
"options": { | ||
"env": { | ||
"DEFMT_LOG": "debug" | ||
} | ||
}, | ||
} | ||
], | ||
"consoleLogLevel": "Info", //Error, Warn, Info, Debug, Trace | ||
"wireProtocol": "Swd" | ||
}, | ||
{ | ||
"type": "probe-rs-debug", | ||
"request": "attach", | ||
"name": "embassy_rp_async attach", | ||
"cwd": "${workspaceFolder}", | ||
"chip": "rp2040", | ||
// RP2040 doesn't support connectUnderReset | ||
"connectUnderReset": false, | ||
"speed": 4000, | ||
"runtimeExecutable": "probe-rs", | ||
"runtimeArgs": [ | ||
"dap-server" | ||
], | ||
"coreConfigs": [ | ||
{ | ||
"coreIndex": 0, | ||
"programBinary": "target/thumbv6m-none-eabi/debug/embassy_rp_async", | ||
"chip": "RP2040", | ||
// Uncomment this if you've downloaded the SVD from | ||
// https://github.com/raspberrypi/pico-sdk/raw/1.5.1/src/rp2040/hardware_regs/rp2040.svd | ||
// and placed it in the .vscode directory | ||
// "svdFile": "./.vscode/rp2040.svd", | ||
"rttEnabled": true, | ||
"options": { | ||
"env": { | ||
"DEFMT_LOG": "debug" | ||
} | ||
}, | ||
} | ||
], | ||
"consoleLogLevel": "Info", //Error, Warn, Info, Debug, Trace | ||
"wireProtocol": "Swd" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"rust-analyzer.cargo.target": "thumbv6m-none-eabi", | ||
"rust-analyzer.check.allTargets": false, | ||
"rust-analyzer.checkOnSave": false, | ||
"editor.formatOnSave": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
[package] | ||
edition = "2021" | ||
name = "nunchuk_async_embassy_rp" | ||
version = "0.1.0" | ||
license = "MIT OR Apache-2.0" | ||
resolver = "2" | ||
|
||
|
||
[dependencies] | ||
wii-ext = { version = "0.3.0", features = [ | ||
"defmt_print", | ||
], path = "../../wii-ext" } | ||
|
||
cortex-m = { version = "0.7.6", features = ["inline-asm"] } | ||
cortex-m-rt = "0.7.0" | ||
|
||
defmt = "0.3" | ||
defmt-rtt = "0.4" | ||
panic-probe = { version = "0.3", features = ["print-defmt"] } | ||
|
||
embassy-embedded-hal = { version = "0.1.0", features = ["defmt"] } | ||
|
||
embassy-executor = { version = "0.5.0", features = [ | ||
"arch-cortex-m", | ||
"executor-thread", | ||
"executor-interrupt", | ||
"defmt", | ||
"integrated-timers", | ||
"task-arena-size-32768", | ||
] } | ||
embassy-futures = { version = "0.1.0" } | ||
embassy-sync = { version = "0.5.0", features = ["defmt"] } | ||
embassy-time = { version = "0.3.0", features = [ | ||
"defmt", | ||
"defmt-timestamp-uptime", | ||
] } | ||
|
||
embassy-rp = { version = "0.1.0", features = [ | ||
"defmt", | ||
"unstable-pac", | ||
"time-driver", | ||
"critical-section-impl", | ||
] } | ||
portable-atomic = { version = "1.5.1", features = ["critical-section"] } | ||
|
||
# cargo build/run | ||
[profile.dev] | ||
codegen-units = 1 | ||
debug = 2 | ||
debug-assertions = true | ||
incremental = false | ||
opt-level = 1 | ||
overflow-checks = true | ||
lto = "off" | ||
|
||
# cargo build/run --release | ||
[profile.release] | ||
codegen-units = 1 | ||
debug = 2 | ||
debug-assertions = false | ||
incremental = false | ||
lto = true | ||
opt-level = 's' | ||
overflow-checks = false | ||
|
||
# do not optimize proc-macro crates = faster builds from scratch | ||
[profile.dev.build-override] | ||
codegen-units = 8 | ||
debug = false | ||
debug-assertions = false | ||
opt-level = 0 | ||
overflow-checks = false | ||
|
||
[profile.release.build-override] | ||
codegen-units = 8 | ||
debug = false | ||
debug-assertions = false | ||
opt-level = 0 | ||
overflow-checks = false | ||
|
||
# cargo test | ||
[profile.test] | ||
codegen-units = 1 | ||
debug = 2 | ||
debug-assertions = true | ||
incremental = false | ||
opt-level = 's' | ||
overflow-checks = true | ||
|
||
# cargo test --release | ||
[profile.bench] | ||
codegen-units = 1 | ||
debug = 2 | ||
debug-assertions = false | ||
incremental = false | ||
lto = true | ||
opt-level = 's' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
MEMORY { | ||
BOOT2 : ORIGIN = 0x10000000, LENGTH = 0x100 | ||
FLASH : ORIGIN = 0x10000100, LENGTH = 2048K - 0x100 | ||
RAM : ORIGIN = 0x20000000, LENGTH = 256K | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#![no_std] | ||
#![no_main] | ||
|
||
use defmt::*; | ||
use embassy_rp::gpio; | ||
use gpio::{Level, Output}; | ||
use wii_ext::async_impl::nunchuk; | ||
use {defmt_rtt as _, panic_probe as _}; | ||
|
||
use embassy_executor::Spawner; | ||
use embassy_rp::bind_interrupts; | ||
use embassy_rp::gpio::AnyPin; | ||
use embassy_rp::i2c::{self, Config, InterruptHandler}; | ||
use embassy_rp::peripherals::I2C0; | ||
use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex; | ||
use embassy_sync::mutex::Mutex; | ||
use embassy_time::{Delay, Duration, Ticker}; | ||
use embassy_time::Timer; | ||
|
||
bind_interrupts!(struct Irqs { | ||
I2C0_IRQ => InterruptHandler<I2C0>; | ||
}); | ||
|
||
type LedType = Mutex<ThreadModeRawMutex, Option<Output<'static, AnyPin>>>; | ||
static LED: LedType = Mutex::new(None); | ||
|
||
#[embassy_executor::main] | ||
async fn main(spawner: Spawner) { | ||
info!("Program start"); | ||
let p = embassy_rp::init(Default::default()); | ||
|
||
// Configure Pico LED to blink once a second | ||
let led = Output::new(AnyPin::from(p.PIN_25), Level::High); | ||
{ | ||
*(LED.lock().await) = Some(led); | ||
} | ||
unwrap!(spawner.spawn(toggle_led(&LED, Duration::from_millis(500)))); | ||
|
||
let sda = p.PIN_8; | ||
let scl = p.PIN_9; | ||
|
||
info!("set up i2c"); | ||
let i2c = i2c::I2c::new_async(p.I2C0, scl, sda, Irqs, Config::default()); | ||
|
||
// Create, initialise and calibrate the controller | ||
info!("initialising controller"); | ||
let mut controller = nunchuk::NunchukAsync::new(i2c, Delay); | ||
controller.init().await.unwrap(); | ||
|
||
|
||
info!("begin polling controller"); | ||
loop { | ||
let input = controller.read().await.unwrap(); | ||
debug!("{:?}", input); | ||
} | ||
} | ||
|
||
#[embassy_executor::task(pool_size = 1)] | ||
async fn toggle_led(led: &'static LedType, delay: Duration) { | ||
let mut ticker = Ticker::every(delay); | ||
loop { | ||
{ | ||
let mut led_unlocked = led.lock().await; | ||
if let Some(pin_ref) = led_unlocked.as_mut() { | ||
pin_ref.toggle(); | ||
} | ||
} | ||
ticker.next().await; | ||
} | ||
} | ||
|
||
#[cortex_m_rt::pre_init] | ||
unsafe fn before_main() { | ||
// Soft-reset doesn't clear spinlocks. Clear the one used by critical-section | ||
// before we hit main to avoid deadlocks when using a debugger | ||
embassy_rp::pac::SIO.spinlock(31).write_value(1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,5 @@ | |
pub mod classic; | ||
/// Async i2c interface code | ||
pub mod interface; | ||
|
||
pub mod nunchuk; |
Oops, something went wrong.